-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.go
103 lines (94 loc) · 2.28 KB
/
README.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
//line README.md:27
package main
import (
"bytes"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"github.com/pgavlin/goldmark"
"github.com/pgavlin/goldmark/ast"
"github.com/pgavlin/goldmark/extension"
goldmark_parser "github.com/pgavlin/goldmark/parser"
"github.com/pgavlin/goldmark/text"
"github.com/pgavlin/goldmark/util"
)
//line README.md:64
type lineIndex []int
func (index lineIndex) lineNumber(offset int) int {
i := sort.Search(len(index), func(i int) bool {
return index[i] > offset
})
return i + 1
}
func indexLines(f []byte) lineIndex {
var index lineIndex
for offset, b := range f {
if b == '\n' {
index = append(index, offset)
}
}
return index
}
//line README.md:90
func convertFile(name string) error {
contents, err := os.ReadFile(name)
if err != nil {
return err
}
index := indexLines(contents)
//line README.md:102
parser := goldmark.DefaultParser()
parser.AddOptions(goldmark_parser.WithParagraphTransformers(
util.Prioritized(extension.NewTableParagraphTransformer(), 200),
))
document := parser.Parse(text.NewReader(contents))
//line README.md:112
var source bytes.Buffer
ast.Walk(document, func(n ast.Node, enter bool) (ast.WalkStatus, error) {
code, ok := n.(*ast.FencedCodeBlock)
if !ok || !enter || string(code.Language(contents)) != "go" {
return ast.WalkContinue, nil
}
lines := code.Lines()
if lines.Len() == 0 {
return ast.WalkContinue, nil
}
//line README.md:129
lineNumber := index.lineNumber(lines.At(0).Start)
fmt.Fprintf(&source, "//line %v:%v\n", name, lineNumber)
for i := 0; i < lines.Len(); i++ {
line := lines.At(i)
source.Write(line.Value(contents))
}
return ast.WalkContinue, nil
})
//line README.md:145
if source.Len() == 0 {
return nil
}
return os.WriteFile(name[:len(name)-3]+".go", source.Bytes(), 0600)
}
//line README.md:162
func main() {
wd, err := os.Getwd()
if err != nil {
log.Fatalf("could not read current directory: %v", err)
}
entries, err := os.ReadDir(wd)
if err != nil {
log.Fatalf("could not read current directory: %v", err)
}
//line README.md:178
for _, entry := range entries {
name := entry.Name()
ext := filepath.Ext(name)
if ext != ".md" {
continue
}
if err = convertFile(name); err != nil {
log.Fatalf("could not convert file '%v': %v", name, err)
}
}
}