-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_html_document.go
More file actions
79 lines (70 loc) · 2.5 KB
/
Copy pathapi_html_document.go
File metadata and controls
79 lines (70 loc) · 2.5 KB
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
71
72
73
74
75
76
77
78
79
package marksplice
import (
"bytes"
"errors"
"io"
"github.com/zoster81/marksplice/internal/renderhtml"
"github.com/zoster81/marksplice/internal/splice"
)
// HTMLMetadataPolicy controls whether standalone HTML maps reviewed front-matter metadata.
type HTMLMetadataPolicy uint8
const (
// HTMLMetadataFrontMatter maps the reviewed title, description, author, and lang
// fields only when they are already source-proven simple front-matter scalars.
HTMLMetadataFrontMatter HTMLMetadataPolicy = iota
// HTMLMetadataOmit omits all front-matter-derived HTML metadata.
HTMLMetadataOmit
)
// HTMLDocumentOptions controls deterministic standalone HTML rendering.
// Its zero value uses the normal fragment-rendering defaults and maps the small
// reviewed front-matter metadata set when those fields are safely available.
type HTMLDocumentOptions struct {
Body HTMLRenderOptions
Metadata HTMLMetadataPolicy
}
// DefaultHTMLDocumentOptions returns the documented zero-value standalone policy.
func DefaultHTMLDocumentOptions() HTMLDocumentOptions {
return HTMLDocumentOptions{}
}
// RenderHTMLDocument streams a deterministic standalone HTML document.
// It performs no filesystem, network, asset, template, or command access.
func (d *Document) RenderHTMLDocument(writer io.Writer, options HTMLDocumentOptions) error {
if d == nil || d.document == nil || writer == nil {
return ErrInvalidRender
}
internal, err := internalHTMLDocumentOptions(options)
if err != nil {
return err
}
if err := d.document.RenderHTMLDocument(writer, internal); err != nil {
if errors.Is(err, renderhtml.ErrInvalidInput) {
return translateError(err, renderhtml.ErrInvalidInput, ErrInvalidRender)
}
return err
}
return nil
}
// HTMLDocument renders a deterministic standalone HTML document into caller-owned bytes.
func (d *Document) HTMLDocument(options HTMLDocumentOptions) ([]byte, error) {
if d == nil || d.document == nil {
return nil, ErrInvalidRender
}
var output bytes.Buffer
if err := d.RenderHTMLDocument(&output, options); err != nil {
return nil, err
}
return output.Bytes(), nil
}
func internalHTMLDocumentOptions(options HTMLDocumentOptions) (splice.HTMLDocumentOptions, error) {
body, err := internalHTMLRenderOptions(options.Body)
if err != nil {
return splice.HTMLDocumentOptions{}, err
}
if options.Metadata > HTMLMetadataOmit {
return splice.HTMLDocumentOptions{}, ErrInvalidRender
}
return splice.HTMLDocumentOptions{
Body: body,
Metadata: splice.HTMLMetadataPolicy(options.Metadata),
}, nil
}