diff options
| author | Anhgelus Morhtuuzh <william@herges.fr> | 2025-09-30 20:06:54 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <william@herges.fr> | 2025-09-30 20:06:54 +0200 |
| commit | 1d524762bc56950a85aa748aa873901621741dc7 (patch) | |
| tree | b896618b8d4bb138de36d52d8833768f83689333 /mardown/ast_paragraph.go | |
| parent | 9b6eb662150adbcd5ffe293a8719ffc4d7c14996 (diff) | |
refactor(markdown): create top layer type paragraph
Diffstat (limited to 'mardown/ast_paragraph.go')
| -rw-r--r-- | mardown/ast_paragraph.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/mardown/ast_paragraph.go b/mardown/ast_paragraph.go new file mode 100644 index 0000000..3ebc519 --- /dev/null +++ b/mardown/ast_paragraph.go @@ -0,0 +1,65 @@ +package mardown + +import ( + "errors" + "html/template" +) + +var ( + ErrInvalidParagraph = errors.New("invalid paragraph") + ErrInvalidCodeBlockPosition = errors.Join(ErrInvalidParagraph, errors.New("invalid code block position")) +) + +type astParagraph struct { + content []block +} + +func (a *astParagraph) Eval() (template.HTML, error) { + var content template.HTML + for _, c := range a.content { + ct, err := c.Eval() + if err != nil { + return "", err + } + content += ct + } + return content, nil +} + +func paragraph(lxs lexers) (block, error) { + tree := new(astParagraph) + n := 0 + lxs.current-- // because we do not use it before the next + for lxs.Next() && n < 2 { + switch lxs.Current().Type { + case lexerBreak: + n++ + case lexerLiteral: + tree.content = append(tree.content, astLiteral(lxs.Current().Value)) + case lexerModifier: + //TODO: handle + case lexerQuote: + //TODO: handle + case lexerEscape: + //TODO: handle + case lexerExternal: + //TODO: handle + case lexerCode: + if len(lxs.Current().Value) == 3 { + if n == 0 { + return nil, ErrInvalidCodeBlockPosition + } + lxs.current-- // because we do not use it before the next + return tree, nil + } + //TODO: handle + } + } + return tree, nil +} + +type astLiteral string + +func (a astLiteral) Eval() (template.HTML, error) { + return template.HTML(template.HTMLEscapeString(string(a))), nil +} |
