aboutsummaryrefslogtreecommitdiff
path: root/markdown/ast_paragraph.go
blob: b945741d5871eeaf94eebbedb9b9ece27ac50603 (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
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
}

func paragraph(lxs *lexers, oneLine bool) (*astParagraph, *ParseError) {
	tree := new(astParagraph)
	tree.oneLine = oneLine
	maxBreak := 2
	if oneLine {
		maxBreak = 1
	}
	n := 0
	lxs.current-- // 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
			}
			tree.content = append(tree.content, astLiteral(lxs.Current().Value))
		case lexerLiteral, lexerHeader:
			s := lxs.Current().Value
			// replace line break by space
			if n > 0 && len(tree.content) != 0 {
				s = " " + s
			}
			n = 0
			tree.content = append(tree.content, astLiteral(s))
		case lexerModifier:
			// replace line break by space
			if n > 0 {
				tree.content = append(tree.content, astLiteral(" "))
			}
			n = 0
			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 != "[" {
				//if lxs.Current().Value == "!" {
				s := lxs.Current().Value
				if n > 0 {
					s = " " + s
				}
				tree.content = append(tree.content, astLiteral(s))
			} else {
				ext, err := external(lxs)
				if err != nil {
					return nil, err
				}
				tree.content = append(tree.content, ext)
			}
			n = 0
		case lexerCode:
			if len(lxs.Current().Value) > 1 {
				return nil, &ParseError{lxs: *lxs, internal: ErrInvalidCodeBlockPosition}
			}
			n = 0
			b, err := code(lxs)
			if err != nil {
				return nil, err
			}
			tree.content = append(tree.content, b)
		}
	}
	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
}