diff options
author | Přemysl Eric Janouch <p@janouch.name> | 2020-09-21 17:45:18 +0200 |
---|---|---|
committer | Přemysl Eric Janouch <p@janouch.name> | 2020-09-21 19:26:54 +0200 |
commit | 750f2139f574dc98de0be185d6be05101630ef41 (patch) | |
tree | e4233ab12dc45aedb280985862fbabfccabc4158 /hswg/main.go | |
parent | 3d002bc54041eb221a93ca66184f02611a35e45d (diff) | |
download | haven-750f2139f574dc98de0be185d6be05101630ef41.tar.gz haven-750f2139f574dc98de0be185d6be05101630ef41.tar.xz haven-750f2139f574dc98de0be185d6be05101630ef41.zip |
hswg: extract attributes from documents
Diffstat (limited to 'hswg/main.go')
-rw-r--r-- | hswg/main.go | 45 |
1 files changed, 31 insertions, 14 deletions
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("</pre>") } + 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 { |