Finished refactoring
This commit is contained in:
parent
f2016b6654
commit
e7436e07a0
|
@ -6514,19 +6514,20 @@ if pandoc ~= nil then
|
||||||
-- @param content pandoc.MetaValue: Pandoc content value
|
-- @param content pandoc.MetaValue: Pandoc content value
|
||||||
-- @return string: Pandoc stringified value
|
-- @return string: Pandoc stringified value
|
||||||
function pandoc.utils.rawstringify(content)
|
function pandoc.utils.rawstringify(content)
|
||||||
if pandoc.utils.type(content) == "Inlines" then
|
return pandoc.utils.stringify(content:walk {
|
||||||
return pandoc.utils.stringify(content:walk {
|
Plain = function(plain)
|
||||||
Quoted = function(quoted)
|
table.insert(plain.content, pandoc.Space())
|
||||||
local q = (quoted.quotetype == SingleQuoted and '"' or "'")
|
return plain
|
||||||
return pandoc.Str(q .. pandoc.utils.stringify(quoted.content) .. q)
|
end,
|
||||||
end,
|
Quoted = function(quoted)
|
||||||
RawInline = function(rawinline)
|
local q = (quoted.quotetype == SingleQuoted and '"' or "'")
|
||||||
return pandoc.Str(rawinline.text:gsub("\\n", "\n"):gsub("\\t", "\t"))
|
return pandoc.Str(q .. pandoc.utils.stringify(quoted.content) .. q)
|
||||||
end,
|
end,
|
||||||
Inline = function(inline) return pandoc.utils.stringify(inline) end,
|
RawInline = function(rawinline)
|
||||||
})
|
return pandoc.Str(rawinline.text:gsub("\\n", "\n"):gsub("\\t", "\t"))
|
||||||
end
|
end,
|
||||||
return pandoc.utils.stringify(content)
|
Inline = function(inline) return pandoc.utils.stringify(inline) end,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Converts pandoc.Meta to table
|
-- Converts pandoc.Meta to table
|
||||||
|
@ -6536,17 +6537,17 @@ if pandoc ~= nil then
|
||||||
-- @param meta pandoc.Meta: Pandoc MetaValues collection
|
-- @param meta pandoc.Meta: Pandoc MetaValues collection
|
||||||
-- @return table: Pandoc metadata as table
|
-- @return table: Pandoc metadata as table
|
||||||
function pandoc.metatotable(meta)
|
function pandoc.metatotable(meta)
|
||||||
|
|
||||||
local newmeta = {}
|
local newmeta = {}
|
||||||
for k, v in pairs(meta) do
|
for k, v in pairs(meta) do
|
||||||
if pandoc.utils.type(v) == "table" then
|
if pandoc.utils.type(v) == "table" then
|
||||||
newmeta[k] = pandoc.metatotable(v)
|
newmeta[k] = pandoc.metatotable(v)
|
||||||
else
|
else
|
||||||
newmeta[k] = pandoc.utils.rawstringify(v)
|
if type(v) == "boolean" then
|
||||||
if newmeta[k] == "true" or meta[k] == "false" then
|
newmeta[k] = v
|
||||||
newmeta[k] = newmeta[k] == "true"
|
elseif tonumber(v) then
|
||||||
elseif tonumber(newmeta[k]) then
|
newmeta[k] = tonumber(v)
|
||||||
newmeta[k] = tonumber(newmeta[k])
|
else
|
||||||
|
newmeta[k] = pandoc.utils.rawstringify(v)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -6692,14 +6693,8 @@ ERROR:
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO
|
-- Examinates (parses and evaluates) document
|
||||||
-- Inserts evaluations in inline code
|
function lit.exam(doc)
|
||||||
function lit.insert(code)
|
|
||||||
return code
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Examinates (parses and evaluates) code blocks
|
|
||||||
function lit.exam(blocks)
|
|
||||||
|
|
||||||
-- Evaluates code block
|
-- Evaluates code block
|
||||||
-- Evals Lisp code
|
-- Evals Lisp code
|
||||||
|
@ -6720,7 +6715,7 @@ function lit.exam(blocks)
|
||||||
]]--
|
]]--
|
||||||
|
|
||||||
-- Parses code block
|
-- Parses code block
|
||||||
local function parse(codeblock, islast)
|
local function parse(codeblock)
|
||||||
|
|
||||||
-- Checks code block
|
-- Checks code block
|
||||||
local function check(parsed)
|
local function check(parsed)
|
||||||
|
@ -6810,32 +6805,34 @@ function lit.exam(blocks)
|
||||||
local checked = (parsed ~= nil and check(parsed) or parsed)
|
local checked = (parsed ~= nil and check(parsed) or parsed)
|
||||||
-- TODO:
|
-- TODO:
|
||||||
-- evaluated = eval(checked)
|
-- evaluated = eval(checked)
|
||||||
if islast and not(lit.status) then
|
|
||||||
lit.puts("aborted")
|
|
||||||
os.exit(1)
|
|
||||||
end
|
|
||||||
-- TODO:
|
|
||||||
-- return evaluated
|
-- return evaluated
|
||||||
return codeblock
|
return codeblock
|
||||||
end
|
end
|
||||||
|
|
||||||
local curr, last = 0, 0
|
-- TODO
|
||||||
blocks:walk { CodeBlock = function(_) last = last + 1 end }
|
-- Evals and inserts inline code
|
||||||
return blocks:walk {
|
local function insert(code)
|
||||||
CodeBlock = function(codeblock)
|
return code
|
||||||
curr = curr + 1
|
end
|
||||||
return parse(codeblock, curr == last)
|
|
||||||
|
-- Asserts literate status
|
||||||
|
local function assert()
|
||||||
|
if not(lit.status) then
|
||||||
|
lit.puts("aborted")
|
||||||
|
os.exit(1)
|
||||||
end
|
end
|
||||||
}
|
end
|
||||||
|
|
||||||
|
return doc:walk { CodeBlock = function(block) return parse(block) end }
|
||||||
|
:walk { Code = function(inline) return insert(inline) end }
|
||||||
|
:walk { Pandoc = function(_) assert() end }
|
||||||
end
|
end
|
||||||
|
|
||||||
------------------------------------ PANDOC -----------------------------------
|
------------------------------------ PANDOC -----------------------------------
|
||||||
|
|
||||||
return {
|
return {
|
||||||
-- Parses and evals literate blocks
|
-- Parses and evals literate programming
|
||||||
{ Blocks = function(blocks) return lit.exam(blocks) end },
|
{ Pandoc = function(doc) return lit.exam(doc) end },
|
||||||
-- Parses and inserts literate evals
|
|
||||||
{ Code = function(code) return lit.insert(code) end },
|
|
||||||
{
|
{
|
||||||
-- Parses and evals natural programming
|
-- Parses and evals natural programming
|
||||||
-- TODO
|
-- TODO
|
||||||
|
|
|
@ -253,19 +253,20 @@ if pandoc ~= nil then
|
||||||
-- @param content pandoc.MetaValue: Pandoc content value
|
-- @param content pandoc.MetaValue: Pandoc content value
|
||||||
-- @return string: Pandoc stringified value
|
-- @return string: Pandoc stringified value
|
||||||
function pandoc.utils.rawstringify(content)
|
function pandoc.utils.rawstringify(content)
|
||||||
if pandoc.utils.type(content) == "Inlines" then
|
return pandoc.utils.stringify(content:walk {
|
||||||
return pandoc.utils.stringify(content:walk {
|
Plain = function(plain)
|
||||||
Quoted = function(quoted)
|
table.insert(plain.content, pandoc.Space())
|
||||||
local q = (quoted.quotetype == SingleQuoted and '"' or "'")
|
return plain
|
||||||
return pandoc.Str(q .. pandoc.utils.stringify(quoted.content) .. q)
|
end,
|
||||||
end,
|
Quoted = function(quoted)
|
||||||
RawInline = function(rawinline)
|
local q = (quoted.quotetype == SingleQuoted and '"' or "'")
|
||||||
return pandoc.Str(rawinline.text:gsub("\\n", "\n"):gsub("\\t", "\t"))
|
return pandoc.Str(q .. pandoc.utils.stringify(quoted.content) .. q)
|
||||||
end,
|
end,
|
||||||
Inline = function(inline) return pandoc.utils.stringify(inline) end,
|
RawInline = function(rawinline)
|
||||||
})
|
return pandoc.Str(rawinline.text:gsub("\\n", "\n"):gsub("\\t", "\t"))
|
||||||
end
|
end,
|
||||||
return pandoc.utils.stringify(content)
|
Inline = function(inline) return pandoc.utils.stringify(inline) end,
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Converts pandoc.Meta to table
|
-- Converts pandoc.Meta to table
|
||||||
|
@ -280,11 +281,12 @@ if pandoc ~= nil then
|
||||||
if pandoc.utils.type(v) == "table" then
|
if pandoc.utils.type(v) == "table" then
|
||||||
newmeta[k] = pandoc.metatotable(v)
|
newmeta[k] = pandoc.metatotable(v)
|
||||||
else
|
else
|
||||||
newmeta[k] = pandoc.utils.rawstringify(v)
|
if type(v) == "boolean" then
|
||||||
if newmeta[k] == "true" or meta[k] == "false" then
|
newmeta[k] = v
|
||||||
newmeta[k] = newmeta[k] == "true"
|
elseif tonumber(v) then
|
||||||
elseif tonumber(newmeta[k]) then
|
newmeta[k] = tonumber(v)
|
||||||
newmeta[k] = tonumber(newmeta[k])
|
else
|
||||||
|
newmeta[k] = pandoc.utils.rawstringify(v)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -94,14 +94,8 @@ function lit.puts(key, ...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO
|
-- Examinates (parses and evaluates) document
|
||||||
-- Inserts evaluations in inline code
|
function lit.exam(doc)
|
||||||
function lit.insert(code)
|
|
||||||
return code
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Examinates (parses and evaluates) code blocks
|
|
||||||
function lit.exam(blocks)
|
|
||||||
|
|
||||||
-- Evaluates code block
|
-- Evaluates code block
|
||||||
-- Evals Lisp code
|
-- Evals Lisp code
|
||||||
|
@ -122,7 +116,7 @@ function lit.exam(blocks)
|
||||||
]]--
|
]]--
|
||||||
|
|
||||||
-- Parses code block
|
-- Parses code block
|
||||||
local function parse(codeblock, islast)
|
local function parse(codeblock)
|
||||||
|
|
||||||
-- Checks code block
|
-- Checks code block
|
||||||
local function check(parsed)
|
local function check(parsed)
|
||||||
|
@ -212,23 +206,27 @@ function lit.exam(blocks)
|
||||||
local checked = (parsed ~= nil and check(parsed) or parsed)
|
local checked = (parsed ~= nil and check(parsed) or parsed)
|
||||||
-- TODO:
|
-- TODO:
|
||||||
-- evaluated = eval(checked)
|
-- evaluated = eval(checked)
|
||||||
if islast and not(lit.status) then
|
|
||||||
lit.puts("aborted")
|
|
||||||
os.exit(1)
|
|
||||||
end
|
|
||||||
-- TODO:
|
|
||||||
-- return evaluated
|
-- return evaluated
|
||||||
return codeblock
|
return codeblock
|
||||||
end
|
end
|
||||||
|
|
||||||
local curr, last = 0, 0
|
-- TODO
|
||||||
blocks:walk { CodeBlock = function(_) last = last + 1 end }
|
-- Evals and inserts inline code
|
||||||
return blocks:walk {
|
local function insert(code)
|
||||||
CodeBlock = function(codeblock)
|
return code
|
||||||
curr = curr + 1
|
end
|
||||||
return parse(codeblock, curr == last)
|
|
||||||
|
-- Asserts literate status
|
||||||
|
local function assert()
|
||||||
|
if not(lit.status) then
|
||||||
|
lit.puts("aborted")
|
||||||
|
os.exit(1)
|
||||||
end
|
end
|
||||||
}
|
end
|
||||||
|
|
||||||
|
return doc:walk { CodeBlock = function(block) return parse(block) end }
|
||||||
|
:walk { Code = function(inline) return insert(inline) end }
|
||||||
|
:walk { Pandoc = function(_) assert() end }
|
||||||
end
|
end
|
||||||
|
|
||||||
return lit
|
return lit
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
------------------------------------ PANDOC -----------------------------------
|
------------------------------------ PANDOC -----------------------------------
|
||||||
|
|
||||||
return {
|
return {
|
||||||
-- Parses and evals literate blocks
|
-- Parses and evals literate programming
|
||||||
{ Blocks = function(blocks) return lit.exam(blocks) end },
|
{ Pandoc = function(doc) return lit.exam(doc) end },
|
||||||
-- Parses and inserts literate evals
|
|
||||||
{ Code = function(code) return lit.insert(code) end },
|
|
||||||
{
|
{
|
||||||
-- Parses and evals natural programming
|
-- Parses and evals natural programming
|
||||||
-- TODO
|
-- TODO
|
||||||
|
|
Loading…
Reference in New Issue