Skip to content
This repository
Browse code

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...
commit ea52f32d62c49efdc743eed9539848bd2838197a 1 parent 7c07865
John MacFarlane authored
96 data/pandoc.lua
@@ -527,6 +527,44 @@ end
527 527
528 528 -- pandoc-specific stuff
529 529
  530 +function isArray(x)
  531 + local mt = getmetatable(x) or {}
  532 + if type(x) ~= "table" then
  533 + return false
  534 + elseif mt.__jsontype == 'array' then
  535 + return true
  536 + elseif x[1] then
  537 + return true
  538 + else
  539 + return (x == {})
  540 + end
  541 +end
  542 +
  543 +function isObject(x)
  544 + local mt = getmetatable(x) or {}
  545 + if type(x) ~= "table" then
  546 + return false
  547 + elseif mt.__jsontype == 'object' then
  548 + return true
  549 + elseif x[1] then
  550 + return false
  551 + else
  552 + return false
  553 + end
  554 +end
  555 +
  556 +function emptyArray()
  557 + local array = {}
  558 + setmetatable(array, {__jsontype = 'array'})
  559 + return array
  560 +end
  561 +
  562 +function emptyObject()
  563 + local obj = {}
  564 + setmetatable(obj, {__jsontype = 'object'})
  565 + return obj
  566 +end
  567 +
530 568 -- Walk a JSON-encoded pandoc structure, performing
531 569 -- 'action' on each object encountered, and possibly
532 570 -- replacing or deleting it. The 'format' parameter
@@ -546,29 +584,46 @@ end
546 584 -- second a value. The original object is replaced
547 585 -- with a new object with the specified key and value.
548 586 function walk(x, action, format)
549   - if type(x) == "table" then
550   - if x[1] then
551   - for i,v in pairs(x) do
552   - walk(v, action, format)
553   - end
554   - else
555   - for k,v in pairs(x) do
556   - local kk, vv = action(k,v,format)
557   - walk(vv, action, format)
558   - if kk then
559   - if kk ~= {} then
560   - x[kk] = vv
561   - end
562   - if k ~= kk then
563   - x[k] = nil
564   - end
565   - else -- recurse
566   - if type(v) == "table" then
567   - walk(v, action, format)
  587 + if isArray(x) then
  588 + local array, j = {}, 1
  589 + setmetatable(array, {__jsontype = 'array'})
  590 + for _,w in pairs(x) do
  591 + if isObject(w) then
  592 + for kk,vv in pairs(w) do -- should be just one
  593 + local res, extra = action(kk, vv, format)
  594 + if res == nil then
  595 + array[j] = walk(w, action, format)
  596 + j = j + 1
  597 + elseif isArray(res) then
  598 + for _,z in pairs(res) do
  599 + array[j] = walk(z, action, format)
  600 + j = j + 1
  601 + end
  602 + elseif type(res) == "string" and extra then
  603 + local newobj = {[res] = walk(extra, action, format)}
  604 + setmetatable(newobj, {__jsontype = 'object'})
  605 + array[j] = newobj
  606 + j = j + 1
  607 + else
  608 + array[j] = walk(res, action, format)
  609 + j = j + 1
568 610 end
569 611 end
  612 + else
  613 + array[j] = walk(w, action, format)
  614 + j = j + 1
570 615 end
571 616 end
  617 + return array
  618 + elseif isObject(x) then
  619 + local obj = {}
  620 + setmetatable(obj, {__jsontype = 'object'})
  621 + for k,v in pairs(x) do
  622 + obj[k] = walk(v, action, format)
  623 + end
  624 + return obj
  625 + else -- basic type
  626 + return x
572 627 end
573 628 end
574 629
@@ -578,8 +633,7 @@ end
578 633 function filter(action)
579 634 return function(text, format)
580 635 local doc = json.decode(text)
581   - walk(doc, action, format)
582   - return json.encode(doc)
  636 + return json.encode(walk(doc, action, format))
583 637 end
584 638 end
585 639
22 luascripts/comments.lua
... ... @@ -0,0 +1,22 @@
  1 +incomment = false
  2 +
  3 +function comment(k,v)
  4 + if k == 'RawBlock' then
  5 + local f,s = unpack(v)
  6 + local format = f['unFormat']
  7 + if format == "html" then
  8 + if string.match(s, "!-- BEGIN COMMENT --") then
  9 + incomment = true
  10 + return emptyArray()
  11 + elseif string.match(s, "!-- END COMMENT --") then
  12 + incomment = false
  13 + return emptyArray()
  14 + end
  15 + end
  16 + end
  17 + if incomment then
  18 + return emptyArray() -- suppress
  19 + end
  20 +end
  21 +
  22 +transform = filter(comment)
3  luascripts/deemph.lua
@@ -6,8 +6,7 @@ end
6 6
7 7 local function deemph(k,v,f)
8 8 if k == 'Emph' and f == 'html' then
9   - walk(v,caps,f)
10   - return 'Span', {attributes{}, v}
  9 + return walk(v,caps,f)
11 10 end
12 11 end
13 12
2  pandoc.cabal
@@ -108,6 +108,8 @@ Data-Files:
108 108 -- sample lua scripts
109 109 luascripts/caps.lua,
110 110 luascripts/deemph.lua,
  111 + luascripts/myemph.lua,
  112 + luascripts/comments.lua,
111 113 -- documentation
112 114 README, INSTALL, COPYRIGHT, BUGS, CONTRIBUTING.md, changelog
113 115 Extra-Source-Files:

0 comments on commit ea52f32

Please sign in to comment.
Something went wrong with that request. Please try again.