From c5fe6f28334d2ac1df7a061d30d3e03a5f1163ea Mon Sep 17 00:00:00 2001 From: sejo Date: Sun, 27 Mar 2022 17:55:43 -0600 Subject: [PATCH] lua generator --- generasitio.lua | 488 ++++++++++++++++++++++++++++++++++++ templates/estilo.css | 100 ++++++++ templates/feedheader.gmi | 9 + templates/gematomheader.txt | 9 + templates/index.gmi | 4 + templates/logheader.gmi | 9 + templates/pagefooter.gmi | 5 + templates/pagefooter.html | 15 ++ templates/pageheader.gmi | 2 + templates/pageheader.html | 29 +++ templates/pageincoming.gmi | 3 + templates/pageincoming.html | 4 + templates/twheader.txt | 1 + templates/webatomheader.txt | 9 + 14 files changed, 687 insertions(+) create mode 100644 generasitio.lua create mode 100644 templates/estilo.css create mode 100644 templates/feedheader.gmi create mode 100644 templates/gematomheader.txt create mode 100644 templates/index.gmi create mode 100644 templates/logheader.gmi create mode 100644 templates/pagefooter.gmi create mode 100644 templates/pagefooter.html create mode 100644 templates/pageheader.gmi create mode 100644 templates/pageheader.html create mode 100644 templates/pageincoming.gmi create mode 100644 templates/pageincoming.html create mode 100644 templates/twheader.txt create mode 100644 templates/webatomheader.txt diff --git a/generasitio.lua b/generasitio.lua new file mode 100644 index 0000000..c8078eb --- /dev/null +++ b/generasitio.lua @@ -0,0 +1,488 @@ +meta = { + domain = "compudanzas.net", + title = "compudanzas", + description = "explorations of computing at a human scale", + fecha = os.date("%Y-%m-%d") +} + +webdir = "webtest" -- web output directory +gemdir = "gemtest" -- gemini output directory +templates = { + webEn = { + header = { path = "pageheader.html", template = ""}, + footer = { path = "pagefooter.html", template = ""}, + incoming = { path = "pageincoming.html", template = ""} + }, + gemEn = { + header = { path = "pageheader.gmi", template = ""}, + footer = { path = "pagefooter.gmi", template = ""}, + incoming = { path = "pageincoming.gmi", template = ""} + }, +} + +pages = {} + +function slugify( s ) + return (string.gsub( s, "%s", "_" )) +end + +function spacify( s ) -- opposite of slugify (?) + return (string.gsub( s, "_", " ")) +end + +function fillTemplate( template, pagemeta ) + return ( string.gsub( template, "({.-})", function (key) + local var = string.sub(key,2,-2) -- remove { } + return meta[var] or pagemeta[var] + end) ) +end + +function getHeader( line ) -- returns title, level + local head, title = string.match(line,"^(#+)%s*(.-)$") + return title, #head +end + +function getLink( line ) -- returns dest, text + return string.match( line, "^=>%s*(%S-)%s+(.-)$") +end + +function isImagePath( text ) + return text:match(".jpg$") or text:match(".gif$") or text:match(".png$") +end + +function sanitize( line ) -- replace relevant html entities + return line:gsub("<","<"):gsub(">",">") +end + +function closeTags() + local close = "" + if flags.list then + close = "\n" + flags.list = false + elseif flags.p then + close = "

\n" + flags.p = false + elseif flags.gallery then + close = "\n" + flags.gallery = false + end + + return close +end + +function insertIncoming( pagename, incomingname ) + if incomingname == "pages" then return false end + + local incoming = pages[pagename].incoming + for i = 1, #incoming do + if incoming[i] == incomingname then + return false + end + end + + table.insert( incoming, incomingname) + return true +end + +function firstPass() + -- load templates + for _,t in pairs(templates) do + -- read templates + for k,subtemp in pairs( t ) do + local f = assert(io.open(string.format("templates/%s",subtemp.path),"r")) + subtemp.template = f:read("a") + f:close() + end + end + + -- for each page: + -- convert gmo to gmi and html + -- and calculate incoming links + for name,page in pairs(pages) do + local pagemeta = pages[name] + + local gmopath = string.format("src/%s.gmo", pagemeta.slug) + + -- open file + local f = assert( io.open(gmopath, "r") ) + + -- initialize flags + flags = { list = false, pre = false , p = false, gallery = false } + + -- table to store the lines to write to file + local lines = { web = {}, gem = {} } + + -- convert one line at a time + local count = 1 + for line in f:lines() do + -- the output line: + local out = { gem = line, web = nil} + + if count == 1 then -- extract title + pagemeta.ptitle = string.match(line, "^#+%s*(.-)$") + elseif count == 2 then -- language + if line == "" then + pagemeta.lang = "en,es-MX" -- default + else + pagemeta.lang, pagemeta.trlang, pagemeta.trname = string.match("^lang=(.-)%s(.-)->(.-)$") + end + elseif count == 3 then -- obtain page description + pagemeta.pdescription,n = string.gsub(line,"[{}]","") + end + + if count <=2 then goto nextline end -- skip normal processing for the first lines + + -- CONVERT LINES + if string.match( line, "^```") then -- preformated + if flags.pre then + out.web = "" + else + out.web = "
"
+				end
+				flags.pre = not flags.pre 
+				goto insert -- skip more checks
+			end
+
+			if flags.pre then
+				out.web = sanitize( line )
+				goto insert
+			end
+
+			if string.match( line, "^%+") then -- + append html
+				out.gem = nil
+				out.web = string.sub(line,3) -- remove "+ "
+
+			elseif string.match( line, "^&") then -- & append gemtext
+				out.gem = string.sub(line,3) -- remove "& "
+
+			elseif string.match( line, "^$") then -- empty line
+				out.web = closeTags()
+
+			elseif string.match( line, "^#+") then -- HEADERS
+				-- TODO create nav
+				local title, level = getHeader( line )	
+
+				local close = closeTags()
+				out.web = string.format("%s%s",close,level,title,level)
+
+			elseif string.match( line, "^>") then -- BLOCKQUOTE
+				local close = closeTags()
+				out.web = string.format("%s
%s
", close, string.sub(line,3)) + + elseif string.match( line, "^%*") then -- LIST ITEMS + local li = string.format("
  • %s
  • ", string.sub(line,3)) -- remove "* " + if not flags.list then -- start list + out.web = string.format("