diff --git a/hswg/main.go b/hswg/main.go index 74cbad7..0a92797 100644 --- a/hswg/main.go +++ b/hswg/main.go @@ -18,9 +18,12 @@ import ( "unicode" "unicode/utf8" - "github.com/bytesparadise/libasciidoc" "github.com/bytesparadise/libasciidoc/pkg/configuration" + "github.com/bytesparadise/libasciidoc/pkg/parser" + "github.com/bytesparadise/libasciidoc/pkg/renderer" + "github.com/bytesparadise/libasciidoc/pkg/renderer/sgml/html5" "github.com/bytesparadise/libasciidoc/pkg/types" + "github.com/bytesparadise/libasciidoc/pkg/validator" ) // isTitle returns the title level if the lines seem to form a title, @@ -68,14 +71,23 @@ func ConvertTitles(w *io.PipeWriter, input []byte) { writeLine(w, last, nil) } +// Metadata contains select metadata about a rendered document. +type Metadata struct { + types.Metadata + + // Note that this includes entries from the front-matter + // (see parser.ApplySubstitutions <- parser.ParseDocument). + Attributes types.Attributes +} + // 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) { +func Render(r io.Reader, config configuration.Configuration) ( + html *bytes.Buffer, meta Metadata, err error) { html = bytes.NewBuffer(nil) var input []byte - if input, err = ioutil.ReadAll(doc); err != nil { + if input, err = ioutil.ReadAll(r); err != nil { return } @@ -88,7 +100,14 @@ func Render(doc io.Reader, config configuration.Configuration) ( // io.Copy(os.Stdout, pr) // return - meta, err = libasciidoc.Convert(pr, html, config) + var doc types.Document + if doc, err = parser.ParseDocument(pr, config); err == nil { + for _, problem := range validator.Validate(&doc) { + fmt.Fprintln(os.Stderr, problem.Message) + } + ctx := renderer.NewContext(doc, config) + meta.Metadata, err = html5.Render(ctx, doc, html) + } if err != nil { // Fallback: output all the text sanitized for direct inclusion. html.Reset() @@ -100,16 +119,17 @@ func Render(doc io.Reader, config configuration.Configuration) ( } _, _ = html.WriteString("") } + meta.Attributes = doc.Attributes 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 + path string // path + mtime time.Time // modification time + metadata Metadata // metadata + document []byte // inner document with expanded LinkWords + backlinks []string // what documents link back here } var extRE = regexp.MustCompile(`\.[^/.]*$`) @@ -143,9 +163,7 @@ func expand(m *map[string]*entry, name string, chunk []byte) []byte { } func singleFile() { - html, meta, err := Render(os.Stdin, configuration.NewConfiguration( - configuration.WithBackEnd("xhtml5"), - )) + html, meta, err := Render(os.Stdin, configuration.NewConfiguration()) if err != nil { log.Println(err) } else if meta.Title != "" { @@ -202,7 +220,6 @@ func main() { var html *bytes.Buffer if html, e.metadata, err = Render(f, configuration.NewConfiguration( - configuration.WithBackEnd("xhtml5"), configuration.WithFilename(e.path), configuration.WithLastUpdated(e.mtime), )); err != nil {