-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathmkref.bas
executable file
·412 lines (379 loc) · 11.4 KB
/
mkref.bas
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#
# Generate the input file using the following SQL query:
#
# 1. import site data into mysql
# mysql -u <username> -p -h localhost smallbasic < backup_xxx.sql
#
# 2. export from mysql
#
# select FDB.body_value, FDCB.comment_body_value, FDB.entity_id
# from d7_field_data_body as FDB
# left outer join d7_comment as C on C.nid = FDB.entity_id
# left outer join d7_field_data_comment_body as FDCB on FDCB.entity_id = C.cid
# where FDB.body_value like '%sbasic reference%'
# order by FDB.body_value, FDB.entity_id
#
# 3. Select the JSON export format from mysql-workbench
#
split trim(command), " ", args
tload args(0), in_str, 1
# cleanup the mysql json
in_str = translate(in_str, "\'", "'")
in_str = translate(in_str, "'entity_id' :", "\"entity_id\":")
in_str = translate(in_str, "'body_value' :", "\"body_value\":")
in_str = translate(in_str, "'comment_body_value' :", "\"comment_body_value\":")
in_map = array(in_str)
if (len(args) == 2 && args(1) == "txt") then
mk_text_reference(in_map)
elif (len(args) == 2 && args(1) == "bas") then
mk_bas(in_map)
elif (len(args) == 2 && args(1) == "jekyll") then
mk_jekyll(in_map)
elif (len(args) == 2 && args(1) == "markdown") then
mk_markdown(in_map)
elif (len(args) == 2 && args(1) == "test") then
mk_test(in_map)
else
mk_help(in_map)
fi
func get_field(row, key, escq)
local n1, n2, result
n1 = instr(row, key) + len(key)
n2 = instr(n1, row, "\\r") - n1
result = mid(row, n1, n2)
if (escq) then
result = translate(result, "\\\"", "\"\"")
else
result = translate(result, "\\\"", "\"")
endif
get_field = result
end
func fix_comments(comments, keyword)
comments = translate(comments, "p. ", "")
comments = translate(comments, "bq. ", "")
comments = translate(comments, "bc. ", "")
comments = translate(comments, "bc.. ", "")
comments = translate(comments, "__", "")
comments = translate(comments, "**", "")
comments = translate(comments, " ", " ")
comments = translate(comments, "\\\"", "\"")
comments = translate(comments, "<code>", "----[ " + keyword + " example. cut here ]--->" + chr(10))
comments = translate(comments, "</code>", chr(10) + "<---[ cut here ]----" + chr(10))
comments = translate(comments, "\\r\\n\\r\\n", chr(10))
comments = translate(comments, "\\r\\n", chr(10))
fix_comments = comments
end
func fix_comments_jekyll(comments, keyword)
comments = translate(comments, "<code>", "<pre>" + chr(10))
comments = translate(comments, "</code>", chr(10) + "</pre>" + chr(10))
comments = translate(comments, "p. ", "<p>")
comments = translate(comments, "bc. ", "<pre>")
comments = fix_comments(comments, keyword)
fix_comments_jekyll = comments
end
func fix_comments_pandoc(comments, keyword)
comments = translate(comments, "<code>", chr(10) + "~~~" + chr(10))
comments = translate(comments, "<?code>", chr(10) + "~~~" + chr(10))
comments = translate(comments, "</code>", chr(10) + "~~~" + chr(10))
comments = translate(comments, "<strong>", "**")
comments = translate(comments, "</strong", "**")
comments = translate(comments, "<cite>", "")
comments = translate(comments, "</cite", "")
comments = translate(comments, "<blockquote>", "> ")
comments = translate(comments, "</blockquote>", "")
comments = translate(comments, "</blockqoute>", "")
comments = translate(comments, "p. ", "")
comments = translate(comments, "bc. ", "> ")
comments = fix_comments(comments, keyword)
return comments
end
sub mk_bas(byref in_map)
local i, row, group, keyword
local in_map_len = len(in_map) - 1
for i = 0 to in_map_len
row = in_map(i).body_value
keyword = get_field(row, "keyword=", true)
group = get_field(row, "group=", true)
? group + " " + keyword
while (i + 1 < in_map_len && in_map(i).entity_id == in_map(i + 1).entity_id)
i++
wend
next i
end
sub mk_help(byref in_map)
local i, row, group, type, keyword, syntax, brief
local in_map_len = len(in_map) - 1
for i = 0 to in_map_len
row = in_map(i).body_value
group = get_field(row, "group=", true)
type = get_field(row, "type=", true)
keyword = get_field(row, "keyword=", true)
syntax = get_field(row, "syntax=", true)
brief = get_field(row, "brief=", true)
while (i + 1 < in_map_len && in_map(i).entity_id == in_map(i + 1).entity_id)
i++
wend
? group + "," + type + "," + keyword + "," + in_map(i).entity_id + ",\"" + syntax + "\",\"" + brief + "\""
next i
end
sub mk_text_reference(byref in_map)
local i, row, group, type, keyword, syntax, brief, comments, counter
local in_map_len = len(in_map) - 1
local end_block = "<!-- end heading block -->"
? "SmallBASIC Language reference"
?
? "Online:
? "http://smallbasic.sourceforge.net/?q=node/201"
?
? "Generated with:
? "https://github.com/smallbasic/SmallBASIC/blob/master/documentation/mkref.bas
?
? " _____ _ _ ____ _____ _____ _____
? " / ____| | | | _ \ /\ / ____|_ _/ ____|
? " | (___ _ __ ___ __ _| | | |_) | / \ | (___ | || |
? " \___ \| '_ ` _ \ / _` | | | _ < / /\ \ \___ \ | || |
? " ____) | | | | | | (_| | | | |_) / ____ \ ____) |_| || |____
? " |_____/|_| |_| |_|\__,_|_|_|____/_/ \_\_____/|_____\_____|
?
?
counter = 0
for i = 0 to in_map_len
row = in_map(i).body_value
group = get_field(row, "group=", false)
type = get_field(row, "type=", false)
keyword = get_field(row, "keyword=", false)
syntax = get_field(row, "syntax=", false)
brief = get_field(row, "brief=", false)
counter++
? counter + ". (" + group + ") " + keyword
?
? syntax
?
? brief
?
pos = instr(row, end_block) + len(end_block)
if (pos < len(row)) then
? fix_comments(mid(row, pos), keyword)
endif
comments = in_map(i).comment_body_value
if (comments != "NULL") then
? fix_comments(comments, keyword)
endif
while (i + 1 < in_map_len && in_map(i).entity_id == in_map(i + 1).entity_id)
i++
? fix_comments(in_map(i).comment_body_value, keyword)
wend
next i
end
'
' make post files for smallbasic.github.io
'
' local test: $ bundle exec jekyll serve
'
sub mk_jekyll(byref in_map)
local i, row, group, type, keyword, syntax, brief, comments, buffer, filename
local in_map_len = len(in_map) - 1
local end_block = "<!-- end heading block -->"
dim buffer
for i = 0 to in_map_len
erase buffer
row = in_map(i).body_value
group = get_field(row, "group=", false)
type = get_field(row, "type=", false)
keyword = get_field(row, "keyword=", false)
syntax = get_field(row, "syntax=", false)
brief = get_field(row, "brief=", false)
buffer << "---"
buffer << "permalink: /" + in_map(i).entity_id
buffer << "layout: post"
buffer << "title: \"" + keyword + "\""
buffer << "categories: " + lower(group)
buffer << "---"
buffer << group
buffer << ""
buffer << syntax
buffer << ""
buffer << brief
buffer << ""
pos = instr(row, end_block) + len(end_block)
if (pos < len(row)) then
buffer << fix_comments_jekyll(mid(row, pos), keyword)
endif
comments = in_map(i).comment_body_value
if (comments != "NULL") then
buffer << fix_comments_jekyll(comments, keyword)
endif
while (i + 1 < in_map_len && in_map(i).entity_id == in_map(i + 1).entity_id)
i++
buffer << fix_comments_jekyll(in_map(i).comment_body_value, keyword)
wend
filename = "2016-06-04-" + lower(group) + "-" + lower(keyword) + ".markdown"
tsave filename, buffer
next i
end
'
' must contain at least 3 | chars
'
func is_table(s)
local z
z = instr(0, s, "|")
if (z!=1) then return false
z = instr(z, s, "|")
if (z==0) then return false
z = instr(z, s, "|")
if (z==0) then return false
return true
end
'
' generate the table pattern
'
func table_markdown(s)
local result = ""
local s_len = len(s)
local i
for i = 1 to s_len
if (mid(s, i, 1) == "|") then
result += " "
else
result += "-"
endif
next i
return result
end
'
' convert textile to markdown
'
func table_row(table_md, s)
local out = trim(leftoflast(rightof(s, "|"), "|"))
local j = instr(table_md, " ")
local x = instr(out, "|")
local n = max(1, j - x)
return translate(out, "|", space(n))
end
'
' convert textile to markdown
'
func update_tables(byref in_str)
local in_table = false
local table_md = ""
local out = ""
local row, rows
split in_str, chr(10), rows
for row in rows
if (is_table(row)) then
if (!in_table) then
table_md = ltrim(table_markdown(row))
in_table = true
out += table_md
out += chr(10)
endif
out += table_row(table_md, row)
else
if (in_table) then
out += table_md
out += chr(10)
in_table = false
endif
out += row
endif
out += chr(10)
next row
return out
end
'
' make post files for smallbasic.github.io
'
sub mk_markdown(byref in_map)
local i, row, group, type, keyword, syntax, brief, comments, buffer, filename
local in_map_len = len(in_map) - 1
local end_block = "<!-- end heading block -->"
sub append_buf(s)
buffer += s
buffer += chr(10)
end
for i = 0 to in_map_len
buffer=""
row = in_map(i).body_value
group = get_field(row, "group=", false)
type = get_field(row, "type=", false)
keyword = get_field(row, "keyword=", false)
syntax = get_field(row, "syntax=", false)
brief = get_field(row, "brief=", false)
append_buf("# " + keyword)
append_buf("")
append_buf("> " + syntax)
append_buf("")
append_buf(brief)
append_buf("")
pos = instr(row, end_block) + len(end_block)
if (pos < len(row)) then
append_buf(fix_comments_pandoc(mid(row, pos), keyword))
endif
comments = in_map(i).comment_body_value
if (comments != "NULL") then
append_buf(fix_comments_pandoc(comments, keyword))
endif
while (i + 1 < in_map_len && in_map(i).entity_id == in_map(i + 1).entity_id)
i++
append_buf(fix_comments_pandoc(in_map(i).comment_body_value, keyword))
wend
filename = in_map(i).entity_id + "-" + lower(group) + "-" + lower(keyword) + ".markdown"
filename = translate(filename, " ", "")
buffer = update_tables(buffer)
if (len(filename) > 200) then
filename = in_map(i).entity_id + ".markdown"
endif
tsave filename, buffer
next i
end
func cmpFunc(l, r)
local f1 = lower(l)
local f2 = lower(r)
cmpFunc = IFF(f1 == f2, 0, IFF(f1 > f2, 1, -1))
end
func fname(s)
local result
result = trim(leftof(s, " "))
if (len(result) == 0) then
result = s
endif
fname = result
end
'
' make a test program from the syntax field
'
sub mk_test(byref in_map)
local i, row, type, prev, syntax, el
local in_len = len(in_map) - 1
dim cmds, funcs
for i = 0 to in_len
row = in_map(i).body_value
type = get_field(row, "type=", false)
syntax = trim(get_field(row, "syntax=", false))
if (type == "command") then
cmds << syntax
else if (type == "function") then
funcs << syntax
endif
next i
sort cmds use cmpFunc(x,y)
in_len = len(cmds) - 1
prev = ""
for i = 0 to in_len
row = cmds(i)
if (row != prev) then
print "print \"" + fname(row) + ":\" ':" + row
endif
prev = row
next i
sort funcs use cmpFunc(x,y)
in_len = len(funcs) - 1
prev = ""
for i = 0 to in_len
row = funcs(i)
if (row != prev) then
print "print \"" + fname(row) + ":\" + " + row
endif
prev = row
next i
end