Change cosmo '$' escape string in markdown renderer

so that we can put 2 selectors next to each other and still be able to
tell where one ends and the other one begins
This commit is contained in:
Catalin Constantin Mititiuc 2025-06-25 14:44:03 -07:00
parent 95e9be6e60
commit 0e848c5e7b

View File

@ -1,5 +1,70 @@
Path = require "sitegen.path" Path = require "sitegen.path"
dollar_temp = "z000sitegen_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)
needs_shell_escape = (str) -> not not str\match "[^%w_-]" needs_shell_escape = (str) -> not not str\match "[^%w_-]"
shell_escape = (str) -> str\gsub "'", "''" shell_escape = (str) -> str\gsub "'", "''"
@ -21,10 +86,13 @@ write_exec = (cmd, content) ->
-- config command like this in site.moon: -- config command like this in site.moon:
-- require("renderers.markdown").cmd = "pandoc --mathjax >" -- require("renderers.markdown").cmd = "pandoc --mathjax >"
class PandocRenderer extends require "sitegen.renderers.markdown" class PandocRenderer extends require "sitegen.renderers.html"
unescape_cosmo = @unescape_cosmo @escape_cosmo: escape_cosmo
escape_cosmo = @escape_cosmo @unescape_cosmo: unescape_cosmo
@parse_cosmo: parse_cosmo
source_ext: "md"
ext: "html"
cmd: "pandoc --mathjax --lua-filter pygments.lua >" cmd: "pandoc --mathjax --lua-filter pygments.lua >"
pandoc: (content) => Path.read_file write_exec @@cmd, content pandoc: (content) => Path.read_file write_exec @@cmd, content