From 60c4ec0054730c72097ba50c8efac05e6e08bddd Mon Sep 17 00:00:00 2001 From: Aleksander Madland Stapnes Date: Thu, 2 Feb 2017 00:49:51 -0300 Subject: [PATCH] Add nohighlight to klipsified code blocks. --- src/cryogen_core/compiler.clj | 17 +++++++++++------ src/cryogen_core/klipse.clj | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/cryogen_core/compiler.clj b/src/cryogen_core/compiler.clj index 6403cca..b10f236 100644 --- a/src/cryogen_core/compiler.clj +++ b/src/cryogen_core/compiler.clj @@ -471,21 +471,26 @@ (catch Exception _ (throw (IllegalArgumentException. "Failed to parse config.edn"))))) +(defn klipsify + "Add the klipse html under the :klipse key and adds nohighlight + classes to any code blocks that are to be klipsified. Expects + configuration to be under :klipse, if there's none it does nothing." + [{:keys [klipse content] :as post-or-page}] + (-> post-or-page + (update :klipse klipse/emit content) + (update :content klipse/tag-nohighlight (:settings klipse)))) + (defn compile-assets "Generates all the html and copies over resources specified in the config" [] (println (green "compiling assets...")) (let [{:keys [^String site-url blog-prefix rss-name recent-posts sass-dest keep-files ignored-files previews? author-root-uri theme] :as config} (read-config) - posts (map (fn [{:keys [klipse content] :as post}] - (assoc post :klipse (klipse/emit klipse content))) - (add-prev-next (read-posts config))) + posts (map klipsify (add-prev-next (read-posts config))) posts-by-tag (group-by-tags posts) posts (tag-posts posts config) latest-posts (->> posts (take recent-posts) vec) - pages (map (fn [{:keys [klipse content] :as page}] - (assoc page :klipse (klipse/emit klipse content))) - (read-pages config)) + pages (map klipsify (read-pages config)) home-page (->> pages (filter #(boolean (:home? %))) (first)) diff --git a/src/cryogen_core/klipse.clj b/src/cryogen_core/klipse.clj index 3735c10..e3be3ee 100644 --- a/src/cryogen_core/klipse.clj +++ b/src/cryogen_core/klipse.clj @@ -51,6 +51,22 @@ ;;;;;;;;;;;; ;; klipse +(defn eval-classes + "Takes the :settings map and returns all values that are css class selectors." + [settings] + (filter #(str/starts-with? % ".") (vals settings))) + +(defn tag-nohighlight + "Takes html as a string and a coll of class-selectors and adds + nohighlight to all code blocks that includes one of them." + [html settings] + (letfn [(tag [h clas] + (enlive/sniptest h + [(keyword (str "code" clas))] + (fn [x] + (update-in x [:attrs :class] #(str % " nohighlight")))))] + (reduce tag html (eval-classes settings)))) + (def defaults {:js-src {:min "https://storage.googleapis.com/app.klipse.tech/plugin_prod/js/klipse_plugin.min.js"