Finished scripts refactoring

This commit is contained in:
perro tuerto 2023-03-23 18:53:36 -07:00
parent 49ac3f53fc
commit e353f5125f
8 changed files with 195 additions and 58 deletions

72
dist/lin.lua vendored
View File

@ -6259,7 +6259,7 @@ do
end end
local fnl = mod local fnl = mod
------------------------------------ MODS ------------------------------------ ---------------------------------- EXTENSIONS ---------------------------------
-- Tries popen -- Tries popen
-- @param ... string: Chunks for popen -- @param ... string: Chunks for popen
@ -6274,12 +6274,15 @@ end
-- Gets OS short name -- Gets OS short name
-- It is just intented to know if it is Linux, macOS or Windows. -- It is just intented to know if it is Linux, macOS or Windows.
-- @return string: "linux" or "macos" or "windows" -- Sorry
-- @return string: "linux" or "bsd" or "macos" or "windows"
function os.uname() function os.uname()
local status, output = io.try("uname") local status, output = io.try("uname")
if status then if status then
output = output:gsub(" .*", ""):lower() output = output:gsub(" .*", ""):lower()
if output ~= "linux" then if output ~= "linux" and output:match("bsd") ~= nil then
return "bsd"
elseif out ~= "linux" then
return "macos" return "macos"
end end
return "linux" return "linux"
@ -6346,6 +6349,8 @@ end
function string:isdir() function string:isdir()
if self:exists() then if self:exists() then
return io.open(self, "a+") == nil return io.open(self, "a+") == nil
elseif self == "." or self == ".." then
return true
end end
return false return false
end end
@ -6357,6 +6362,66 @@ function string:read_text()
return io.open(self):read("*a") return io.open(self):read("*a")
end end
end end
function string:stem()
return self:gsub("%.%a+$", "")
end
-- Gets file extensions
-- @return table: list of file extensions
function string:suffixes()
local suffixes = {}
for suffix in self:gmatch("%.%a+") do
table.insert(suffixes, suffix)
end
return suffixes
end
-- Gets file final extension
-- @return string: final file extension
function string:suffix()
local suffixes = self:suffixes()
if suffixes[#suffixes] then
return suffixes[#suffixes]
end
return ""
end
-- Requires Pandoc
-- Check: https://pandoc.org/lua-filters.html#module-pandoc
if pandoc ~= nil then
-- Gets file extension namespace
-- Check: https://pandoc.org/MANUAL.html#general-options
-- @param file string: File name
-- @return string: File extension according to Pandoc format namespaces
function pandoc.getext(file)
local ext = file:suffix():gsub("^.", "")
return ((ext == "md" or ext:isempty()) and "markdown" or ext)
end
-- Pandoc converter
-- Converts input file name into output format name.
-- If ofile is not nil, writes output file name instead of returning content.
-- If iformat is nil, defaults to ifile extension name.
-- @param ifile string: Input file name
-- @param oformat string: Output format name
-- @param ofile string or nil: Output file name
-- @param iformat string or nil: Input format name
-- @return string or nil: Output file content
function pandoc.convert(ifile, oformat, ofile, iformat)
iformat = (iformat == nil and pandoc.getext(ifile) or iformat)
local doc = pandoc.write(pandoc.read(ifile:read_text(), iformat), oformat)
if ofile == nil then
return doc
else
local eol = (os:isunix() and "\n" or "\r\n")
io.open(ofile, "w"):write(doc, eol):close()
end
end
end
----------------------------------- NATURAL ----------------------------------- ----------------------------------- NATURAL -----------------------------------
local nat = {} local nat = {}
@ -6409,7 +6474,6 @@ function lit.checkmetatype(type, meta, key)
local err = "" local err = ""
if type ~= mtype then if type ~= mtype then
if type == "path" then if type == "path" then
-- TODO
if not(pandoc.path.directory(mval):isdir()) then if not(pandoc.path.directory(mval):isdir()) then
err = "Invalid path '" .. mval .. "' in key '" .. key .. "'" err = "Invalid path '" .. mval .. "' in key '" .. key .. "'"
end end

View File

@ -1,10 +1,29 @@
-- Adds Lua custom extensions -- Makes bundle distribution
require "../src/extensions"
-- Adds Lua custom extensions
require "src.extensions"
-- Makes distribution
local function make_dist()
-- Chomps file
local function chomp(str, without)
local without = without or false
str = string.read_text(str):strip()
if without then
return str:gsub("\n[^\n]+$", "")
end
return "\n" .. str .. "\n"
end
function make_dist()
-- Variables -- Variables
local name = "lin.lua" local name = "lin.lua"
local dist = pandoc.path.join({"dist", name}) local dist = pandoc.path.join({"dist", name})
local ext = chomp("src/extensions.lua")
local pan = chomp("src/pandoc.lua")
local fnl = chomp("opt/fennel.lua", true)
local nat = chomp("src/natural.lua", true)
local lit = chomp("src/literate.lua", true)
local license = string.strip([[ local license = string.strip([[
Computable Pandoc & Fennel Bundle: Computable Pandoc & Fennel Bundle:
A Pandoc filter for literate and natural programming A Pandoc filter for literate and natural programming
@ -19,22 +38,6 @@ Fennel:
Website: https://fennel-lang.org Website: https://fennel-lang.org
]]) ]])
function chomp(str, without)
local without = without or false
str = string.read_text(str):strip()
str = str:gsub('\n+--[^\n]+\nrequire "extensions"', "")
if without then
return str:gsub("\n[^\n]+$", "")
end
return "\n" .. str .. "\n"
end
local ext = chomp("src/extensions.lua")
local pan = chomp("src/pandoc.lua")
local fnl = chomp("opt/fennel.lua", true)
local nat = chomp("src/natural.lua", true)
local lit = chomp("src/literate.lua", true)
-- Bundles Fennel and Computable Pandoc -- Bundles Fennel and Computable Pandoc
file = io.open(dist, "w") file = io.open(dist, "w")
file:write("--[[\n", license, "\n]]--\n") file:write("--[[\n", license, "\n]]--\n")

24
scripts/make_jsons.lua Normal file
View File

@ -0,0 +1,24 @@
-- Makes JSON test files
-- Adds Lua custom extensions
require "src.extensions"
-- Variables
local filter = "dist/lin.lua"
-- Gets command according to OS
local function getcmd(str)
local system = (os.isunix() and "unix" or "win")
if os:isunix() then
return "ls"
else
return "dir"
end
end
-- Makes JSON from markup files
for file in io.popen(getcmd("ls") .. " tests/asts"):lines() do
local json = pandoc.path.join({"tests", "asts", file})
local mark = pandoc.path.join({"tests", pandoc.path.filename(json:stem())})
pandoc.convert(mark, "json", json)
end

View File

@ -1,11 +0,0 @@
# Makes JSON test files
# Variables
FILTER=dist/lin.lua
FILES="tests/*.md"
for file in $FILES; do
if [ -f $file.json ]; then
pandoc -L $FILTER -o $file.json $file
fi
done

View File

@ -1,22 +1,24 @@
-- Makes tests
-- Adds Lua custom extensions -- Adds Lua custom extensions
require "../src/extensions" require "src.extensions"
require "../scripts/make_dist" require "scripts.make_dist"
-- Variables -- Variables
local filter = "-L dist/lin.lua" local filter = "dist/lin.lua"
local verbose = "" local verbose = ""
local trace = "" local trace = ""
local files = {} local files = {}
local cmds = { local cmds = {
["unix"] = { ["unix"] = {
["pandoc"] = "pandoc ", ["pandoc"] = "pandoc",
["clear"] = "clear ", ["clear"] = "clear",
["ls"] = "ls ", ["ls"] = "ls",
}, },
["win"] = { ["win"] = {
["pandoc"] = "pandoc.exe ", ["pandoc"] = "pandoc.exe",
["clear"] = "cls ", ["clear"] = "cls",
["ls"] = "dir ", ["ls"] = "dir",
}, },
} }
@ -81,16 +83,13 @@ end
-- Default files -- Default files
if #files == 0 then if #files == 0 then
for file in io.popen(getcmd("ls") .. "tests"):lines() do for file in io.popen(getcmd("ls") .. " tests"):lines() do
if file ~= "asts" then if file ~= "asts" then
table.insert(files, pandoc.path.join({"tests", file})) table.insert(files, pandoc.path.join({"tests", file}))
end end
end end
end end
-- Makes distribution bundle
make_dist()
-- Clears terminal -- Clears terminal
os.execute(getcmd("clear")) os.execute(getcmd("clear"))
print "🐾 Starting tests" print "🐾 Starting tests"
@ -98,7 +97,7 @@ print "🐾 Starting tests"
-- Does tests -- Does tests
for _, file in ipairs(files) do for _, file in ipairs(files) do
local expectation = pandoc.path.filename(file):gsub("%W.+$", "") local expectation = pandoc.path.filename(file):gsub("%W.+$", "")
local result, output = getresult(file, filter, verbose, trace) local result, output = getresult(file, "-L", filter, verbose, trace)
print("⚗️ " .. file .. ":") print("⚗️ " .. file .. ":")
print(" Expect: " .. expectation) print(" Expect: " .. expectation)
print(" Result: " .. result) print(" Result: " .. result)

View File

@ -1,4 +1,4 @@
------------------------------------ MODS ------------------------------------ ---------------------------------- EXTENSIONS ---------------------------------
-- Tries popen -- Tries popen
-- @param ... string: Chunks for popen -- @param ... string: Chunks for popen
@ -13,12 +13,15 @@ end
-- Gets OS short name -- Gets OS short name
-- It is just intented to know if it is Linux, macOS or Windows. -- It is just intented to know if it is Linux, macOS or Windows.
-- @return string: "linux" or "macos" or "windows" -- Sorry
-- @return string: "linux" or "bsd" or "macos" or "windows"
function os.uname() function os.uname()
local status, output = io.try("uname") local status, output = io.try("uname")
if status then if status then
output = output:gsub(" .*", ""):lower() output = output:gsub(" .*", ""):lower()
if output ~= "linux" then if output ~= "linux" and output:match("bsd") ~= nil then
return "bsd"
elseif out ~= "linux" then
return "macos" return "macos"
end end
return "linux" return "linux"
@ -85,6 +88,8 @@ end
function string:isdir() function string:isdir()
if self:exists() then if self:exists() then
return io.open(self, "a+") == nil return io.open(self, "a+") == nil
elseif self == "." or self == ".." then
return true
end end
return false return false
end end
@ -96,3 +101,63 @@ function string:read_text()
return io.open(self):read("*a") return io.open(self):read("*a")
end end
end end
function string:stem()
return self:gsub("%.%a+$", "")
end
-- Gets file extensions
-- @return table: list of file extensions
function string:suffixes()
local suffixes = {}
for suffix in self:gmatch("%.%a+") do
table.insert(suffixes, suffix)
end
return suffixes
end
-- Gets file final extension
-- @return string: final file extension
function string:suffix()
local suffixes = self:suffixes()
if suffixes[#suffixes] then
return suffixes[#suffixes]
end
return ""
end
-- Requires Pandoc
-- Check: https://pandoc.org/lua-filters.html#module-pandoc
if pandoc ~= nil then
-- Gets file extension namespace
-- Check: https://pandoc.org/MANUAL.html#general-options
-- @param file string: File name
-- @return string: File extension according to Pandoc format namespaces
function pandoc.getext(file)
local ext = file:suffix():gsub("^.", "")
return ((ext == "md" or ext:isempty()) and "markdown" or ext)
end
-- Pandoc converter
-- Converts input file name into output format name.
-- If ofile is not nil, writes output file name instead of returning content.
-- If iformat is nil, defaults to ifile extension name.
-- @param ifile string: Input file name
-- @param oformat string: Output format name
-- @param ofile string or nil: Output file name
-- @param iformat string or nil: Input format name
-- @return string or nil: Output file content
function pandoc.convert(ifile, oformat, ofile, iformat)
iformat = (iformat == nil and pandoc.getext(ifile) or iformat)
local doc = pandoc.write(pandoc.read(ifile:read_text(), iformat), oformat)
if ofile == nil then
return doc
else
local eol = (os:isunix() and "\n" or "\r\n")
io.open(ofile, "w"):write(doc, eol):close()
end
end
end

View File

@ -1,8 +1,5 @@
----------------------------------- LITERATE ---------------------------------- ----------------------------------- LITERATE ----------------------------------
-- Adds Lua custom extensions
require "extensions"
-- Variable for all literate stuff -- Variable for all literate stuff
local lit = {} local lit = {}
@ -46,7 +43,6 @@ function lit.checkmetatype(type, meta, key)
local err = "" local err = ""
if type ~= mtype then if type ~= mtype then
if type == "path" then if type == "path" then
-- TODO
if not(pandoc.path.directory(mval):isdir()) then if not(pandoc.path.directory(mval):isdir()) then
err = "Invalid path '" .. mval .. "' in key '" .. key .. "'" err = "Invalid path '" .. mval .. "' in key '" .. key .. "'"
end end

View File

@ -1,8 +1,5 @@
----------------------------------- NATURAL ----------------------------------- ----------------------------------- NATURAL -----------------------------------
-- Adds Lua custom extensions
require "extensions"
local nat = {} local nat = {}
function nat.get(str) function nat.get(str)