Support Pandoc rendering

Mon, 20 Apr 2020 11:48:40 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 20 Apr 2020 11:48:40 -0500
changeset 16
e377590e9b19
parent 15
82d254b8f01e
child 17
bea2ea0b07d9

Support Pandoc rendering

README.md file | annotate | diff | comparison | revisions
example/src/style/default.css file | annotate | diff | comparison | revisions
example/src/style/pandoc.css file | annotate | diff | comparison | revisions
example/src/subpage/index.html.lg file | annotate | diff | comparison | revisions
handlers/render.lua file | annotate | diff | comparison | revisions
pandoc.lua file | annotate | diff | comparison | revisions
--- a/README.md	Wed Jan 31 19:51:10 2018 +0000
+++ b/README.md	Mon Apr 20 11:48:40 2020 -0500
@@ -4,10 +4,16 @@
 
 It is not particularly supported or maintained; it was created back in 2009
 to build [my own website](http://tuomov.iki.fi/). The engine is still being
-used for that purpose. I decided to put the code online now in 2017 after
+used for that purpose. I decided to put the code online in 2017 after
 having had to spend some time to convert it to Lua 5.3 from which the
 module() function had been removed.
 
-Aside from the included compat_env.lua and the ltp engine (which is has not
+Features (see the included example):
+
+  * Embedded Lua code within `<% ... %>` with the ltp engine.
+  * Advanced markdown features, including syntax highlighting of code blocks, via Pandoc
+    (select `renderer: pandoc` in the page meta section).
+
+Aside from the included `compat_env.lua` and the ltp engine (which is has not
 updated to Lua 5.3, and requires on the compat_env module), the code is in
 the public domain.
--- a/example/src/style/default.css	Wed Jan 31 19:51:10 2018 +0000
+++ b/example/src/style/default.css	Mon Apr 20 11:48:40 2020 -0500
@@ -1,6 +1,8 @@
+
+@import "pandoc.css";
 
 body {
-	font-family: Optima, Linux Biolinum, Candara, sans-serif;
+    font-family: Optima, Linux Biolinum, Candara, sans-serif;
     #font-weight: 300;
     background: #ffffff; 
     color: #000000;   
@@ -357,3 +359,4 @@
 	background: #ff9900;
 }
 
+@import "pandoc.css";
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/src/style/pandoc.css	Mon Apr 20 11:48:40 2020 -0500
@@ -0,0 +1,69 @@
+/* Extracted manually from `pandoc -s  some-file-with-syntax-highlighting` */
+code{white-space: pre-wrap;}
+span.smallcaps{font-variant: small-caps;}
+span.underline{text-decoration: underline;}
+div.column{display: inline-block; vertical-align: top; width: 50%;}
+div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
+ul.task-list{list-style: none;}
+pre > code.sourceCode { white-space: pre; position: relative; }
+pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
+pre > code.sourceCode > span:empty { height: 1.2em; }
+code.sourceCode > span { color: inherit; text-decoration: inherit; }
+div.sourceCode { margin: 1em 0; }
+pre.sourceCode { margin: 0; }
+@media screen {
+div.sourceCode { overflow: auto; }
+}
+@media print {
+pre > code.sourceCode { white-space: pre-wrap; }
+pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
+}
+pre.numberSource code
+  { counter-reset: source-line 0; }
+pre.numberSource code > span
+  { position: relative; left: -4em; counter-increment: source-line; }
+pre.numberSource code > span > a:first-child::before
+  { content: counter(source-line);
+    position: relative; left: -1em; text-align: right; vertical-align: baseline;
+    border: none; display: inline-block;
+    -webkit-touch-callout: none; -webkit-user-select: none;
+    -khtml-user-select: none; -moz-user-select: none;
+    -ms-user-select: none; user-select: none;
+    padding: 0 4px; width: 4em;
+    color: #aaaaaa;
+  }
+pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
+div.sourceCode
+  {   }
+@media screen {
+pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
+}
+code span.al { color: #ff0000; font-weight: bold; } /* Alert */
+code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
+code span.at { color: #7d9029; } /* Attribute */
+code span.bn { color: #40a070; } /* BaseN */
+code span.bu { } /* BuiltIn */
+code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
+code span.ch { color: #4070a0; } /* Char */
+code span.cn { color: #880000; } /* Constant */
+code span.co { color: #60a0b0; font-style: italic; } /* Comment */
+code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
+code span.do { color: #ba2121; font-style: italic; } /* Documentation */
+code span.dt { color: #902000; } /* DataType */
+code span.dv { color: #40a070; } /* DecVal */
+code span.er { color: #ff0000; font-weight: bold; } /* Error */
+code span.ex { } /* Extension */
+code span.fl { color: #40a070; } /* Float */
+code span.fu { color: #06287e; } /* Function */
+code span.im { } /* Import */
+code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
+code span.kw { color: #007020; font-weight: bold; } /* Keyword */
+code span.op { color: #666666; } /* Operator */
+code span.ot { color: #007020; } /* Other */
+code span.pp { color: #bc7a00; } /* Preprocessor */
+code span.sc { color: #4070a0; } /* SpecialChar */
+code span.ss { color: #bb6688; } /* SpecialString */
+code span.st { color: #4070a0; } /* String */
+code span.va { color: #19177c; } /* Variable */
+code span.vs { color: #4070a0; } /* VerbatimString */
+code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
--- a/example/src/subpage/index.html.lg	Wed Jan 31 19:51:10 2018 +0000
+++ b/example/src/subpage/index.html.lg	Mon Apr 20 11:48:40 2020 -0500
@@ -1,5 +1,14 @@
 ---
 title: This is a sub-page of an lgen test page
+renderer: pandoc
 ---
 
-There is no content
+With the pandoc-renderer in this page, we can use GitHub-flavoured markdown,
+including syntax highlighting
+
+```lua
+if math.random() > 0.5
+    io.write('Hello World')
+end
+```
+
--- a/handlers/render.lua	Wed Jan 31 19:51:10 2018 +0000
+++ b/handlers/render.lua	Mon Apr 20 11:48:40 2020 -0500
@@ -10,6 +10,7 @@
 local filecache=require('filecache')
 local log=require('log')
 local dependency=require('dependency')
+local pandoc=require('pandoc')
 
 --
 -- Phase 1: load & markup
@@ -88,11 +89,17 @@
     if data then
         local newenv=handlers_render.env_for(file, env, path_prefix)
         local data2=handlers_render.process_lua(data, newenv)
-        meta=env.pages[file].meta
-        if meta.markdown == nil or meta.markdown == "true" then
+        meta = env.pages[file].meta
+        renderer = meta.renderer or "markdown"
+        print("Renderer", meta.renderer, "\n")
+        if renderer == "pandoc" then
+            return pandoc.pandoc(data2)
+        elseif renderer == "markdown" then
             return markdown(data2)
+        elseif renderer == "none" then
+            return data2
         else
-            return data2
+            error('Unknown renderer')
         end
     end
 end
@@ -118,9 +125,9 @@
         build=dependency.simple_update_check(dst, deps)
     end
     
-    if build then 
+    if build then
         log.log("Render "..file.."\n")
-        local content=handlers_render.render(file, env)
+        local content=handlers_render.render(file, env, page.meta)
         local page_template=filecache.get(tmpl)
 
         local newenv=table.join({content=content}, handlers_render.env_for(file, env))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pandoc.lua	Mon Apr 20 11:48:40 2020 -0500
@@ -0,0 +1,21 @@
+local pandoc={}
+
+local io = require('io')
+local os = require('os')
+
+function pandoc.pandoc(str)
+    -- Lua doesn't have bidirectional pipes without the posix library.
+    -- Keep things and compatible and use temporary files.
+    tmpname = os.tmpname()
+    file = io.open(tmpname, 'w')
+    file:write(str)
+    cmd = 'pandoc --from=markdown --to=html "' .. tmpname .. '"'
+    io.stdout:write('Executing ' .. cmd .. '\n')
+    h = io.popen(cmd, 'r')
+    result = h:read("*a")
+    h:close()
+    file:close()
+    return result
+end
+
+return pandoc

mercurial