Skip to content

Commit

Permalink
Closes #33 : Now NodeRenderers render attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Dec 8, 2019
1 parent 0a62f6a commit 7d8bee1
Show file tree
Hide file tree
Showing 6 changed files with 309 additions and 32 deletions.
20 changes: 10 additions & 10 deletions _test/options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@

## Title3 ## {#id_3 .class-3}

## Title4 ## {attr3=value3}
## Title4 ## {data-attr3=value3}

## Title5 ## {#id_5 attr5=value5}
## Title5 ## {#id_5 data-attr5=value5}

## Title6 ## {#id_6 .class6 attr6=value6}
## Title6 ## {#id_6 .class6 data-attr6=value6}

## Title7 ## {#id_7 attr7="value \"7"}
## Title7 ## {#id_7 data-attr7="value \"7"}

## Title8 {#id .className attrName=attrValue class="class1 class2"}
## Title8 {#id .className data-attrName=attrValue class="class1 class2"}
//- - - - - - - - -//
<h2 id="title-0">Title 0</h2>
<h2 id="id_1" class="class-1">Title1</h2>
<h2 id="id_2">Title2</h2>
<h2 id="id_3" class="class-3">Title3</h2>
<h2 attr3="value3" id="title4">Title4</h2>
<h2 id="id_5" attr5="value5">Title5</h2>
<h2 id="id_6" class="class6" attr6="value6">Title6</h2>
<h2 id="id_7" attr7="value &quot;7">Title7</h2>
<h2 id="id" class="className class1 class2" attrName="attrValue">Title8</h2>
<h2 data-attr3="value3" id="title4">Title4</h2>
<h2 id="id_5" data-attr5="value5">Title5</h2>
<h2 id="id_6" class="class6" data-attr6="value6">Title6</h2>
<h2 id="id_7" data-attr7="value &quot;7">Title7</h2>
<h2 id="id" class="className class1 class2" data-attrName="attrValue">Title8</h2>
//= = = = = = = = = = = = = = = = = = = = = = = =//

2
Expand Down
33 changes: 29 additions & 4 deletions extension/definition_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,31 +196,56 @@ func (r *DefinitionListHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFunc
reg.Register(ast.KindDefinitionDescription, r.renderDefinitionDescription)
}

// DefinitionListAttributeFilter defines attribute names which dl elements can have.
var DefinitionListAttributeFilter = html.GlobalAttributeFilter

func (r *DefinitionListHTMLRenderer) renderDefinitionList(w util.BufWriter, source []byte, n gast.Node, entering bool) (gast.WalkStatus, error) {
if entering {
_, _ = w.WriteString("<dl>\n")
if n.Attributes() != nil {
_, _ = w.WriteString("<dl")
html.RenderAttributes(w, n, DefinitionListAttributeFilter)
_, _ = w.WriteString(">\n")
} else {
_, _ = w.WriteString("<dl>\n")
}
} else {
_, _ = w.WriteString("</dl>\n")
}
return gast.WalkContinue, nil
}

// DefinitionTermAttributeFilter defines attribute names which dd elements can have.
var DefinitionTermAttributeFilter = html.GlobalAttributeFilter

func (r *DefinitionListHTMLRenderer) renderDefinitionTerm(w util.BufWriter, source []byte, n gast.Node, entering bool) (gast.WalkStatus, error) {
if entering {
_, _ = w.WriteString("<dt>")
if n.Attributes() != nil {
_, _ = w.WriteString("<dt")
html.RenderAttributes(w, n, DefinitionTermAttributeFilter)
_ = w.WriteByte('>')
} else {
_, _ = w.WriteString("<dt>")
}
} else {
_, _ = w.WriteString("</dt>\n")
}
return gast.WalkContinue, nil
}

// DefinitionDescriptionAttributeFilter defines attribute names which dd elements can have.
var DefinitionDescriptionAttributeFilter = html.GlobalAttributeFilter

func (r *DefinitionListHTMLRenderer) renderDefinitionDescription(w util.BufWriter, source []byte, node gast.Node, entering bool) (gast.WalkStatus, error) {
if entering {
n := node.(*ast.DefinitionDescription)
_, _ = w.WriteString("<dd")
if n.Attributes() != nil {
html.RenderAttributes(w, n, DefinitionDescriptionAttributeFilter)
}
if n.IsTight {
_, _ = w.WriteString("<dd>")
_, _ = w.WriteString(">")
} else {
_, _ = w.WriteString("<dd>\n")
_, _ = w.WriteString(">\n")
}
} else {
_, _ = w.WriteString("</dd>\n")
Expand Down
13 changes: 10 additions & 3 deletions extension/footnote.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,11 @@ func (r *FootnoteHTMLRenderer) renderFootnote(w util.BufWriter, source []byte, n
if entering {
_, _ = w.WriteString(`<li id="fn:`)
_, _ = w.WriteString(is)
_, _ = w.WriteString(`" role="doc-endnote">`)
_, _ = w.WriteString("\n")
_, _ = w.WriteString(`" role="doc-endnote"`)
if node.Attributes() != nil {
html.RenderAttributes(w, node, html.ListItemAttributeFilter)
}
_, _ = w.WriteString(">\n")
} else {
_, _ = w.WriteString("</li>\n")
}
Expand All @@ -284,7 +287,11 @@ func (r *FootnoteHTMLRenderer) renderFootnoteList(w util.BufWriter, source []byt
if entering {
_, _ = w.WriteString("<")
_, _ = w.WriteString(tag)
_, _ = w.WriteString(` class="footnotes" role="doc-endnotes">`)
_, _ = w.WriteString(` class="footnotes" role="doc-endnotes"`)
if node.Attributes() != nil {
html.RenderAttributes(w, node, html.GlobalAttributeFilter)
}
_ = w.WriteByte('>')
if r.Config.XHTML {
_, _ = w.WriteString("\n<hr />\n")
} else {
Expand Down
13 changes: 11 additions & 2 deletions extension/strikethrough.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,20 @@ func (r *StrikethroughHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncR
reg.Register(ast.KindStrikethrough, r.renderStrikethrough)
}

// StrikethroughAttributeFilter defines attribute names which dd elements can have.
var StrikethroughAttributeFilter = html.GlobalAttributeFilter

func (r *StrikethroughHTMLRenderer) renderStrikethrough(w util.BufWriter, source []byte, n gast.Node, entering bool) (gast.WalkStatus, error) {
if entering {
w.WriteString("<del>")
if n.Attributes() != nil {
_, _ = w.WriteString("<del")
html.RenderAttributes(w, n, StrikethroughAttributeFilter)
_ = w.WriteByte('>')
} else {
_, _ = w.WriteString("<del>")
}
} else {
w.WriteString("</del>")
_, _ = w.WriteString("</del>")
}
return gast.WalkContinue, nil
}
Expand Down
Loading

0 comments on commit 7d8bee1

Please sign in to comment.