Skip to content

Commit

Permalink
fix(renderer): render predefined attributes as HTML entities
Browse files Browse the repository at this point in the history
includes `{nbps}`, etc.
also, simplify rendering (no template needed)

Fixes #953

Signed-off-by: Xavier Coulon <[email protected]>
  • Loading branch information
xcoulon committed Feb 26, 2022
1 parent 76352c9 commit 6a520e7
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 75 deletions.
19 changes: 0 additions & 19 deletions pkg/renderer/sgml/attribute_substitution.go

This file was deleted.

4 changes: 0 additions & 4 deletions pkg/renderer/sgml/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ func (r *sgmlRenderer) renderListElements(ctx *renderer.Context, elements []inte
func (r *sgmlRenderer) renderElement(ctx *renderer.Context, element interface{}) (string, error) {
// log.Debugf("rendering element of type `%T`", element)
switch e := element.(type) {
case []interface{}:
return r.renderElements(ctx, e)
case *types.TableOfContents:
return r.renderTableOfContents(ctx, e)
case *types.Section:
Expand Down Expand Up @@ -115,8 +113,6 @@ func (r *sgmlRenderer) renderElement(ctx *renderer.Context, element interface{})
case *types.FrontMatter:
ctx.Attributes.AddAll(e.Attributes)
return "", nil
case *types.AttributeReference:
return r.renderAttributeSubstitution(ctx, e)
default:
return "", errors.Errorf("unsupported type of element: %T", element)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/renderer/sgml/html5/paragraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ some content`
})

It("paragraph with predefined attribute", func() {
source := "hello {plus} world"
source := "hello{nbsp}{plus}{nbsp}world"
expected := `<div class="paragraph">
<p>hello &#43; world</p>
<p>hello&#160;&#43;&#160;world</p>
</div>
`
Expect(RenderHTML(source)).To(MatchHTML(expected))
Expand Down
5 changes: 0 additions & 5 deletions pkg/renderer/sgml/html5/predefined_attribute.go

This file was deleted.

1 change: 0 additions & 1 deletion pkg/renderer/sgml/html5/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ var templates = sgml.Templates{
PassthroughBlock: passthroughBlock,
Paragraph: paragraphTmpl,
Preamble: preambleTmpl,
PredefinedAttribute: predefinedAttributeTmpl,
QAndAList: qAndAListTmpl,
QAndAListItem: qAndAListItemTmpl,
QuoteBlock: quoteBlockTmpl,
Expand Down
14 changes: 1 addition & 13 deletions pkg/renderer/sgml/predefined_attribute.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
package sgml

import (
"strings"

"github.com/bytesparadise/libasciidoc/pkg/types"
"github.com/pkg/errors"
)

func (r *sgmlRenderer) renderPredefinedAttribute(a *types.PredefinedAttribute) (string, error) {
result := &strings.Builder{}
if err := r.predefinedAttribute.Execute(result, struct {
Name string
}{
Name: a.Name,
}); err != nil {
return "", errors.Wrap(err, "error while rendering predefined attribute")
}
// log.Debugf("rendered predefined attribute for '%s': '%s'", a.Name, result.String())
return result.String(), nil
return predefinedAttribute(a.Name), nil
}
20 changes: 10 additions & 10 deletions pkg/renderer/sgml/predefined_attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ var _ = DescribeTable("predefined attributes",
Entry("sp", "sp", " "),
Entry("blank", "blank", ""),
Entry("empty", "empty", ""),
Entry("nbsp", "nbsp", "\u00a0"),
Entry("zwsp", "zwsp", "\u200b"),
Entry("wj", "wj", "\u2060"),
Entry("nbsp", "nbsp", "&#160;"),
Entry("zwsp", "zwsp", "&#8203;"),
Entry("wj", "wj", "&#8288;"),
Entry("apos", "apos", "&#39;"),
Entry("quot", "quot", "&#34;"),
Entry("lsquo", "lsquo", "\u2018"),
Entry("rsquo", "rsquo", "\u2019"),
Entry("ldquo", "ldquo", "\u201c"),
Entry("rdquo", "rdquo", "\u201d"),
Entry("deg", "deg", "\u00b0"),
Entry("plus", "plus", "+"),
Entry("brvbar", "brvbar", "\u00a6"),
Entry("lsquo", "lsquo", "&#8216;"),
Entry("rsquo", "rsquo", "&#8217;"),
Entry("ldquo", "ldquo", "&#8220;"),
Entry("rdquo", "rdquo", "&#8221;"),
Entry("deg", "deg", "&#176;"),
Entry("plus", "plus", "&#43;"),
Entry("brvbar", "brvbar", "&#166;"),
Entry("vbar", "vbar", "|"),
Entry("amp", "amp", "&"),
Entry("lt", "lt", "<"),
Expand Down
35 changes: 17 additions & 18 deletions pkg/renderer/sgml/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ func NewRenderer(t Templates) Renderer {
templates: t,
// Establish some default function handlers.
functions: funcMap{
"escape": EscapeString,
"trimRight": trimRight,
"trimLeft": trimLeft,
"trim": trimBoth,
"predefinedAttribute": predefinedAttribute,
"halign": halign,
"valign": valign,
"lastInStrings": lastInStrings,
"escape": EscapeString,
"trimRight": trimRight,
"trimLeft": trimLeft,
"trim": trimBoth,
"halign": halign,
"valign": valign,
"lastInStrings": lastInStrings,
},
}
return r
Expand All @@ -65,20 +64,20 @@ var predefinedAttributes = map[string]string{
"sp": " ",
"blank": "",
"empty": "",
"nbsp": "\u00a0",
"zwsp": "\u200b",
"wj": "\u2060",
"nbsp": "&#160;",
"zwsp": "&#8203;",
"wj": "&#8288;",
"apos": "&#39;",
"quot": "&#34;",
"lsquo": "\u2018",
"rsquo": "\u2019",
"ldquo": "\u201c",
"rdquo": "\u201d",
"deg": "\u00b0",
"lsquo": "&#8216;",
"rsquo": "&#8217;",
"ldquo": "&#8220;",
"rdquo": "&#8221;",
"deg": "&#176;",
"plus": "&#43;",
"brvbar": "\u00a6",
"brvbar": "&#166;",
"vbar": "|", // TODO: maybe convert this because of tables?
"amp": "&amp;",
"amp": "&",
"lt": "<",
"gt": ">",
"startsb": "[",
Expand Down
2 changes: 0 additions & 2 deletions pkg/renderer/sgml/sgml_renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ type sgmlRenderer struct {
paragraph *textTemplate
passthroughBlock *textTemplate
preamble *textTemplate
predefinedAttribute *textTemplate
qAndAList *textTemplate
qAndAListItem *textTemplate
quoteBlock *textTemplate
Expand Down Expand Up @@ -141,7 +140,6 @@ func (r *sgmlRenderer) prepareTemplates() error {
r.paragraph, err = r.newTemplate("paragraph", tmpls.Paragraph, err)
r.passthroughBlock, err = r.newTemplate("passthrough", tmpls.PassthroughBlock, err)
r.preamble, err = r.newTemplate("preamble", tmpls.Preamble, err)
r.predefinedAttribute, err = r.newTemplate("predefined attribute", tmpls.PredefinedAttribute, err)
r.qAndAList, err = r.newTemplate("qanda-list", tmpls.QAndAList, err)
r.qAndAListItem, err = r.newTemplate("qanda-list-item", tmpls.QAndAListItem, err)
r.quoteBlock, err = r.newTemplate("quote-block", tmpls.QuoteBlock, err)
Expand Down
13 changes: 13 additions & 0 deletions pkg/renderer/sgml/sgml_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sgml_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestSgml(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Sgml Suite")
}
1 change: 0 additions & 1 deletion pkg/renderer/sgml/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ type Templates struct {
Paragraph string
PassthroughBlock string
Preamble string
PredefinedAttribute string
QAndAList string
QAndAListItem string
QuoteBlock string
Expand Down

0 comments on commit 6a520e7

Please sign in to comment.