forked from driusan/lmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacro.go
533 lines (431 loc) · 14 KB
/
macro.go
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//// <<< macro.go >>>
//line addons/008_MacroNames.md:106
// Code generated with lmt DO NOT EDIT.
//go:generate sh -c "go run main.go -m -o $GOFILE README.md addons/*.md && go fmt $GOFILE && echo please use main.go to produce a binary."
// This file is full of line directives, they are very useful when compiling and/or in user reports.
// If you are unconfortable with them, please look in lmt.go in the same directory.
//// <<< "main code" >>>
//line addons/006_GoGenerate.md:55
package main
import (
//// <<< "main.go imports" >>>
//line README.md:149
"fmt"
"io"
"os"
//line README.md:212
"bufio"
//line README.md:385
"regexp"
//line README.md:510
"strings"
//line addons/002_SubdirectoryFiles.md:35
"path/filepath"
//line addons/005_Flags.md:11
"flag"
//line addons/007_Extract.md:137
"errors"
"sort"
//// <<< "main code" >>>
//line addons/006_GoGenerate.md:59
)
//// <<< "global block variables" >>>
//line addons/003_LineNumbers.md:25
type File string
type CodeBlock []CodeLine
type BlockName string
type language string
//// <<< "Codeline type definition" >>>
//line addons/008_MacroNames.md:80
type CodeLine struct {
text string
file File
lang language
number int
macro BlockName
}
//// <<< "global block variables" >>>
//line addons/003_LineNumbers.md:30
var blocks map[BlockName]CodeBlock
var files map[File]CodeBlock
//line addons/004_MarkupExpansion.md:91
type codefence struct {
char string // This should probably be a rune for purity
count int
}
//line addons/005_Flags.md:19
var flags struct {
//// <<< "flags for cli" >>>
//line addons/005_Flags.md:29
outfile string
publishable bool
//line addons/007_Extract.md:19
concatenate string
extract string
listblocks bool
listfiles bool
//line addons/008_MacroNames.md:36
macro bool
//// <<< "global block variables" >>>
//line addons/005_Flags.md:21
}
//// <<< "global variables" >>>
//line README.md:402
var namedBlockRe *regexp.Regexp
//line README.md:432
var fileBlockRe *regexp.Regexp
//line README.md:516
var replaceRe *regexp.Regexp
//// <<< "main code" >>>
//line addons/006_GoGenerate.md:62
//// <<< "ProcessFile Declaration" >>>
//line addons/003_LineNumbers.md:118
// Updates the blocks and files map for the markdown read from r.
func ProcessFile(r io.Reader, inputfilename string) error {
//// <<< "process file implementation variables" >>>
//line addons/003_LineNumbers.md:82
scanner := bufio.NewReader(r)
var err error
var line CodeLine
line.file = File(inputfilename)
var inBlock, appending bool
var bname BlockName
var fname File
var block CodeBlock
//line addons/004_MarkupExpansion.md:193
var fence codefence
//// <<< "process file implementation" >>>
//line addons/003_LineNumbers.md:99
for {
line.number++
line.text, err = scanner.ReadString('\n')
switch err {
case io.EOF:
return nil
case nil:
// Nothing special
default:
return err
}
//// <<< "Handle file line" >>>
//line addons/004_MarkupExpansion.md:210
if !inBlock {
//// <<< "Check block start" >>>
//line addons/004_MarkupExpansion.md:225
if len(line.text) >= 3 && (line.text[0:3] == "```" || line.text[0:3] == "~~~") {
inBlock = true
// We were outside of a block and now we are in one,
// so just blindly reset the block variable.
block = make(CodeBlock, 0)
//// <<< "Check block header" >>>
//line addons/008_MacroNames.md:94
fname, bname, appending, line.lang, fence = parseHeader(line.text)
if fname != "" {
line.macro = BlockName(fname)
}
if bname != "" {
line.macro = BlockName(fmt.Sprintf(`"%v"`, bname))
}
//// <<< "Check block start" >>>
//line addons/004_MarkupExpansion.md:231
}
//// <<< "Handle file line" >>>
//line addons/004_MarkupExpansion.md:212
continue
}
if l := strings.TrimSpace(line.text); len(l) >= fence.count && strings.Replace(l, fence.char, "", -1) == "" {
//// <<< "Handle block ending" >>>
//line addons/003_LineNumbers.md:56
inBlock = false
// Update the files map if it's a file.
if fname != "" {
if appending {
files[fname] = append(files[fname], block...)
} else {
files[fname] = block
}
}
// Update the named block map if it's a named block.
if bname != "" {
if appending {
blocks[bname] = append(blocks[bname], block...)
} else {
blocks[bname] = block
}
}
//// <<< "Handle file line" >>>
//line addons/004_MarkupExpansion.md:216
continue
}
//// <<< "Handle block line" >>>
//line addons/003_LineNumbers.md:48
block = append(block, line)
//// <<< "process file implementation" >>>
//line addons/003_LineNumbers.md:111
}
//// <<< "ProcessFile Declaration" >>>
//line addons/003_LineNumbers.md:121
}
//// <<< "ParseHeader Declaration" >>>
//line addons/004_MarkupExpansion.md:129
func parseHeader(line string) (File, BlockName, bool, language, codefence) {
line = strings.TrimSpace(line) // remove indentation and trailing spaces
// lets iterate over the regexps we have.
for _, re := range []*regexp.Regexp{namedBlockRe, fileBlockRe} {
if m := namedMatchesfromRe(re, line); m != nil {
var fence codefence
fence.char = m["fence"][0:1]
fence.count = len(m["fence"])
return File(m["file"]), BlockName(m["name"]), (m["append"] == "+="), language(m["language"]), fence
}
}
// An empty return value for unnamed or broken fences to codeblocks.
return "", "", false, "", codefence{}
}
//// <<< "Replace Declaration" >>>
//line addons/001_WhitespacePreservation.md:34
// Replace expands all macros in a CodeBlock and returns a CodeBlock with no
// references to macros.
func (c CodeBlock) Replace(prefix string) (ret CodeBlock) {
//// <<< "Replace codeblock implementation" >>>
//line addons/003_LineNumbers.md:251
var line string
for _, v := range c {
line = v.text
//// <<< "Handle replace line" >>>
//line addons/003_LineNumbers.md:234
matches := replaceRe.FindStringSubmatch(line)
if matches == nil {
if v.text != "\n" {
v.text = prefix + v.text
}
ret = append(ret, v)
continue
}
//// <<< "Lookup replacement and add to ret" >>>
//line addons/003_LineNumbers.md:220
bname := BlockName(matches[2])
if val, ok := blocks[bname]; ok {
ret = append(ret, val.Replace(prefix+matches[1])...)
} else {
fmt.Fprintf(os.Stderr, "Warning: Block named %s referenced but not defined.\n", bname)
ret = append(ret, v)
}
//// <<< "Replace codeblock implementation" >>>
//line addons/003_LineNumbers.md:255
}
return
//// <<< "Replace Declaration" >>>
//line addons/001_WhitespacePreservation.md:38
}
//// <<< "Finalize Declaration" >>>
//line addons/008_MacroNames.md:12
// Finalize extract the textual lines from CodeBlocks and (if needed) prepend a
// notice about "unexpected" filename or line changes, which is extracted from
// the contained CodeLines. The result is a string with newlines ready to be
// pasted into a file.
func (block CodeBlock) Finalize() (ret string) {
var prev CodeLine
var lineformatstring string
var macroformatstring string
for _, current := range block {
if !flags.publishable && (prev.number+1 != current.number || prev.file != current.file) {
//// <<< "Finalize format" >>>
//line addons/008_MacroNames.md:48
switch current.lang {
//// <<< "Finalize format languages" >>>
//line addons/008_MacroNames.md:62
case "bash", "shell", "sh", "zsh", "python", "perl":
macroformatstring = "# <<< %v >>>\n"
lineformatstring = "\n#line %v \"%v\"\n"
case "go", "golang":
macroformatstring = "//// <<< %v >>>\n"
lineformatstring = "\n//line %[2]v:%[1]v\n"
case "CPP", "cpp", "Cpp":
macroformatstring = "// <<< %v >>>\n"
lineformatstring = "\n#line %v \"%v\"\n"
case "C", "c":
// No surefire way to make line comments in c, we might be in a comment block already.
lineformatstring = "\n#line %v \"%v\"\n"
//// <<< "Finalize format" >>>
//line addons/008_MacroNames.md:50
}
if flags.macro && macroformatstring != "" && prev.macro != current.macro {
ret += fmt.Sprintf(macroformatstring, current.macro)
}
if lineformatstring != "" {
ret += fmt.Sprintf(lineformatstring, current.number, current.file)
}
//// <<< "Finalize Declaration" >>>
//line addons/008_MacroNames.md:25
}
ret += current.text
prev = current
}
return
}
//// <<< "Extract named matches from regexps" >>>
//line addons/004_MarkupExpansion.md:155
// namedMatchesfromRe takes an regexp and a string to match and returns a map
// of named groups to the matches. If not matches are found it returns nil.
func namedMatchesfromRe(re *regexp.Regexp, toMatch string) (ret map[string]string) {
substrings := re.FindStringSubmatch(toMatch)
if substrings == nil {
return nil
}
ret = make(map[string]string)
names := re.SubexpNames()
for i, s := range substrings {
ret[names[i]] = s
}
// The names[0] and names[x] from unnamed regex grous are an empty string.
// Instead of checking every names[x] we simply overwrite the previous
// ret[""] and discard it at the end.
delete(ret, "")
return
}
//// <<< "Extract a codeblock by a name" >>>
//line addons/007_Extract.md:84
// getBlockByName takes a string as a name and use it as a key in files and
// blocks and return the first codeblock it could find. If no codeblocks are
// found by that name getBlockByName returns an error.
func getBlockByName(bn string) (CodeBlock, error) {
// TODO: Why not make files a simple list and store all codeblocks in blocks?
if _, filesiscb := files[File(bn)]; filesiscb {
return files[File(bn)], nil
}
if _, blockiscb := blocks[BlockName(bn)]; blockiscb {
return blocks[BlockName(bn)], nil
}
return nil, errors.New("No CodeBlock by that name")
}
//// <<< "main code" >>>
//line addons/006_GoGenerate.md:64
func main() {
//// <<< "main implementation" >>>
//line addons/007_Extract.md:31
//// <<< "Initialize" >>>
//line README.md:157
// Initialize the maps
blocks = make(map[BlockName]CodeBlock)
files = make(map[File]CodeBlock)
//// <<< "Namedblock Regex" >>>
//line addons/004_MarkupExpansion.md:104
namedBlockRe = regexp.MustCompile("^(?P<fence>`{3,}|~{3,})\\s?(?P<language>\\w*)\\s*\"(?P<name>.+)\"\\s*(?P<append>[+][=])?$")
//// <<< "Fileblock Regex" >>>
//line addons/004_MarkupExpansion.md:113
fileBlockRe = regexp.MustCompile("^(?P<fence>`{3,}|~{3,})\\s?(?P<language>\\w+)\\s+(?P<file>[\\w\\.\\-\\/]+)\\s*(?P<append>[+][=])?$")
//// <<< "Replace Regex" >>>
//line addons/004_MarkupExpansion.md:83
replaceRe = regexp.MustCompile(`^(?P<prefix>\s*)(?:<<|//)<(?P<name>.+)>>>\s*$`)
//// <<< "Initialize" >>>
//line addons/005_Flags.md:38
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] files...\n", os.Args[0])
flag.PrintDefaults()
}
flag.StringVar(&flags.outfile, "o", "", "output a specific file instead of all files.")
flag.BoolVar(&flags.publishable, "p", false, "publishable output, without line directives.")
//line addons/007_Extract.md:10
flag.StringVar(&flags.concatenate, "c", "", "Concatenate a codeblock and print to standard out.")
flag.StringVar(&flags.extract, "e", "", "Extract, expand a codeblock and print to standard out.")
flag.BoolVar(&flags.listblocks, "l", false, "List all codeblocks.")
flag.BoolVar(&flags.listfiles, "f", false, "List all output files.")
//line addons/008_MacroNames.md:39
flag.BoolVar(&flags.macro, "m", false, "macro names added in comments")
//// <<< "main implementation" >>>
//line addons/007_Extract.md:33
flag.Parse()
for _, file := range flag.Args() {
//// <<< "Open and process file" >>>
//line addons/003_LineNumbers.md:127
f, err := os.Open(file)
if err != nil {
fmt.Fprintln(os.Stderr, "error: ", err)
continue
}
if err := ProcessFile(f, file); err != nil {
fmt.Fprintln(os.Stderr, "error: ", err)
}
// Don't defer since we're in a loop, we don't want to wait until the function
// exits.
f.Close()
//// <<< "main implementation" >>>
//line addons/007_Extract.md:37
}
//// <<< "Override filelist" >>>
//line addons/005_Flags.md:73
if flags.outfile != "" {
f := make(map[File]CodeBlock)
if files[File(flags.outfile)] != nil {
f[File(flags.outfile)] = files[File(flags.outfile)]
} else {
fmt.Fprintf(os.Stderr, "Warning: File named \"%s\" requested but not defined.\n", flags.outfile)
}
files = f
}
//// <<< "main implementation" >>>
//line addons/007_Extract.md:39
switch {
//// <<< "Implement flags to list files" >>>
//line addons/007_Extract.md:124
case flags.listfiles:
fn := make([]string, 0, len(files))
for n := range files {
fn = append(fn, string(n))
}
sort.Strings(fn)
fmt.Println(strings.Join(fn, "\n"))
//// <<< "Implement flags to list codeblocks" >>>
//line addons/007_Extract.md:114
case flags.listblocks:
bn := make([]string, 0, len(blocks))
for n := range blocks {
bn = append(bn, string(n))
}
sort.Strings(bn)
fmt.Println(strings.Join(bn, "\n"))
//// <<< "Check flags to print content to standard out" >>>
//line addons/007_Extract.md:61
case flags.concatenate != "", flags.extract != "":
for i, v := range map[rune]string{'c': flags.concatenate, 'e': flags.extract} {
if v != "" {
cb, err := getBlockByName(v)
if err != nil {
fmt.Fprintf(os.Stderr, "Warning: Block named \"%s\" requested but not defined.\n", v)
return
}
switch i {
case 'c':
fmt.Fprintf(os.Stdout, "%s", cb.Finalize())
case 'e':
fmt.Fprintf(os.Stdout, "%s", cb.Replace("").Finalize())
}
}
}
//// <<< "main implementation" >>>
//line addons/007_Extract.md:41
default:
//// <<< "Output files" >>>
//line addons/003_LineNumbers.md:318
for filename, codeblock := range files {
if dir := filepath.Dir(string(filename)); dir != "." {
if err := os.MkdirAll(dir, 0775); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
f, err := os.Create(string(filename))
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
continue
}
fmt.Fprintf(f, "%s", codeblock.Replace("").Finalize())
// We don't defer this so that it'll get closed before the loop finishes.
f.Close()
}
//// <<< "main implementation" >>>
//line addons/007_Extract.md:43
}
//// <<< "main code" >>>
//line addons/006_GoGenerate.md:67
}