Skip to content

Commit f8a5d89

Browse files
authored
Merge pull request #183 from nicorikken/asciidoctor-plugin-redo
feat: asciidoctor plugin
2 parents 62f409a + d422a8c commit f8a5d89

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

src/io/perun.clj

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,55 @@
458458
:meta meta
459459
:cmd-opts cmd-opts))))
460460

461+
(def ^:private ^:deps asciidoctor-deps
462+
'[[org.clojure/tools.namespace "0.3.0-alpha3"]
463+
[org.asciidoctor/asciidoctorj "1.5.4"]])
464+
465+
(def ^:private +asciidoctor-defaults+
466+
{:out-dir "public"
467+
:out-ext ".html"
468+
:filterer identity
469+
:extensions [".ad" ".asc" ".adoc" ".asciidoc"]
470+
:meta {:original true
471+
:include-rss true
472+
:include-atom true}})
473+
474+
(deftask asciidoctor*
475+
"Parse asciidoc files using Asciidoctor
476+
477+
Asciidoctor has basic support for markdown, and can therefore also be used
478+
for parsing `.md` files."
479+
[d out-dir OUTDIR str "the output directory"
480+
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
481+
e extensions EXTENSIONS [str] "extensions of files to process"
482+
m meta META edn "metadata to set on each entry"]
483+
(let [pod (create-pod asciidoctor-deps)
484+
options (merge +asciidoctor-defaults+ *opts*)]
485+
(content-task
486+
{:render-form-fn (fn [data] `(io.perun.asciidoctor/process-asciidoctor ~data))
487+
:paths-fn #(content-paths % options)
488+
:passthru-fn content-passthru
489+
:task-name "asciidoctor"
490+
:tracer :io.perun/asciidoctor
491+
:rm-originals true
492+
:pod pod})))
493+
494+
(deftask asciidoctor
495+
"Parse asciidoc files with yaml front matter using Asciidoctor
496+
497+
Asciidoctor has basic support for markdown, and can therefore also be used
498+
for parsing `.md` files."
499+
[d out-dir OUTDIR str "the output directory"
500+
_ filterer FILTER code "predicate to use for selecting entries (default: `identity`)"
501+
e extensions EXTENSIONS [str] "extensions of files to process"
502+
m meta META edn "metadata to set on each entry"]
503+
(let [{:keys [out-dir filterer extensions meta]} (merge +asciidoctor-defaults+ *opts*)]
504+
(comp (yaml-metadata :filterer filterer :extensions extensions)
505+
(asciidoctor* :out-dir out-dir
506+
:filterer filterer
507+
:extensions extensions
508+
:meta meta))))
509+
461510
(deftask global-metadata
462511
"Read global metadata from `perun.base.edn` or configured file.
463512

src/io/perun/asciidoctor.clj

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
(ns io.perun.asciidoctor
2+
(:require [io.perun.core :as perun]
3+
[clojure.java.io :as io])
4+
(:import [org.asciidoctor Asciidoctor Asciidoctor$Factory]))
5+
6+
(def container
7+
(Asciidoctor$Factory/create ""))
8+
9+
(defn asciidoctor-to-html [file-content]
10+
(.convert container file-content {}))
11+
12+
(defn process-asciidoctor [{:keys [entry]}]
13+
(perun/report-debug "asciidoctor" "processing asciidoctor" (:filename entry))
14+
(let [file-content (-> entry :full-path io/file slurp)
15+
html (asciidoctor-to-html file-content)]
16+
(assoc entry :rendered html)))

test/io/perun_test.clj

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,23 @@
188188
189189
This --- be ___markdown___.")
190190

191+
(def adoc-content
192+
"= Hello there
193+
194+
This --- be _asciidoc_.")
195+
191196
(def input-strings (map #(str "---\n" % "\n---\n" md-content) yamls))
192197

198+
(def adoc-input-strings (map #(str "---\n" % "\n---\n" adoc-content) yamls))
199+
193200
(def parsed-md-basic "<h1><a href=\"#hello-there\" id=\"hello-there\"></a>Hello there</h1>\n<p>This --- be <strong><em>markdown</em></strong>.</p>\n")
194201

195202
(def parsed-pandoc-basic "<h1 id=\"hello-there\">Hello there</h1>\n<p>This --- be <strong><em>markdown</em></strong>.</p>\n")
196203

204+
(def parsed-asciidoctor-md "<div class=\"paragraph\">\n<p>This --- be <em><em>markdown</em></em>.</p>\n</div>")
205+
206+
(def parsed-asciidoctor-adoc "<div class=\"paragraph\">\n<p>This --- be <em>asciidoc</em>.</p>\n</div>")
207+
197208
(def parsed-md-smarts "<h1><a href=\"#hello-there\" id=\"hello-there\"></a>Hello there</h1>\n<p>This &mdash; be <strong><em>markdown</em></strong>.</p>\n")
198209

199210
(def js-content "(function somejs() { console.log('$foo'); })();")
@@ -246,6 +257,20 @@ This --- be ___markdown___.")
246257
:content parsed-pandoc-basic
247258
:msg "`pandoc` should populate HTML file with parsed content"))
248259

260+
(add-txt-file :path "2017-01-01-test.adoc" :content (nth input-strings 0))
261+
(p/asciidoctor)
262+
(testing "asciidoctor for markdown"
263+
(content-check :path (perun/url-to-path "public/2017-01-01-test.html")
264+
:content parsed-asciidoctor-md
265+
:msg "`asciidoctor` should populate HTML file with parsed markdown content"))
266+
267+
(add-txt-file :path "2017-01-01-test.adoc" :content (nth adoc-input-strings 0))
268+
(p/asciidoctor)
269+
(testing "asciidoctor for asciidoc"
270+
(content-check :path (perun/url-to-path "public/2017-01-01-test.html")
271+
:content parsed-asciidoctor-adoc
272+
:msg "`asciidoctor` should populate HTML file with parsed asciidoc content"))
273+
249274
(add-txt-file :path "2017-01-01-test.md" :content (nth input-strings 0))
250275
(p/markdown)
251276

@@ -399,6 +424,26 @@ This --- be ___markdown___.")
399424
:content "Hello there\n===========\n\nThis --- be ***markdown***.\n"
400425
:msg "`pandoc` should parse HTML to markdown"))
401426

427+
(add-txt-file :path "test.md" :content (nth input-strings 0))
428+
(p/asciidoctor :out-dir nil
429+
:filterer #(= (:path %) "test.md")
430+
:extensions [".md"]
431+
:meta {:asciidoctor-set :metadata})
432+
(testing "asciidoctor for markdown"
433+
(content-check :path "test.html"
434+
:content parsed-asciidoctor-md
435+
:msg "`asciidoctor` should populate HTML with parsed markdown content"))
436+
437+
(add-txt-file :path "test.adoc" :content (nth adoc-input-strings 0))
438+
(p/asciidoctor :out-dir nil
439+
:filterer #(= (:path %) "test.adoc")
440+
:extensions [".adoc"]
441+
:meta {:asciidoctor-set :metadata})
442+
(testing "asciidoctor for asciidoc"
443+
(content-check :path "test.html"
444+
:content parsed-asciidoctor-adoc
445+
:msg "`asciidoctor` should populate HTML with parsed asciidoc content"))
446+
402447
(add-txt-file :path "test.md" :content (nth input-strings 0))
403448
(p/markdown :out-dir "hammock"
404449
:filterer #(= (:path %) "test.md")

0 commit comments

Comments
 (0)