hswg: extract attributes from documents

This commit is contained in:
Přemysl Eric Janouch 2020-09-21 17:45:18 +02:00
parent 3d002bc540
commit 750f2139f5
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 31 additions and 14 deletions

View File

@ -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,6 +119,7 @@ func Render(doc io.Reader, config configuration.Configuration) (
}
_, _ = html.WriteString("</pre>")
}
meta.Attributes = doc.Attributes
return
}
@ -107,7 +127,7 @@ func Render(doc io.Reader, config configuration.Configuration) (
type entry struct {
path string // path
mtime time.Time // modification time
metadata types.Metadata // metadata
metadata Metadata // metadata
document []byte // inner document with expanded LinkWords
backlinks []string // what documents link back here
}
@ -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 {