2014-12-29 16:15:48 +00:00
|
|
|
(ns cryogen-core.markup
|
|
|
|
(:require [markdown.core :refer [md-to-html-string]]
|
2014-12-30 12:31:46 +00:00
|
|
|
[markdown.transformers :refer [transformer-vector]]
|
2014-12-29 16:15:48 +00:00
|
|
|
[clojure.string :as s])
|
|
|
|
(:import org.asciidoctor.Asciidoctor$Factory
|
|
|
|
java.util.Collections))
|
|
|
|
|
|
|
|
(defprotocol Markup
|
2014-12-30 12:31:46 +00:00
|
|
|
"A markup engine comprising a dir(ectory) containing markup files,
|
|
|
|
an ext(ension) for finding markup file names, and a render-fn that returns
|
|
|
|
a fn with the signature [java.io.Reader config] -> String (HTML)."
|
2014-12-29 16:15:48 +00:00
|
|
|
(dir [this])
|
|
|
|
(ext [this])
|
|
|
|
(render-fn [this]))
|
|
|
|
|
2014-12-30 12:31:46 +00:00
|
|
|
(defn- rewrite-hrefs
|
|
|
|
"Injects the blog prefix in front of any local links
|
|
|
|
|
|
|
|
ex. <img src='/img/cryogen.png'/> becomes <img src='/blog/img/cryogen.png'/>"
|
|
|
|
[{:keys [blog-prefix]} text state]
|
|
|
|
[(clojure.string/replace text #"href=.?/|src=.?/" #(str (subs % 0 (dec (count %))) blog-prefix "/"))
|
|
|
|
state])
|
|
|
|
|
|
|
|
(defn- markdown
|
|
|
|
"Returns a Markdown (https://daringfireball.net/projects/markdown/)
|
|
|
|
implementation of the Markup protocol."
|
|
|
|
[]
|
2014-12-29 16:15:48 +00:00
|
|
|
(reify Markup
|
|
|
|
(dir [this] "md")
|
|
|
|
(ext [this] ".md")
|
|
|
|
(render-fn [this]
|
2014-12-30 12:31:46 +00:00
|
|
|
(fn [rdr config]
|
2014-12-29 16:15:48 +00:00
|
|
|
(md-to-html-string
|
2015-01-09 12:10:49 +00:00
|
|
|
(->> (java.io.BufferedReader. rdr)
|
|
|
|
(line-seq)
|
|
|
|
(s/join "\n"))
|
|
|
|
:reference-links? true
|
|
|
|
:heading-anchors true
|
|
|
|
:replacement-transformers (conj transformer-vector (partial rewrite-hrefs-transformer config)))))))
|
2014-12-29 16:15:48 +00:00
|
|
|
|
2014-12-30 12:31:46 +00:00
|
|
|
(defn- asciidoc
|
|
|
|
"Returns an Asciidoc (http://asciidoc.org/) implementation of the
|
|
|
|
Markup protocol."
|
|
|
|
[]
|
2014-12-29 16:15:48 +00:00
|
|
|
(reify Markup
|
|
|
|
(dir [this] "asc")
|
|
|
|
(ext [this] ".asc")
|
|
|
|
(render-fn [this]
|
2014-12-30 12:31:46 +00:00
|
|
|
(let [attributes (java.util.HashMap. {"toc" "macro"})
|
|
|
|
options (java.util.HashMap. {"attributes" attributes})]
|
|
|
|
(fn [rdr _]
|
|
|
|
(.convert (Asciidoctor$Factory/create)
|
|
|
|
(->> (java.io.BufferedReader. rdr)
|
|
|
|
(line-seq)
|
|
|
|
(s/join "\n"))
|
|
|
|
options))))))
|
2014-12-29 16:15:48 +00:00
|
|
|
|
2014-12-30 12:31:46 +00:00
|
|
|
(defn markups
|
|
|
|
"Return a vector of Markup implementations. This is the primary entry point
|
|
|
|
for a client of this ns. This vector should be used to iterate over supported
|
|
|
|
Markups."
|
|
|
|
[]
|
2014-12-29 16:15:48 +00:00
|
|
|
[(markdown) (asciidoc)])
|