Add custom plugin

This commit is contained in:
Catalin Constantin Mititiuc 2025-05-27 19:20:08 -07:00
parent 947075902d
commit c72580f717
3 changed files with 147 additions and 23 deletions

24
code.md
View File

@ -61,27 +61,9 @@ class Person extends Thing
with Person!
.name = "MoonScript"
\say_name!
export my_func
x = 2323
collection =
height: 32434
hats: {"tophat", "bball", "bowler"}
my_func = (a) -> x + a
print my_func 100
import concat, insert from table
double_args = (...) ->
[x * 2 for x in *{...}]
tuples = [{k, v} for k,v in ipairs my_table]
```
## `CoffeeScript`
## `coffeescript`
```coffeescript
author = "Wittgenstein"
@ -90,13 +72,13 @@ quote = "A picture is a fact. -- #{ author }"
sentence = "#{ 22 / 7 } is a decent approximation of π"
```
## `JavaScript`
## `javascript`
```javascript
let a = "what is this?"
```
## `Lua`
## `lua`
```lua
local test = "this is a string"

61
plugins/pygments.moon Normal file
View File

@ -0,0 +1,61 @@
html = require "sitegen.html"
class CustomPygments extends require "sitegen.plugins.pygments"
filter: (text, page) =>
-- require("moon").p text
-- super(text, page)
text -- pandoc output
super(text, page) -- plugin output
highlight: (lang, code) =>
-- print "HIGHLIGHT CALLED"
-- require("moon").p code
fname = os.tmpname!
with io.open fname, "w"
\write code
\close!
-- p = io.popen ("pygmentize -f html -l %s %s")\format lang, 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?"
cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s"
if lang == "moon" or lang == "moonscript"
p = io.popen (cmd .. " -x")\format "moonscript.py", fname
-- -- p = io.popen (cmd)\format "moonscript", fname
out = p\read"*a"
p\close!
-- out
-- 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?"
else
if lang == nil or lang == "heex"
-- if block.classes[1] == nil
-- cb = pandoc.CodeBlock(block.text, class: "sourceCode")
-- pandoc.Div cb, class: "sourceCode"
-- else
-- block
-- "code(nil or heex): #{code}"
code
else
status, handle_or_error = pcall ->
io.popen (cmd)\format lang, fname
sout = handle_or_error\read"*a"
handle_or_error\close!
if sout\len! == 0
-- "code: #{code}"
-- raw code
code
else
-- get rid of the div and pre inserted by pygments
assert sout\match('^<div class="highlight"><pre>(.-)\n?</pre></div>'),
"Failed to parse pygmentize result, is pygments installed?"

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"
@ -16,17 +17,80 @@ Site.config.out_dir = "html/"
-- Compile lua-filter used by Pandoc to highlight MoonScript syntax
os.execute "moonc pygments.moon"
try_compile = (text, options=implicitly_return_root: false) ->
to_lua text, options
-- PygmentsPlugin.highlight = (lang, code) =>
-- fname = os.tmpname!
-- with io.open fname, "w"
-- \write code
-- \close!
--
-- p = io.popen ("pygmentize -f html -l %s %s")\format lang, 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?"
-- print "HIGHLIGHT CALLED"
--
-- fname = os.tmpname!
-- with io.open fname, "w"
-- \write block.text
-- \close!
--
-- cmd = "pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l %s %s"
--
-- if lang == "moon" or lang == "moonscript"
-- -- p = io.popen ("pygmentize -f html -O style=dracula,wrapcode,classprefix=py- -l moonscript.py %s -x")\format fname
-- p = io.popen (cmd .. " -x")\format "moonscript.py", fname
-- -- p = io.popen (cmd)\format "moonscript", fname
--
-- -- p = io.open fname
-- out = p\read"*a"
-- p\close!
-- out
-- -- pandoc.RawBlock "html", "moonscript"
-- else
-- if lang == nil or lang == "heex"
-- -- if block.classes[1] == nil
-- -- cb = pandoc.CodeBlock(block.text, class: "sourceCode")
-- -- pandoc.Div cb, class: "sourceCode"
-- -- else
-- -- block
--
-- "code: #{code}"
-- else
-- status, handle_or_error = pcall ->
-- io.popen (cmd)\format lang, fname
-- -- io.open fname
--
-- sout = handle_or_error\read"*a"
-- -- handle_or_error\close!
-- if sout\len! == 0
-- -- cb = pandoc.CodeBlock(block.text, class: "sourceCode")
-- -- pandoc.Div cb, class: "sourceCode"
-- "code: #{code}"
-- else
-- -- pandoc.RawBlock "html", sout
-- sout
-- Configure the command our custom renderer uses to convert markdown to html
rend = "renderers.markdown"
require(rend).cmd = "pandoc --mathjax --lua-filter pygments.lua >"
-- require(rend).cmd = "pandoc --mathjax --no-highlight >"
require(rend).cmd = "pandoc --mathjax >"
-- require(rend).cmd = "pandoc --mathjax --lua-filter pygments.lua >"
-- Insert custom renderer in the first position so it will be preferred
table.insert Site.default_renderers, 1, rend
-- Remove "pygments" plugin because it conflicts with pandoc syntax highlighting
-- Remove "pygments" plugin
Site.default_plugins = for v in *Site.default_plugins
if v\find "pygments" then continue else v
table.insert Site.default_plugins, 1, "plugins.pygments"
-- from https://github.com/leafo/sitegen/blob/v0.2/spec/sitegen_spec.moon#L9-L18
get_files = (path, prefix=path) ->
files = Path.read_exec "find", path, "-type", "f"
@ -131,6 +195,23 @@ sitegen.create =>
publish_date: format_date date true
-- add_all files_from "docs"
-- with PygmentsPlugin.custom_highlighters
-- .moon = (code_text, page, options) =>
-- lua_text, err = try_compile code_text, options
-- return err if not lua_text
--
-- html.build ->
-- tag.span "moon custom highlighter"
--
-- .heex = (code_text, page, options) =>
-- lua_text, err = try_compile code_text, options
-- return err if not lua_text
--
-- html.build ->
-- tag.span "heex custom highlighter"
copy "app.css"
copy "pygments.css"
copy "moonscript.css"