A big ol' cleanup

This commit is contained in:
Carmen La 2017-01-16 02:37:19 -05:00
parent abf45945d0
commit dec80185e5
11 changed files with 223 additions and 278 deletions

View file

@ -4,16 +4,16 @@
:license {:name "Eclipse Public License" :license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"} :url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"] :dependencies [[org.clojure/clojure "1.8.0"]
[clj-rss "0.2.3"]
[me.raynes/fs "1.4.6"]
[crouton "0.1.2"]
[cheshire "5.6.3"] [cheshire "5.6.3"]
[clj-rss "0.2.3"]
[clj-text-decoration "0.0.3"] [clj-text-decoration "0.0.3"]
[io.aviso/pretty "0.1.33"] [crouton "0.1.2"]
[hiccup "1.0.5"] [enlive "1.1.6"]
[selmer "1.10.3"]
[pandect "0.6.1"]
[hawk "0.2.11"] [hawk "0.2.11"]
[enlive "1.1.6"]] [hiccup "1.0.5"]
[io.aviso/pretty "0.1.33"]
[me.raynes/fs "1.4.6"]
[pandect "0.6.1"]
[selmer "1.10.3"]]
:deploy-repositories [["snapshots" :clojars] :deploy-repositories [["snapshots" :clojars]
["releases" :clojars]]) ["releases" :clojars]])

View file

@ -1,19 +1,17 @@
(ns cryogen-core.compiler (ns cryogen-core.compiler
(:require [selmer.parser :refer [cache-off! render-file]] (:require [clojure.java.io :as io]
[selmer.util :refer [set-custom-resource-path!]]
[io.aviso.exception :refer [write-exception]]
[clojure.java.io :refer [copy file reader writer]]
[clojure.string :as s] [clojure.string :as s]
[text-decoration.core :refer :all] [io.aviso.exception :refer [write-exception]]
[net.cgrand.enlive-html :as enlive] [net.cgrand.enlive-html :as enlive]
[cryogen-core.toc :refer [generate-toc]] [selmer.parser :refer [cache-off! render-file]]
[cryogen-core.sass :as sass] [selmer.util :refer [set-custom-resource-path!]]
[text-decoration.core :refer :all]
[cryogen-core.io :as cryogen-io]
[cryogen-core.markup :as m] [cryogen-core.markup :as m]
[cryogen-core.io :refer [cryogen-core.rss :as rss]
[get-resource find-assets create-folder create-file-recursive create-file wipe-public-folder [cryogen-core.sass :as sass]
copy-resources copy-resources-from-theme path]]
[cryogen-core.sitemap :as sitemap] [cryogen-core.sitemap :as sitemap]
[cryogen-core.rss :as rss]) [cryogen-core.toc :as toc])
(:import java.util.Locale)) (:import java.util.Locale))
(cache-off!) (cache-off!)
@ -36,12 +34,14 @@
looking under the implemented protocol's subdirectory, but fallsback to look looking under the implemented protocol's subdirectory, but fallsback to look
at the templates directory." at the templates directory."
[root mu ignored-files] [root mu ignored-files]
(let [assets (find-assets (path "templates" (m/dir mu) root) (let [assets (cryogen-io/find-assets
(cryogen-io/path "templates" (m/dir mu) root)
(m/ext mu) (m/ext mu)
ignored-files)] ignored-files)]
(if (seq assets) (if (seq assets)
assets assets
(find-assets (path "templates" root) (cryogen-io/find-assets
(cryogen-io/path "templates" root)
(m/ext mu) (m/ext mu)
ignored-files)))) ignored-files))))
@ -66,9 +66,9 @@
([file-name params] ([file-name params]
(page-uri file-name nil params)) (page-uri file-name nil params))
([file-name uri-type {:keys [blog-prefix clean-urls?] :as params}] ([file-name uri-type {:keys [blog-prefix clean-urls?] :as params}]
(let [page-uri (params uri-type) (let [page-uri (get params uri-type)
uri-end (if clean-urls? (s/replace file-name #"(index)?\.html" "/") file-name)] uri-end (if clean-urls? (s/replace file-name #"(index)?\.html" "/") file-name)]
(path "/" blog-prefix page-uri uri-end)))) (cryogen-io/path "/" blog-prefix page-uri uri-end))))
(defn read-page-meta (defn read-page-meta
"Returns the clojure map from the top of a markdown page/post" "Returns the clojure map from the top of a markdown page/post"
@ -82,7 +82,7 @@
"Returns a map with the given page's file-name, metadata and content parsed from "Returns a map with the given page's file-name, metadata and content parsed from
the file with the given markup." the file with the given markup."
[^java.io.File page config markup] [^java.io.File page config markup]
(with-open [rdr (java.io.PushbackReader. (reader page))] (with-open [rdr (java.io.PushbackReader. (io/reader page))]
(let [re-root (re-pattern (str "^.*?(" (:page-root config) "|" (:post-root config) ")/")) (let [re-root (re-pattern (str "^.*?(" (:page-root config) "|" (:post-root config) ")/"))
page-fwd (s/replace (str page) "\\" "/") ;; make it work on Windows page-fwd (s/replace (str page) "\\" "/") ;; make it work on Windows
page-name (s/replace page-fwd re-root "") page-name (s/replace page-fwd re-root "")
@ -101,7 +101,7 @@
{:file-name file-name {:file-name file-name
:content content :content content
:toc (if-let [toc (:toc page-meta)] :toc (if-let [toc (:toc page-meta)]
(generate-toc content :list-type toc))})) (toc/generate-toc content :list-type toc))}))
(defn parse-page (defn parse-page
"Parses a page/post and returns a map of the content, uri, date etc." "Parses a page/post and returns a map of the content, uri, date etc."
@ -121,7 +121,7 @@
(let [date (if (:date page-meta) (let [date (if (:date page-meta)
(.parse (java.text.SimpleDateFormat. (:post-date-format config)) (:date page-meta)) (.parse (java.text.SimpleDateFormat. (:post-date-format config)) (:date page-meta))
(parse-post-date file-name (:post-date-format config))) (parse-post-date file-name (:post-date-format config)))
archive-fmt (java.text.SimpleDateFormat. (get config :archive-group-format "yyyy MMMM") (Locale/getDefault)) archive-fmt (java.text.SimpleDateFormat. (:archive-group-format config "yyyy MMMM") (Locale/getDefault))
formatted-group (.format archive-fmt date)] formatted-group (.format archive-fmt date)]
{:date date {:date date
:formatted-archive-group formatted-group :formatted-archive-group formatted-group
@ -133,13 +133,13 @@
"Returns a sequence of maps representing the data from markdown files of posts. "Returns a sequence of maps representing the data from markdown files of posts.
Sorts the sequence by post date." Sorts the sequence by post date."
[config] [config]
(->> (mapcat (->> (m/markups)
(mapcat
(fn [mu] (fn [mu]
(->> (->>
(find-posts config mu) (find-posts config mu)
(pmap #(parse-post % config mu)) (pmap #(parse-post % config mu))
(remove #(= (:draft? %) true)))) (remove #(= (:draft? %) true)))))
(m/markups))
(sort-by :date) (sort-by :date)
reverse reverse
(drop-while #(and (:hide-future-posts? config) (.after (:date %) (java.util.Date.)))))) (drop-while #(and (:hide-future-posts? config) (.after (:date %) (java.util.Date.))))))
@ -148,12 +148,12 @@
"Returns a sequence of maps representing the data from markdown files of pages. "Returns a sequence of maps representing the data from markdown files of pages.
Sorts the sequence by post date." Sorts the sequence by post date."
[config] [config]
(->> (mapcat (->> (m/markups)
(mapcat
(fn [mu] (fn [mu]
(->> (->>
(find-pages config mu) (find-pages config mu)
(map #(parse-page % config mu)))) (map #(parse-page % config mu)))))
(m/markups))
(sort-by :page-index))) (sort-by :page-index)))
(defn tag-post (defn tag-post
@ -161,7 +161,8 @@
[tags post] [tags post]
(reduce (fn [tags tag] (reduce (fn [tags tag]
(update-in tags [tag] (fnil conj []) (select-keys post [:uri :title :content :date :enclosure]))) (update-in tags [tag] (fnil conj []) (select-keys post [:uri :title :content :date :enclosure])))
tags (:tags post))) tags
(:tags post)))
(defn group-by-tags (defn group-by-tags
"Maps all the tags with a list of posts that contain each tag" "Maps all the tags with a list of posts that contain each tag"
@ -219,26 +220,26 @@
"When `clean-urls?` is set, appends `/index.html` before spit; otherwise just spits." "When `clean-urls?` is set, appends `/index.html` before spit; otherwise just spits."
[file-uri {:keys [clean-urls?]} data] [file-uri {:keys [clean-urls?]} data]
(if clean-urls? (if clean-urls?
(create-file-recursive (path file-uri "index.html") data) (cryogen-io/create-file-recursive (cryogen-io/path file-uri "index.html") data)
(create-file file-uri data))) (cryogen-io/create-file file-uri data)))
(defn compile-pages (defn compile-pages
"Compiles all the pages into html and spits them out into the public folder" "Compiles all the pages into html and spits them out into the public folder"
[{:keys [blog-prefix page-root-uri debug?] :as params} pages] [{:keys [blog-prefix page-root-uri debug?] :as params} pages]
(when-not (empty? pages) (when-not (empty? pages)
(println (blue "compiling pages")) (println (blue "compiling pages"))
(create-folder (path "/" blog-prefix page-root-uri)) (cryogen-io/create-folder (cryogen-io/path "/" blog-prefix page-root-uri))
(doseq [{:keys [uri] :as page} pages] (doseq [{:keys [uri] :as page} pages]
(println "\t-->" (cyan uri)) (println "\t-->" (cyan uri))
(when debug? (when debug?
(println "\t-->" (cyan page))) (println "\t\tdebug-->" (cyan page)))
(write-html uri (write-html uri
params params
(render-file (str "/html/" (:layout page)) (render-file (str "/html/" (:layout page))
(merge params (merge params
{:active-page "pages" {:active-page "pages"
:home false :home false
:servlet-context (path "/" blog-prefix "/") :servlet-context (cryogen-io/path "/" blog-prefix "/")
:page page :page page
:uri uri})))))) :uri uri}))))))
@ -247,28 +248,27 @@
[{:keys [blog-prefix post-root-uri disqus-shortname debug?] :as params} posts] [{:keys [blog-prefix post-root-uri disqus-shortname debug?] :as params} posts]
(when-not (empty? posts) (when-not (empty? posts)
(println (blue "compiling posts")) (println (blue "compiling posts"))
(create-folder (path "/" blog-prefix post-root-uri)) (cryogen-io/create-folder (cryogen-io/path "/" blog-prefix post-root-uri))
(doseq [post posts] (doseq [{:keys [uri] :as post} posts]
(println "\t-->" (cyan (:uri post))) (println "\t-->" (cyan uri))
(println "\t-->" (cyan debug?))
(when debug? (when debug?
(println "\t-->" (cyan post))) (println "\t\tdebug-->" (cyan post)))
(write-html (:uri post) (write-html uri
params params
(render-file (str "/html/" (:layout post)) (render-file (str "/html/" (:layout post))
(merge params (merge params
{:active-page "posts" {:active-page "posts"
:servlet-context (path "/" blog-prefix "/") :servlet-context (cryogen-io/path "/" blog-prefix "/")
:post post :post post
:disqus-shortname disqus-shortname :disqus-shortname disqus-shortname
:uri (:uri post)})))))) :uri uri}))))))
(defn compile-tags (defn compile-tags
"Compiles all the tag pages into html and spits them out into the public folder" "Compiles all the tag pages into html and spits them out into the public folder"
[{:keys [blog-prefix tag-root-uri] :as params} posts-by-tag] [{:keys [blog-prefix tag-root-uri] :as params} posts-by-tag]
(when-not (empty? posts-by-tag) (when-not (empty? posts-by-tag)
(println (blue "compiling tags")) (println (blue "compiling tags"))
(create-folder (path "/" blog-prefix tag-root-uri)) (cryogen-io/create-folder (cryogen-io/path "/" blog-prefix tag-root-uri))
(doseq [[tag posts] posts-by-tag] (doseq [[tag posts] posts-by-tag]
(let [{:keys [name uri]} (tag-info params tag)] (let [{:keys [name uri]} (tag-info params tag)]
(println "\t-->" (cyan uri)) (println "\t-->" (cyan uri))
@ -277,12 +277,13 @@
(render-file "/html/tag.html" (render-file "/html/tag.html"
(merge params (merge params
{:active-page "tags" {:active-page "tags"
:servlet-context (path "/" blog-prefix "/") :servlet-context (cryogen-io/path "/" blog-prefix "/")
:name name :name name
:posts posts :posts posts
:uri uri}))))))) :uri uri})))))))
(defn compile-tags-page [{:keys [blog-prefix] :as params}] (defn compile-tags-page [{:keys [blog-prefix] :as params}]
"Compiles a page with links to each tag page. Spits the page into the public folder"
(println (blue "compiling tags page")) (println (blue "compiling tags page"))
(let [uri (page-uri "tags.html" params)] (let [uri (page-uri "tags.html" params)]
(write-html uri (write-html uri
@ -290,7 +291,7 @@
(render-file "/html/tags.html" (render-file "/html/tags.html"
(merge params (merge params
{:active-page "tags" {:active-page "tags"
:servlet-context (path "/" blog-prefix "/") :servlet-context (cryogen-io/path "/" blog-prefix "/")
:uri uri}))))) :uri uri})))))
(defn content-until-more-marker (defn content-until-more-marker
@ -315,7 +316,7 @@
(defn create-previews (defn create-previews
"Returns a sequence of vectors, each containing a set of post previews" "Returns a sequence of vectors, each containing a set of post previews"
[posts-per-page blocks-per-preview posts] [posts posts-per-page blocks-per-preview]
(->> posts (->> posts
(map #(create-preview blocks-per-preview %)) (map #(create-preview blocks-per-preview %))
(partition-all posts-per-page) (partition-all posts-per-page)
@ -327,38 +328,41 @@
[previews params] [previews params]
(mapv (fn [[prev target next]] (mapv (fn [[prev target next]]
(merge target (merge target
{:prev (if prev (page-uri (path "p" (str (:index prev) ".html")) params) nil) {:prev (if prev (page-uri (cryogen-io/path "p" (str (:index prev) ".html")) params) nil)
:next (if next (page-uri (path "p" (str (:index next) ".html")) params) nil)})) :next (if next (page-uri (cryogen-io/path "p" (str (:index next) ".html")) params) nil)}))
(partition 3 1 (flatten [nil previews nil])))) (partition 3 1 (flatten [nil previews nil]))))
(defn compile-preview-pages (defn compile-preview-pages
"Compiles a series of pages containing 'previews' from each post" "Compiles a series of pages containing 'previews' from each post"
[{:keys [blog-prefix posts-per-page blocks-per-preview] :as params} posts] [{:keys [blog-prefix posts-per-page blocks-per-preview] :as params} posts]
(when-not (empty? posts) (when-not (empty? posts)
(let [previews (-> (create-previews posts-per-page blocks-per-preview posts) (let [previews (-> posts
(create-previews posts-per-page blocks-per-preview)
(create-preview-links params)) (create-preview-links params))
previews (if (> (count previews) 1) (assoc-in previews [1 :prev] (page-uri "index.html" params)) previews)] previews (if (> (count previews) 1)
(create-folder (path "/" blog-prefix "p")) (assoc-in previews [1 :prev] (page-uri "index.html" params))
previews)]
(cryogen-io/create-folder (cryogen-io/path "/" blog-prefix "p"))
(doseq [{:keys [index posts prev next]} previews (doseq [{:keys [index posts prev next]} previews
:let [index-page? (= 1 index)]] :let [index-page? (= 1 index)]]
(write-html (if index-page? (page-uri "index.html" params) (page-uri (path "p" (str index ".html")) params)) (write-html
(if index-page? (page-uri "index.html" params)
(page-uri (cryogen-io/path "p" (str index ".html")) params))
params params
(render-file "/html/previews.html" (render-file "/html/previews.html"
(merge params (merge params
{:active-page "preview" {:active-page "preview"
:home (when index-page? true) :home (when index-page? true)
:servlet-context (path "/" blog-prefix "/") :servlet-context (cryogen-io/path "/" blog-prefix "/")
:posts posts :posts posts
:prev-uri prev :prev-uri prev
:next-uri next}))))))) :next-uri next})))))))
(defn compile-index (defn compile-index
"Compiles the index page into html and spits it out into the public folder" "Compiles the index page into html and spits it out into the public folder"
[{:keys [disqus?] :as params}] [{:keys [disqus? debug? home-page] :as params}]
(println (blue "compiling index")) (println (blue "compiling index"))
(let [uri (page-uri "index.html" params) (let [uri (page-uri "index.html" params)
debug? (-> params :debug?)
home-page (-> params :home-page)
meta {:active-page "home" meta {:active-page "home"
:home true :home true
:disqus? disqus? :disqus? disqus?
@ -366,7 +370,7 @@
:post home-page :post home-page
:page home-page}] :page home-page}]
(when debug? (when debug?
(println "\t-->" (cyan meta))) (println "\t\tdebug-->" (cyan meta)))
(write-html uri (write-html uri
params params
(render-file (str "/html/" (:layout home-page)) (render-file (str "/html/" (:layout home-page))
@ -385,14 +389,14 @@
{:active-page "archives" {:active-page "archives"
:archives true :archives true
:groups (group-for-archive posts) :groups (group-for-archive posts)
:servlet-context (path "/" blog-prefix "/") :servlet-context (cryogen-io/path "/" blog-prefix "/")
:uri uri}))))) :uri uri})))))
(defn compile-authors (defn compile-authors
"For each author, creates a page with filtered posts." "For each author, creates a page with filtered posts."
[{:keys [blog-prefix author-root-uri author] :as params} posts] [{:keys [blog-prefix author-root-uri author] :as params} posts]
(println (blue "compiling authors")) (println (blue "compiling authors"))
(create-folder (path "/" blog-prefix author-root-uri)) (cryogen-io/create-folder (cryogen-io/path "/" blog-prefix author-root-uri))
;; if the post author is empty defaults to config's :author ;; if the post author is empty defaults to config's :author
(doseq [{:keys [author posts]} (group-for-author posts author)] (doseq [{:keys [author posts]} (group-for-author posts author)]
(let [uri (page-uri (str author ".html") :author-root-uri params)] (let [uri (page-uri (str author ".html") :author-root-uri params)]
@ -403,7 +407,7 @@
(merge params (merge params
{:author author {:author author
:groups (group-for-archive posts) :groups (group-for-archive posts)
:servlet-context (path "/" blog-prefix "/") :servlet-context (cryogen-io/path "/" blog-prefix "/")
:uri uri})))))) :uri uri}))))))
(defn tag-posts (defn tag-posts
@ -414,7 +418,7 @@
(defn- template-dir? (defn- template-dir?
"Checks that the dir exists in the templates directory." "Checks that the dir exists in the templates directory."
[dir] [dir]
(.isDirectory (file (str "resources/templates/" dir)))) (.isDirectory (io/file (str "resources/templates/" dir))))
(defn- markup-entries [post-root page-root] (defn- markup-entries [post-root page-root]
(let [entries (for [mu (m/markups) (let [entries (for [mu (m/markups)
@ -427,7 +431,7 @@
[{:keys [post-root page-root] :as config}] [{:keys [post-root page-root] :as config}]
(let [folders (->> (markup-entries post-root page-root) (let [folders (->> (markup-entries post-root page-root)
(filter template-dir?))] (filter template-dir?))]
(copy-resources (cryogen-io/copy-resources
(merge config (merge config
{:resources folders {:resources folders
:ignored-files (map #(re-pattern-from-ext (m/ext %)) (m/markups))})))) :ignored-files (map #(re-pattern-from-ext (m/ext %)) (m/markups))}))))
@ -437,7 +441,7 @@
[] []
(try (try
(let [config (-> "templates/config.edn" (let [config (-> "templates/config.edn"
get-resource cryogen-io/get-resource
slurp slurp
read-string read-string
(update-in [:blog-prefix] (fnil str "")) (update-in [:blog-prefix] (fnil str ""))
@ -464,7 +468,8 @@
"Generates all the html and copies over resources specified in the config" "Generates all the html and copies over resources specified in the config"
[] []
(println (green "compiling assets...")) (println (green "compiling assets..."))
(let [{:keys [^String site-url blog-prefix rss-name recent-posts sass-src sass-dest sass-path compass-path keep-files ignored-files previews? clean-urls? debug? author-root-uri] :as config} (read-config) (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 (add-prev-next (read-posts config)) posts (add-prev-next (read-posts config))
pages (add-prev-next (read-pages config)) pages (add-prev-next (read-pages config))
home-pages (filter #(boolean (:home? %)) pages) home-pages (filter #(boolean (:home? %)) pages)
@ -488,18 +493,18 @@
:archives-uri (page-uri "archives.html" config) :archives-uri (page-uri "archives.html" config)
:index-uri (page-uri "index.html" config) :index-uri (page-uri "index.html" config)
:tags-uri (page-uri "tags.html" config) :tags-uri (page-uri "tags.html" config)
:rss-uri (path "/" blog-prefix rss-name) :rss-uri (cryogen-io/path "/" blog-prefix rss-name)
:site-url (if (.endsWith site-url "/") (.substring site-url 0 (dec (count site-url))) site-url) :site-url (if (.endsWith site-url "/") (.substring site-url 0 (dec (count site-url))) site-url)
:theme-path (str "file:resources/templates/themes/" (:theme config))})] :theme-path (str "file:resources/templates/themes/" theme)})]
(println (blue "debug info")) (println (blue "debug info"))
(println "\t-->" (cyan navbar-pages)) (println "\t-->" (cyan navbar-pages))
(set-custom-resource-path! (:theme-path params)) (set-custom-resource-path! (:theme-path params))
(wipe-public-folder keep-files) (cryogen-io/wipe-public-folder keep-files)
(println (blue "copying theme resources")) (println (blue "copying theme resources"))
(copy-resources-from-theme config) (cryogen-io/copy-resources-from-theme config)
(println (blue "copying resources")) (println (blue "copying resources"))
(copy-resources config) (cryogen-io/copy-resources config)
(copy-resources-from-markup-folders config) (copy-resources-from-markup-folders config)
(compile-pages params pages-without-home) (compile-pages params pages-without-home)
(compile-posts params posts) (compile-posts params posts)
@ -514,19 +519,16 @@
(println (blue "generating authors views")) (println (blue "generating authors views"))
(compile-authors params posts)) (compile-authors params posts))
(println (blue "generating site map")) (println (blue "generating site map"))
(create-file (path "/" blog-prefix "sitemap.xml") (sitemap/generate site-url ignored-files)) (cryogen-io/create-file (cryogen-io/path "/" blog-prefix "sitemap.xml") (sitemap/generate site-url ignored-files))
(println (blue "generating main rss")) (println (blue "generating main rss"))
(create-file (path "/" blog-prefix rss-name) (rss/make-channel config posts)) (cryogen-io/create-file (cryogen-io/path "/" blog-prefix rss-name) (rss/make-channel config posts))
(println (blue "generating filtered rss")) (println (blue "generating filtered rss"))
(rss/make-filtered-channels config posts-by-tag) (rss/make-filtered-channels config posts-by-tag)
(println (blue "compiling sass")) (println (blue "compiling sass"))
(sass/compile-sass->css! (sass/compile-sass->css!
{:path-sass sass-path (merge (select-keys config [:sass-path :compass-path :sass-src :ignored-files])
:path-compass compass-path {:dest-sass (cryogen-io/path ".." "public" blog-prefix sass-dest)
:src-sass sass-src :base-dir "resources/templates/"}))))
:dest-sass (path ".." "public" blog-prefix sass-dest)
:ignored-files ignored-files
:base-dir "resources/templates/"})))
(defn compile-assets-timed [] (defn compile-assets-timed []
(time (time

View file

@ -1,37 +0,0 @@
(ns cryogen-core.github
(:require [cheshire.core :as json])
(:import (org.apache.commons.codec.binary Base64 StringUtils)))
(defn get-gist [gist-uri]
(let [gist-id (last (clojure.string/split gist-uri #"/+")) ;;just need id for git api
gist-resp (try (slurp (str "https://api.github.com/gists/" gist-id))
(catch Exception e {:error (.getMessage e)}))]
(when-not (:error gist-resp)
(if-let [gist (-> (json/parse-string gist-resp)
(get "files")
first ;;todo: optionally get all gist files?
val)]
{:content (get gist "content")
:language (get gist "language")
:name (get gist "filename")
:id gist-id}))))
(defn get-src [git-file]
(let [git-re (re-find #"github.com/(.*)/blob/(.+?)/(.+)" git-file) ;;want second and last now (user/repo,file) for git api
git-res (str "https://api.github.com/repos/" (second git-re) "/contents/" (last git-re))
git-resp (try (slurp git-res)
(catch Exception e {:error (.getMessage e)}))]
(when-not (:error git-resp)
(if-let [git-src (json/parse-string git-resp)]
{:content (String. ^bytes (Base64/decodeBase64 ^String (get git-src "content")) "UTF-8")
:name (get git-src "name")
:uri (get (get git-src "_links") "html")}))))
(defn get-gits-ex []
[(get-gist "https://gist.github.com/viperscape/cec68f0791687f5959f1")
(get-src "https://github.com/viperscape/kuroshio/blob/master/examples/pubsub.clj")])
;(prn (get-gits-ex))

View file

@ -1,6 +1,5 @@
(ns cryogen-core.markup (ns cryogen-core.markup
(:require [clojure.string :as s]) (:require [clojure.string :as s]))
(:import java.util.Collections))
(defonce markup-registry (atom [])) (defonce markup-registry (atom []))
@ -18,7 +17,7 @@
[blog-prefix text] [blog-prefix text]
(if (s/blank? blog-prefix) (if (s/blank? blog-prefix)
text text
(clojure.string/replace text #"href=.?/|src=.?/" #(str (subs % 0 (dec (count %))) blog-prefix "/")))) (s/replace text #"href=.?/|src=.?/" #(str (subs % 0 (dec (count %))) blog-prefix "/"))))
(defn markups (defn markups
"Return a vector of Markup implementations. This is the primary entry point "Return a vector of Markup implementations. This is the primary entry point

View file

@ -1,6 +1,5 @@
(ns cryogen-core.plugins (ns cryogen-core.plugins
(:require [cryogen-core.compiler :refer [compile-assets-timed]] (:require [clojure.edn :as edn]
[clojure.edn :as edn]
[clojure.string :as s] [clojure.string :as s]
[text-decoration.core :refer :all])) [text-decoration.core :refer :all]))

View file

@ -1,7 +1,7 @@
(ns cryogen-core.rss (ns cryogen-core.rss
(:require [clj-rss.core :as rss] (:require [clj-rss.core :as rss]
[text-decoration.core :refer :all] [text-decoration.core :refer :all]
[cryogen-core.io :refer [create-file path]]) [cryogen-core.io :as cryogen-io])
(:import java.util.Date)) (:import java.util.Date))
@ -31,6 +31,6 @@
(defn make-filtered-channels [{:keys [rss-filters blog-prefix] :as config} posts-by-tag] (defn make-filtered-channels [{:keys [rss-filters blog-prefix] :as config} posts-by-tag]
(doseq [filter rss-filters] (doseq [filter rss-filters]
(let [uri (path "/" blog-prefix (str (name filter) ".xml"))] (let [uri (cryogen-io/path "/" blog-prefix (str (name filter) ".xml"))]
(println "\t-->" (cyan uri)) (println "\t-->" (cyan uri))
(create-file uri (make-channel config (get posts-by-tag filter)))))) (cryogen-io/create-file uri (make-channel config (get posts-by-tag filter))))))

View file

@ -1,8 +1,8 @@
(ns cryogen-core.sass (ns cryogen-core.sass
(:require [clojure.java.shell :as shell] (:require [clojure.java.io :as io]
[clojure.java.io :as io] [clojure.java.shell :as shell]
[text-decoration.core :refer :all] [text-decoration.core :refer :all]
[cryogen-core.io :refer [ignore match-re-filter]])) [cryogen-core.io :as cryogen-io]))
(defmacro sh (defmacro sh
[& args] [& args]
@ -11,14 +11,14 @@
(defn sass-installed? (defn sass-installed?
"Checks for the installation of Sass." "Checks for the installation of Sass."
[path-sass] [sass-path]
(= 0 (:exit (sh path-sass "--version")))) (zero? (:exit (sh sass-path "--version"))))
(defn compass-installed? (defn compass-installed?
"Checks for the installation of Compass." "Checks for the installation of Compass."
[path-compass] [compass-path]
(try (try
(= 0 (:exit (sh path-compass "--version"))) (zero? (:exit (sh compass-path "--version")))
(catch java.io.IOException _ (catch java.io.IOException _
false))) false)))
@ -26,49 +26,36 @@
"Given a Diretory, gets files, Filtered to those having scss or sass "Given a Diretory, gets files, Filtered to those having scss or sass
extention. Ignores files matching any ignored regexps." extention. Ignores files matching any ignored regexps."
[base-dir dir ignored-files] [base-dir dir ignored-files]
(let [^java.io.FilenameFilter filename-filter (match-re-filter #"(?i:s[ca]ss$)")] (let [^java.io.FilenameFilter filename-filter (cryogen-io/match-re-filter #"(?i:s[ca]ss$)")]
(->> (.listFiles (io/file base-dir dir) filename-filter) (->> (.listFiles (io/file base-dir dir) filename-filter)
(filter #(not (.isDirectory ^java.io.File %))) (filter #(not (.isDirectory ^java.io.File %)))
(filter (ignore ignored-files)) (filter (cryogen-io/ignore ignored-files))
(map #(.getName ^java.io.File %))))) (map #(.getName ^java.io.File %)))))
(defn compile-sass-file! (defn compile-sass-file!
"Given a sass file which might be in src-sass directory, "Given a sass file which might be in sass-src directory,
output the resulting css in dest-sass. All error handling is output the resulting css in sass-dest. All error handling is
done by sh / launching the sass command." done by sh / launching the sass command."
[{:keys [src-sass [{:keys [sass-src sass-dest sass-path compass-path base-dir]}]
dest-sass
path-sass
path-compass
base-dir]}]
(shell/with-sh-dir base-dir (shell/with-sh-dir base-dir
(if (compass-installed? path-compass) (if (compass-installed? compass-path)
(sh path-sass "--compass" "--update" (str src-sass ":" dest-sass)) (sh sass-path "--compass" "--update" (str sass-src ":" sass-dest))
(sh path-sass "--update" (str src-sass ":" dest-sass))))) (sh sass-path "--update" (str sass-src ":" sass-dest)))))
(defn compile-sass->css! (defn compile-sass->css!
"Given a directory src-sass, looks for all sass files and compiles them into "Given a directory sass-src, looks for all sass files and compiles them into
dest-sass. Prompts you to install sass if he finds sass files and can't find sass-dest. Prompts you to install sass if he finds sass files and can't find
the command. Shows you any problems it comes across when compiling. " the command. Shows you any problems it comes across when compiling. "
[{:keys [src-sass [{:keys [sass-src sass-dest sass-path ignored-files base-dir] :as opts}]
dest-sass (when (seq (find-sass-files base-dir sass-src ignored-files))
path-sass (if (sass-installed? sass-path)
ignored-files
base-dir] :as opts}]
(when-let [sass-files (seq (find-sass-files base-dir src-sass ignored-files))]
(if (sass-installed? path-sass)
;; I found sass files,
;; If sass is installed
(do (do
(println "\t" (cyan src-sass) "-->" (cyan dest-sass)) (println "\t" (cyan sass-src) "-->" (cyan sass-dest))
(let [result (compile-sass-file! opts)] (let [result (compile-sass-file! opts)]
(if (zero? (:exit result)) (if (zero? (:exit result))
;; no problems in sass compilation
(println "Successfully compiled sass files") (println "Successfully compiled sass files")
;; else I show the error
(println (red (:err result)) (println (red (:err result))
(red (:out result)))))) (red (:out result))))))
;; Else I prompt to install Sass
(println "Sass seems not to be installed, but you have scss / sass files in " (println "Sass seems not to be installed, but you have scss / sass files in "
src-sass sass-src
" - You might want to install it here: sass-lang.com")))) " - You might want to install it here: sass-lang.com"))))

View file

@ -1,6 +1,6 @@
(ns cryogen-core.sitemap (ns cryogen-core.sitemap
(:require [clojure.xml :refer [emit]] (:require [clojure.xml :refer [emit]]
[cryogen-core.io :refer [get-resource find-assets]]) [cryogen-core.io :as cryogen-io])
(:import java.util.Date)) (:import java.util.Date))
;;generate sitemaps using the sitemap spec ;;generate sitemaps using the sitemap spec
@ -19,7 +19,7 @@
{:tag :urlset {:tag :urlset
:attrs {:xmlns "http://www.sitemaps.org/schemas/sitemap/0.9"} :attrs {:xmlns "http://www.sitemaps.org/schemas/sitemap/0.9"}
:content :content
(for [^java.io.File f (find-assets "public" ".html" ignored-files)] (for [^java.io.File f (cryogen-io/find-assets "public" ".html" ignored-files)]
{:tag :url {:tag :url
:content :content
[{:tag :loc [{:tag :loc

View file

@ -1,20 +1,20 @@
(ns cryogen-core.watcher (ns cryogen-core.watcher
(:require [clojure.java.io :refer [file]] (:require [clojure.java.io :as io]
[cryogen-core.io :refer [ignore]] [clojure.set :as set]
[pandect.algo.md5 :refer [md5]]
[hawk.core :as hawk] [hawk.core :as hawk]
[clojure.set :as set])) [pandect.algo.md5 :as md5]
[cryogen-core.io :as cryogen-io]))
(defn get-assets [path ignored-files] (defn get-assets [path ignored-files]
(->> path (->> path
file io/file
file-seq file-seq
(filter #(not (.isDirectory ^java.io.File %))) (filter #(not (.isDirectory ^java.io.File %)))
(filter (ignore ignored-files)))) (filter (cryogen-io/ignore ignored-files))))
(defn checksums [path ignored-files] (defn checksums [path ignored-files]
(let [files (get-assets path ignored-files)] (let [files (get-assets path ignored-files)]
(zipmap (map md5 files) files))) (zipmap (map md5/md5 files) files)))
(defn find-changes [old-sums new-sums] (defn find-changes [old-sums new-sums]
(let [old-sum-set (-> old-sums keys set) (let [old-sum-set (-> old-sums keys set)

View file

@ -1,9 +1,8 @@
(ns cryogen-core.compiler-test (ns cryogen-core.compiler-test
(:require [clojure.test :refer :all] (:require [clojure.test :refer :all]
[me.raynes.fs :as fs]
[cryogen-core.compiler :refer :all] [cryogen-core.compiler :refer :all]
[cryogen-core.io :refer [path]] [cryogen-core.markup :as m])
[cryogen-core.markup :as m]
[me.raynes.fs :as fs])
(:import [java.io File])) (:import [java.io File]))
; Test that the content-until-more-marker return nil or correct html text. ; Test that the content-until-more-marker return nil or correct html text.

View file

@ -1,7 +1,6 @@
(ns cryogen-core.toc-test (ns cryogen-core.toc-test
(:require [clojure.test :refer :all] (:require [clojure.test :refer :all]
[clojure.string :refer [join]] [clojure.string :as s]
[clojure.zip :as z]
[crouton.html :as html] [crouton.html :as html]
[hiccup.core :as hiccup] [hiccup.core :as hiccup]
[cryogen-core.toc :refer :all])) [cryogen-core.toc :refer :all]))
@ -9,8 +8,6 @@
; Reimport private functions ; Reimport private functions
(def get-headings #'cryogen-core.toc/get-headings) (def get-headings #'cryogen-core.toc/get-headings)
(def make-toc-entry #'cryogen-core.toc/make-toc-entry) (def make-toc-entry #'cryogen-core.toc/make-toc-entry)
(def zip-toc-tree-to-insertion-point #'cryogen-core.toc/zip-toc-tree-to-insertion-point)
(def insert-toc-tree-entry #'cryogen-core.toc/insert-toc-tree-entry)
(def build-toc-tree #'cryogen-core.toc/build-toc-tree) (def build-toc-tree #'cryogen-core.toc/build-toc-tree)
(def build-toc #'cryogen-core.toc/build-toc) (def build-toc #'cryogen-core.toc/build-toc)
@ -46,11 +43,13 @@
(deftest test-build-toc (deftest test-build-toc
(let [simplest-header [:div [:h2 [:a {:name "test"}] "Test"]] (let [simplest-header [:div [:h2 [:a {:name "test"}] "Test"]]
no-headers [:div [:p "This is not a header"]] no-headers [:div [:p "This is not a header"]]
closing-header-larger-than-opening-1 closing-header-larger-than-opening-1
[:div [:h2 [:a {:name "starting_low"}] [:div [:h2 [:a {:name "starting_low"}]
"Starting Low"] "Starting Low"]
[:h1 [:a {:name "finishing_high"}] [:h1 [:a {:name "finishing_high"}]
"Finishing High"]] "Finishing High"]]
closing-header-larger-than-opening-2 closing-header-larger-than-opening-2
[:div [:h2 [:a {:name "starting_low"}] [:div [:h2 [:a {:name "starting_low"}]
"Starting Low"] "Starting Low"]
@ -61,38 +60,35 @@
[:h2 [:a {:name "to_the_top"}] [:h2 [:a {:name "to_the_top"}]
"To the top"]]] "To the top"]]]
(is (= [:ol.content (seq [[:li [:a {:href "#test"} "Test"]]])] (is (= [:ol.content (seq [[:li [:a {:href "#test"} "Test"]]])]
(-> simplest-header parse-to-headings build-toc-tree (-> simplest-header
(build-toc :ol)))) (parse-to-headings)
(is (nil? (build-toc-tree)
(-> no-headers parse-to-headings build-toc-tree
(build-toc :ol)))) (build-toc :ol))))
(is (-> no-headers
(parse-to-headings)
(build-toc-tree)
(build-toc :ol)
(nil?)))
(is (= [:ol.content (seq [[:li [:a {:href "#starting_low"} "Starting Low"]] (is (= [:ol.content (seq [[:li [:a {:href "#starting_low"} "Starting Low"]]
[:li [:a {:href "#finishing_high"} "Finishing High"]]])] [:li [:a {:href "#finishing_high"} "Finishing High"]]])]
(-> closing-header-larger-than-opening-1 (-> closing-header-larger-than-opening-1
parse-to-headings (parse-to-headings)
build-toc-tree (build-toc-tree)
(build-toc :ol))) (build-toc :ol)))
"No outer header should be less indented than the first header tag.") "No outer header should be less indented than the first header tag.")
(is (= [:ul.content (is (= [:ul.content
(seq [ (seq [(seq [[:li [:a {:href "#starting_low"} "Starting Low"]]
(seq [
[:li [:a {:href "#starting_low"} "Starting Low"]]
[:ul [:ul
(seq [ (seq [[:li [:a {:href "#jumping_in"} "Jumping Right In"]]
[:li [:a {:href "#jumping_in"} "Jumping Right In"]] [:li [:a {:href "#pulling_back"} "But then pull back"]]])]])
[:li [:a {:href "#pulling_back"} "But then pull back"]] [:li [:a {:href "#to_the_top"} "To the top"]]])]
])
] ])
[:li [:a {:href "#to_the_top"} "To the top"]]
])]
(-> closing-header-larger-than-opening-2 (-> closing-header-larger-than-opening-2
parse-to-headings (parse-to-headings)
build-toc-tree (build-toc-tree)
(build-toc :ul))) (build-toc :ul)))
(join "" ["Inner headers can be more indented, " (s/join "" ["Inner headers can be more indented, "
"but outer headers cannot be less indented " "but outer headers cannot be less indented "
"than the original header."])) "than the original header."]))))
))
(deftest test-generate-toc (deftest test-generate-toc