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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
local lfs = require("lfs")
require("ext")
local ret = {}
ret.options = {
paths = {
type = "folder",
multiple = true,
short = "-p",
long = "--path",
consumes = 1,
},
output = {
type = "folder",
short = "-o",
long = "--output",
consumes = 1,
default = ".",
},
title = {
type = "string",
short = "-t",
long = "--tile",
consumes = 1,
default = "Mdoc Generated Page",
},
index = {
type = "file",
short = "-i",
long = "--index",
consumes = 1,
},
document_paths = {
type = "folder",
multiple = true,
short = "-d",
long = "--document",
consumes = 1,
},
parser = {
type = "executable",
short = "-m",
long = "--markup-parser",
consumes = 1,
default = "markdown",
},
--append = {
--type = "file",
--short = "-a",
--long = "--append",
--consumes = 1,
--multiple = true,
--},
verbose = {
type = "flag",
short = "-v",
long = "--verbose"
},
help = {
type = "flag",
short = "-h",
long = "--help",
}
}
local function check_file_type(path, type)
local attr, err = lfs.attributes(path)
if attr == nil then
return attr, err
end
if attr.mode == type then
return path
else
return false, string.format("%s was not a folder, it was a %s",path, attr.mode)
end
end
ret.check_flag = function(...)
return true
end
ret.check_folder = function(path)
if path:match("/$") then
path = path:sub(1,-2)
end
return check_file_type(path,"directory")
end
ret.check_string = function(str)
if type(str) == "string" then
return str
else
return false, string.format("%s was not a string, it was a %s",tostring(str), type(str))
end
end
ret.check_file = function(path)
return check_file_type(path,"file")
end
ret.check_executable = function(name)
local tmpname = "./" .. os.tmpname()
local pd = assert(io.popen(name .. " > " .. tmpname, "w"))
pd:write("Hello, world!")
pd:close()
local id = assert(io.open(tmpname,"r"))
local dat = id:read("*a")
id:close()
assert(os.remove(tmpname))
if string.len(dat) > 0 then
return name
else
return false, string.format("Tried to execute %q, but it did not create any data with the input 'Hello, world!', are you sure it's an executale on your PATH?", name)
end
end
local option_lookup = {}
for k,v in pairs(ret.options) do
if v.short then
option_lookup[v.short] = k
end
if v.long then
option_lookup[v.long] = k
end
end
ret.parse_options = function(args)
print("parsing args:",args)
local parsed = {}
local i = 1
while i <= #args do
local option_name = option_lookup[args[i]]
print("found option:",option_name)
if not option_name then
errorf("Unknown option #%d: %q",i, args[i])
end
local option = ret.options[option_name]
assertf(#args > i + (option.consumes or 0) - 1, "Option #%d (%q) consumes %d arguments, but found end of arguments",i,args[i],option.consumes)
local args_for_option = {}
for j = i, i+(option.consumes or 0) do
table.insert(args_for_option,args[j+1])
end
--special, if we only consume 1 option, just pass that.
if option.consumes == 1 then
args_for_option = args_for_option[1]
end
print("checking",option.type)
print("with",args_for_option)
local check = assert(ret["check_" .. option.type](args_for_option))
if option.multiple then
parsed[option_name] = parsed[option_name] or {}
table.insert(parsed[option_name],check)
else
parsed[option_name] = check
end
i = i + 1 + (option.consumes or 0)
end
--Set defaults for things that don't have them yet
for option_name, option in pairs(ret.options) do
if parsed[option_name] == nil then
if option.multiple and not option.default then
parsed[option_name] = {}
else
parsed[option_name] = option.default
end
end
end
return parsed
end
ret.help = function()
print([=[
mdoc - lua documentation
mdoc -p <folder> [-p <folder> ...][ -o <folder>][ -t "title"][ -i <file>][ -d <folder>[ -d <folder> ...]][ -m <executable>][ -h]
-p | --path <folder> : Path to search for source files
-o | --output <folder> = "." : Folder to output HTML files to (and a cache folder)
-t | --title "name" = "Mdoc Generated Page" : Title for the html files
-i | --index <file> : File to use for the index file
-d | --document <folder> : Path to search for files to put inder the References section
-m | --markup-parser <executable> : Executable to use to parse the descriptions and refrence documents.
Executable should accept a file path as it's argument, and generate html as it's output.
-h | --help : print this help
]=])
end
return ret
|