Use markdown-it as default markdown parser.

Mon, 06 Jul 2020 10:56:23 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Mon, 06 Jul 2020 10:56:23 -0500
changeset 21
3c71c525cec2
parent 20
3b937ef20faa
child 22
8c8e70569d81

Use markdown-it as default markdown parser.

.hgignore file | annotate | diff | comparison | revisions
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
example/tmpl/page.template file | annotate | diff | comparison | revisions
handlers/render.lua file | annotate | diff | comparison | revisions
lgen.lua file | annotate | diff | comparison | revisions
markdown_it.js file | annotate | diff | comparison | revisions
markdown_it.lua file | annotate | diff | comparison | revisions
package.json file | annotate | diff | comparison | revisions
--- a/.hgignore	Mon Jul 06 09:36:27 2020 -0500
+++ b/.hgignore	Mon Jul 06 10:56:23 2020 -0500
@@ -1,2 +1,6 @@
 syntax: glob
 example/out/
+node_modules/
+package-lock.json
+
+
--- a/README.md	Mon Jul 06 09:36:27 2020 -0500
+++ b/README.md	Mon Jul 06 10:56:23 2020 -0500
@@ -2,20 +2,35 @@
 # lgen
 
 This is a very simple website templating engine based on markdown and Lua,
-written by Tuomo Valkonen 〈<tuomov@iki.fi>〉.
+written by Tuomo Valkonen ⟨<tuomov@iki.fi>⟩.
 
 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 in 2017 after
-having had to spend some time to convert it to Lua 5.3 from which the
-module() function had been removed.
+having had to spend some time to convert it from earlier versions to [Lua 5.3][Lua] from which the `module()` function had been removed.
 
-Features (see the included example):
+## Features
+
+For details see the included example.
 
   * Embedded Lua code with the ltp engine.
-  * Advanced markdown features, including syntax highlighting of code blocks, via Pandoc
-    (select `renderer: pandoc` in the page meta section).
+  * Advanced markdown features, including syntax highlighting of code blocks
+    and LaTeX mathematics via
+    [markdown-it][] and [KaTeX][] or [Pandoc][]. Markdown-it is now the default renderer. To select lua-markdown, use `renderer: lua-markdown` in the page YAML meta section. Use `renderer: pandoc` for Pandoc.
+
+## Installation
+
+You need to install [Lua 5.3][Lua].
+To use [markdown-it][], you need to install [NodeJS][] and then execute `npm install` to install in the top directory of this project.
+
+## License
 
 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.
+
+[KaTeX]: https://katex.org/
+[markdown-it]: https://github.com/markdown-it/markdown-it
+[Pandoc]: https://pandoc.org/
+[Lua]: http://www.lua.org/
+[NodeJS]: https://nodejs.org/en/
--- a/example/src/style/default.css	Mon Jul 06 09:36:27 2020 -0500
+++ b/example/src/style/default.css	Mon Jul 06 10:56:23 2020 -0500
@@ -1,9 +1,6 @@
-
-@import "pandoc.css";
 
 body {
     font-family: Optima, Linux Biolinum, Candara, sans-serif;
-    #font-weight: 300;
     background: #ffffff; 
     color: #000000;   
     margin: 10px 40px 40px 40px;
@@ -359,4 +356,4 @@
 	background: #ff9900;
 }
 
-@import "pandoc.css";
+.katex { font-size: normal; }
--- a/example/src/style/pandoc.css	Mon Jul 06 09:36:27 2020 -0500
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,69 +0,0 @@
-/* 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	Mon Jul 06 09:36:27 2020 -0500
+++ b/example/src/subpage/index.html.lg	Mon Jul 06 10:56:23 2020 -0500
@@ -1,6 +1,6 @@
 ---
 title: This is a sub-page of an lgen test page
-renderer: pandoc
+renderer: markdown-it
 ---
 
 With the pandoc-renderer in this page, we can use GitHub-flavoured markdown,
@@ -12,3 +12,7 @@
 end
 ```
 
+We can also do math $x^2$ with KaTeX
+$$
+    \sum_{k=1}^\infty \frac{1}{k} = \infty.
+$$
--- a/example/tmpl/page.template	Mon Jul 06 09:36:27 2020 -0500
+++ b/example/tmpl/page.template	Mon Jul 06 10:56:23 2020 -0500
@@ -1,10 +1,14 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
+<!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
   <head>
     <meta http-equiv="content-type" content="text/xhtml; charset=utf-8" />
     <meta name="author" content="lgen" />
     <meta name="viewport" content="initial-scale=1" />
     <link rel="stylesheet" type="text/css" href="<%= base_url %>style/default.css" />
+    <!-- TERRIBLY BROKEN <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css" integrity="sha384-zB1R0rpPzHqg7Kpt0Aljp8JPLqbXI3bhnPWROx27a9N0Ll6ZP/+DiW/UqRcLbRjq"
+    crossorigin="anonymous">-->
+    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.5.1/katex.min.css">
+    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.0/styles/default.min.css">
     <link rel="apple-touch-icon-precomposed" href="<%= base_url %>style/icon144.png" type="144x144"/>
     <link rel="shortcut icon" href="<%= base_url %>style/icon16.png" type="image/png" />
     <!--
--- a/handlers/render.lua	Mon Jul 06 09:36:27 2020 -0500
+++ b/handlers/render.lua	Mon Jul 06 10:56:23 2020 -0500
@@ -11,6 +11,7 @@
 local log=require('log')
 local dependency=require('dependency')
 local pandoc=require('pandoc')
+local markdown_it=require('markdown_it')
 
 --
 -- Phase 1: load & markup
@@ -90,10 +91,12 @@
         local newenv=handlers_render.env_for(file, env, path_prefix)
         local data2=handlers_render.process_lua(data, newenv)
         meta = env.pages[file].meta
-        renderer = meta.renderer or "markdown"
-        if renderer == "pandoc" then
+        renderer = meta.renderer or "markdown-it"
+        if renderer == "markdown-it" then
+            return markdown_it.markdown(data2, env)
+        elseif renderer == "pandoc" then
             return pandoc.pandoc(data2)
-        elseif renderer == "markdown" then
+        elseif renderer == "lua-markdown" then
             return markdown(data2)
         elseif renderer == "none" then
             return data2
--- a/lgen.lua	Mon Jul 06 09:36:27 2020 -0500
+++ b/lgen.lua	Mon Jul 06 10:56:23 2020 -0500
@@ -1,7 +1,8 @@
 
-local path=string.gsub(arg[0], "(.*)/[^/]*", "%1");
-if path~=nil then
-    package.path=package.path .. ';' .. path .. '/?.lua'
+local lgen={}
+local lgen_path=string.gsub(arg[0], "(.*)/[^/]*", "%1");
+if lgen_path~=nil then
+    package.path=package.path .. ';' .. lgen_path .. '/?.lua'
 end
 
 -- Lua 5.1 setfenv/getfenv compatibility for Lua 5.2
@@ -39,6 +40,7 @@
         src=src,
         dst=dst,
         tmpl=tmpl,
+        lgen_location=lgen_path,
     },
     pages={
     },
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/markdown_it.js	Mon Jul 06 10:56:23 2020 -0500
@@ -0,0 +1,48 @@
+#!/usr/bin/env node
+/*eslint no-console:0*/
+
+var fs = require('fs');
+var hljs = require('highlight.js');
+
+// process.argv[0] seems to be the node executable itself
+if(process.argv.length<=2){
+    console.error(`Usage: ${process.argv[1]} input_file`)
+    process.exit(1);
+}
+
+fs.readFile(process.argv[2], 'utf8', function (err, input) {
+    var output, md;
+
+    if(err){
+        console.error(err.stack || err.message || String(err));
+        process.exit(1);
+    }
+
+    try{
+        var md = require('markdown-it')({
+                html: true,
+                xhtmlOut: true,
+                breaks: false,
+                linkify: true,
+                highlight: function (str, lang) {
+                    if (lang && hljs.getLanguage(lang)) {
+                        try{
+                            return hljs.highlight(lang, str).value;
+                        } catch (__){}
+                    }
+                    return '';
+                }
+            }),
+            mk = require('markdown-it-katex'),
+            mm = require('markdown-it-mark');
+        md.use(mk).use(mm);
+        output = md.render(input);
+    } catch(e) {
+        console.error(e.stack || e.message || String(e));
+        process.exit(1);
+    }
+
+    process.stdout.write(output);
+});
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/markdown_it.lua	Mon Jul 06 10:56:23 2020 -0500
@@ -0,0 +1,23 @@
+local markdown_it={}
+
+local io = require('io')
+local os = require('os')
+
+function markdown_it.markdown(str, env)
+    -- 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 = string.format("node '%s/markdown_it.js' '%s'",
+                        env.paths.lgen_location,
+                        tmpname)
+    io.stdout:write('Executing ' .. cmd .. '\n')
+    h = io.popen(cmd, 'r')
+    result = h:read("*a")
+    h:close()
+    file:close()
+    return result
+end
+
+return markdown_it
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/package.json	Mon Jul 06 10:56:23 2020 -0500
@@ -0,0 +1,25 @@
+{
+  "name": "lgen",
+  "version": "0.0.0",
+  "description": "Lgen [markdown-it dependencies]",
+  "main": "markdown_it.js",
+  "repository": {
+    "type": "mercurial",
+    "url": "https://tuomov.iki.fi/repos/lgen/"
+  },
+  "directories": {
+    "example": "example"
+  },
+  "dependencies": {
+    "highlight.js": "^10.1.1",
+    "markdown-it": "^11.0.0",
+    "markdown-it-katex": "^2.0.3",
+    "markdown-it-mark": "^3.0.0"
+  },
+  "devDependencies": {},
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1"
+  },
+  "author": "Tuomo Valkonen",
+  "license": "ISC"
+}

mercurial