-
-
Notifications
You must be signed in to change notification settings - Fork 142
/
org.go
65 lines (49 loc) · 1.53 KB
/
org.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
package lint
import (
"regexp"
"strings"
"github.com/niklasfasching/go-org/org"
"github.com/errata-ai/vale/v3/internal/core"
"github.com/errata-ai/vale/v3/internal/nlp"
)
var orgConverter = org.New()
var orgWriter = org.NewHTMLWriter()
var orgExample = "\n#+BEGIN_EXAMPLE\n$1\n#+END_EXAMPLE\n"
var reOrgAttribute = regexp.MustCompile(`(#(?:\+| )[^\s]+:.+)`)
var reOrgProps = regexp.MustCompile(`(:PROPERTIES:\n.+\n:END:)`)
var reOrgSrc = regexp.MustCompile(`(?i)#\+BEGIN_SRC .+`)
type ExtendedHTMLWriter struct {
*org.HTMLWriter
}
func (w *ExtendedHTMLWriter) WriteComment(n org.Comment) {
w.HTMLWriter.WriteString("<!-- ")
w.HTMLWriter.WriteString(n.Content)
w.HTMLWriter.WriteString(" -->\n")
}
func (l Linter) lintOrg(f *core.File) error {
extendedWriter := &ExtendedHTMLWriter{orgWriter}
orgWriter.ExtendingWriter = extendedWriter
old := f.Content
s := reOrgAttribute.ReplaceAllString(f.Content, "\n=$1=\n")
s = reOrgProps.ReplaceAllString(s, orgExample)
f.Content = s
s, err := l.Transform(f)
if err != nil {
return err
}
f.Content = old
// We don't want to find matches in `begin_src` lines.
body := reOrgSrc.ReplaceAllStringFunc(f.Content, func(m string) string {
return strings.Repeat("*", nlp.StrLen(m))
})
doc := orgConverter.Parse(strings.NewReader(s), f.Path)
// We don't want to introduce any *new* content into our HTML,
// so we clear the outline.
doc.Outline.Children = nil
html, err := doc.Write(orgWriter)
if err != nil {
return err
}
f.Content = body
return l.lintHTMLTokens(f, []byte(html), 0)
}