Still no actual progress.

This commit is contained in:
Simon Brooke 2020-02-13 21:07:36 +00:00
parent 8032ad60af
commit 37d850d30a
No known key found for this signature in database
GPG key ID: A7A4F18D1D4DF987
7 changed files with 102 additions and 63 deletions

View file

@ -32,7 +32,21 @@
:default-locale "en-GB" ;; default language used for messages
:formatters ;; formatters for processing markdown
;; extensions.
{:vega {:formatter "smeagol.extensions.vega/process-vega"
{:backticks {:formatter "smeagol.formatting/process-backticks"
:scripts {}
:styles {}}
:mermaid {:formatter "smeagol.extensions.mermaid/process-mermaid"
:scripts {:core {:local "vendor/mermaid/dist/mermaid.js"}}
:styles {}}
:pswp {:formatter "smeagol.extensions.photoswipe/process-photoswipe"
:scripts {:core {:local "/vendor/node_modules/photoswipe/dist/photoswipe.min.js"
:remote "https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/photoswipe.min.js"}
:ui {:local "/vendor/node_modules/photoswipe/dist/photoswipe-ui-default.min.js"
:remote "https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/photoswipe-ui-default.min.js"}}
:styles {:core {:local "/vendor/node_modules/photoswipe/dist/photoswipe.css"
:remote "/vendor/node_modules/photoswipe/dist/default-skin/default-skin.css"}}}
:test {:formatter "smeagol.extensions.test/process-test" }
:vega {:formatter "smeagol.extensions.vega/process-vega"
:scripts {:core {:remote "https://cdnjs.cloudflare.com/ajax/libs/vega/5.9.1/vega.min.js"}
:lite {:remote "https://cdnjs.cloudflare.com/ajax/libs/vega-lite/4.1.1/vega-lite.min.js"}
:embed {:remote "https://cdnjs.cloudflare.com/ajax/libs/vega-embed/6.2.2/vega-embed.min.js"}
@ -42,19 +56,6 @@
:lite {:remote "https://cdnjs.cloudflare.com/ajax/libs/vega-lite/4.1.1/vega-lite.min.js"}
:embed {:remote "https://cdnjs.cloudflare.com/ajax/libs/vega-embed/6.2.2/vega-embed.min.js"}
:styles {}}}
:mermaid {:formatter "smeagol.extensions.mermaid/process-mermaid"
:scripts {:core {:local "vendor/mermaid/dist/mermaid.js"}}
:styles {}}
:backticks {:formatter "smeagol.formatting/process-backticks"
:scripts {}
:styles {}}
:pswp {:formatter "smeagol.extensions.photoswipe/process-photoswipe"
:scripts {:core {:local "/vendor/node_modules/photoswipe/dist/photoswipe.min.js"
:remote "https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/photoswipe.min.js"}
:ui {:local "/vendor/node_modules/photoswipe/dist/photoswipe-ui-default.min.js"
:remote "https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/photoswipe-ui-default.min.js"}}
:styles {:core {:local "/vendor/node_modules/photoswipe/dist/photoswipe.css"
:remote "/vendor/node_modules/photoswipe/dist/default-skin/default-skin.css"}}}
}
:log-level :info ;; the minimum logging level; one of
;; :trace :debug :info :warn :error :fatal

View file

@ -176,12 +176,8 @@
(defn process-photoswipe
[^String url-or-pswp-spec ^Integer index]
(let [data (resource-url-or-data->data url-or-pswp-spec)
spec (cs/trim (:data data))
result
spec (cs/trim (:data data))]
(if
(cs/starts-with? spec "![")
(process-simple-photoswipe spec index)
(process-full-photoswipe spec index))]
;; (log/info "process-photoswipe returning `" result "`.")
result
))
(process-full-photoswipe spec index))))

View file

@ -0,0 +1,10 @@
(ns ^{:doc "Very simple extension for testing the extension processing flow."
:author "Simon Brooke"}
smeagol.extensions.test)
(def process-test-return-value "<!-- The test extension has run and this is its output -->")
(defn process-test
[^String fragment ^Integer index]
process-test-return-value)

View file

@ -1,4 +1,4 @@
(ns ^{:doc "Format Semagol's extended markdown format."
(ns ^{:doc "Format vega/vis extensions to Semagol's extended markdown format."
:author "Simon Brooke"}
smeagol.extensions.vega
(:require [clojure.data.json :as json]

View file

@ -93,10 +93,20 @@
fragments
(cons fragment processed)))
(defn deep-merge [v & vs]
"Cripped in its entirety from https://clojuredocs.org/clojure.core/merge."
(letfn [(rec-merge [v1 v2]
(if (and (map? v1) (map? v2))
(merge-with deep-merge v1 v2)
v2))]
(if (some identity vs)
(reduce #(rec-merge %1 %2) v vs)
(last vs))))
(defn apply-formatter
"Within the context of `process-text`, process a fragment for which an explicit
§formatter has been identified.
`formatter` has been identified.
As with `process-text`, this function returns a map with two top-level keys:
`:inclusions`, a map of constructed keywords to inclusion specifications,
@ -104,16 +114,28 @@
corresponding inclusion should be inserted."
[index result fragments processed fragment token formatter]
(let
[kw (keyword (str "inclusion-" index))]
(assoc-in
[ident (keyword (str "inclusion-" index))]
(process-text
(inc index)
(deep-merge
result
(rest fragments)
(cons kw processed))
[:inclusions kw]
(apply formatter (list (subs fragment (count token)) index)))))
{:inclusions {ident (apply formatter (list (subs fragment (count token)) index))}
:extensions (cons (keyword token) (:extensions result))})
fragments
(cons ident processed))))
(apply-formatter
3
{:inclusions {}}
'()
'()
"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)"
"pswp"
smeagol.extensions.photoswipe/process-photoswipe)
(defn process-text
"Process this `text`, assumed to be markdown potentially containing both local links
@ -124,7 +146,7 @@
inclusion specifications, and `:text`, an HTML text string with the keywords
present where the corresponding inclusion should be inserted."
([^String text]
(process-text 0 {:inclusions {}} (cs/split (or 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,
@ -154,24 +176,28 @@
formatter
;; We've found a formatter to apply to the current fragment, and recurse
;; on down the list
(let [result (apply-formatter
index
(let
[ident (keyword (str "inclusion-" index))]
(deep-merge
(process-text
(inc index)
result
fragments
processed
fragment
first-token
formatter)]
;; TODO: consistency: either these things are `extensions`, or
;; they're `formatters`. I incline to the view that they're
;; `:extensions`
(assoc-in result [:extensions kw] (-> config :formatters kw)))
(rest fragments)
(cons ident processed))
{:inclusions {ident (apply formatter (list (subs fragment (count first-token)) index))}
:extensions (cons kw (:extensions result))}))
:else
;; Otherwise process the current fragment as markdown and recurse on
;; down the list
(process-markdown-fragment
index result remarked (rest fragments) processed)))))
(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

View file

@ -61,10 +61,11 @@
:version (System/getProperty "smeagol.version")}))
(defn- raw-get-messages
(def get-messages
"Return the most acceptable messages collection we have given the
`Accept-Language` header in this `request`."
[request]
(memoize
(fn [request]
(let [specifier ((:headers request) "accept-language")
messages (try
(i18n/get-messages specifier "i18n" "en-GB")
@ -78,10 +79,7 @@
{}))]
(merge
messages
config)))
(def get-messages (memoize raw-get-messages))
config)))))
(defn get-message

View file

@ -1,6 +1,7 @@
(ns smeagol.test.formatting
(:require [clojure.test :refer :all]
[smeagol.formatting :refer [local-links no-text-error]]))
[smeagol.formatting :refer [local-links no-text-error]]
[smeagol.extensions.test :refer :all]))
(deftest test-local-links
(testing "Rewriting of local links"
@ -10,3 +11,10 @@
(let [text (str "# This is a heading"
"[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)))))