Skip to content

Commit

Permalink
#45: OK, it doesn't work, but it's close.
Browse files Browse the repository at this point in the history
Still getting fragment index instead of fragment text.
  • Loading branch information
simon-brooke committed Feb 13, 2020
1 parent 2f22b73 commit 40ab296
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 37 deletions.
28 changes: 23 additions & 5 deletions resources/config.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 6 additions & 14 deletions resources/templates/wiki.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
{% extends "templates/base.html" %}

{% block extra-headers %}
<!-- ifequal js-from ":cloudflare" -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/photoswipe.min.js"></script>
{% style "/vendor/node_modules/photoswipe/dist/photoswipe.css" %}
{% style "/vendor/node_modules/photoswipe/dist/default-skin/default-skin.css" %}
<script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/8.4.6/mermaid.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega/5.9.1/vega.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega-lite/4.1.1/vega-lite.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega-embed/6.2.2/vega-embed.min.js"></script>
<!-- else -->
<!-- TODO: currently `lein npm install` fails to build packages on all platforms, and
fails SILENTLY. Consequently setting js-from to local is not advised. Investigting. -->
<!-- script "vendor/mermaid/dist/mermaid.js" %} -->
<!-- endifequal -->
{% for script in scripts %}
<script src="{{script}}"></script>
{% endfor %}
{% for style in styles %}
<link href="{{style}}" rel="stylesheet" type="text/css" />
{% endfor %}
{% endblock %}

{% block content %}
Expand Down
14 changes: 11 additions & 3 deletions src/smeagol/formatting.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand All @@ -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)))

Expand Down
59 changes: 46 additions & 13 deletions src/smeagol/routes/wiki.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/smeagol/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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")}))


Expand Down

0 comments on commit 40ab296

Please sign in to comment.