Skip to content

Commit

Permalink
Thank fuck, it works. Now to remove all the crud.
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-brooke committed Feb 19, 2020
1 parent 0f0f2ec commit 0649ecf
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 81 deletions.
163 changes: 101 additions & 62 deletions src/smeagol/formatting.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
[smeagol.configuration :refer [config]]
[smeagol.extensions.mermaid :refer [process-mermaid]]
[smeagol.extensions.photoswipe :refer [process-photoswipe]]
[smeagol.extensions.vega :refer [process-vega]]
[taoensso.timbre :as log]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -68,15 +69,17 @@

(defn process-backticks
"Effectively, escape the backticks surrounding this `text`, by protecting them
from the `md->html` filter."
from the `process-text` filter."
[^String text ^Integer index]
(str "<pre class=\"backticks\">```" (.trim text) "\n```</pre>"))


(defn get-first-token
"Return the first space-separated token of this `string`."
[^String string]
(if string (first (cs/split string #"[^a-zA-Z0-9]+"))))
(try
(if string (first (cs/split (first (cs/split-lines string)) #"[^a-zA-Z0-9]+")))
(catch NullPointerException _ nil)))


(defn- process-markdown-fragment
Expand All @@ -86,7 +89,7 @@
`:inclusions`, a map of constructed keywords to inclusion specifications,
and `:text`, an HTML text string with the keywords present where the
corresponding inclusion should be inserted."
[index result fragment fragments processed]
[^Integer index ^clojure.lang.Associative result ^String fragment fragments processed]
(process-text
(inc index)
result
Expand All @@ -113,14 +116,29 @@
`:inclusions`, a map of constructed keywords to inclusion specifications,
and `:text`, an HTML text string with the keywords present where the
corresponding inclusion should be inserted."
[index result fragments processed fragment token formatter]
[^Integer index
^clojure.lang.Associative result
fragments
processed
^String fragment
^String token
formatter]
(log/info "index:" index "(type result):" (type result) "(type fragments):" (type fragments) "fragment:" fragment "token:" token ":formatter" formatter)
(let
[kw (keyword (str "inclusion-" index))]
[inky (keyword (str "inclusion-" index))
fkey (keyword token)]
(process-text
(inc index)
(assoc-in result [:inclusions kw] (apply formatter (list (subs fragment (count token)) index)))
(rest fragments)
(cons kw processed))))
(deep-merge
result
{:inclusions {inky (eval (list formatter (subs fragment (count token)) index))}
:extensions {fkey (-> config :formatters fkey)}})
;; (assoc-in
;; (assoc-in result [:inclusions inky] (eval (list formatter (subs fragment (count token)) index)))
;; [:extensions fkey] (-> config :formatters fkey))
(rest fragments)
(cons inky processed))))


;; (apply-formatter
;; 3
Expand All @@ -135,55 +153,16 @@
;; "pswp"
;; smeagol.extensions.photoswipe/process-photoswipe)

(defn process-text
"Process this `text`, assumed to be markdown potentially containing both local links
and YAML visualisation specifications, and return a map comprising JSON visualisation
specification, and HTML text with markers for where those should be reinserted.
The map has two top-level keys: `:inclusions`, a map of constructed keywords to
inclusion specifications, and `:text`, an HTML text string with the keywords
present where the corresponding inclusion should be inserted."

;; TODO: the inclusion->index bug is in here somewhere.
([^String text]
(process-text 0 {} (cs/split (or text "") #"```") '()))
([index result fragments processed]
(let [fragment (first fragments)
;; if I didn't find a formatter for a back-tick marked fragment,
;; I need to put the backticks back in.
remarked (if (odd? index) (str "```" fragment "\n```") fragment)
first-token (get-first-token fragment)
kw (if-not (empty? first-token) (keyword first-token))
formatter (if-not
(empty? first-token)
(try
(read-string (-> config :formatters kw :formatter))
(catch Exception _
(do
(log/info "No formatter found for extension `" kw "`")
;; no extension registered - there sometimes won't be,
;; and it doesn't matter
nil))))]
(cond
(empty? fragments)
;; We've come to the end of the list of fragments. Reassemble them into
;; a single HTML text and pass it back.
(assoc result :text
(local-links
(md/md-to-html-string
(cs/join "\n\n" (reverse processed))
:heading-anchors true)))
formatter
(apply-formatter index result fragments processed fragment first-token formatter)
true
(process-markdown-fragment index result remarked (rest fragments) processed)))))
(defn reassemble-text
"Reassemble these processed strings into a complete text, and process it as
Markdown."
[result processed]
(assoc result :text
(local-links
(md/md-to-html-string
(cs/join "\n\n" (reverse processed))
:heading-anchors true))))

;; (process-text
;; "pswp
;; ![Frost on a gate, Laurieston](content/uploads/g1.jpg)
;; ![Feathered crystals on snow surface, Taliesin](content/uploads/g2.jpg)
;; ![Feathered snow on log, Taliesin](content/uploads/g3.jpg)
;; ![Crystaline growth on seed head, Taliesin](content/uploads/g4.jpg)" )

(defn reintegrate-inclusions
"Given a map of the form produced by `process-text`, return a string of HTML text
Expand Down Expand Up @@ -212,13 +191,73 @@
(cs/replace (kw inclusions) "\\/" "/"))))))))


(defn process-text
[^Integer index ^clojure.lang.Associative result fragments processed]
(let [fragment (first fragments)
;; if I didn't find a formatter for a back-tick marked fragment,
;; I need to put the backticks back in.
remarked (if (odd? index) (str "```" fragment "\n```") fragment)
first-token (get-first-token fragment)
kw (if-not (empty? first-token) (keyword first-token))
formatter (if
kw
(try
(read-string (-> config :formatters kw :formatter))
(catch Exception _
(do
(log/info "No formatter found for extension `" kw "`")
;; no extension registered - there sometimes won't be,
;; and it doesn't matter
nil))))]
(cond
(empty? fragments)
;; We've come to the end of the list of fragments. Reassemble them into
;; a single HTML text and pass it back.
(reassemble-text result processed)
formatter
(apply-formatter index result fragments processed fragment first-token formatter)
true
(process-markdown-fragment index result remarked (rest fragments) processed))))

(defn md->html
"Take this `md-src` markdown source, and return a map in which:
1. the key `:content` is bound to the equivalent HTML source;
2. the key `:extensions`. is bound to details of the extensions
used."
[md-src]
(reintegrate-inclusions (process-text md-src)))
"Process this `text`, assumed to be markdown potentially containing both local links
and YAML visualisation specifications, and return a map comprising JSON visualisation
specification, and HTML text with markers for where those should be reinserted.
The map has two top-level keys: `:inclusions`, a map of constructed keywords to
inclusion specifications, and `:text`, an HTML text string with the keywords
present where the corresponding inclusion should be inserted."
[^clojure.lang.Associative context]
(reintegrate-inclusions
(process-text
0
(assoc context :extensions #{})
(cs/split (or (:source context) "") #"```")
'())))


;; (def first-token "pswp")
;; (def kw (keyword "pswp"))
;; (def fragment "pswp
;; ![Frost on a gate, Laurieston](content/uploads/g1.jpg)
;; ![Feathered crystals on snow surface, Taliesin](content/uploads/g2.jpg)
;; ![Feathered snow on log, Taliesin](content/uploads/g3.jpg)
;; ![Crystaline growth on seed head, Taliesin](content/uploads/g4.jpg)")
;; (def index 0)
;; (def formatter (read-string (-> config :formatters kw :formatter)))
;; formatter
;; (eval (list formatter (subs fragment (count first-token)) index))
;; (process-photoswipe (subs fragment (count first-token)) index)

;; (process-text
;; {:source "pswp
;; ![Frost on a gate, Laurieston](content/uploads/g1.jpg)
;; ![Feathered crystals on snow surface, Taliesin](content/uploads/g2.jpg)
;; ![Feathered snow on log, Taliesin](content/uploads/g3.jpg)
;; ![Crystaline growth on seed head, Taliesin](content/uploads/g4.jpg)"} )

;; (process-text {:source (slurp (clojure.java.io/file smeagol.util/content-dir "Extensible Markup.md"))})




Expand Down
19 changes: 11 additions & 8 deletions src/smeagol/routes/wiki.clj
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
(merge (util/standard-params request)
{:title (str (util/get-message :edit-title-prefix request) " " page)
:page page
:side-bar (md->html (slurp (cjio/file util/content-dir side-bar)))
:side-bar (md->html (assoc request :source (slurp (cjio/file util/content-dir side-bar))))
:content (if exists? (slurp file-path) "")
:exists exists?})))))))

Expand Down Expand Up @@ -175,13 +175,15 @@
(keys (-> processed-text :extensions extension-key resource-type))))
(keys (:extensions processed-text))))))

(cjio/file content-dir "vendor/node_modules/photoswipe/dist/photoswipe.min.js")
;; (cjio/file content-dir "vendor/node_modules/photoswipe/dist/photoswipe.min.js")

(def processed-text (md->html (slurp "resources/public/content/Simplified example gallery.md" )))
;; (def processed-text (md->html {:source (slurp "resources/public/content/Simplified example gallery.md" )}))

(preferred-source (-> processed-text :extensions :pswp :scripts :core) :pswp)
;; (preferred-source (-> processed-text :extensions :pswp :scripts :core) :pswp)

(collect-preferred processed-text :scripts)
;; (-> processed-text :extensions)

;; (collect-preferred processed-text :scripts)

(defn wiki-page
"Render the markdown page specified in this `request`, if any. If none found, redirect to edit-page"
Expand All @@ -198,9 +200,10 @@
(do
(log/info (format "Showing page '%s' from file '%s'" page file-path))
(let [processed-text (md->html
(include/expand-include-md
(:includer md-include-system)
(slurp file-path)))]
(assoc request :source
(include/expand-include-md
(:includer md-include-system)
(slurp file-path))))]
(layout/render "wiki.html"
(merge (util/standard-params request)
processed-text
Expand Down
12 changes: 7 additions & 5 deletions src/smeagol/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
(:require [clojure.java.io :as cjio]
[clojure.string :as cs]
[environ.core :refer [env]]
[markdown.core :as md]
[me.raynes.fs :as fs]
[noir.io :as io]
[noir.session :as session]
[scot.weft.i18n.core :as i18n]
[smeagol.authenticate :as auth]
[smeagol.configuration :refer [config]]
[smeagol.formatting :refer [md->html]]
[taoensso.timbre :as log]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -124,8 +124,8 @@
"In `smeagol.util/local-url `" file-path "` is not a servable resource:" any)
(str "404-not-found?path=" file-path))))

(local-url? "vendor/node_modules/photoswipe/dist/photoswipe.min.js")
(local-url? "/home/simon/workspace/smeagol/resources/public/vendor/node_modules/photoswipe/dist/photoswipe.min.js")
;; (local-url? "vendor/node_modules/photoswipe/dist/photoswipe.min.js")
;; (local-url? "/home/simon/workspace/smeagol/resources/public/vendor/node_modules/photoswipe/dist/photoswipe.min.js")

(defn standard-params
"Return a map of standard parameters to pass to the template renderer."
Expand All @@ -134,8 +134,10 @@
{:user user
:admin (auth/get-admin user)
:js-from (:js-from config)
:side-bar (:content (md->html (slurp (cjio/file content-dir "_side-bar.md"))))
:header (:content (md->html (slurp (cjio/file content-dir "_header.md"))))
:side-bar (md/md-to-html-string
(slurp (cjio/file content-dir "_side-bar.md")):heading-anchors true)
:header (md/md-to-html-string
(slurp (cjio/file content-dir "_header.md")) :heading-anchors true)
:version (System/getProperty "smeagol.version")}))


Expand Down
12 changes: 6 additions & 6 deletions test/smeagol/test/formatting.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"[This is a foreign link](http://to.somewhere)")]
(is (= (local-links text) text) "Foreign links should be unchanged"))))

(deftest test-process-text
(testing "The process-text flow"
(let [expected process-test-return-value
actual (process-text "```test
```")]
(is (= actual expected)))))
;; (deftest test-process-text
;; (testing "The process-text flow"
;; (let [expected process-test-return-value
;; actual (process-text "```test
;; ```")]
;; (is (= actual expected)))))

0 comments on commit 0649ecf

Please sign in to comment.