package document // import "twos.dev/winter/document" import ( "bytes" "fmt" "html/template" "io" "net/url" "os" "path/filepath" "regexp" "strings" "github.com/alecthomas/chroma" chromahtml "github.com/alecthomas/chroma/formatters/html" "github.com/alecthomas/chroma/lexers" "github.com/alecthomas/chroma/styles" "golang.org/x/net/html" "golang.org/x/net/html/atom" ) type replacement struct { pattern *regexp.Regexp new []byte } var ( // styleWrapper is used to surround any text that, // if the entire page is in a monospace font, // deserves to be in a variable-width font. // // For example, dashes.html shows the difference between the en and em dashes. // In a monospace font, they're nearly identical. styleWrapper = []byte("$0") // earlyReplacements are raw text replacements that will happen before HTML is parsed or rendered. earlyReplacements = map[string][]byte{ // Break some special characters out of monospace homogeneity "—": styleWrapper, // Em dash "—": styleWrapper, // Em dash "–": styleWrapper, // En dash "–": styleWrapper, // En dash "ƒ": styleWrapper, // f-stop symbol // Figure dash is included but commented to call out that we do NOT want // to change its font as it would violate the whole point of the figure dash to make its font // different from surrounding digits. // "‒": styleWrapper, // Figure dash "―": styleWrapper, // Horizontal bar "⁃": styleWrapper, // Hyphen bullet "⁓": styleWrapper, // Swung dash } earlyReplacementRegexes = mustCompileReplacements(earlyReplacements) // lateReplacements are raw text replacements that will happen after HTML has been parsed. lateReplacements = map[string][]byte{ """: []byte("\""), "'": []byte("'"), """: []byte("\""), } ) func mustCompileReplacements(repls map[string][]byte) []replacement { compiled := make([]replacement, 0, len(repls)) for pattern, repl := range repls { re, err := regexp.Compile(pattern) if err != nil { panic(fmt.Sprintf("invalid replacement %q: %v", pattern, err)) } compiled = append(compiled, replacement{pattern: re, new: repl}) } return compiled } // applyEarlyReplacements applies early replacements to the HTML node tree. // It parses each replacement fragment into HTML before inserting it. func applyEarlyReplacements(root *html.Node) error { var walk func(*html.Node, bool) error walk = func(n *html.Node, inPre bool) error { if n.Type == html.ElementNode && n.DataAtom == atom.Pre { inPre = true } if n.Type == html.TextNode && !inPre { if n.Parent == nil { return nil } original := n.Data updated := original for _, repl := range earlyReplacementRegexes { updated = repl.pattern.ReplaceAllString(updated, string(repl.new)) } if updated != original { fragments, err := html.ParseFragment(strings.NewReader(updated), n.Parent) if err != nil { return err } parent := n.Parent for _, frag := range fragments { parent.InsertBefore(frag, n) } parent.RemoveChild(n) return nil } } for child := n.FirstChild; child != nil; { next := child.NextSibling if err := walk(child, inPre); err != nil { return err } child = next } return nil } return walk(root, false) } // HTMLDocument represents the HTML for a source file. // // The document's source content may or may not be in HTML. // It may be that the source file was written in another language, // like Markdown, then converted to HTML via [MarkdownDocument]. // // Therefore, any page generated from a file in src will at some point be an HTMLDocument. // // HTMLDocument implements [Document]. type HTMLDocument struct { // deps is a set of paths to source files that, // when changed, // should cause a rebuild of this document. deps map[string]struct{} meta *Metadata next Document result []byte // root is the topmost HTML tag in the parsed document, // usually or its parent. root *html.Node } // NewHTMLDocument creates a new document whose original source is at path src. // // Nothing is read from disk; src is metadata. // It may or may not point to a file containing HTML. // To read and parse HTML, call [Load]. func NewHTMLDocument(src string, meta *Metadata, next Document) *HTMLDocument { return &HTMLDocument{ deps: map[string]struct{}{ src: {}, "public/style.css": {}, }, meta: meta, next: next, } } func (doc *HTMLDocument) DependsOn(src string) bool { if _, ok := doc.deps[src]; ok { return true } if doc.meta.Layout == src { return true } if doc.meta.ParentFilename == src { return true } if strings.HasSuffix(src, ".css") { return true } if strings.HasPrefix(filepath.Clean(src), "src/templates/") { return true } return doc.next.DependsOn(src) } // Load reads HTML from r and loads it into doc. // // If called more than once, the last call wins. func (doc *HTMLDocument) Load(r io.Reader) error { body, err := doc.meta.UnmarshalDocument(r) if err != nil { return fmt.Errorf("cannot load template frontmatter for %q: %w", doc.meta.SourcePath, err) } root, err := html.Parse(bytes.NewReader(body)) if err != nil { return err } doc.root = root if err := applyEarlyReplacements(doc.root); err != nil { return err } if err := doc.Massage(); err != nil { return err } var buf bytes.Buffer if err := html.Render(&buf, doc.root); err != nil { return fmt.Errorf("cannot render HTML to build %q: %w", doc.meta.WebPath, err) } byts := buf.Bytes() for old, new := range lateReplacements { re, err := regexp.Compile(old) if err != nil { return err } byts = re.ReplaceAll(byts, new) } doc.result = byts if doc.next == nil { return nil } if err := doc.next.Load(bytes.NewReader(doc.result)); err != nil { return fmt.Errorf("cannot load forward from %T to %T: %w", doc, doc.next, err) } return nil } // Massage messes with loaded content to improve the page when it is ultimately rendered. // // Massage performs these tasks: // // - Linkifies and stylizes the first
tag which is a directy child of a tag.
func codeBlocks(root *html.Node) map[*html.Node]struct{} {
blocks := map[*html.Node]struct{}{}
for _, node := range allOfTypes(root, map[atom.Atom]struct{}{atom.Code: {}}) {
if node.Parent.DataAtom == atom.Pre {
blocks[node] = struct{}{}
}
}
return blocks
}
// firstTag returns the first and outermost descendant of n with the given tag.
func firstTag(n *html.Node, t atom.Atom) *html.Node {
if n.Type == html.ElementNode && n.DataAtom == t {
return n
}
for child := n.FirstChild; child != nil; child = child.NextSibling {
if el := firstTag(child, t); el != nil {
return el
}
}
return nil
}
func (doc *HTMLDocument) highlightCode() error {
for codeBlock := range codeBlocks(doc.root) {
lang := lang(codeBlock)
formatted, err := syntaxHighlight(lang, codeBlock.FirstChild.Data)
if err != nil {
return err
}
ancestry := doc.root
if ancestry.Type == html.DocumentNode {
ancestry = ancestry.FirstChild
}
pre, err := html.ParseFragment(strings.NewReader(formatted), ancestry)
if err != nil {
return fmt.Errorf("can't parse HTML %q: %w", formatted, err)
}
originalPre := codeBlock.Parent
for _, fragment := range pre {
if fragment.DataAtom == atom.Head {
continue
}
if fragment.DataAtom == atom.Body {
f := fragment.FirstChild
f.Parent = nil
originalPre.Parent.InsertBefore(f, originalPre)
}
}
originalPre.Parent.RemoveChild(originalPre)
}
return nil
}
func lang(code *html.Node) string {
for _, class := range strings.Fields(attr(code, atom.Class)) {
if _, l, ok := strings.Cut(class, "language-"); ok {
return l
}
}
return ""
}
func (doc *HTMLDocument) openExternalLinkInNewTab(a *html.Node) error {
var href *html.Attribute
for _, attr := range a.Attr {
switch attr.Key {
case "target":
// Don't overwrite explicitly set targets.
return nil
case "href":
href = &attr
}
}
if href == nil {
// Probably an element.
return nil
}
url, err := url.Parse(href.Val)
if err != nil {
return fmt.Errorf("cannot parse link %q: %w", href.Val, err)
}
if url.Host == "" {
// Don't make internal links open in new tabs.
return nil
}
a.Attr = append(a.Attr, html.Attribute{
Key: "target",
Val: "_blank",
})
return nil
}
func (doc *HTMLDocument) openExternalLinksInNewTab() error {
for _, a := range allOfTypes(doc.root, map[atom.Atom]struct{}{atom.A: {}}) {
if err := doc.openExternalLinkInNewTab(a); err != nil {
return err
}
}
return nil
}
func syntaxHighlight(lang, code string) (string, error) {
// Determine lexer.
lexer := lexers.Get(lang)
if lexer == nil {
lexer = lexers.Analyse(code)
}
if lexer == nil {
lexer = lexers.Fallback
}
lexer = chroma.Coalesce(lexer)
formatter := chromahtml.New(
chromahtml.Standalone(false),
chromahtml.TabWidth(2),
chromahtml.WithClasses(true),
chromahtml.WithLineNumbers(true),
)
// This has ~no effect because we specify colors in style.css manually, and
// pass chromahtml.WithClasses(true) above, meaning no inline styles get added
s := styles.Get("dracula")
it, err := lexer.Tokenise(nil, code)
if err != nil {
return "", err
}
var buf bytes.Buffer
if err := formatter.Format(&buf, s, it); err != nil {
return "", err
}
return buf.String(), nil
}