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'/>"
|
2015-01-09 12:11:38 +00:00
|
|
|
[blog-prefix text]
|
|
|
|
(clojure.string/replace text #"href=.?/|src=.?/" #(str (subs % 0 (dec (count %))) blog-prefix "/")))
|
|
|
|
|
|
|
|
(defn- rewrite-hrefs-transformer
|
|
|
|
"A :replacement-transformer for use in markdown.core that will inject the
|
|
|
|
given blog prefix in front of local links."
|
2014-12-30 12:31:46 +00:00
|
|
|
[{:keys [blog-prefix]} text state]
|
2015-01-09 12:11:38 +00:00
|
|
|
[(rewrite-hrefs blog-prefix text) state])
|
2014-12-30 12:31:46 +00:00
|
|
|
|
|
|
|
(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]
|
2015-01-09 12:12:56 +00:00
|
|
|
(fn [rdr config]
|
|
|
|
(->>
|
2014-12-30 12:31:46 +00:00
|
|
|
(.convert (Asciidoctor$Factory/create)
|
2015-01-09 12:12:56 +00:00
|
|
|
(->> (java.io.BufferedReader. rdr)
|
|
|
|
(line-seq)
|
|
|
|
(s/join "\n"))
|
|
|
|
(Collections/emptyMap))
|
|
|
|
(rewrite-hrefs (:blog-prefix config)))))))
|
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)])
|