Skip to content

Commit

Permalink
20.0.2 fix(lua): Fix #18 and other lint errors:
Browse files Browse the repository at this point in the history
- `src/lua`: Fix #18 and other lint errors,
mainly by making functions local or adding them to an object.
  • Loading branch information
s6mike committed Feb 10, 2023
1 parent d1b0c06 commit 4327948
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
- Add note about linking/using templates (html and latex) with pandoc.
- Add references to argmap specs spreadsheet?

## argmap 20.0.2

- `src/lua`: Fix #18 and other lint errors, mainly by making functions local or adding them to an object.

## argmap 20.0.1

- `mapjs/package.json`: Update dev dependencies to latest version.
Expand Down
16 changes: 8 additions & 8 deletions src/lua/argmap2mup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ local function default_to_nil(var)
return var
end

function pipe_in_out(cmd, s)
local function pipe_in_out(cmd, s)
-- a function for piping through unix commands
local tmp = os.tmpname()
local tmpout = os.tmpname()
Expand All @@ -480,14 +480,14 @@ local function trim(s)
return (s:gsub("\n", ""))
end

function markdown_to_plain(s)
local function markdown_to_plain(s)
if Script_context == 'client' then
return s
end
return trim(pipe_in_out("pandoc --wrap=none -t plain -f markdown", s))
end

function parse_special(t, s)
local function parse_special(t, s)
-- a function for parsing notes, labels, and strengths
for i, v in pairs(t) do
if string.match(i, "^" .. s .. "$") then
Expand Down Expand Up @@ -541,7 +541,7 @@ function a2m.parse_claims(t)
["title"] = claim,
["id"] = gid,
["attr"] = default_to_nil(attr), -- default_to_nil(attr) stops empty attr breaking json output.
["ideas"] = parse_reasons(v)
["ideas"] = a2m.parse_reasons(v)
}
end
end
Expand All @@ -550,7 +550,7 @@ function a2m.parse_claims(t)
return default_to_nil(o)
end

function parse_reasons(t)
function a2m.parse_reasons(t)
local o = {}
local n = 0
for i, v in pairs(t) do
Expand Down Expand Up @@ -578,7 +578,7 @@ function parse_reasons(t)
return default_to_nil(o)
end

function help()
local function help()
return [[argmap2mup <options> <file>
-u, --upload : upload to Google Drive
-g ID, --gdrive_id ID : update file with ID on Google Drive
Expand All @@ -588,7 +588,7 @@ function help()
-h, --help]]
end

function parse_options(a)
local function parse_options(a)
local opts = {}
local flags, args = pl.app.parse_args(a, { g = true, gdrive_id = true, n = true, name = true, f = true, folder = true })

Expand Down Expand Up @@ -621,7 +621,7 @@ function parse_options(a)
return opts
end

function main()
local function main()
-- print(args) -- What is this line for? Debugging?
Logger:debug("arg: ")
Logger:debug(arg)
Expand Down
44 changes: 24 additions & 20 deletions src/lua/argmap2tikz.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ local pl = require 'pl.import_into' ()
-- uses lyaml to parse yaml
local lyaml = require "lyaml"

local a2t = {}

-- initialize a global counter for providing unique ids for each node
local gn = 0
-- initialize an indent so our output is human readable
Expand Down Expand Up @@ -69,8 +71,8 @@ local styles = [=[
implicit/.style={dashed}
]=]

function pipe_in_out(cmd, s)
-- a function for piping through unix commands
-- a function for piping through unix commands
local function pipe_in_out(cmd, s)
local tmp = os.tmpname()
local tmpout = os.tmpname()
local f = assert(io.open(tmp, 'w'))
Expand All @@ -88,13 +90,13 @@ local function trim(s)
return (s:gsub("\n", ""))
end

function markdown_to_latex(s)
local function markdown_to_latex(s)
return trim(pipe_in_out("pandoc --wrap=none -t latex -f markdown", s))
end

-- TODO: enable parsing of notes
function parse_special(t, s)
-- a function for parsing notes, labels, and strengths
-- a function for parsing notes, labels, and strengths
-- TODO: enable parsing of notes
local function parse_special(t, s)
for i, v in pairs(t) do
if string.match(i, "^" .. s .. "$") then
return v
Expand All @@ -103,8 +105,8 @@ function parse_special(t, s)
return nil
end

function parse_claims(t)
-- a function that parses claims
-- a function that parses claims
local function parse_claims(t)
local o = ""
indent = indent + 2
for i, v in pairs(t) do
Expand All @@ -131,15 +133,15 @@ function parse_claims(t)
-- construct the claim in tikz, e.g,: c1/"Eating meat is morally acceptable"[claim, implicit]
local claimline = string.rep(" ", indent) .. "c" .. gid .. "/\"" .. claim .. "\"[" .. attr .. "]"
-- parse the reasons for the claim, and add the result to output.
o = o .. parse_reasons(v, claimline)
o = o .. a2t.parse_reasons(v, claimline)
end
end
indent = indent - 2
return o
end

function parse_reasons(t, claimline)
-- function that parses reasons for a claim and returns the appropriate tikz
-- function that parses reasons for a claim and returns the appropriate tikz
function a2t.parse_reasons(t, claimline)
-- initialize output string
local o = ""
indent = indent + 2
Expand Down Expand Up @@ -200,8 +202,8 @@ function parse_reasons(t, claimline)
return o
end

function create_graph(t)
-- parse the map and generate the tikz \graph
-- parse the map and generate the tikz \graph
local function create_graph(t)
local o = string.rep(" ", indent) .. "\\graph [layered layout, " ..
"grow down, " ..
"level distance=5em, " ..
Expand All @@ -214,8 +216,8 @@ function create_graph(t)
return o
end

function create_tikzpicture(t)
-- generate the tikz \graph and wrap in a tikzpicture environment
-- generate the tikz \graph and wrap in a tikzpicture environment
local function create_tikzpicture(t)
-- TODO: is there more appropriate way to specify rounded corners other than calling pdfsetcornerarced?
indent = indent + 2
local o = "\\begin{tikzpicture}\n" ..
Expand All @@ -228,8 +230,8 @@ function create_tikzpicture(t)
return o
end

function create_texdocument(t)
-- generate a the tikzpicture and wrap it in a standalone tex document
-- generate a the tikzpicture and wrap it in a standalone tex document
local function create_texdocument(t)
local documentclass = [[\documentclass[tikz]{standalone}]]
return "\\documentclass[tikz]{standalone}\n" ..
includes .. "\n" ..
Expand All @@ -238,15 +240,15 @@ function create_texdocument(t)
"\\end{document}"
end

function help()
local function help()
return [[argmap2tikz <options> <file>
-s, --standalone: generate standalone tex file
-i, --includes: dump includes required for tex preamble
-t, --template: dump boilerplate for adding to preamble in a pandoc template
-h, --help: help]]
end

function parse_options(a)
local function parse_options(a)
local opts = {}
local flags, args = pl.app.parse_args(a)
opts["standalone"] = flags["s"] or flags["standalone"]
Expand All @@ -259,7 +261,7 @@ function parse_options(a)
return opts
end

function main()
local function main()
local opts = parse_options(arg)
if opts["help"] then
return help()
Expand Down Expand Up @@ -288,3 +290,5 @@ function main()
end

print(main())

return a2t
18 changes: 11 additions & 7 deletions src/lua/mup2argmap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ local lyaml = require 'lyaml'
local json = require 'rxi-json-lua'
local logging = require 'logging'

local m2a = {}

-- [LuaLogging: A simple API to use logging features in Lua](https://neopallium.github.io/lualogging/manual.html#introduction)
local logger = logging.new(function(self, level, message)
io.stderr:write(message)
Expand All @@ -24,7 +26,7 @@ end)
-- https://www.lua.org/manual/5.3/manual.html#6.10
logger:setLevel(logging.ERROR)

function dig_in(table, fields)
local function dig_in(table, fields)
local t = table
local result = true
for _, f in pairs(fields) do
Expand All @@ -42,7 +44,7 @@ function dig_in(table, fields)
end
end

function parse_claims(claims, strength, label)
local function parse_claims(claims, strength, label)
local argmap = {}
if strength then
argmap["strength"] = strength
Expand All @@ -59,7 +61,7 @@ function parse_claims(claims, strength, label)
local note = dig_in(items, { "attr", "note", "text" })
local reasons = {}
if items["ideas"] then
reasons = parse_reasons(items["ideas"], note)
reasons = m2a.parse_reasons(items["ideas"], note)
elseif note then
reasons = reasons["note"]
end
Expand All @@ -68,7 +70,7 @@ function parse_claims(claims, strength, label)
return argmap
end

function parse_reasons(reasons, note)
function m2a.parse_reasons(reasons, note)
local argmap = {}
argmap["note"] = note
for index, items in pairs(reasons) do
Expand All @@ -90,14 +92,14 @@ function parse_reasons(reasons, note)
return argmap
end

function help()
local function help()
return [[mup2argmap <options> <file>
-e, --embed: wrap in code block for embedding in pandoc markdown
-g, --gdrive_id: parse mup file with corresponding Google Drive ID
-h: show this help]]
end

function parse_options(a)
local function parse_options(a)
local opts = {}
local flags, args = pl.app.parse_args(a, { g = true, gdrive_id = true })
opts["help"] = flags["h"] or flags["help"]
Expand All @@ -112,7 +114,7 @@ function parse_options(a)
return opts
end

function main()
local function main()
local opts = parse_options(arg)
if opts["help"] then
return help()
Expand Down Expand Up @@ -150,3 +152,5 @@ end

-- TODO is print best practice for this purpose?
print(main())

return m2a

0 comments on commit 4327948

Please sign in to comment.