2022-06-11 09:30:22 +02:00
|
|
|
package formatter
|
2022-03-22 06:53:01 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html"
|
|
|
|
|
2022-06-11 09:30:22 +02:00
|
|
|
"ewintr.nl/adoc/document"
|
2022-03-22 06:53:01 +01:00
|
|
|
"ewintr.nl/adoc/element"
|
|
|
|
"ewintr.nl/go-kit/slugify"
|
|
|
|
)
|
|
|
|
|
2022-06-10 14:58:36 +02:00
|
|
|
const htmlPageTemplate = `<!DOCTYPE html>
|
2022-03-22 06:53:01 +01:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>%s</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
%s</body>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
|
2022-06-11 09:30:22 +02:00
|
|
|
type HTML struct{}
|
|
|
|
|
|
|
|
func NewHTML() *HTML {
|
|
|
|
return &HTML{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *HTML) Format(doc *document.Document) string {
|
|
|
|
return fmt.Sprintf(htmlPageTemplate, html.EscapeString(doc.Title), h.FormatFragments(doc.Content...))
|
2022-03-22 06:53:01 +01:00
|
|
|
}
|
|
|
|
|
2022-06-11 09:30:22 +02:00
|
|
|
func (h *HTML) FormatFragments(els ...element.Element) string {
|
2022-03-22 06:53:01 +01:00
|
|
|
var html string
|
|
|
|
for _, el := range els {
|
2022-06-11 09:30:22 +02:00
|
|
|
html += h.htmlElement(el)
|
2022-03-22 06:53:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return html
|
|
|
|
}
|
|
|
|
|
2022-06-11 09:30:22 +02:00
|
|
|
func (h *HTML) htmlElement(el element.Element) string {
|
2022-03-22 06:53:01 +01:00
|
|
|
switch v := el.(type) {
|
|
|
|
case element.SubTitle:
|
|
|
|
return fmt.Sprintf("<h2 id=%q>%s</h2>\n", slugify.Slugify(v.Text()), html.EscapeString(v.Text()))
|
|
|
|
case element.SubSubTitle:
|
|
|
|
return fmt.Sprintf("<h3 id=%q>%s</h3>\n", slugify.Slugify(v.Text()), html.EscapeString(v.Text()))
|
|
|
|
case element.List:
|
|
|
|
var items []element.Element
|
|
|
|
for _, i := range v {
|
|
|
|
items = append(items, i)
|
|
|
|
}
|
2022-06-11 09:30:22 +02:00
|
|
|
return fmt.Sprintf("<ul>\n%s</ul>\n", h.FormatFragments(items...))
|
2022-03-22 06:53:01 +01:00
|
|
|
case element.ListItem:
|
2022-06-11 09:30:22 +02:00
|
|
|
return fmt.Sprintf("<li>%s</li>\n", h.FormatFragments(v...))
|
2022-03-22 06:53:01 +01:00
|
|
|
case element.CodeBlock:
|
2022-03-31 06:56:26 +02:00
|
|
|
return fmt.Sprintf("<pre><code>%s</code></pre>", html.EscapeString(v.Text()))
|
2022-03-22 06:53:01 +01:00
|
|
|
case element.Paragraph:
|
2022-06-11 09:30:22 +02:00
|
|
|
return fmt.Sprintf("<p>%s</p>\n", h.FormatFragments(v.Elements...))
|
2022-03-22 06:53:01 +01:00
|
|
|
case element.Strong:
|
2022-06-11 09:30:22 +02:00
|
|
|
return fmt.Sprintf("<strong>%s</strong>", h.FormatFragments(v...))
|
2022-03-22 06:53:01 +01:00
|
|
|
case element.Emphasis:
|
2022-06-11 09:30:22 +02:00
|
|
|
return fmt.Sprintf("<em>%s</em>", h.FormatFragments(v...))
|
2022-03-22 06:53:01 +01:00
|
|
|
case element.Code:
|
2022-06-11 09:30:22 +02:00
|
|
|
return fmt.Sprintf("<code>%s</code>", h.FormatFragments(v...))
|
2022-03-22 06:53:01 +01:00
|
|
|
case element.Link:
|
|
|
|
return fmt.Sprintf("<a href=%q>%s</a>", v.URL, html.EscapeString(v.Title))
|
|
|
|
case element.Word:
|
|
|
|
return html.EscapeString(v.Text())
|
|
|
|
case element.WhiteSpace:
|
|
|
|
return " "
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|