// Program hswg is a static website generator employing libasciidoc with added // support for two-line/underlined titles, and postprocessing "wiki" InterLinks. package main import ( "bytes" "encoding/xml" "fmt" "io" "io/ioutil" "log" "os" "path/filepath" "regexp" "sort" "strings" "time" "unicode" "unicode/utf8" "github.com/bytesparadise/libasciidoc" "github.com/bytesparadise/libasciidoc/pkg/configuration" "github.com/bytesparadise/libasciidoc/pkg/types" ) // 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) } // 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 mtime time.Time // modification time metadata types.Metadata // metadata document []byte // inner document with expanded LinkWords backlinks []string // what documents link back here } var extRE = regexp.MustCompile(`\.[^/.]*$`) func stripExtension(path string) string { return extRE.ReplaceAllString(path, "") } func resultPath(path string) string { if m := extRE.FindStringIndex(path); m != nil { return path[:m[0]] + ".html" } return path + ".html" } func makeLink(m *map[string]*entry, name string) string { e := (*m)[name] return fmt.Sprintf("%s", resultPath(e.path), name) } var linkWordRE = regexp.MustCompile(`\b\p{Lu}\p{L}*\b`) func expand(m *map[string]*entry, name string, chunk []byte) []byte { return linkWordRE.ReplaceAllFunc(chunk, func(match []byte) []byte { if link, ok := (*m)[string(match)]; ok { link.backlinks = append(link.backlinks, name) return []byte(makeLink(m, string(match))) } return match }) } func singleFile() { html, meta, err := Render(os.Stdin, configuration.NewConfiguration()) if err != nil { log.Println(err) } else if meta.Title != "" { _, _ = os.Stdout.WriteString("
Links here: %s
\n", strings.Join(backlinks, ", "))) } _, _ = f.Write(e.document) _, _ = f.WriteString(fmt.Sprintf(" \n", e.metadata.LastUpdated, e.path)) } }