Refactoring

This commit is contained in:
yuin 2019-05-05 15:08:50 +09:00
parent 00356b97b1
commit 08a89f162a
3 changed files with 24 additions and 7 deletions

View file

@ -400,6 +400,7 @@ func NewAutoLink(typ AutoLinkType, value *Text) *AutoLink {
// A RawHTML struct represents an inline raw HTML of the Markdown text.
type RawHTML struct {
BaseInline
Segments *textm.Segments
}
// Inline implements Inline.Inline.
@ -407,7 +408,14 @@ func (n *RawHTML) Inline() {}
// Dump implements Node.Dump.
func (n *RawHTML) Dump(source []byte, level int) {
DumpHelper(n, source, level, nil, nil)
m := map[string]string{}
t := []string{}
for i := 0; i < n.Segments.Len(); i++ {
segment := n.Segments.At(i)
t = append(t, string(segment.Value(source)))
}
m["RawText"] = strings.Join(t, "")
DumpHelper(n, source, level, m, nil)
}
// KindRawHTML is a NodeKind of the RawHTML node.
@ -421,6 +429,6 @@ func (n *RawHTML) Kind() NodeKind {
// NewRawHTML returns a new RawHTML node.
func NewRawHTML() *RawHTML {
return &RawHTML{
BaseInline: BaseInline{},
Segments: textm.NewSegments(),
}
}

View file

@ -65,7 +65,7 @@ func (s *rawHTMLParser) parseSingleLineRegexp(reg *regexp.Regexp, block text.Rea
return nil
}
node := ast.NewRawHTML()
node.AppendChild(node, ast.NewRawTextSegment(segment.WithStop(segment.Start+match[1])))
node.Segments.Append(segment.WithStop(segment.Start + match[1]))
block.Advance(match[1])
return node
}
@ -108,7 +108,7 @@ func (s *rawHTMLParser) parseMultiLineRegexp(reg *regexp.Regexp, block text.Read
end = esegment.Start
}
node.AppendChild(node, ast.NewRawTextSegment(text.NewSegment(start, end)))
node.Segments.Append(text.NewSegment(start, end))
if l == eline {
block.Advance(end - start)
break

View file

@ -455,9 +455,18 @@ func (r *Renderer) renderImage(w util.BufWriter, source []byte, node ast.Node, e
return ast.WalkSkipChildren, nil
}
func (r *Renderer) renderRawHTML(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) {
func (r *Renderer) renderRawHTML(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkSkipChildren, nil
}
if r.Unsafe {
return ast.WalkContinue, nil
n := node.(*ast.RawHTML)
l := n.Segments.Len()
for i := 0; i < l; i++ {
segment := n.Segments.At(i)
w.Write(segment.Value(source))
}
return ast.WalkSkipChildren, nil
}
w.WriteString("<!-- raw HTML omitted -->")
return ast.WalkSkipChildren, nil
@ -470,7 +479,7 @@ func (r *Renderer) renderText(w util.BufWriter, source []byte, node ast.Node, en
n := node.(*ast.Text)
segment := n.Segment
if n.IsRaw() {
w.Write(segment.Value(source))
r.Writer.RawWrite(w, segment.Value(source))
} else {
r.Writer.Write(w, segment.Value(source))
if n.HardLineBreak() || (n.SoftLineBreak() && r.HardWraps) {