aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--markdown/ast_external.go6
-rw-r--r--markdown/ast_paragraph.go13
-rw-r--r--markdown/ast_paragraph_test.go14
-rw-r--r--markdown/ast_test.go18
-rw-r--r--markdown/eval.go1
5 files changed, 45 insertions, 7 deletions
diff --git a/markdown/ast_external.go b/markdown/ast_external.go
index 7223a9b..be63131 100644
--- a/markdown/ast_external.go
+++ b/markdown/ast_external.go
@@ -26,7 +26,11 @@ func (a *astLink) Eval(opt *Option) (template.HTML, *ParseError) {
}
rr := opt.RenderLink(string(content), string(href))
if a.addSpace {
- return " " + rr, nil
+ s, err := astBreak{}.Eval(opt)
+ if err != nil {
+ return "", err
+ }
+ return s + rr, nil
}
return rr, nil
}
diff --git a/markdown/ast_paragraph.go b/markdown/ast_paragraph.go
index 70d3414..30fd4a1 100644
--- a/markdown/ast_paragraph.go
+++ b/markdown/ast_paragraph.go
@@ -34,6 +34,15 @@ func (a *astParagraph) Eval(opt *Option) (template.HTML, *ParseError) {
).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
@@ -46,11 +55,11 @@ func paragraph(lxs *lexers, oneLine bool) (*astParagraph, *ParseError) {
s := lxs.Current().Value
// replace line break by space
if n > 0 && len(tree.content) != 0 {
- s = " " + s
+ tree.content = append(tree.content, astBreak{})
}
tree.content = append(tree.content, conv(s))
}
- lxs.current-- // because we do not use it before the next
+ lxs.Before() // because we do not use it before the next
for lxs.Next() && n < maxBreak {
switch lxs.Current().Type {
case lexerBreak:
diff --git a/markdown/ast_paragraph_test.go b/markdown/ast_paragraph_test.go
index ab3ab6c..c18dfc7 100644
--- a/markdown/ast_paragraph_test.go
+++ b/markdown/ast_paragraph_test.go
@@ -7,10 +7,18 @@ func TestParagraph(t *testing.T) {
t.Run("simple", test("bonsoir", `<p>bonsoir</p>`))
})
t.Run("replacer", func(t *testing.T) {
- opt := &Option{
- Replaces: map[rune]string{'~': "&thinsp;"},
- }
+ opt := &Option{Replaces: map[rune]string{'~': "&thinsp;"}}
t.Run("empty", testWithOptions(opt, "bonsoir", `<p>bonsoir</p>`))
t.Run("simple", testWithOptions(opt, "bonsoir~!", `<p>bonsoir&thinsp;!</p>`))
})
+ t.Run("poem", func(t *testing.T) {
+ opt := &Option{Poem: true}
+ t.Run("simple", testWithOptions(opt, "bonsoir", `<p>bonsoir</p>`))
+ t.Run("one_break", testWithOptions(opt, `bonsoir
+world`, `<p>bonsoir<br />world</p>`))
+ t.Run("mult_break", testWithOptions(opt, `bonsoir
+world
+
+new line`, `<p>bonsoir<br />world</p><p>new line</p>`))
+ })
}
diff --git a/markdown/ast_test.go b/markdown/ast_test.go
index ed2ef87..c0c7fb3 100644
--- a/markdown/ast_test.go
+++ b/markdown/ast_test.go
@@ -41,7 +41,22 @@ var parsed = `
<ul><li>hehe</li></ul>
<figure>
<img alt="Ceci est ma pfp :3" src="https://cdn.anhgelus.world/pfp.jpg" />
-<figcaption> <a href="https://now.anhgelus.world/" target="_blank" rel="noreferer">Ma pfp</a> hehe :D Elle est <b>magnifique</b>, n&#39;est-ce pas ?</figcaption>
+<figcaption><a href="https://now.anhgelus.world/" target="_blank" rel="noreferer">Ma pfp</a> hehe :D Elle est <b>magnifique</b>, n&#39;est-ce pas ?</figcaption>
+</figure>
+`
+
+var parsedPoem = `
+<h1>Je suis un titre</h1>
+<p>Avec une description classique,<br />sur plusieurs lignes !</p>
+<p>Et je peux mettre du texte en <b>gras</b>,<br />en <em>italique</em> et les <b><em>deux en même temps</em></b> !</p>
+<div class="quote"><blockquote>Je suis une magnifique citation sur plusieurs lignes</blockquote><p>avec une source</p></div>
+<div class="quote"><blockquote>qui recommence après !</blockquote><p>qui a elle aussi une source :D</p></div>
+<ul><li>Ceci est une liste</li><li>pas ordonnée</li></ul>
+<ol><li>et maintenant</li><li>elle l&#39;est</li></ol>
+<ul><li>hehe</li></ul>
+<figure>
+<img alt="Ceci est ma pfp :3" src="https://cdn.anhgelus.world/pfp.jpg" />
+<figcaption><a href="https://now.anhgelus.world/" target="_blank" rel="noreferer">Ma pfp</a> hehe :D Elle est <b>magnifique</b>, n&#39;est-ce pas ?</figcaption>
</figure>
`
@@ -65,5 +80,6 @@ func testWithOptions(opt *Option, input, expected string) func(*testing.T) {
func TestAst(t *testing.T) {
t.Run("ast", func(t *testing.T) {
t.Run("complete", test(raw, strings.ReplaceAll(parsed, "\n", "")))
+ t.Run("poem", testWithOptions(&Option{Poem: true}, raw, strings.ReplaceAll(parsedPoem, "\n", "")))
})
}
diff --git a/markdown/eval.go b/markdown/eval.go
index ae4bafc..9b0e099 100644
--- a/markdown/eval.go
+++ b/markdown/eval.go
@@ -8,6 +8,7 @@ type Option struct {
ImageSource func(source string) string
RenderLink func(content, href string) template.HTML
Replaces map[rune]string
+ Poem bool
}
func Parse(s string, opt *Option) (template.HTML, *ParseError) {