WIP: use pandoc to convert markdown

This commit is contained in:
Catalin Constantin Mititiuc 2025-05-14 19:11:06 -07:00
parent 2993a35c80
commit 1d41551905
3 changed files with 163 additions and 5 deletions

View File

@ -1,11 +1,42 @@
-- ========================================================================= -- =========================================================================
-- capture output of a system command -- capture output of a system command
-- ========================================================================= -- =========================================================================
handle = io.popen "ls" -- handle = io.popen "ls"
result = handle\read("*a") -- result = handle\read("*a")
print result -- print result
-- handle\close!
-- os.execute "pandoc"
-- txt = "# hello"
-- md = "<<EOF\n#{txt}\nEOF"
-- handle = io.popen "pandoc #{md}", "r"
-- result = handle\read("*a")
-- handle\close!
n = os.tmpname!
handle = io.popen "pandoc > " .. n, "w"
handle\write "# hello"
handle\close! handle\close!
f = io.open n, "r"
result = f\read("*a")
f\close!
-- output = io.input(handle)\read("*a")
-- handle\write "# hello"
-- handle\write "EOF"
-- result = handle\read "*a"
-- handle\close!
-- handle\flush!
print result
-- result\close!
-- require("moon").p handle
-- ========================================================================= -- =========================================================================
-- how changing the config in the Site class works -- how changing the config in the Site class works
-- ========================================================================= -- =========================================================================

118
markdown.moon Normal file
View File

@ -0,0 +1,118 @@
import Renderer from require "sitegen.renderer"
dollar_temp = "0000sitegen_markdown00dollar0000"
-- a constructor for quote delimited strings
simple_string = (delim) ->
import P from require "lpeg"
inner = P("\\#{delim}") + "\\\\" + (1 - P delim)
inner = inner^0
P(delim) * inner * P(delim)
lua_string = ->
import P, C, Cmt, Cb, Cg from require "lpeg"
check_lua_string = (str, pos, right, left) ->
#left == #right
string_open = P"[" * P"="^0 * "["
string_close = P"]" * P"="^0 * "]"
valid_close = Cmt C(string_close) * Cb"string_open", check_lua_string
Cg(string_open, "string_open") *
(1 - valid_close)^0 * string_close
-- returns a pattern that parses a cosmo template. Can be used to have
-- pre-processors ignore text that would be handled by cosmo
parse_cosmo = ->
import P, R, Cmt, Cs, V from require "lpeg"
curly = P {
P"{" * (
simple_string("'") +
simple_string('"') +
lua_string! +
V(1) +
(P(1) - "}")
)^0 * P"}"
}
alphanum = R "az", "AZ", "09", "__"
P"$" * alphanum^1 * (curly)^-1
escape_cosmo = (str) ->
escapes = {}
import P, R, Cmt, Cs, V from require "lpeg"
counter = 0
cosmo = parse_cosmo! / (tpl) ->
counter += 1
key = "#{dollar_temp}.#{counter}"
escapes[key] = tpl
key
patt = Cs (cosmo + P(1))^0 * P(-1)
str = patt\match(str) or str, escapes
str, escapes
unescape_cosmo = (str, escapes) ->
import P, R, Cmt, Cs from require "lpeg"
escape_patt = P(dollar_temp) * P(".") * R("09")^1 / (key) ->
escapes[key] or error "bad key for unescape_cosmo"
patt = Cs (escape_patt + P(1))^0 * P(-1)
assert patt\match(str)
-- Converts input from markdown, then passes through cosmo filter from HTML
-- renderer
class MarkdownRenderer extends require "sitegen.renderers.html"
@escape_cosmo: escape_cosmo
@unescape_cosmo: unescape_cosmo
@parse_cosmo: parse_cosmo
source_ext: "md"
ext: "html"
render: (page, md_source) =>
discount = require "discount"
md_source = page\pipe "renderer.markdown.pre_render", md_source
md_source, escapes = escape_cosmo md_source
-- require("moon").p md_source
--mathjax --output=../priv/static/posts/#{output_file}.html
-- handle = io.popen "pandoc"
-- handle\write "# hello"
-- handle\flush!
-- result = handle\read("*a")
-- print result
-- handle\close!
-- require("moon").p result
-- txt = "# hello"
-- md = "<<EOF\n#{md_source}\nEOF"
-- require("moon").p md
-- n = os.tmpname!
-- handle = io.popen "pandoc > " .. n, "w"
-- handle\write "# hello"
-- result = handle\read("*a")
-- handle\close!
n = os.tmpname!
handle = io.popen "pandoc > " .. n, "w"
handle\write md_source
handle\close!
f = io.open n, "r"
result = f\read("*a")
f\close!
html_source = assert result
-- html_source = assert discount md_source
html_source = unescape_cosmo html_source, escapes
super page, html_source

View File

@ -119,12 +119,21 @@ sitegen.create =>
-- @what! -- @what!
add "index.html", o: one add "index.html"
add path, target: out, template: "blog", is_a: "post", post: { add path, target: out, template: "blog", is_a: "post", post: {
publish_date: publish_date(path) publish_date: publish_date(path)
}, id: extract_id(path) for path, out in pairs posts "docs" }, id: extract_id(path) for path, out in pairs posts "docs"
add "test.html", id: "test" -- add "docs/2023-11-15-test-mix-task-file-modify.md",
-- target: "posts/2023-11-15-test-mix-task-file-modify.html"
-- template: "blog"
-- is_a: "post"
-- post: {
-- publish_date: publish_date "docs/2023-11-15-test-mix-task-file-modify.md"
-- }
-- id: extract_id "docs/2023-11-15-test-mix-task-file-modify.md"
-- add "test.md"
-- feed "posts.moon", "feed.xml" -- feed "posts.moon", "feed.xml"
-- require("moon").p site -- require("moon").p site