-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhmlgen.lua
98 lines (82 loc) · 2.29 KB
/
hmlgen.lua
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
local seq = require'seq'
local strf = string.format
local hmlbuilder = require'luahml_builder'
local luahml = require'luahml'
local function append_chars(hml, para, line, missing_styles)
local function cook_(str)
str = str:gsub('&', '&')
str = str:gsub('&#(x?[0-9a-fA-F]+);', '&#%1;')
str = str:gsub('<', '<')
str = str:gsub('\t', '<TAB/>')
return str
end
local stack = {''}
repeat
local i = line:find('[<!]')
if i == nil then
para = hml:append_span(para, cook_(line), prev_style)
return para, missing_styles
end
local j, k, m = line:find('^<!!(.-) +', i)
if j then
if j > 1 then
para = hml:append_span(para, cook_(line:sub(1, j-1)), stack[#stack])
end
line = line:sub(k+1)
if hml.doc.char_styles[m] == nil then
missing_styles[m] = true
m = ''
end
stack[#stack+1] = m
else
j, k = line:find('^!!>', i)
if j then
para = hml:append_span(para, cook_(line:sub(1, j-1)), stack[#stack])
table.remove(stack)
if #stack == 0 then stack[1] = '' end
line = line:sub(k+1)
else
para = hml:append_span(para, cook_(line:sub(1, i)), stack[#stack])
line = line:sub(i+1)
end
end
until #line == 0
return para, missing_styles
end
local function convert(infile, outfile, tplfile)
local doc_, err = luahml.load(tplfile)
--print(doc_, err); os.exit(-1)
local hml = hmlbuilder.make_buildable(doc_)
local lines = seq.new_from_file(infile)
local missing_styles = {para = {}, char = {} }
for line in lines:iter() do
line = line:gsub('^ *', '')
local para_style = 'Normal'
local i, j, m = line:find('^](.-) ')
if i then
para_style = m
line = line:sub(j+1)
end
if hml.doc.para_styles[para_style] == nil then
line = '(!!missing style: ' .. para_style .. ')' .. line
missing_styles.para[para_style] = true
para_style = 'Normal'
end
local para = hml:make_para(para_style)
para = append_chars(hml, para, line, missing_styles.char)
hml:append_para(para)
end
hml:save(outfile)
return missing_styles
end
local infile = arg[1] or 'test.txt'
local outfile = arg[2] or 'test.hml'
local tplfile = arg[3] or 'tpl.hml'
local missing_styles = convert(infile, outfile, tplfile)
print('missing styles:')
for k, v in pairs(missing_styles.para) do
print(k)
end
for k, v in pairs(missing_styles.char) do
print(k)
end