Compare commits

...

3 Commits

1 changed files with 90 additions and 55 deletions

View File

@ -4,34 +4,58 @@
(global pp (fn [x]
(print (fennel.view x))))
; config variables
;;; Configuration. You will need to modify these for your site.
;; Directories
; URL of your site over the gemini protocol.
(var gemini-baseurl "gemini://tilde.town/~nihilazo")
; URL of your site over the HTTP protocol (on the web).
(var html-baseurl "https://itwont.work")
; the directory generated gemini files will be saved to.
(var gemini-outdir "public_gemini")
; the directory generated HTML files will be saved to.
(var html-outdir "public_html")
; the directory that your input content is stored in.
(var content-dir "content")
;; TODO header/footer files instead of variables?
;; Headers and footers
;; Headers and footers are are added in this order:
;; <header>
;; <blog-header> (on the blog index page)
;; <page content>
;; <footer>
; header of the blog index page on gemini
(var gemini-blog-header "# lipu pi jan Niko\n\n")
; footer added to all gemini output
(var gemini-footer "\n=> gemini://tilde.town/~nihilazo Go Home\n")
; header added to the blog index page on the web
(var html-blog-header "<h1>lipu pi jan Niko</h1>\n\n")
; header added to all pages on the web
(var html-header "<!DOCTYPE html><head><link rel=stylesheet href='https://itwont.work/style.css'><title>lipu pi jan Niko</title><body><article>")
; footer added to all pages on the web
(var html-footer "</article><footer>
<a href=https://itwont.work>Go Home</a><br>
<a href='https://webring.xxiivv.com/#random' target='_blank'><img id='webring' src='https://webring.xxiivv.com/icon.white.svg'/></a></footer></body>")
(var ass-feed-header "# Actually Simple Syndication - https://tilde.town/~dzwdz/ass/\n" )
;; Header of the RSS feed. Here you should set your site title (in between the <title> and </title> tags) and name (in between the <name> and </name> tags).
(var rss-feed-header "<?xml version=\"1.0\" encoding=\"utf-8\"?>
<feed xmlns=\"http://www.w3.org/2005/Atom\">
<title>lipu pi jan Niko</title>
<author><name>jan Niko</name></author>\n")
;; END OF CONFIG SECTION
; working variables
(var dirs [])
(var files [])
(var posts [])
; converts a gemini link line to a html link line, with the correct changes to function
(fn html-link [line]
"converts a gemini link line to an html link line, with file extension and protocol link changes if required"
(var url "")
(let [(u name) (line:match "=> ([^%s]+) (.+)")] ; get the url and name
(set url u) ; if the link isn't an offsite gemini link, change .gmi extensions to .html
@ -42,8 +66,8 @@
(string.format "<a href=\"%s%s\">%s</a><br>\n" html-baseurl url name)
(string.format "<a href=\"%s\">%s</a><br>\n" url name))))
; generates log roll page for gemini
(fn generate-gemini-log []
"generates the gemini blog index page"
(var gemini-index gemini-blog-header)
(each [_ p (ipairs posts)]
(let [{: date : title : path} p
@ -51,12 +75,11 @@
(set gemini-index
(.. gemini-index (string.format "=> %s %s %s\n" url date title)))))
(set gemini-index (.. gemini-index gemini-footer))
(let [f (io.open (.. gemini-outdir "/log/index.gmi") :w+)]
(f:write gemini-index)
(f:close)))
(with-open [f (io.open (.. gemini-outdir "/log/index.gmi") :w+)]
(f:write gemini-index)))
; generate rss/atom feeds
(fn generate-rss-feeds []
"generates rss/atom feeds"
(var gemini-rss-feed (.. rss-feed-header "<id>" gemini-baseurl "</id>\n"))
(var html-rss-feed (.. rss-feed-header "<id>" html-baseurl "</id>\n"))
; add last updated info to rss feeds
@ -79,34 +102,31 @@
(set html-rss-feed (.. html-rss-feed "</feed>"))
(set gemini-rss-feed (.. gemini-rss-feed "</feed>"))
; write out rss feeds
(let [f (io.open (.. gemini-outdir "/atom.xml") :w+)]
(f:write gemini-rss-feed)
(f:close))
(let [f (io.open (.. html-outdir "/atom.xml") :w+)]
(f:write html-rss-feed)
(f:close)))
(with-open [f (io.open (.. gemini-outdir "/atom.xml") :w+)]
(f:write gemini-rss-feed))
(with-open [f (io.open (.. html-outdir "/atom.xml") :w+)]
(f:write html-rss-feed)))
; generate ass (https://tilde.town/~dzwdz/ass) feeds
(fn generate-ass-feeds []
(var gemini-ass-feed ass-feed-header)
(var html-ass-feed ass-feed-header)
; add posts to ass feeds
(each [_ p (ipairs posts)]
(let [ {: date : title : path} p]
(set html-ass-feed (.. html-ass-feed
(string.format "%s\t%s%s\t%s\n" date html-baseurl (path:gsub ".gmi" ".html") title)))
(set gemini-ass-feed (.. gemini-ass-feed
(string.format "%s\t%s%s\t%s\n" date gemini-baseurl path title)))))
"generates ass (https://tilde.town/~dzwdz/ass) feeds"
(let [header "# Actually Simple Syndication - https://tilde.town/~dzwdz/ass/\n"]
(var gemini-ass-feed header)
(var html-ass-feed header)
; add posts to ass feeds
(each [_ p (ipairs posts)]
(let [ {: date : title : path} p]
(set html-ass-feed (.. html-ass-feed
(string.format "%s\t%s%s\t%s\n" date html-baseurl (path:gsub ".gmi" ".html") title)))
(set gemini-ass-feed (.. gemini-ass-feed
(string.format "%s\t%s%s\t%s\n" date gemini-baseurl path title)))))
; write out ass feeds
(let [f (io.open (.. gemini-outdir "/feed.ass") :w+)]
(f:write gemini-ass-feed)
(f:close))
(let [f (io.open (.. html-outdir "/feed.ass") :w+)]
(f:write html-ass-feed)
(f:close)))
(with-open [f (io.open (.. gemini-outdir "/feed.ass") :w+)]
(f:write gemini-ass-feed))
(with-open [f (io.open (.. html-outdir "/feed.ass") :w+)]
(f:write html-ass-feed))))
; generates log roll page for html
(fn generate-html-log []
"generates the blog index page for HTML"
(var html-index (.. html-header html-blog-header :<ul>))
(each [_ p (ipairs posts)]
(let [{: date : title : path} p
@ -116,35 +136,44 @@
title))
"</li>"))))
(set html-index (.. html-index "</ul>" html-footer))
(let [f (io.open (.. html-outdir "/log/index.html") :w+)]
(f:write html-index)
(f:close)))
(with-open [f (io.open (.. html-outdir "/log/index.html") :w+)]
(f:write html-index)))
; generate-log generates the log pages and feeds from the posts index
(fn generate-logs []
(table.sort posts (fn [a b]
(> (. a :date) (. b :date))))
(generate-gemini-log)
(generate-html-log)
(generate-ass-feeds)
(generate-rss-feeds))
"generates the log index pages and feeds from the posts index (if there are any posts)"
(when (not= 0 (length posts))
(table.sort posts (fn [a b]
(> (. a :date) (. b :date))))
(generate-gemini-log)
(generate-html-log)
(generate-ass-feeds)
(generate-rss-feeds)))
; index-post adds a log post to the post index
(fn index-post [f]
(fn index-post [f]
"adds a log post from the file f to the post index"
(io.input (.. content-dir f)) ; open file
(let [post {}]
(tset post :path f)
(let [line (io.read) ; read first line, match title
title (line:match "^#%s*([^\n]+)%s*$")]
(tset post :title title))
(let [line (io.read) ; read second line, match date
date (line:match "^%s*(.+)%s*")]
(tset post :date date))
(let [line (io.read)] ; read first line, match title
(if (not= line nil)
(let [title (line:match "^#%s*([^\n]+)%s*$")]
(if (not= title nil)
(tset post :title title)
(error (.. "cannot read title from file " f ". Is it misformatted?"))))
(error (.. "cannot read title from file " f ". Is it missing?" ))))
(let [line (io.read)] ; read second line, match date
(if (not= line nil)
(let [date (line:match "^%s*(%d%d%d%d%-%d%d%-%d%d)%s*")]
(if (not= date nil)
(tset post :date date)
(error (.. "cannot read date from file " f ". Is it misformatted?"))))
(error (.. "cannot read date from file " f ". It is missing?"))))
(table.insert posts post))
(io.close))
; HTML-escapes and trims a string
(fn html-clean [l]
"trims and escapes a string for inclusion in HTML"
(let [s (l:match "%s*(.+)%s*")]
(-> s
(string.gsub "&" "&amp;")
@ -163,6 +192,7 @@
; true if we are in a list, false otherwise
(var blockquoting false)
(fn to-html [line]
"converts a line of gemtext to a line of HTML"
(var prefix "")
(if (and listing (not (line:match "^%*%s")))
(do
@ -209,6 +239,7 @@
; processes, converts, and writes out an input gemini page in html
(fn process-html-page [f]
"processes, converts, and writes out the input page f in html"
(let [infile (.. content-dir f)
outfile (io.open (.. html-outdir (string.gsub f "%.gmi" ".html")) :w+)]
(outfile:write html-header)
@ -221,6 +252,7 @@
; processes and writes out an input gemini page into an output one
(fn process-gemini-page [f]
"processes and writes out the input page f for the gemini protocol"
(let [infile (.. content-dir f)
outfile (io.open (.. gemini-outdir f) :w+)]
(each [l (io.lines infile)]
@ -235,8 +267,8 @@
(outfile:write gemini-footer)
(outfile:close)))
; process-file runs for every input file to process t
(fn process-file [f]
"runs for every input file to process it"
(if (string.match f ".+log/.+%.gmi")
(do
(index-post f)
@ -254,12 +286,12 @@
; process-dir is run for every input directory to copy the tree into output directories
(fn process-dir [f]
"run for every input directory to re-create the directory structure in the output"
(lfs.mkdir (.. gemini-outdir f))
(lfs.mkdir (.. html-outdir f)))
; walk the input directory tree recursively, processing every file and directory using the process-file and process-dir functions. "base" is the directory that is being walked. "position" is the current position in the directory tree.
(fn walk [base position] ; for each file in the current directory
"walks through the input directory tree and populate the `files` and `dirs` tables for future processing"
(each [file (lfs.dir (.. base position))]
(let [fullpath (.. base position "/" file)]
(let [{:mode filemode} (lfs.attributes fullpath)] ; get the mode (file or directory)
@ -272,12 +304,15 @@
(= filemode :file) ; process the file
(table.insert files (.. position "/" file))))))))
; do everything
; main section
; recreate output dirs
(os.execute (.. "rm -rf " gemini-outdir))
(os.execute (.. "rm -rf " html-outdir))
(lfs.mkdir gemini-outdir)
(lfs.mkdir html-outdir)
; walk and process files
(print "getting files")
(walk content-dir "")