@ -2,20 +2,16 @@
( :require [ clojure.java.io :as io ]
[ clojure.string :as string ]
[ 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 ) )
( def input-root "resources/page" )
( def output-root "target/page" )
( defn filename [ file ]
( -> file
.toPath
.getFileName
.toString ) )
( defn is-md-file? [ file ]
( .endsWith ( filename file ) ".md" ) )
( .endsWith ( core/filename file ) ".md" ) )
( defn md-files [ root ]
( ->> root
@ -29,45 +25,32 @@
( defn index-template [ ]
( slurp ( str input-root "/index.html" ) ) )
( defn clean-name [ filename ]
( let [ end ( - ( count filename ) 3 )
start ( inc ( or ( string/index-of filename "+" ) -1 ) ) ]
( subs filename start end ) ) )
( defn navcode [ sites ]
( let [ sorted-sites ( sort-by # ( filename % ) sites )
clean-file-name-fn # ( clean-name ( filename % ) )
nav-line-fn # ( str "<li>"
"<a href=\""
( clean-file-name-fn % )
".html\">"
( clean-file-name-fn % )
"</a></li>" ) ]
( str "<ol>"
( reduce # ( str %1 ( nav-line-fn %2 ) )
""
sorted-sites )
"</ol>" ) ) )
( comment
( defn navcode [ sites ]
( let [ sorted-sites ( sort-by # ( core/filename % ) sites )
clean-file-name-fn # ( core/clean-name ( core/filename % ) )
nav-line-fn # ( str "<li>"
"<a href=\""
( clean-file-name-fn % )
".html\">"
( clean-file-name-fn % )
"</a></li>" ) ]
( str "<ol>"
( reduce # ( str %1 ( nav-line-fn %2 ) )
""
sorted-sites )
"</ol>" ) ) ) )
( defn site-destination [ filename ]
( str output-root
"/"
( c lean-name filename )
( c ore/c lean-name filename )
".html" ) )
( comment
( site-destination "01+allo.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---" )
( defn page-metadata [ content ]
@ -91,28 +74,102 @@
( page-content "---\nDINGE\n---\nwulle" )
( 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 ]
( 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
( particles-in-content "asd &particle:hullu" ) )
( defn now-str [ ]
( .toString ( LocalDateTime/now ) ) )
( defn particle-content [ particle ]
( -> ( str input-root "/particles/" particle ".md" )
io/file
slurp
render-markdown ) )
( defn fill-in-placeholders [ context html page ]
( 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 [ html page ]
( let [ simple ( -> html
( string/replace # "&:nav" ( :navcode context "" ) )
;; (string/replace #"&:nav" (:navcode context "") )
( string/replace # "&:content" ( :content page "" ) )
( string/replace # "&:title" ( get-in page [ :metadata :title ] "" ) )
( string/replace # "&:generated-at" ( now-str ) ) )
particles ( particles-in-content simple ) ]
( reduce ( fn [ result particle ]
( string/replace result
( first particle )
( particle-content ( second particle ) ) ) )
simple
particles ) ) )
particles ( particles-in-content simple )
last-blog-sites ( last-blog-sites-in-content simple )
particle-fn ( fn [ result particle ]
( string/replace
result
( first particle )
( particle-content ( second particle ) ) ) )
blog-sites-fn ( fn [ result [ match-str blog-name ] ]
( string/replace
result
match-str
( blog-sites-preview blog-name ) ) ) ]
( as-> simple $
( reduce particle-fn $ particles )
( reduce blog-sites-fn $ last-blog-sites ) ) ) )
( comment
( fill-in-placeholders { :navcode "hhh" }
@ -123,19 +180,13 @@
{ :content "dinge" } )
)
( defn write-sites [ context template sites ]
( defn write-sites [ template sites ]
( doseq [ site sites
:let [ translated ( fill-in-placeholders context template site )
filename ( filename ( :file site ) )
:let [ translated ( fill-in-placeholders template site )
filename ( core/ filename ( :file site ) )
destination ( site-destination filename ) ] ]
( 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 ]
( doseq [ file ( file-seq dir ) ]
@ -150,40 +201,43 @@
io/file
.listFiles
( filter # ( .isDirectory % ) )
( sort-by # ( filename % ) ) ) )
( sort-by # ( core/ filename % ) ) ) )
( comment
( blog-dirs ) )
( defn render-blog [ context template dir ]
( let [ dir-name ( filename dir )
( defn render-blog [ template dir ]
( let [ dir-name ( core/ filename dir )
index-file ( blog/index-file dir-name input-root )
config ( merge ( blog/config dir-name
( analysed-page index-file ) )
config ( merge ( blog/config dir-name )
{ :dir dir } )
blog-sites ( ->> ( blog/sites-dir dir-name input-root )
md-files
( map analysed-page ) ) ]
blog-sites ( blog-sites dir-name ) ]
( blog/write-files config
output-root
blog-sites
( partial fill-in-placeholders
context template) )
template) )
config ) )
( defn copy-resources [ ]
( fs/copy-dir ( str input-root
"/resources" )
( str output-root
"/resources" ) ) )
( 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 ]
( 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" ) )
rendered-sites ( map analysed-page sites )
template ( index-template )
context { :navcode ( navcode sites ) } ]
( write-sites context template rendered-sites )
( for [ blog-dir ( blog-dirs ) ]
( render-blog context template blog-dir ) ) ) )
template ( index-template ) ]
( write-sites template rendered-sites )
( doseq [ blog-dir ( blog-dirs ) ]
( render-blog template blog-dir ) ) ) )
( comment
( render { } )
( doall ( navcode ( md-files "resources/user/sites" ) ) ) )
( render { } ) )