I think I may have figured it out

This commit is contained in:
Catalin Constantin Mititiuc 2025-06-16 20:18:05 -07:00
parent d22283905a
commit 92a1650e75
6 changed files with 90 additions and 19 deletions

View File

@ -1,4 +1,4 @@
$render{"lexer-test"}
$render{"test"}
$render{"templates/wares"}

12
pygments.lua Normal file
View File

@ -0,0 +1,12 @@
function CodeBlock(block)
if block.classes[1] == "moon" then
local fname = os.tmpname()
local handle = io.open(fname, "w")
handle:write(block.text)
handle:close()
local p = io.popen(string.format("pygmentize -f html -l moonscript.py %s -x", fname))
local out = p:read("*a")
return pandoc.RawBlock("html", out)
end
end

View File

@ -19,7 +19,18 @@ convert = =>
(md_source) ->
filename = os.tmpname!
write_exec md_source, @cmd, filename
Path.read_file filename
a = Path.read_file filename
require("moon").p a
a
-- fname = os.tmpname!
-- with io.open fname, "w"
-- \write md_source
-- \close!
--
-- p = io.popen (@cmd)\format lang, fname
-- out = p\read"*a"
-- out
-- config command like this in site.moon:
-- require("renderers.markdown").cmd = "pandoc --mathjax >"
@ -28,7 +39,11 @@ class PandocRenderer extends require "sitegen.renderers.markdown"
escape_cosmo = @escape_cosmo
pandoc = convert @
cmd: "pandoc >"
-- cmd: "pandoc >"
cmd: "pandoc --mathjax --lua-filter pygments.lua >"
-- cmd: "pygmentize -f html -l %s %s"
-- cmd: "pygmentize -f html -l %s %s -x"
-- cmd: "pygmentize -f html -l moonscript.py test.moon -x -o lexer-test.html"
render: (page, md_source) =>
md_source = page\pipe "renderer.markdown.pre_render", md_source

View File

@ -1,6 +1,7 @@
sitegen = require "sitegen"
Site = require "sitegen.site"
Path = require "sitegen.path"
PygmentsPlugin = require "sitegen.plugins.pygments"
html = require "sitegen.html"
common = require "sitegen.common"
cosmo = require "sitegen.cosmo"
@ -13,17 +14,40 @@ import p from require "moon" -- debug
-- Change output dir to what Openresty prefers
Site.config.out_dir = "html/"
-- PygmentsPlugin.highlight = (lang, code) =>
-- fname = os.tmpname!
-- with io.open fname, "w"
-- \write code
-- \close!
--
-- p lang
--
-- -- p = io.popen ("pygmentize -f html -l %s %s")\format lang, fname
-- p = io.popen ("pygmentize -f html -l moonscript.py %s -x")\format fname
-- out = p\read"*a"
--
-- -- get rid of the div and pre inserted by pygments
-- assert out\match('^<div class="highlight"><pre>(.-)\n?</pre></div>'),
-- "Failed to parse pygmentize result, is pygments installed?"
-- Configure the command our custom renderer uses to convert markdown to html
rend = "renderers.markdown"
require(rend).cmd = "pandoc --mathjax >"
-- require(rend).cmd = "pandoc --mathjax --no-highlight >"
-- require(rend).cmd = "pandoc --mathjax -f json >"
-- require(rend).cmd = "pandoc --mathjax --no-highlight --lua-filter ./pygments.lua >"
-- Insert custom renderer in the first position so it will be preferred
-- table.insert Site.default_renderers, 1, rend
table.insert Site.default_renderers, 1, rend
-- Remove "pygments" plugin because it conflicts with pandoc syntax highlighting
-- Site.default_plugins = for v in *Site.default_plugins
-- if v\find "pygments" then continue else v
Site.default_plugins = for v in *Site.default_plugins
if v\find "pygments" then continue else v
-- plugin = "plugins.pygments"
-- table.insert Site.default_plugins, 1, plugin
-- from https://github.com/leafo/sitegen/blob/v0.2/spec/sitegen_spec.moon#L9-L18
get_files = (path, prefix=path) ->
@ -118,8 +142,8 @@ sitegen.create =>
} for path in *files
add "index.html", title: "Catalin Mititiuc"
-- add "blog.html", title: "Posts", target: "posts/index", template: "blog"
-- add_all files_from "docs"
add "blog.html", title: "Posts", target: "posts/index", template: "blog"
add_all files_from "docs"
-- replace post markdown yaml headers with moonscript headers
filter "docs", (body) =>
@ -128,6 +152,7 @@ sitegen.create =>
moonscript_header = header\gsub "%.%.%.", "}"
moonscript_header
copy "moonscript.py"
copy "pygments.lua"
copy "app.css"
-- copy "pygments.css"
copy "moonscript.css"

28
test.md Normal file
View File

@ -0,0 +1,28 @@
```elixir
if config_env() != :prod do
# Configure pandoc (the version is required)
config :pandoc,
version: "3.6.1",
hello: [
args: fn extra_args ->
{_, [input_file], _} = OptionParser.parse(extra_args, switches: [])
~w(--output=../priv/static/posts/#{Path.rootname(input_file)}.html)
end,
cd: Path.expand("../documents", __DIR__)
]
end
```
**this is a string!!**
```moon
class Thing
name: "unknown"
class Person extends Thing
say_name: => print "Hel#lo, I am #{@name}!"
with Person!
.name = "MoonScript"
\say_name!
```

View File

@ -1,9 +0,0 @@
class Thing
name: "unknown"
class Person extends Thing
say_name: => print "Hel#lo, I am #{@name}!"
with Person!
.name = "MoonScript"
\say_name!