Browse files

Rewrote pandoc.lua.

Now you can replace an object with an array, and it will splice in.

Added emptyArray(), emptyObject() convenience functions.
  • Loading branch information...
1 parent 7c07865 commit ea52f32d62c49efdc743eed9539848bd2838197a @jgm committed Aug 13, 2013
Showing with 100 additions and 23 deletions.
  1. +75 −21 data/pandoc.lua
  2. +22 −0 luascripts/comments.lua
  3. +1 −2 luascripts/deemph.lua
  4. +2 −0 pandoc.cabal
View
96 data/pandoc.lua
@@ -527,6 +527,44 @@ end
-- pandoc-specific stuff
+function isArray(x)
+ local mt = getmetatable(x) or {}
+ if type(x) ~= "table" then
+ return false
+ elseif mt.__jsontype == 'array' then
+ return true
+ elseif x[1] then
+ return true
+ else
+ return (x == {})
+ end
+end
+
+function isObject(x)
+ local mt = getmetatable(x) or {}
+ if type(x) ~= "table" then
+ return false
+ elseif mt.__jsontype == 'object' then
+ return true
+ elseif x[1] then
+ return false
+ else
+ return false
+ end
+end
+
+function emptyArray()
+ local array = {}
+ setmetatable(array, {__jsontype = 'array'})
+ return array
+end
+
+function emptyObject()
+ local obj = {}
+ setmetatable(obj, {__jsontype = 'object'})
+ return obj
+end
+
-- Walk a JSON-encoded pandoc structure, performing
-- 'action' on each object encountered, and possibly
-- replacing or deleting it. The 'format' parameter
@@ -546,29 +584,46 @@ end
-- second a value. The original object is replaced
-- with a new object with the specified key and value.
function walk(x, action, format)
- if type(x) == "table" then
- if x[1] then
- for i,v in pairs(x) do
- walk(v, action, format)
- end
- else
- for k,v in pairs(x) do
- local kk, vv = action(k,v,format)
- walk(vv, action, format)
- if kk then
- if kk ~= {} then
- x[kk] = vv
- end
- if k ~= kk then
- x[k] = nil
- end
- else -- recurse
- if type(v) == "table" then
- walk(v, action, format)
+ if isArray(x) then
+ local array, j = {}, 1
+ setmetatable(array, {__jsontype = 'array'})
+ for _,w in pairs(x) do
+ if isObject(w) then
+ for kk,vv in pairs(w) do -- should be just one
+ local res, extra = action(kk, vv, format)
+ if res == nil then
+ array[j] = walk(w, action, format)
+ j = j + 1
+ elseif isArray(res) then
+ for _,z in pairs(res) do
+ array[j] = walk(z, action, format)
+ j = j + 1
+ end
+ elseif type(res) == "string" and extra then
+ local newobj = {[res] = walk(extra, action, format)}
+ setmetatable(newobj, {__jsontype = 'object'})
+ array[j] = newobj
+ j = j + 1
+ else
+ array[j] = walk(res, action, format)
+ j = j + 1
end
end
+ else
+ array[j] = walk(w, action, format)
+ j = j + 1
end
end
+ return array
+ elseif isObject(x) then
+ local obj = {}
+ setmetatable(obj, {__jsontype = 'object'})
+ for k,v in pairs(x) do
+ obj[k] = walk(v, action, format)
+ end
+ return obj
+ else -- basic type
+ return x
end
end
@@ -578,8 +633,7 @@ end
function filter(action)
return function(text, format)
local doc = json.decode(text)
- walk(doc, action, format)
- return json.encode(doc)
+ return json.encode(walk(doc, action, format))
end
end
View
22 luascripts/comments.lua
@@ -0,0 +1,22 @@
+incomment = false
+
+function comment(k,v)
+ if k == 'RawBlock' then
+ local f,s = unpack(v)
+ local format = f['unFormat']
+ if format == "html" then
+ if string.match(s, "!-- BEGIN COMMENT --") then
+ incomment = true
+ return emptyArray()
+ elseif string.match(s, "!-- END COMMENT --") then
+ incomment = false
+ return emptyArray()
+ end
+ end
+ end
+ if incomment then
+ return emptyArray() -- suppress
+ end
+end
+
+transform = filter(comment)
View
3 luascripts/deemph.lua
@@ -6,8 +6,7 @@ end
local function deemph(k,v,f)
if k == 'Emph' and f == 'html' then
- walk(v,caps,f)
- return 'Span', {attributes{}, v}
+ return walk(v,caps,f)
end
end
View
2 pandoc.cabal
@@ -108,6 +108,8 @@ Data-Files:
-- sample lua scripts
luascripts/caps.lua,
luascripts/deemph.lua,
+ luascripts/myemph.lua,
+ luascripts/comments.lua,
-- documentation
README, INSTALL, COPYRIGHT, BUGS, CONTRIBUTING.md, changelog
Extra-Source-Files:

0 comments on commit ea52f32

Please sign in to comment.