Compare commits
2 Commits
5b432fcc0b
...
be27f00685
Author | SHA1 | Date |
---|---|---|
Přemysl Eric Janouch | be27f00685 | |
Přemysl Eric Janouch | 61083027a3 |
|
@ -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)
|
||||||
|
}
|
77
hswg/main.go
77
hswg/main.go
|
@ -19,8 +19,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
|
||||||
"unicode/utf8"
|
|
||||||
|
|
||||||
"github.com/bytesparadise/libasciidoc/pkg/configuration"
|
"github.com/bytesparadise/libasciidoc/pkg/configuration"
|
||||||
"github.com/bytesparadise/libasciidoc/pkg/parser"
|
"github.com/bytesparadise/libasciidoc/pkg/parser"
|
||||||
|
@ -30,51 +28,6 @@ import (
|
||||||
"github.com/bytesparadise/libasciidoc/pkg/validator"
|
"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.
|
// Metadata contains select metadata about a rendered document.
|
||||||
type Metadata struct {
|
type Metadata struct {
|
||||||
types.Metadata
|
types.Metadata
|
||||||
|
@ -88,6 +41,21 @@ type Metadata struct {
|
||||||
// be linked anywhere else.
|
// be linked anywhere else.
|
||||||
func (m *Metadata) IsDraft() bool { return m.Attributes.Has("draft") }
|
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
|
// 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.
|
// the file could be read at all, it will always return a non-empty document.
|
||||||
func Render(r io.Reader, config configuration.Configuration) (
|
func Render(r io.Reader, config configuration.Configuration) (
|
||||||
|
@ -446,6 +414,17 @@ func watchDirectory(dirname string) (<-chan *watchEvent, error) {
|
||||||
return ch, nil
|
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() {
|
func singleFile() {
|
||||||
html, meta, err := Render(os.Stdin, configuration.NewConfiguration())
|
html, meta, err := Render(os.Stdin, configuration.NewConfiguration())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -474,7 +453,7 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
tmplEntry, err := template.New("entry").Parse(string(header))
|
tmplEntry, err := template.New("entry").Funcs(funcs).Parse(string(header))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
@ -484,7 +463,7 @@ func main() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
tmplIndex, err := template.New("index").Parse(string(index))
|
tmplIndex, err := template.New("index").Funcs(funcs).Parse(string(index))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue