diff options
| author | William Hergès <william@herges.fr> | 2025-12-25 16:25:11 +0100 |
|---|---|---|
| committer | William Hergès <william@herges.fr> | 2025-12-25 16:25:11 +0100 |
| commit | 07d0ce1e9128a4598e97ce30a9eb0cfd47c05318 (patch) | |
| tree | f44cc6b4df6f2ae24726f219d88cc08b57ec4f7c /markdown/ast_paragraph.go | |
| parent | 66d6ab349196fd5d95937f533faa9db6ba65ebb3 (diff) | |
refactor(markdown): simplify paragraph parsing
Diffstat (limited to 'markdown/ast_paragraph.go')
| -rw-r--r-- | markdown/ast_paragraph.go | 61 |
1 files changed, 24 insertions, 37 deletions
diff --git a/markdown/ast_paragraph.go b/markdown/ast_paragraph.go index 30fd4a1..6bad197 100644 --- a/markdown/ast_paragraph.go +++ b/markdown/ast_paragraph.go @@ -51,16 +51,10 @@ func paragraph(lxs *lexers, oneLine bool) (*astParagraph, *ParseError) { 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 { + var err *ParseError + var b block switch lxs.Current().Type { case lexerBreak: n += len(lxs.Current().Value) @@ -69,45 +63,46 @@ func paragraph(lxs *lexers, oneLine bool) (*astParagraph, *ParseError) { lxs.Before() // because we did not use it return tree, nil } - asLiteral(toAstLiteral) + b = astLiteral(lxs.Current().Value) case lexerLiteral, lexerHeading: - asLiteral(toAstLiteral) + b = astLiteral(lxs.Current().Value) case lexerReplace: - asLiteral(toAstReplacer) + b = astReplacer(lxs.Current().Value) 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} + var e error + b, e = modifier(lxs) + if e != nil { + err = &ParseError{lxs: *lxs, internal: e} } - 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) + b = astLiteral(lxs.Current().Value) } else { - ext, err := external(lxs) - if err != nil { - return nil, err - } - tree.content = append(tree.content, ext) + b, err = external(lxs) } case lexerCode: if len(lxs.Current().Value) > 1 { - return nil, &ParseError{lxs: *lxs, internal: ErrInvalidCodeBlockPosition} + err = &ParseError{lxs: *lxs, internal: ErrInvalidCodeBlockPosition} + } else { + b, err = code(lxs) } - b, err := code(lxs) - if err != nil { - return nil, err + } + + if err != nil { + return nil, err + } + + if b != nil { + if n > 0 && len(tree.content) != 0 { + tree.content = append(tree.content, astBreak{}) } tree.content = append(tree.content, b) } + if lxs.Current().Type != lexerBreak { n = 0 } @@ -122,16 +117,8 @@ 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) -} |
