aboutsummaryrefslogtreecommitdiff
path: root/markdown/ast_paragraph.go
blob: 30fd4a1f8445bf4b89b045b668ff42ad0d929924 (plain)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package markdown

import (
	"errors"
	"html/template"
	"strings"

	"git.anhgelus.world/anhgelus/small-web/dom"
)

var (
	ErrInvalidParagraph = errors.New("invalid paragraph")
)

type astParagraph struct {
	content []block
	oneLine bool
}

func (a *astParagraph) Eval(opt *Option) (template.HTML, *ParseError) {
	var content template.HTML
	for _, c := range a.content {
		ct, err := c.Eval(opt)
		if err != nil {
			return "", err
		}
		content += ct
	}
	if a.oneLine {
		return content, nil
	}
	return dom.NewParagraph(
		template.HTML(strings.TrimSpace(string(content))),
	).Render(), nil
}

type astBreak struct{}

func (a astBreak) Eval(opt *Option) (template.HTML, *ParseError) {
	if opt.Poem {
		return dom.NewVoidElement("br").Render(), nil
	}
	return " ", nil
}

func paragraph(lxs *lexers, oneLine bool) (*astParagraph, *ParseError) {
	tree := new(astParagraph)
	tree.oneLine = oneLine
	maxBreak := 2
	if oneLine {
		maxBreak = 1
	}
	n := 0
	asLiteral := func(conv func(s string) block) {
		s := lxs.Current().Value
		// replace line break by space
		if n > 0 && len(tree.content) != 0 {
			tree.content = append(tree.content, astBreak{})
		}
		tree.content = append(tree.content, conv(s))
	}
	lxs.Before() // because we do not use it before the next
	for lxs.Next() && n < maxBreak {
		switch lxs.Current().Type {
		case lexerBreak:
			n += len(lxs.Current().Value)
		case lexerQuote, lexerList:
			if n > 0 {
				lxs.Before() // because we did not use it
				return tree, nil
			}
			asLiteral(toAstLiteral)
		case lexerLiteral, lexerHeading:
			asLiteral(toAstLiteral)
		case lexerReplace:
			asLiteral(toAstReplacer)
		case lexerModifier:
			// replace line break by space
			if n > 0 {
				tree.content = append(tree.content, astLiteral(" "))
			}
			mod, err := modifier(lxs)
			if err != nil {
				return nil, &ParseError{lxs: *lxs, internal: err}
			}
			tree.content = append(tree.content, mod)
		case lexerExternal:
			if n > 0 && lxs.Current().Value == "![" {
				lxs.Before() // because we did not use it
				return tree, nil
			}
			if lxs.Current().Value != "[" {
				asLiteral(toAstLiteral)
			} else {
				ext, err := external(lxs)
				if err != nil {
					return nil, err
				}
				tree.content = append(tree.content, ext)
			}
		case lexerCode:
			if len(lxs.Current().Value) > 1 {
				return nil, &ParseError{lxs: *lxs, internal: ErrInvalidCodeBlockPosition}
			}
			b, err := code(lxs)
			if err != nil {
				return nil, err
			}
			tree.content = append(tree.content, b)
		}
		if lxs.Current().Type != lexerBreak {
			n = 0
		}
	}
	lxs.Before() // because we never handle the last item
	return tree, nil
}

type astLiteral string

func (a astLiteral) Eval(_ *Option) (template.HTML, *ParseError) {
	return template.HTML(template.HTMLEscapeString(string(a))), nil
}

func toAstLiteral(s string) block {
	return astLiteral(s)
}

type astReplacer string

func (a astReplacer) Eval(opt *Option) (template.HTML, *ParseError) {
	return template.HTML(opt.Replaces[[]rune(a)[0]]), nil
}

func toAstReplacer(s string) block {
	return astReplacer(s)
}