forked from bytesparadise/libasciidoc
/
libasciidoc.go
70 lines (64 loc) · 2.6 KB
/
libasciidoc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Package libasciidoc is an open source Go library that converts Asciidoc
// content into HTML.
package libasciidoc
import (
"context"
"io"
"os"
"time"
"github.com/bytesparadise/libasciidoc/pkg/parser"
"github.com/bytesparadise/libasciidoc/pkg/renderer"
htmlrenderer "github.com/bytesparadise/libasciidoc/pkg/renderer/html5"
"github.com/bytesparadise/libasciidoc/pkg/types"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
var (
// BuildCommit lastest build commit (set by Makefile)
BuildCommit = ""
// BuildTag if the `BuildCommit` matches a tag
BuildTag = ""
// BuildTime set by build script (set by Makefile)
BuildTime = ""
)
// ConvertFileToHTML converts the content of the given filename into an HTML document.
// The conversion result is written in the given writer `output`, whereas the document metadata (title, etc.) (or an error if a problem occurred) is returned
// as the result of the function call.
func ConvertFileToHTML(ctx context.Context, filename string, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
file, err := os.Open(filename)
if err != nil {
return nil, errors.Wrapf(err, "error opening %s", filename)
}
defer file.Close()
return ConvertToHTML(ctx, file, output, options...)
}
// ConvertToHTML converts the content of the given reader `r` into a full HTML document, written in the given writer `output`.
// Returns an error if a problem occurred
func ConvertToHTML(ctx context.Context, r io.Reader, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
log.Debugf("parsing the asciidoc source...")
start := time.Now()
stats := parser.Stats{}
doc, err := parser.ParseReader("", r, parser.Statistics(&stats, "no match"))
if err != nil {
return nil, errors.Wrapf(err, "error while parsing the document")
}
duration := time.Since(start)
if err != nil {
log.Warnf("failed to produce stats: %v", err.Error())
}
log.Debugf("parsing stats:")
log.Debugf("- parsing duration: %v", duration)
log.Debugf("- expressions processed: %v", stats.ExprCnt)
return convertToHTML(ctx, doc.(types.Document), output, options...)
}
func convertToHTML(ctx context.Context, doc types.Document, output io.Writer, options ...renderer.Option) (map[string]interface{}, error) {
start := time.Now()
metadata, err := htmlrenderer.Render(renderer.Wrap(ctx, doc, options...), output)
if err != nil {
return nil, errors.Wrapf(err, "error while rendering the document")
}
log.Debugf("Done processing document")
duration := time.Since(start)
log.Debugf("rendered the HTML output in %v", duration)
return metadata, nil
}