Last active
December 31, 2017 23:22
-
-
Save yusanish/3f19cb5d138b26db32c9425b22515771 to your computer and use it in GitHub Desktop.
Pandoc (github Markdown -> Scrapbox). Using Custom template, indent does not work correctly. So, I swaped double and triple indented list to "--", before I use pandoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This is a sample custom writer for pandoc. | |
-- Scrapbox | |
-- Character escaping | |
local function escape(s, in_attribute) | |
return s:gsub("[<>&\"']", | |
function(x) | |
if x == '<' then | |
return '<' | |
elseif x == '>' then | |
return '>' | |
elseif x == '&' then | |
return '&' | |
elseif x == '"' then | |
return '"' | |
elseif x == "'" then | |
return ''' | |
else | |
return x | |
end | |
end) | |
end | |
-- Helper function to convert an attributes table into | |
-- a string that can be put into HTML tags. | |
local function attributes(attr) | |
local attr_table = {} | |
for x,y in pairs(attr) do | |
if y and y ~= "" then | |
table.insert(attr_table, ' ' .. x .. '="' .. escape(y,true) .. '"') | |
end | |
end | |
return table.concat(attr_table) | |
end | |
-- Run cmd on a temporary file containing inp and return result. | |
local function pipe(cmd, inp) | |
local tmp = os.tmpname() | |
local tmph = io.open(tmp, "w") | |
tmph:write(inp) | |
tmph:close() | |
local outh = io.popen(cmd .. " " .. tmp,"r") | |
local result = outh:read("*all") | |
outh:close() | |
os.remove(tmp) | |
return result | |
end | |
-- remove Note | |
-- Blocksep is used to separate block elements. | |
function Blocksep() | |
return "\n\n" | |
end | |
-- This function is called once for the whole document. Parameters: | |
-- body is a string, metadata is a table, variables is a table. | |
-- This gives you a fragment. You could use the metadata table to | |
-- fill variables in a custom lua template. Or, pass `--template=...` | |
-- to pandoc, and pandoc will add do the template processing as | |
-- usual. | |
function Doc(body, metadata, variables) | |
local buffer = {} | |
local function add(s) | |
table.insert(buffer, s) | |
end | |
add(body) | |
return table.concat(buffer,'\n') | |
end | |
-- The functions that follow render corresponding pandoc elements. | |
-- s is always a string, attr is always a table of attributes, and | |
-- items is always an array of strings (the items in a list). | |
-- Comments indicate the types of other variables. | |
function Str(s) | |
return escape(s) | |
end | |
function Space() | |
return " " | |
end | |
function SoftBreak() | |
return "\n" | |
end | |
function LineBreak() | |
return "\n" | |
end | |
function Emph(s) | |
return "[/ " .. s .. "]" | |
end | |
function Strong(s) | |
return "[[" .. s .. "]]" | |
end | |
-- remove Subscript | |
-- remove Superscript | |
-- remove SmallCaps | |
function Strikeout(s) | |
return '[- ' .. s .. ']' | |
end | |
function Link(s, src, tit, attr) | |
return "[" .. escape(s,true) .. " " .. escape(src,true) .. "]" | |
end | |
function Image(s, src, tit, attr) | |
return "[" .. escape(s,true) .. " " .. escape(src,true) .. "]" | |
end | |
function Code(s, attr) | |
return "`" .. escape(s) .. "`\n" | |
end | |
function InlineMath(s) | |
return "[$ " .. escape(s) .. "]" | |
end | |
function DisplayMath(s) | |
return "[$" .. escape(s) .. "]" | |
end | |
function Note(s) | |
return "((" .. s .. "))" | |
end | |
function Span(s, attr) | |
return attributes(attr) .. s | |
end | |
-- function RawInline(format, str) | |
-- if format == "html" then | |
-- return str | |
-- else | |
-- return '' | |
-- end | |
-- end | |
-- remove Cite | |
function Plain(s) | |
return s | |
end | |
function Para(s) | |
return s | |
end | |
-- lev is an integer, the header level. | |
function Header(lev, s, attr) | |
local buffer = {} | |
table.insert(buffer, "[") | |
for i = 1, 5 do | |
table.insert(buffer, "*") | |
end | |
for i = 1, lev do | |
table.remove(buffer) | |
end | |
return table.concat(buffer, "") .. " " .. s .. "]" | |
end | |
function BlockQuote(s) | |
local buffer = {} | |
local lines = Split(s, "\n") | |
for _, line in pairs(lines) do | |
table.insert(buffer, "> " .. line .. "\n") | |
end | |
return table.concat(buffer, "") | |
end | |
-- remove HorizontalRule | |
-- remove LineBlock | |
-- add Split for CodeBlock | |
function Split(str, pat) | |
local t = {} -- NOTE: use {n = 0} in Lua-5.0 | |
local fpat = "(.-)" .. pat | |
local last_end = 1 | |
local s, e, cap = str:find(fpat, 1) | |
while s do | |
if s ~= 1 or cap ~= "" then | |
table.insert(t,cap) | |
end | |
last_end = e+1 | |
s, e, cap = str:find(fpat, last_end) | |
end | |
if last_end <= #str then | |
cap = str:sub(last_end) | |
table.insert(t, cap) | |
end | |
return t | |
end | |
function CodeBlock(s, attr) | |
local buffer = {} | |
local lines = Split(s, "\n") | |
return "code:temp" .. table.concat(attr, "") .. "\n\t" .. | |
table.concat(lines, "\n\t") .. "\n" | |
end | |
function BulletList(items) | |
local buffer = {} | |
for _, item in pairs(items) do | |
table.insert(buffer, "\t" .. item) | |
end | |
return table.concat(buffer, "\n") | |
end | |
function OrderedList(items) | |
local buffer = {} | |
for _, item in pairs(items) do | |
table.insert(buffer, "\t" .. item) | |
end | |
return table.concat(buffer, "\n") | |
end | |
-- Revisit association list STackValue instance. | |
function DefinitionList(items) | |
local buffer = {} | |
for _,item in pairs(items) do | |
for k, v in pairs(item) do | |
table.insert(buffer, "\t" .. k .. ":" .. table.concat(v, "") .. "\n") | |
end | |
end | |
return table.concat(buffer, "\n") | |
end | |
-- remove html Align | |
-- edit CaptionedImage | |
function CaptionedImage(src, tit, caption) | |
return '[' .. escape(caption,true) .. " " .. escape(src,true) .. ']' | |
end | |
-- edit | |
-- Caption is a string, aligns is an array of strings, | |
-- widths is an array of floats, headers is an array of | |
-- strings, rows is an array of arrays of strings. | |
function Table(caption, aligns, widths, headers, rows) | |
local buffer = {} | |
table.insert(buffer, "table:temp") | |
local function add(s) | |
table.insert(buffer, s) | |
end | |
if caption ~= "" then | |
add(caption) | |
end | |
local header_row = {} | |
local empty_header = true | |
for i, h in pairs(headers) do | |
table.insert(header_row, h) | |
empty_header = empty_header and h == "" | |
end | |
if empty_header then | |
head = "" | |
else | |
head = "\t" .. table.concat(header_row, "\t") | |
end | |
add(head) | |
for _, row in pairs(rows) do | |
local rows = {} | |
for i,c in pairs(row) do | |
table.insert(rows, c) | |
end | |
add("\t" .. table.concat(rows, "\t")) | |
end | |
return table.concat(buffer,'\n') | |
end | |
function RawBlock(format, str) | |
if format == "html" then | |
return str | |
else | |
return '' | |
end | |
end | |
function Div(s, attr) | |
return s | |
end | |
-- The following code will produce runtime warnings when you haven't defined | |
-- all of the functions you need for the custom writer, so it's useful | |
-- to include when you're working on a writer. | |
local meta = {} | |
meta.__index = | |
function(_, key) | |
io.stderr:write(string.format("WARNING: Undefined function '%s'\n",key)) | |
return function() return "" end | |
end | |
setmetatable(_G, meta) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment