diff options
author | Přemysl Eric Janouch <p@janouch.name> | 2020-08-15 04:21:56 +0200 |
---|---|---|
committer | Přemysl Eric Janouch <p@janouch.name> | 2020-08-15 04:26:50 +0200 |
commit | 3a5cc216bb6d497b28103d19b8471b5a89498cf9 (patch) | |
tree | a9dde1c590ceab784f0796da96fafa9ab4775167 /hasp | |
parent | a049249d8163a3ea02fb1c91cceaf2d1c6700f30 (diff) | |
download | haven-3a5cc216bb6d497b28103d19b8471b5a89498cf9.tar.gz haven-3a5cc216bb6d497b28103d19b8471b5a89498cf9.tar.xz haven-3a5cc216bb6d497b28103d19b8471b5a89498cf9.zip |
hswg: merge in hasp as a mode
No need to have the two-line header processor in two places.
Diffstat (limited to 'hasp')
-rw-r--r-- | hasp/main.go | 91 |
1 files changed, 0 insertions, 91 deletions
diff --git a/hasp/main.go b/hasp/main.go deleted file mode 100644 index dd2ba7a..0000000 --- a/hasp/main.go +++ /dev/null @@ -1,91 +0,0 @@ -// Program hasp is a preprocessor for libasciidoc to make it understand -// two-line/underlined titles, intended to be used in Gitea. -package main - -import ( - "bytes" - "encoding/xml" - "io" - "io/ioutil" - "os" - "strings" - "unicode" - "unicode/utf8" - - "github.com/bytesparadise/libasciidoc" - "github.com/bytesparadise/libasciidoc/pkg/configuration" -) - -// isTitle returns the title level if the lines seem to form a title, -// zero otherwise. Input lines may inclide trailing newlines. -func isTitle(line1, line2 []byte) int { - // This is a very naïve method, we should target graphemes (thus at least - // NFC normalize the lines first) and account for wide characters. - diff := utf8.RuneCount(line1) - utf8.RuneCount(line2) - if len(line2) < 2 || diff < -1 || diff > 1 { - return 0 - } - - // "Don't be fooled by back-to-back delimited blocks." - // Still gets fooled by other things, though. - if bytes.IndexFunc(line1, func(r rune) bool { - return unicode.IsLetter(r) || unicode.IsNumber(r) - }) < 0 { - return 0 - } - - // The underline must be homogenous. - for _, r := range bytes.TrimRight(line2, "\r\n") { - if r != line2[0] { - return 0 - } - } - return 1 + strings.IndexByte("=-~^+", line2[0]) -} - -func writeLine(w *io.PipeWriter, cur, next []byte) []byte { - if level := isTitle(cur, next); level > 0 { - w.Write(append(bytes.Repeat([]byte{'='}, level), ' ')) - next = nil - } - w.Write(cur) - return next -} - -// ConvertTitles converts AsciiDoc two-line (underlined) titles to single-line. -func ConvertTitles(w *io.PipeWriter, input []byte) { - var last []byte - for _, cur := range bytes.SplitAfter(input, []byte{'\n'}) { - last = writeLine(w, last, cur) - } - writeLine(w, last, nil) -} - -func main() { - input, err := ioutil.ReadAll(os.Stdin) - if err != nil { - panic(err) - } - - pr, pw := io.Pipe() - go func() { - defer pw.Close() - ConvertTitles(pw, input) - }() - - // io.Copy(os.Stdout, pr) - // return - - config := configuration.NewConfiguration( - configuration.WithHeaderFooter(true)) - _, err = libasciidoc.ConvertToHTML(pr, os.Stdout, config) - if err != nil { - // Fallback: output all the text sanitized for direct inclusion. - os.Stdout.WriteString("<pre>") - for _, line := range bytes.Split(input, []byte{'\n'}) { - xml.EscapeText(os.Stdout, line) - os.Stdout.WriteString("\n") - } - os.Stdout.WriteString("</pre>") - } -} |