From 6a520e7fa6f119ea0a2bb9f258169f115f52a037 Mon Sep 17 00:00:00 2001 From: Xavier Coulon Date: Sat, 26 Feb 2022 15:18:47 +0100 Subject: [PATCH] fix(renderer): render predefined attributes as HTML entities includes `{nbps}`, etc. also, simplify rendering (no template needed) Fixes #953 Signed-off-by: Xavier Coulon --- pkg/renderer/sgml/attribute_substitution.go | 19 ---------- pkg/renderer/sgml/elements.go | 4 --- pkg/renderer/sgml/html5/paragraph_test.go | 4 +-- .../sgml/html5/predefined_attribute.go | 5 --- pkg/renderer/sgml/html5/templates.go | 1 - pkg/renderer/sgml/predefined_attribute.go | 14 +------- .../sgml/predefined_attribute_test.go | 20 +++++------ pkg/renderer/sgml/renderer.go | 35 +++++++++---------- pkg/renderer/sgml/sgml_renderer.go | 2 -- pkg/renderer/sgml/sgml_suite_test.go | 13 +++++++ pkg/renderer/sgml/templates.go | 1 - 11 files changed, 43 insertions(+), 75 deletions(-) delete mode 100644 pkg/renderer/sgml/attribute_substitution.go delete mode 100644 pkg/renderer/sgml/html5/predefined_attribute.go create mode 100644 pkg/renderer/sgml/sgml_suite_test.go diff --git a/pkg/renderer/sgml/attribute_substitution.go b/pkg/renderer/sgml/attribute_substitution.go deleted file mode 100644 index 0ee8ce91..00000000 --- a/pkg/renderer/sgml/attribute_substitution.go +++ /dev/null @@ -1,19 +0,0 @@ -package sgml - -import ( - "github.com/bytesparadise/libasciidoc/pkg/renderer" - "github.com/bytesparadise/libasciidoc/pkg/types" -) - -// TODO: deprecated? -func (r *sgmlRenderer) renderAttributeSubstitution(ctx *renderer.Context, e *types.AttributeReference) (string, error) { - if v, found := ctx.Attributes[e.Name]; found { - switch v := v.(type) { - case string: - return v, nil - default: - return r.renderElement(ctx, v) - } - } - return "{" + e.Name + "}", nil -} diff --git a/pkg/renderer/sgml/elements.go b/pkg/renderer/sgml/elements.go index 1f19b281..6daae20c 100644 --- a/pkg/renderer/sgml/elements.go +++ b/pkg/renderer/sgml/elements.go @@ -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: @@ -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) } diff --git a/pkg/renderer/sgml/html5/paragraph_test.go b/pkg/renderer/sgml/html5/paragraph_test.go index 4e8214f4..07f35a3c 100644 --- a/pkg/renderer/sgml/html5/paragraph_test.go +++ b/pkg/renderer/sgml/html5/paragraph_test.go @@ -94,9 +94,9 @@ some content` }) It("paragraph with predefined attribute", func() { - source := "hello {plus} world" + source := "hello{nbsp}{plus}{nbsp}world" expected := `
-

hello + world

+

hello + world

` Expect(RenderHTML(source)).To(MatchHTML(expected)) diff --git a/pkg/renderer/sgml/html5/predefined_attribute.go b/pkg/renderer/sgml/html5/predefined_attribute.go deleted file mode 100644 index bfa42d88..00000000 --- a/pkg/renderer/sgml/html5/predefined_attribute.go +++ /dev/null @@ -1,5 +0,0 @@ -package html5 - -const ( - predefinedAttributeTmpl = `{{ predefinedAttribute .Name }}` -) diff --git a/pkg/renderer/sgml/html5/templates.go b/pkg/renderer/sgml/html5/templates.go index 4a1a59fd..900c9de6 100644 --- a/pkg/renderer/sgml/html5/templates.go +++ b/pkg/renderer/sgml/html5/templates.go @@ -58,7 +58,6 @@ var templates = sgml.Templates{ PassthroughBlock: passthroughBlock, Paragraph: paragraphTmpl, Preamble: preambleTmpl, - PredefinedAttribute: predefinedAttributeTmpl, QAndAList: qAndAListTmpl, QAndAListItem: qAndAListItemTmpl, QuoteBlock: quoteBlockTmpl, diff --git a/pkg/renderer/sgml/predefined_attribute.go b/pkg/renderer/sgml/predefined_attribute.go index 28d7f80c..7175be9d 100644 --- a/pkg/renderer/sgml/predefined_attribute.go +++ b/pkg/renderer/sgml/predefined_attribute.go @@ -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 } diff --git a/pkg/renderer/sgml/predefined_attribute_test.go b/pkg/renderer/sgml/predefined_attribute_test.go index ceb01685..104f270a 100644 --- a/pkg/renderer/sgml/predefined_attribute_test.go +++ b/pkg/renderer/sgml/predefined_attribute_test.go @@ -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", " "), + Entry("zwsp", "zwsp", "​"), + Entry("wj", "wj", "⁠"), Entry("apos", "apos", "'"), Entry("quot", "quot", """), - 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", "‘"), + Entry("rsquo", "rsquo", "’"), + Entry("ldquo", "ldquo", "“"), + Entry("rdquo", "rdquo", "”"), + Entry("deg", "deg", "°"), + Entry("plus", "plus", "+"), + Entry("brvbar", "brvbar", "¦"), Entry("vbar", "vbar", "|"), Entry("amp", "amp", "&"), Entry("lt", "lt", "<"), diff --git a/pkg/renderer/sgml/renderer.go b/pkg/renderer/sgml/renderer.go index 24248407..dd073fbe 100644 --- a/pkg/renderer/sgml/renderer.go +++ b/pkg/renderer/sgml/renderer.go @@ -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 @@ -65,20 +64,20 @@ var predefinedAttributes = map[string]string{ "sp": " ", "blank": "", "empty": "", - "nbsp": "\u00a0", - "zwsp": "\u200b", - "wj": "\u2060", + "nbsp": " ", + "zwsp": "​", + "wj": "⁠", "apos": "'", "quot": """, - "lsquo": "\u2018", - "rsquo": "\u2019", - "ldquo": "\u201c", - "rdquo": "\u201d", - "deg": "\u00b0", + "lsquo": "‘", + "rsquo": "’", + "ldquo": "“", + "rdquo": "”", + "deg": "°", "plus": "+", - "brvbar": "\u00a6", + "brvbar": "¦", "vbar": "|", // TODO: maybe convert this because of tables? - "amp": "&", + "amp": "&", "lt": "<", "gt": ">", "startsb": "[", diff --git a/pkg/renderer/sgml/sgml_renderer.go b/pkg/renderer/sgml/sgml_renderer.go index c375e48b..a32def42 100644 --- a/pkg/renderer/sgml/sgml_renderer.go +++ b/pkg/renderer/sgml/sgml_renderer.go @@ -58,7 +58,6 @@ type sgmlRenderer struct { paragraph *textTemplate passthroughBlock *textTemplate preamble *textTemplate - predefinedAttribute *textTemplate qAndAList *textTemplate qAndAListItem *textTemplate quoteBlock *textTemplate @@ -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) diff --git a/pkg/renderer/sgml/sgml_suite_test.go b/pkg/renderer/sgml/sgml_suite_test.go new file mode 100644 index 00000000..ce9c5d03 --- /dev/null +++ b/pkg/renderer/sgml/sgml_suite_test.go @@ -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") +} diff --git a/pkg/renderer/sgml/templates.go b/pkg/renderer/sgml/templates.go index 8e7e06bd..a266677b 100644 --- a/pkg/renderer/sgml/templates.go +++ b/pkg/renderer/sgml/templates.go @@ -51,7 +51,6 @@ type Templates struct { Paragraph string PassthroughBlock string Preamble string - PredefinedAttribute string QAndAList string QAndAListItem string QuoteBlock string