-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmup2argmap
executable file
·129 lines (121 loc) · 3.51 KB
/
mup2argmap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env lua
-- A pipe that reads mindmup `mup` files and outputs `yaml` argument maps.
local pl = require'pl.import_into'()
local lyaml = require "lyaml"
local json = require 'json'
function dig_in(table,fields)
local t = table
local result = true
for _,f in pairs(fields) do
if t[f] then
t = t[f]
else
result = false
break
end
end
if result then
return t
else
return nil
end
end
function parse_claims(claims,strength,label)
local argmap = {}
if strength then
argmap["strength"] = strength
end
if label then
argmap["label"] = label
end
for _,items in pairs(claims) do
local key = items["title"]
local styles = dig_in(items,{"attr","styleNames"})
if styles and styles[1] == "attr_implicit_claim" then
key = "-" .. key
end
local note = dig_in(items,{"attr","note","text"})
local reasons = {}
if items["ideas"] then
reasons = parse_reasons(items["ideas"],note)
elseif note then
reasons = reasons["note"]
end
argmap[key] = reasons
end
return argmap
end
function parse_reasons(reasons,note)
local argmap = {}
argmap["note"] = note
for index,items in pairs(reasons) do
local key = ""
local type = items["attr"]["group"]
if type == "opposing" then
key = "o" .. index
else
key = "r" .. index
end
local strength = dig_in(items, {"attr","parentConnector","width"})
local label = dig_in(items, {"attr","parentConnector","label"})
local claims = parse_claims(items["ideas"],strength,label)
argmap[key] = claims
end
return argmap
end
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 opts = {}
local flags,args = pl.app.parse_args(a,{g=true,gdrive_id=true})
opts["help"] = flags["h"] or flags["help"]
opts["wrap"] = flags["e"] or flags["embed"]
opts["gdrive_id"] = flags["g"]
if not opts["gdrive_id"] then
opts["gdrive_id"] = flags["gdrive_id"]
end
if #args > 0 then
opts["file"] = args[1]
end
return opts
end
function main()
local opts = parse_options(args)
if opts["help"] then
return help()
else
local input = ""
if opts["gdrive_id"] then
local f = assert(io.popen("gdrive download --stdout --no-progress " .. " " .. opts["gdrive_id"]))
input = f:read("*all")
f:close()
elseif opts["file"] then
local f = io.open(opts["file"],'r')
input = f:read("*all")
f:close()
else
input = io.read("*all")
end
local mup = json.decode(input)
local argmap = parse_claims(mup["ideas"],nil,nil)
local name = mup["title"]
local yml = lyaml.dump({argmap})
if opts["wrap"] then
local attr = ""
if name then
attr = " name=\"" .. name .. "\""
end
if opts["gdrive_id"] then
attr = attr .. " gid=\"" .. opts["gdrive_id"] .. "\""
end
return "```{.argmap " .. attr .. "}\n" .. yml .. "```\n"
else
return yml
end
end
end
print(main())