From 40ab296d1a4c7130323e48f0363cea8e424d9b4d Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Thu, 13 Feb 2020 14:44:52 +0000 Subject: [PATCH] #45: OK, it doesn't work, but it's close. Still getting fragment index instead of fragment text. --- resources/config.edn | 28 ++++++++++++++--- resources/templates/wiki.html | 20 ++++-------- src/smeagol/formatting.clj | 14 +++++++-- src/smeagol/routes/wiki.clj | 59 +++++++++++++++++++++++++++-------- src/smeagol/util.clj | 4 +-- 5 files changed, 88 insertions(+), 37 deletions(-) diff --git a/resources/config.edn b/resources/config.edn index 6ff0faf..7c462b3 100644 --- a/resources/config.edn +++ b/resources/config.edn @@ -32,11 +32,29 @@ :default-locale "en-GB" ;; default language used for messages :formatters ;; formatters for processing markdown ;; extensions. - {:vega {:formatter "smeagol.extensions.vega/process-vega" } - :vis {:formatter "smeagol.extensions.vega/process-vega" } - :mermaid {:formatter "smeagol.extensions.mermaid/process-mermaid" } - :backticks {:formatter "smeagol.formatting/process-backticks" } - :pswp {:formatter "smeagol.extensions.photoswipe/process-photoswipe" } + {: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"} + :styles {}}} + :vis {: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"} + :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 diff --git a/resources/templates/wiki.html b/resources/templates/wiki.html index cf0655e..7342854 100644 --- a/resources/templates/wiki.html +++ b/resources/templates/wiki.html @@ -1,20 +1,12 @@ {% extends "templates/base.html" %} {% block extra-headers %} - - - - {% style "/vendor/node_modules/photoswipe/dist/photoswipe.css" %} - {% style "/vendor/node_modules/photoswipe/dist/default-skin/default-skin.css" %} - - - - - - - - + {% for script in scripts %} + + {% endfor %} + {% for style in styles %} + + {% endfor %} {% endblock %} {% block content %} diff --git a/src/smeagol/formatting.clj b/src/smeagol/formatting.clj index 8eff3ec..115a2ca 100644 --- a/src/smeagol/formatting.clj +++ b/src/smeagol/formatting.clj @@ -182,7 +182,7 @@ fragment first-token formatter)] - (assoc result :extensions (cons kw (:extensions result)))) + (assoc-in result [:extensions kw] (-> config :formatters kw))) true ;; Otherwise process the current fragment as markdown and recurse on ;; down the list @@ -194,7 +194,12 @@ "Given a map of the form produced by `process-text`, return a string of HTML text with the inclusions (if any) reintegrated." ([processed-text] - (reintegrate-inclusions (:inclusions processed-text) (:text processed-text))) + (assoc + processed-text + :content + (reintegrate-inclusions + (:inclusions processed-text) + (:text processed-text)))) ([inclusions text] (let [ks (keys inclusions)] (if (empty? (keys inclusions)) @@ -213,7 +218,10 @@ (defn md->html - "Take this markdown source, and return 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))) diff --git a/src/smeagol/routes/wiki.clj b/src/smeagol/routes/wiki.clj index e33cbdc..e4ca7ba 100644 --- a/src/smeagol/routes/wiki.clj +++ b/src/smeagol/routes/wiki.clj @@ -25,6 +25,7 @@ [smeagol.uploads :as ul] [taoensso.timbre :as log] [com.stuartsierra.component :as component] + [smeagol.configuration :refer [config]] [smeagol.include.resolve-local-file :as resolve] [smeagol.include :as include])) @@ -123,6 +124,34 @@ (include/new-includer) [:resolver])))) +(defn preferred-source + "Here, `component` is expected to be a map with two keys, `:local` and + `:remote`. If the value of `:extensions-from` in `config.edn` is remote + AND the value of `:remote` is not nil, then the value of `:remote` will + be returned. Otherwise, if the value of `:local` is nil and the value of + `:remote` is non-nil, the value of `:remote` will be returned. By default, + the value of `:local` will be returned." + [component] + (let [l (:local component) ;; TODO: look at the trick in Selmer to get relative URL + r (:remote component)] + (cond + (= (:extensions-from config) :remote) (if (empty? r) l r) + (empty? l) r + :else l))) + +(defn collect-preferred + "From extensions referenced in this `processed-text`, extract the preferred + URLs for this keyword `k`, expected to be either `:scripts` or `:styles`." + [processed-text k] + (set + (remove + nil? + (map + preferred-source + (apply + concat + (map vals (map k (vals (:extensions processed-text))))))))) + (defn wiki-page "Render the markdown page specified in this `request`, if any. If none found, redirect to edit-page" [request] @@ -134,19 +163,23 @@ file-name (str page ".md") file-path (cjio/file util/content-dir file-name) exists? (.exists (clojure.java.io/as-file file-path))] - (cond exists? - (do - (log/info (format "Showing page '%s' from file '%s'" page file-path)) - (layout/render "wiki.html" - (merge (util/standard-params request) - {:title page - :page page - :content (md->html - (include/expand-include-md - (:includer md-include-system) - (slurp file-path))) - :editable true}))) - true (response/redirect (str "/edit?page=" page)))))) + (if exists? + (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)))] + (layout/render "wiki.html" + (merge (util/standard-params request) + processed-text + {:title page + :scripts (collect-preferred processed-text :scripts) + :styles (collect-preferred processed-text :styles) + :page page + :editable true})))) + ;else + (response/redirect (str "/edit?page=" page)))))) (defn history-page diff --git a/src/smeagol/util.clj b/src/smeagol/util.clj index 0a5863a..972a0a7 100644 --- a/src/smeagol/util.clj +++ b/src/smeagol/util.clj @@ -56,8 +56,8 @@ {:user user :admin (auth/get-admin user) :js-from (:js-from config) - :side-bar (md->html (slurp (cjio/file content-dir "_side-bar.md"))) - :header (md->html (slurp (cjio/file content-dir "_header.md"))) + :side-bar (:content (md->html (slurp (cjio/file content-dir "_side-bar.md")))) + :header (:content (md->html (slurp (cjio/file content-dir "_header.md")))) :version (System/getProperty "smeagol.version")}))