@ -2,20 +2,16 @@
( :require [ clojure.java.io :as io ]
( :require [ clojure.java.io :as io ]
[ clojure.string :as string ]
[ clojure.string :as string ]
[ markdown.core :as md ]
[ markdown.core :as md ]
[ equilibrium.blog :as blog ] )
[ equilibrium.blog :as blog ]
[ equilibrium.core :as core ]
[ me.raynes.fs :as fs ] )
( :import java.time.LocalDateTime ) )
( :import java.time.LocalDateTime ) )
( def input-root "resources/page" )
( def input-root "resources/page" )
( def output-root "target/page" )
( def output-root "target/page" )
( defn filename [ file ]
( -> file
.toPath
.getFileName
.toString ) )
( defn is-md-file? [ file ]
( defn is-md-file? [ file ]
( .endsWith ( filename file ) ".md" ) )
( .endsWith ( core/filename file ) ".md" ) )
( defn md-files [ root ]
( defn md-files [ root ]
( ->> root
( ->> root
@ -29,14 +25,10 @@
( defn index-template [ ]
( defn index-template [ ]
( slurp ( str input-root "/index.html" ) ) )
( slurp ( str input-root "/index.html" ) ) )
( defn clean-name [ filename ]
( comment
( let [ end ( - ( count filename ) 3 )
start ( inc ( or ( string/index-of filename "+" ) -1 ) ) ]
( subs filename start end ) ) )
( defn navcode [ sites ]
( defn navcode [ sites ]
( let [ sorted-sites ( sort-by # ( filename % ) sites )
( let [ sorted-sites ( sort-by # ( core/filename % ) sites )
clean-file-name-fn # ( c lean-name ( filename % ) )
clean-file-name-fn # ( core/clean-name ( core/filename % ) )
nav-line-fn # ( str "<li>"
nav-line-fn # ( str "<li>"
"<a href=\""
"<a href=\""
( clean-file-name-fn % )
( clean-file-name-fn % )
@ -47,27 +39,18 @@
( reduce # ( str %1 ( nav-line-fn %2 ) )
( reduce # ( str %1 ( nav-line-fn %2 ) )
""
""
sorted-sites )
sorted-sites )
"</ol>" ) ) )
"</ol>" ) ) ) )
( defn site-destination [ filename ]
( defn site-destination [ filename ]
( str output-root
( str output-root
"/"
"/"
( c lean-name filename )
( c ore/c lean-name filename )
".html" ) )
".html" ) )
( comment
( comment
( site-destination "01+allo.md" )
( site-destination "01+allo.md" )
( site-destination "aside.md" ) )
( site-destination "aside.md" ) )
( defn particle-content [ particle ]
( -> ( str input-root "/particles/" particle ".md" )
io/file
slurp
render-markdown ) )
( comment
( particle-content "aside" ) )
( def metadata-regex # "^---\n(.+?)\n---" )
( def metadata-regex # "^---\n(.+?)\n---" )
( defn page-metadata [ content ]
( defn page-metadata [ content ]
@ -91,28 +74,102 @@
( page-content "---\nDINGE\n---\nwulle" )
( page-content "---\nDINGE\n---\nwulle" )
( page-content "aiaiaiai" ) )
( page-content "aiaiaiai" ) )
( defn now-str [ ]
( .toString ( LocalDateTime/now ) ) )
( defn file->relpath [ file ]
( let [ fullpath ( -> file .toPath .toString )
root-pos ( string/index-of fullpath input-root ) ]
( subs fullpath
( + root-pos ( count input-root ) 1 ) ) ) )
( defn analysed-page [ file ]
( let [ content ( slurp file )
relpath ( file->relpath file )
blog? ( string/starts-with? relpath "blogs/" )
blog-dir ( when blog?
( -> relpath io/file .toPath
( .subpath 1 2 ) .toString ) )
filename ( core/clean-name ( core/filename file ) )
link ( if blog?
( str blog-dir "/" filename ".html" )
( str filename ".html" ) ) ]
{ :file file
:relpath relpath
:blog? blog?
:link link
:filename ( core/filename file )
:content ( -> content page-content render-markdown )
:metadata ( page-metadata content ) } ) )
( defn particles-in-content [ content ]
( defn particles-in-content [ content ]
( re-seq # "&:particle:([^\s]+)" content ) )
( re-seq # "&:particle:([^\s<]+)" content ) )
( defn last-blog-sites-in-content [ content ]
( re-seq # "&:last-blog-sites:([^\s<]+)" content ) )
( comment
( comment
( particles-in-content "asd &particle:hullu" ) )
( particles-in-content "asd &particle:hullu" ) )
( defn now-str [ ]
( defn particle-content [ particle ]
( .toString ( LocalDateTime/now ) ) )
( -> ( str input-root "/particles/" particle ".md" )
io/file
slurp
render-markdown ) )
( defn blog-sites [ dir-name ]
( ->> ( blog/sites-dir dir-name input-root )
md-files
( map analysed-page )
( sort-by :filename ) ) )
( comment
( particle-content "aside" ) )
( defn blog-sites-preview [ blog-name ]
( reduce ( fn [ res site ]
( let [ content ( :content site )
divider-pos ( string/index-of content "<p><blink></p>" )
length ( count content )
end ( or divider-pos length )
pre-content ( subs content 0 end )
article-link ( str "<a href=\""
( :link site )
"\">↪</a>" ) ]
( str res
"<article>"
pre-content
"<p class=\"article-link\">"
article-link
"</p>"
"</article>" ) ) )
""
( ->> blog-name
blog-sites
( take-last 5 )
reverse ) ) )
( defn fill-in-placeholders [ context html page ]
( defn fill-in-placeholders [ html page ]
( let [ simple ( -> html
( let [ simple ( -> html
( string/replace # "&:nav" ( :navcode context "" ) )
;; (string/replace #"&:nav" (:navcode context "") )
( string/replace # "&:content" ( :content page "" ) )
( string/replace # "&:content" ( :content page "" ) )
( string/replace # "&:title" ( get-in page [ :metadata :title ] "" ) )
( string/replace # "&:title" ( get-in page [ :metadata :title ] "" ) )
( string/replace # "&:generated-at" ( now-str ) ) )
( string/replace # "&:generated-at" ( now-str ) ) )
particles ( particles-in-content simple ) ]
particles ( particles-in-content simple )
( reduce ( fn [ result particle ]
last-blog-sites ( last-blog-sites-in-content simple )
( string/replace result
particle-fn ( fn [ result particle ]
( string/replace
result
( first particle )
( first particle )
( particle-content ( second particle ) ) ) )
( particle-content ( second particle ) ) ) )
simple
blog-sites-fn ( fn [ result [ match-str blog-name ] ]
particles ) ) )
( string/replace
result
match-str
( blog-sites-preview blog-name ) ) ) ]
( as-> simple $
( reduce particle-fn $ particles )
( reduce blog-sites-fn $ last-blog-sites ) ) ) )
( comment
( comment
( fill-in-placeholders { :navcode "hhh" }
( fill-in-placeholders { :navcode "hhh" }
@ -123,19 +180,13 @@
{ :content "dinge" } )
{ :content "dinge" } )
)
)
( defn write-sites [ context template sites ]
( defn write-sites [ template sites ]
( doseq [ site sites
( doseq [ site sites
:let [ translated ( fill-in-placeholders context template site )
:let [ translated ( fill-in-placeholders template site )
filename ( filename ( :file site ) )
filename ( core/ filename ( :file site ) )
destination ( site-destination filename ) ] ]
destination ( site-destination filename ) ] ]
( spit destination translated ) ) )
( spit destination translated ) ) )
( defn analysed-page [ file ]
( let [ content ( slurp file ) ]
{ :file file
:filename ( filename file )
:content ( -> content page-content render-markdown )
:metadata ( page-metadata content ) } ) )
( defn clear-dir [ dir ]
( defn clear-dir [ dir ]
( doseq [ file ( file-seq dir ) ]
( doseq [ file ( file-seq dir ) ]
@ -150,40 +201,43 @@
io/file
io/file
.listFiles
.listFiles
( filter # ( .isDirectory % ) )
( filter # ( .isDirectory % ) )
( sort-by # ( filename % ) ) ) )
( sort-by # ( core/ filename % ) ) ) )
( comment
( comment
( blog-dirs ) )
( blog-dirs ) )
( defn render-blog [ context template dir ]
( defn render-blog [ template dir ]
( let [ dir-name ( filename dir )
( let [ dir-name ( core/ filename dir )
index-file ( blog/index-file dir-name input-root )
index-file ( blog/index-file dir-name input-root )
config ( merge ( blog/config dir-name
config ( merge ( blog/config dir-name )
( analysed-page index-file ) )
{ :dir dir } )
{ :dir dir } )
blog-sites ( ->> ( blog/sites-dir dir-name input-root )
blog-sites ( blog-sites dir-name ) ]
md-files
( map analysed-page ) ) ]
( blog/write-files config
( blog/write-files config
output-root
output-root
blog-sites
blog-sites
( partial fill-in-placeholders
( partial fill-in-placeholders
context template) )
template) )
config ) )
config ) )
( defn copy-resources [ ]
( fs/copy-dir ( str input-root
"/resources" )
( str output-root
"/resources" ) ) )
( defn render
( defn render
"Renders all markdown pages under resources/user using the structure in resources/user/structure to target/page"
" Renders all markdown pages under resources/user using the structure in
resources/user/structure to target/page "
[ project ]
[ project ]
( clear-dir ( io/file output-root ) )
( clear-dir ( io/file output-root ) )
( io/make-parents output-root )
( io/make-parents ( str output-root "/a" ) )
( copy-resources )
( let [ sites ( md-files ( str input-root "/sites" ) )
( let [ sites ( md-files ( str input-root "/sites" ) )
rendered-sites ( map analysed-page sites )
rendered-sites ( map analysed-page sites )
template ( index-template )
template ( index-template ) ]
context { :navcode ( navcode sites ) } ]
( write-sites template rendered-sites )
( write-sites context template rendered-sites )
( doseq [ blog-dir ( blog-dirs ) ]
( for [ blog-dir ( blog-dirs ) ]
( render-blog template blog-dir ) ) ) )
( render-blog context template blog-dir ) ) ) )
( comment
( comment
( render { } )
( render { } ) )
( doall ( navcode ( md-files "resources/user/sites" ) ) ) )