Compare commits

...

2 Commits

Author SHA1 Message Date
Přemysl Eric Janouch be27f00685
hswg: add .Attr, .AttrList, contains
Trying to figure out a sensible way of handle tags.
2021-06-29 04:14:47 +02:00
Přemysl Eric Janouch 61083027a3
hswg: split out asciidoc.go 2021-06-29 04:14:47 +02:00
2 changed files with 82 additions and 49 deletions

54
hswg/asciidoc.go Normal file
View File

@ -0,0 +1,54 @@
package main
import (
"bytes"
"io"
"strings"
"unicode"
"unicode/utf8"
)
// isTitle returns the title level if the lines seem to form a title,
// zero otherwise. Input lines may inclide trailing newlines.
func isTitle(line1, line2 []byte) int {
// This is a very naïve method, we should target graphemes (thus at least
// NFC normalize the lines first) and account for wide characters.
diff := utf8.RuneCount(line1) - utf8.RuneCount(line2)
if len(line2) < 2 || diff < -1 || diff > 1 {
return 0
}
// "Don't be fooled by back-to-back delimited blocks."
// Still gets fooled by other things, though.
if bytes.IndexFunc(line1, func(r rune) bool {
return unicode.IsLetter(r) || unicode.IsNumber(r)
}) < 0 {
return 0
}
// The underline must be homogenous.
for _, r := range bytes.TrimRight(line2, "\r\n") {
if r != line2[0] {
return 0
}
}
return 1 + strings.IndexByte("=-~^+", line2[0])
}
func writeLine(w *io.PipeWriter, cur, next []byte) []byte {
if level := isTitle(cur, next); level > 0 {
w.Write(append(bytes.Repeat([]byte{'='}, level), ' '))
next = nil
}
w.Write(cur)
return next
}
// ConvertTitles converts AsciiDoc two-line (underlined) titles to single-line.
func ConvertTitles(w *io.PipeWriter, input []byte) {
var last []byte
for _, cur := range bytes.SplitAfter(input, []byte{'\n'}) {
last = writeLine(w, last, cur)
}
writeLine(w, last, nil)
}

View File

@ -19,8 +19,6 @@ import (
"strings"
"syscall"
"time"
"unicode"
"unicode/utf8"
"github.com/bytesparadise/libasciidoc/pkg/configuration"
"github.com/bytesparadise/libasciidoc/pkg/parser"
@ -30,51 +28,6 @@ import (
"github.com/bytesparadise/libasciidoc/pkg/validator"
)
// isTitle returns the title level if the lines seem to form a title,
// zero otherwise. Input lines may inclide trailing newlines.
func isTitle(line1, line2 []byte) int {
// This is a very naïve method, we should target graphemes (thus at least
// NFC normalize the lines first) and account for wide characters.
diff := utf8.RuneCount(line1) - utf8.RuneCount(line2)
if len(line2) < 2 || diff < -1 || diff > 1 {
return 0
}
// "Don't be fooled by back-to-back delimited blocks."
// Still gets fooled by other things, though.
if bytes.IndexFunc(line1, func(r rune) bool {
return unicode.IsLetter(r) || unicode.IsNumber(r)
}) < 0 {
return 0
}
// The underline must be homogenous.
for _, r := range bytes.TrimRight(line2, "\r\n") {
if r != line2[0] {
return 0
}
}
return 1 + strings.IndexByte("=-~^+", line2[0])
}
func writeLine(w *io.PipeWriter, cur, next []byte) []byte {
if level := isTitle(cur, next); level > 0 {
w.Write(append(bytes.Repeat([]byte{'='}, level), ' '))
next = nil
}
w.Write(cur)
return next
}
// ConvertTitles converts AsciiDoc two-line (underlined) titles to single-line.
func ConvertTitles(w *io.PipeWriter, input []byte) {
var last []byte
for _, cur := range bytes.SplitAfter(input, []byte{'\n'}) {
last = writeLine(w, last, cur)
}
writeLine(w, last, nil)
}
// Metadata contains select metadata about a rendered document.
type Metadata struct {
types.Metadata
@ -88,6 +41,21 @@ type Metadata struct {
// be linked anywhere else.
func (m *Metadata) IsDraft() bool { return m.Attributes.Has("draft") }
// Attr is a shortcut for retrieving document attributes by name.
func (m *Metadata) Attr(name string) string {
return m.Attributes.GetAsStringWithDefault(name, "")
}
// AttrList is similar to Attr, but splits the result at commas,
// and trims whitespace around array elements.
func (m *Metadata) AttrList(name string) []string {
res := strings.Split(m.Attr(name), ",")
for i := range res {
res[i] = strings.TrimSpace(res[i])
}
return res
}
// 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.
func Render(r io.Reader, config configuration.Configuration) (
@ -446,6 +414,17 @@ func watchDirectory(dirname string) (<-chan *watchEvent, error) {
return ch, nil
}
var funcs = template.FuncMap{
"contains": func(needle string, haystack []string) bool {
for _, el := range haystack {
if el == needle {
return true
}
}
return false
},
}
func singleFile() {
html, meta, err := Render(os.Stdin, configuration.NewConfiguration())
if err != nil {
@ -474,7 +453,7 @@ func main() {
if err != nil {
log.Fatalln(err)
}
tmplEntry, err := template.New("entry").Parse(string(header))
tmplEntry, err := template.New("entry").Funcs(funcs).Parse(string(header))
if err != nil {
log.Fatalln(err)
}
@ -484,7 +463,7 @@ func main() {
if err != nil {
log.Fatalln(err)
}
tmplIndex, err := template.New("index").Parse(string(index))
tmplIndex, err := template.New("index").Funcs(funcs).Parse(string(index))
if err != nil {
log.Fatalln(err)
}