From 3a5cc216bb6d497b28103d19b8471b5a89498cf9 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch Date: Sat, 15 Aug 2020 04:21:56 +0200 Subject: hswg: merge in hasp as a mode No need to have the two-line header processor in two places. --- hswg/main.go | 75 +++++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 56 insertions(+), 19 deletions(-) (limited to 'hswg') diff --git a/hswg/main.go b/hswg/main.go index 595bb4a..2a46c6e 100644 --- a/hswg/main.go +++ b/hswg/main.go @@ -4,6 +4,7 @@ package main import ( "bytes" + "encoding/xml" "fmt" "io" "io/ioutil" @@ -67,6 +68,41 @@ func ConvertTitles(w *io.PipeWriter, input []byte) { writeLine(w, last, nil) } +// Render converts an io.Reader with an AsciiDoc document to HTML. So long as +// the file could be read at all, it will always return a non-empty document. +func Render(doc io.Reader, config configuration.Configuration) ( + html *bytes.Buffer, meta types.Metadata, err error) { + html = bytes.NewBuffer(nil) + + var input []byte + if input, err = ioutil.ReadAll(doc); err != nil { + return + } + + pr, pw := io.Pipe() + go func() { + defer pw.Close() + ConvertTitles(pw, input) + }() + + // io.Copy(os.Stdout, pr) + // return + + meta, err = libasciidoc.ConvertToHTML(pr, html, config) + if err != nil { + // Fallback: output all the text sanitized for direct inclusion. + html.Reset() + + _, _ = html.WriteString("
")
+		for _, line := range bytes.Split(input, []byte{'\n'}) {
+			_ = xml.EscapeText(html, line)
+			_, _ = html.WriteString("\n")
+		}
+		_, _ = html.WriteString("
") + } + return +} + // entry contains all context information about a single page. type entry struct { path string // path @@ -106,7 +142,23 @@ func expand(m *map[string]*entry, name string, chunk []byte) []byte { }) } +func singleFile() { + html, meta, err := Render(os.Stdin, configuration.NewConfiguration()) + if err != nil { + log.Println(err) + } else if meta.Title != "" { + _, _ = os.Stdout.WriteString("

") + _ = xml.EscapeText(os.Stdout, []byte(meta.Title)) + _, _ = os.Stdout.WriteString("

\n") + } + _, _ = io.Copy(os.Stdout, html) +} + func main() { + if len(os.Args) < 2 { + singleFile() + return + } if len(os.Args) < 3 { log.Fatalf("usage: %s TEMPLATE GLOB...\n", os.Args[0]) } @@ -146,32 +198,17 @@ func main() { e.mtime = i.ModTime() } - input, err := ioutil.ReadAll(f) - if err != nil { - log.Fatalln(err) - } - - pr, pw := io.Pipe() - go func() { - defer pw.Close() - ConvertTitles(pw, input) - }() - - config := configuration.NewConfiguration( - configuration.WithHeaderFooter(false), + var html *bytes.Buffer + if html, e.metadata, err = Render(f, configuration.NewConfiguration( configuration.WithFilename(e.path), configuration.WithLastUpdated(e.mtime), - ) - - buf := bytes.NewBuffer(nil) - e.metadata, err = libasciidoc.ConvertToHTML(pr, buf, config) - if err != nil { + )); err != nil { log.Fatalln(err) } // Expand LinkWords anywhere between . // We want something like the inverse of Regexp.ReplaceAllStringFunc. - raw, last, expanded := buf.Bytes(), 0, bytes.NewBuffer(nil) + raw, last, expanded := html.Bytes(), 0, bytes.NewBuffer(nil) for _, where := range tagRE.FindAllIndex(raw, -1) { _, _ = expanded.Write(expand(&entries, name, raw[last:where[0]])) _, _ = expanded.Write(raw[where[0]:where[1]]) -- cgit v1.2.3-70-g09d2