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"
"unicode/utf8" "unicode/utf8"
"github.com/bytesparadise/libasciidoc"
"github.com/bytesparadise/libasciidoc/pkg/configuration" "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/types"
"github.com/bytesparadise/libasciidoc/pkg/validator"
) )
// isTitle returns the title level if the lines seem to form a title, // 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) 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 // 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. // the file could be read at all, it will always return a non-empty document.
func Render(doc io.Reader, config configuration.Configuration) ( func Render(r io.Reader, config configuration.Configuration) (
html *bytes.Buffer, meta types.Metadata, err error) { html *bytes.Buffer, meta Metadata, err error) {
html = bytes.NewBuffer(nil) html = bytes.NewBuffer(nil)
var input []byte var input []byte
if input, err = ioutil.ReadAll(doc); err != nil { if input, err = ioutil.ReadAll(r); err != nil {
return return
} }
@ -88,7 +100,14 @@ func Render(doc io.Reader, config configuration.Configuration) (
// io.Copy(os.Stdout, pr) // io.Copy(os.Stdout, pr)
// return // 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 { if err != nil {
// Fallback: output all the text sanitized for direct inclusion. // Fallback: output all the text sanitized for direct inclusion.
html.Reset() html.Reset()
@ -100,16 +119,17 @@ func Render(doc io.Reader, config configuration.Configuration) (
} }
_, _ = html.WriteString("</pre>") _, _ = html.WriteString("</pre>")
} }
meta.Attributes = doc.Attributes
return return
} }
// entry contains all context information about a single page. // entry contains all context information about a single page.
type entry struct { type entry struct {
path string // path path string // path
mtime time.Time // modification time mtime time.Time // modification time
metadata types.Metadata // metadata metadata Metadata // metadata
document []byte // inner document with expanded LinkWords document []byte // inner document with expanded LinkWords
backlinks []string // what documents link back here backlinks []string // what documents link back here
} }
var extRE = regexp.MustCompile(`\.[^/.]*$`) var extRE = regexp.MustCompile(`\.[^/.]*$`)
@ -143,9 +163,7 @@ func expand(m *map[string]*entry, name string, chunk []byte) []byte {
} }
func singleFile() { func singleFile() {
html, meta, err := Render(os.Stdin, configuration.NewConfiguration( html, meta, err := Render(os.Stdin, configuration.NewConfiguration())
configuration.WithBackEnd("xhtml5"),
))
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} else if meta.Title != "" { } else if meta.Title != "" {
@ -202,7 +220,6 @@ func main() {
var html *bytes.Buffer var html *bytes.Buffer
if html, e.metadata, err = Render(f, configuration.NewConfiguration( if html, e.metadata, err = Render(f, configuration.NewConfiguration(
configuration.WithBackEnd("xhtml5"),
configuration.WithFilename(e.path), configuration.WithFilename(e.path),
configuration.WithLastUpdated(e.mtime), configuration.WithLastUpdated(e.mtime),
)); err != nil { )); err != nil {