diff --git a/index.html b/index.html index b43e6a1..4a0728b 100644 --- a/index.html +++ b/index.html @@ -1,4 +1,4 @@ -$render{"lexer-test"} +$render{"test"} $render{"templates/wares"} diff --git a/pygments.lua b/pygments.lua new file mode 100644 index 0000000..4ad5cb3 --- /dev/null +++ b/pygments.lua @@ -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 diff --git a/renderers/markdown.moon b/renderers/markdown.moon index 59a8539..2eba08c 100644 --- a/renderers/markdown.moon +++ b/renderers/markdown.moon @@ -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 diff --git a/site.moon b/site.moon index 8865234..71081ae 100644 --- a/site.moon +++ b/site.moon @@ -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('^
(.-)\n?
'), +-- "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" diff --git a/test.md b/test.md new file mode 100644 index 0000000..84f3476 --- /dev/null +++ b/test.md @@ -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! +``` diff --git a/test.moon b/test.moon deleted file mode 100644 index da5d10b..0000000 --- a/test.moon +++ /dev/null @@ -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!