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
local fnl = mod
------------------------------------ MODS ------------------------------------
---------------------------------- EXTENSIONS ---------------------------------
-- Tries popen
-- @param ... string: Chunks for popen
@ -6274,12 +6274,15 @@ end
-- Gets OS short name
-- 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()
local status, output = io.try("uname")
if status then
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"
end
return "linux"
@ -6346,6 +6349,8 @@ end
function string:isdir()
if self:exists() then
return io.open(self, "a+") == nil
elseif self == "." or self == ".." then
return true
end
return false
end
@ -6357,6 +6362,66 @@ function string:read_text()
return io.open(self):read("*a")
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 -----------------------------------
local nat = {}
@ -6409,7 +6474,6 @@ function lit.checkmetatype(type, meta, key)
local err = ""
if type ~= mtype then
if type == "path" then
-- TODO
if not(pandoc.path.directory(mval):isdir()) then
err = "Invalid path '" .. mval .. "' in key '" .. key .. "'"
end

View File

@ -1,10 +1,29 @@
-- Adds Lua custom extensions
require "../src/extensions"
-- Makes bundle distribution
-- 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
local name = "lin.lua"
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([[
Computable Pandoc & Fennel Bundle:
A Pandoc filter for literate and natural programming
@ -19,22 +38,6 @@ Fennel:
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
file = io.open(dist, "w")
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
require "../src/extensions"
require "../scripts/make_dist"
require "src.extensions"
require "scripts.make_dist"
-- Variables
local filter = "-L dist/lin.lua"
local filter = "dist/lin.lua"
local verbose = ""
local trace = ""
local files = {}
local cmds = {
["unix"] = {
["pandoc"] = "pandoc ",
["clear"] = "clear ",
["ls"] = "ls ",
["pandoc"] = "pandoc",
["clear"] = "clear",
["ls"] = "ls",
},
["win"] = {
["pandoc"] = "pandoc.exe ",
["clear"] = "cls ",
["ls"] = "dir ",
["pandoc"] = "pandoc.exe",
["clear"] = "cls",
["ls"] = "dir",
},
}
@ -81,16 +83,13 @@ end
-- Default files
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
table.insert(files, pandoc.path.join({"tests", file}))
end
end
end
-- Makes distribution bundle
make_dist()
-- Clears terminal
os.execute(getcmd("clear"))
print "🐾 Starting tests"
@ -98,7 +97,7 @@ print "🐾 Starting tests"
-- Does tests
for _, file in ipairs(files) do
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(" Expect: " .. expectation)
print(" Result: " .. result)

View File

@ -1,4 +1,4 @@
------------------------------------ MODS ------------------------------------
---------------------------------- EXTENSIONS ---------------------------------
-- Tries popen
-- @param ... string: Chunks for popen
@ -13,12 +13,15 @@ end
-- Gets OS short name
-- 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()
local status, output = io.try("uname")
if status then
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"
end
return "linux"
@ -85,6 +88,8 @@ end
function string:isdir()
if self:exists() then
return io.open(self, "a+") == nil
elseif self == "." or self == ".." then
return true
end
return false
end
@ -96,3 +101,63 @@ function string:read_text()
return io.open(self):read("*a")
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 ----------------------------------
-- Adds Lua custom extensions
require "extensions"
-- Variable for all literate stuff
local lit = {}
@ -46,7 +43,6 @@ function lit.checkmetatype(type, meta, key)
local err = ""
if type ~= mtype then
if type == "path" then
-- TODO
if not(pandoc.path.directory(mval):isdir()) then
err = "Invalid path '" .. mval .. "' in key '" .. key .. "'"
end

View File

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