From e353f5125f76dc6e5e4bdcdec157b9fab1068997 Mon Sep 17 00:00:00 2001 From: perro Date: Thu, 23 Mar 2023 18:53:36 -0700 Subject: [PATCH] Finished scripts refactoring --- dist/lin.lua | 72 +++++++++++++++++++++++++++++++++++++++--- scripts/make_dist.lua | 41 +++++++++++++----------- scripts/make_jsons.lua | 24 ++++++++++++++ scripts/make_jsons.sh | 11 ------- scripts/test.lua | 27 ++++++++-------- src/extensions.lua | 71 +++++++++++++++++++++++++++++++++++++++-- src/literate.lua | 4 --- src/natural.lua | 3 -- 8 files changed, 195 insertions(+), 58 deletions(-) create mode 100644 scripts/make_jsons.lua delete mode 100644 scripts/make_jsons.sh diff --git a/dist/lin.lua b/dist/lin.lua index e5311ad..0de774e 100644 --- a/dist/lin.lua +++ b/dist/lin.lua @@ -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 diff --git a/scripts/make_dist.lua b/scripts/make_dist.lua index 2d7fed0..4e4b453 100644 --- a/scripts/make_dist.lua +++ b/scripts/make_dist.lua @@ -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") diff --git a/scripts/make_jsons.lua b/scripts/make_jsons.lua new file mode 100644 index 0000000..e2ce0d0 --- /dev/null +++ b/scripts/make_jsons.lua @@ -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 diff --git a/scripts/make_jsons.sh b/scripts/make_jsons.sh deleted file mode 100644 index 861b4b9..0000000 --- a/scripts/make_jsons.sh +++ /dev/null @@ -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 diff --git a/scripts/test.lua b/scripts/test.lua index 2f975cc..012be66 100644 --- a/scripts/test.lua +++ b/scripts/test.lua @@ -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) diff --git a/src/extensions.lua b/src/extensions.lua index 08fbee9..88ae7b1 100644 --- a/src/extensions.lua +++ b/src/extensions.lua @@ -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 diff --git a/src/literate.lua b/src/literate.lua index c3bd8d4..a70cd94 100644 --- a/src/literate.lua +++ b/src/literate.lua @@ -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 diff --git a/src/natural.lua b/src/natural.lua index 57978a0..ace3905 100644 --- a/src/natural.lua +++ b/src/natural.lua @@ -1,8 +1,5 @@ ----------------------------------- NATURAL ----------------------------------- --- Adds Lua custom extensions -require "extensions" - local nat = {} function nat.get(str)