Finished refactoring

This commit is contained in:
perro tuerto 2023-04-17 17:59:58 -07:00
parent f2016b6654
commit e7436e07a0
4 changed files with 82 additions and 87 deletions

85
dist/lin.lua vendored
View File

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

View File

@ -253,19 +253,20 @@ if pandoc ~= nil then
-- @param content pandoc.MetaValue: Pandoc content value
-- @return string: Pandoc stringified value
function pandoc.utils.rawstringify(content)
if pandoc.utils.type(content) == "Inlines" then
return pandoc.utils.stringify(content:walk {
Quoted = function(quoted)
local q = (quoted.quotetype == SingleQuoted and '"' or "'")
return pandoc.Str(q .. pandoc.utils.stringify(quoted.content) .. q)
end,
RawInline = function(rawinline)
return pandoc.Str(rawinline.text:gsub("\\n", "\n"):gsub("\\t", "\t"))
end,
Inline = function(inline) return pandoc.utils.stringify(inline) end,
})
end
return pandoc.utils.stringify(content)
return pandoc.utils.stringify(content:walk {
Plain = function(plain)
table.insert(plain.content, pandoc.Space())
return plain
end,
Quoted = function(quoted)
local q = (quoted.quotetype == SingleQuoted and '"' or "'")
return pandoc.Str(q .. pandoc.utils.stringify(quoted.content) .. q)
end,
RawInline = function(rawinline)
return pandoc.Str(rawinline.text:gsub("\\n", "\n"):gsub("\\t", "\t"))
end,
Inline = function(inline) return pandoc.utils.stringify(inline) end,
})
end
-- Converts pandoc.Meta to table
@ -280,11 +281,12 @@ if pandoc ~= nil then
if pandoc.utils.type(v) == "table" then
newmeta[k] = pandoc.metatotable(v)
else
newmeta[k] = pandoc.utils.rawstringify(v)
if newmeta[k] == "true" or meta[k] == "false" then
newmeta[k] = newmeta[k] == "true"
elseif tonumber(newmeta[k]) then
newmeta[k] = tonumber(newmeta[k])
if type(v) == "boolean" then
newmeta[k] = v
elseif tonumber(v) then
newmeta[k] = tonumber(v)
else
newmeta[k] = pandoc.utils.rawstringify(v)
end
end
end

View File

@ -94,14 +94,8 @@ function lit.puts(key, ...)
end
end
-- TODO
-- Inserts evaluations in inline code
function lit.insert(code)
return code
end
-- Examinates (parses and evaluates) code blocks
function lit.exam(blocks)
-- Examinates (parses and evaluates) document
function lit.exam(doc)
-- Evaluates code block
-- Evals Lisp code
@ -122,7 +116,7 @@ function lit.exam(blocks)
]]--
-- Parses code block
local function parse(codeblock, islast)
local function parse(codeblock)
-- Checks code block
local function check(parsed)
@ -212,23 +206,27 @@ function lit.exam(blocks)
local checked = (parsed ~= nil and check(parsed) or parsed)
-- TODO:
-- evaluated = eval(checked)
if islast and not(lit.status) then
lit.puts("aborted")
os.exit(1)
end
-- TODO:
-- return evaluated
return codeblock
end
local curr, last = 0, 0
blocks:walk { CodeBlock = function(_) last = last + 1 end }
return blocks:walk {
CodeBlock = function(codeblock)
curr = curr + 1
return parse(codeblock, curr == last)
-- TODO
-- Evals and inserts inline code
local function insert(code)
return code
end
-- Asserts literate status
local function assert()
if not(lit.status) then
lit.puts("aborted")
os.exit(1)
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
return lit

View File

@ -1,10 +1,8 @@
------------------------------------ PANDOC -----------------------------------
return {
-- Parses and evals literate blocks
{ Blocks = function(blocks) return lit.exam(blocks) end },
-- Parses and inserts literate evals
{ Code = function(code) return lit.insert(code) end },
-- Parses and evals literate programming
{ Pandoc = function(doc) return lit.exam(doc) end },
{
-- Parses and evals natural programming
-- TODO