add a lua version
This commit is contained in:
11
filter.hs
11
filter.hs
@@ -6,15 +6,14 @@ import qualified Data.Text as T
|
||||
|
||||
file2Block :: FilePath -> IO [Block]
|
||||
file2Block f = do
|
||||
contents <- TIO.readFile f
|
||||
return [Plain [Str contents]]
|
||||
contents <- TIO.readFile f
|
||||
return [Plain [Str contents]]
|
||||
|
||||
doInclude :: Block -> IO Block
|
||||
doInclude cb@(Div (id, classes, namevals) contents) =
|
||||
case lookup (T.pack "include") namevals of
|
||||
Just f -> Div (id, classes, namevals) <$>
|
||||
file2Block (T.unpack f)
|
||||
Nothing -> return cb
|
||||
case lookup (T.pack "include") namevals of
|
||||
Just f -> Div (id, classes, namevals) <$> file2Block (T.unpack f)
|
||||
Nothing -> return cb
|
||||
doInclude x = return x
|
||||
|
||||
main :: IO ()
|
||||
|
||||
20
filter.lua
Normal file
20
filter.lua
Normal file
@@ -0,0 +1,20 @@
|
||||
function File2Block(filename)
|
||||
local fh = io.open(filename, "r")
|
||||
if not fh then
|
||||
io.stderr:write("Cannot open file: " .. filename .. "\n")
|
||||
return { pandoc.Plain { pandoc.Str("[Error: cannot read file " .. filename .. "]") } }
|
||||
end
|
||||
local contents = fh:read("*a")
|
||||
fh:close()
|
||||
return { pandoc.Plain { pandoc.Str(contents) } }
|
||||
end
|
||||
|
||||
function Div(e)
|
||||
local include = e.attributes["include"]
|
||||
if include then
|
||||
local blocks = File2Block(include)
|
||||
-- Replace the Div contents
|
||||
return pandoc.Div(blocks, e.attr)
|
||||
end
|
||||
return nil -- no change
|
||||
end
|
||||
5
makefile
5
makefile
@@ -4,8 +4,11 @@
|
||||
nofilter:
|
||||
pandoc -fmarkdown -thtml input.md > output.html
|
||||
|
||||
filter:
|
||||
hs:
|
||||
pandoc -fmarkdown -thtml input.md --filter ./filter.hs > output.html
|
||||
|
||||
lua:
|
||||
pandoc -fmarkdown -thtml input.md --lua-filter ./filter.lua > output.html
|
||||
|
||||
showAST:
|
||||
pandoc -fmarkdown -tnative input.md
|
||||
15
output.html
15
output.html
@@ -1,5 +1,6 @@
|
||||
<p>Here’s the pandoc README:</p>
|
||||
<div class="sourceCode" id="cb1" data-include="filter.hs"><pre
|
||||
<div class="sourceCode" id="cb1" data-include="filter.hs"
|
||||
style="background-color: red"><pre
|
||||
class="sourceCode haskell"><code class="sourceCode haskell"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="dt">Foo</span><span class="ot">::</span> <span class="dt">Div</span> <span class="ot">-></span> <span class="dt">Div</span></span></code></pre></div>
|
||||
<div data-include="filter.hs">
|
||||
#!/usr/bin/env runhaskell
|
||||
@@ -10,15 +11,15 @@ import qualified Data.Text as T
|
||||
|
||||
file2Block :: FilePath -> IO [Block]
|
||||
file2Block f = do
|
||||
contents <- TIO.readFile f
|
||||
return [Plain [Str contents]]
|
||||
contents <- TIO.readFile f
|
||||
return [Plain [Str contents]]
|
||||
|
||||
doInclude :: Block -> IO Block
|
||||
doInclude cb@(Div (id, classes, namevals) contents) =
|
||||
case lookup (T.pack "include") namevals of
|
||||
Just f -> Div (id, classes, namevals) <$>
|
||||
file2Block (T.unpack f)
|
||||
Nothing -> return cb
|
||||
case lookup (T.pack "include") namevals of
|
||||
Just f -> Div (id, classes, namevals) <$> file2Block
|
||||
(T.unpack f)
|
||||
Nothing -> return cb
|
||||
doInclude x = return x
|
||||
|
||||
main :: IO ()
|
||||
|
||||
Reference in New Issue
Block a user