-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.go
110 lines (98 loc) · 2.86 KB
/
list.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// This file is part of gmnhg.
// gmnhg is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// gmnhg is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with gmnhg. If not, see <https://www.gnu.org/licenses/>.
package renderer
import (
"fmt"
"io"
"github.com/gomarkdown/markdown/ast"
)
var (
itemIndent = []byte{'\t'}
itemPrefix = []byte("* ")
)
func (r Renderer) renderFootnotes(w io.Writer, links []ast.Node) (count uint) {
for _, link := range links {
if link, ok := link.(*ast.Link); ok && link.Footnote != nil {
r.link(w, link, true)
w.Write(lineBreak)
count++
}
}
return
}
func (r Renderer) renderImages(w io.Writer, links []ast.Node) (count uint) {
for _, link := range links {
if link, ok := link.(*ast.Image); ok {
r.image(w, link, true)
w.Write(lineBreak)
count++
}
}
return
}
func (r Renderer) renderLinks(w io.Writer, links []ast.Node) (count uint) {
for _, link := range links {
if link, ok := link.(*ast.Link); ok && link.Footnote == nil {
r.link(w, link, true)
w.Write(lineBreak)
count++
}
}
return
}
func (r Renderer) linksList(w io.Writer, links []ast.Node) {
for _, renderer := range []func(Renderer, io.Writer, []ast.Node) uint{
Renderer.renderFootnotes,
Renderer.renderImages,
Renderer.renderLinks,
} {
linksRendered := renderer(r, w, links)
// ensure breaks between link blocks of the same type
if linksRendered > 0 {
w.Write(lineBreak)
}
}
}
func (r Renderer) list(w io.Writer, node *ast.List, level int) {
// the text/gemini spec included with the current Gemini spec does
// not specify anything about the formatting of lists of level >= 2,
// as of now this will just render them like in Markdown
isNumbered := (node.ListFlags & ast.ListTypeOrdered) != 0
for number, item := range node.Children {
item, ok := item.(*ast.ListItem)
if !ok {
panic("rendering anything but list items is not supported")
}
isTerm := (item.ListFlags & ast.ListTypeTerm) == ast.ListTypeTerm
if l := len(item.Children); l >= 1 {
// add extra line break to split up definitions
if isTerm && number > 0 {
w.Write(lineBreak)
}
for i := 0; i < level; i++ {
w.Write(itemIndent)
}
if isNumbered {
w.Write([]byte(fmt.Sprintf("%d. ", number+1)))
} else if !isTerm {
w.Write(itemPrefix)
}
r.text(w, item, true)
w.Write(lineBreak)
if l >= 2 {
if list, ok := item.Children[1].(*ast.List); ok {
r.list(w, list, level+1)
}
}
}
}
}