From e16365c3014080628cb20e8156e63725d23d2afa Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Wed, 2 Jan 2019 18:27:35 +0100 Subject: [PATCH] renderer/html: fix outputting references (#47) Dont print the reference part of a reference if it isn't defined. And enable the parsing test of the .md files in the rfc directory. Also add HTML and markdown parsing to the list, so we exercise all renderers. Signed-off-by: Miek Gieben --- render/mhtml/hook.go | 5 +++- rfc_test.go | 68 ++++++++++++++++++++++++++++++-------------- 2 files changed, 51 insertions(+), 22 deletions(-) diff --git a/render/mhtml/hook.go b/render/mhtml/hook.go index 0d3986f..e55a56b 100644 --- a/render/mhtml/hook.go +++ b/render/mhtml/hook.go @@ -99,6 +99,10 @@ func RenderHook(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool func bibliographyItem(w io.Writer, bib *mast.BibliographyItem, entering bool) { io.WriteString(w, `
`+fmt.Sprintf("[%s]", bib.Anchor)+"
\n") io.WriteString(w, `
`) + defer io.WriteString(w, "
\n") + if bib.Reference == nil { + return + } io.WriteString(w, ``+bib.Reference.Front.Author.Fullname+"\n") io.WriteString(w, ``+bib.Reference.Front.Title+"\n") if bib.Reference.Format != nil && bib.Reference.Format.Target != "" { @@ -107,7 +111,6 @@ func bibliographyItem(w io.Writer, bib *mast.BibliographyItem, entering bool) { if bib.Reference.Front.Date.Year != "" { io.WriteString(w, ``+bib.Reference.Front.Date.Year+"\n") } - io.WriteString(w, "\n") } func firstSubItem(node ast.Node) bool { diff --git a/rfc_test.go b/rfc_test.go index 30d000b..ea4c2a5 100644 --- a/rfc_test.go +++ b/rfc_test.go @@ -1,5 +1,3 @@ -// +build xml2rfc - package main import ( @@ -11,22 +9,30 @@ import ( "github.com/gomarkdown/markdown" "github.com/gomarkdown/markdown/ast" + "github.com/gomarkdown/markdown/html" "github.com/gomarkdown/markdown/parser" "github.com/mmarkdown/mmark/mast" "github.com/mmarkdown/mmark/mparser" - "github.com/mmarkdown/mmark/xml" - "github.com/mmarkdown/mmark/xml2" + mmarkdown "github.com/mmarkdown/mmark/render/markdown" + "github.com/mmarkdown/mmark/render/mhtml" + "github.com/mmarkdown/mmark/render/xml" + "github.com/mmarkdown/mmark/render/xml2" ) -// TestRFC3 parses the RFC in the rfc/ directory and runs xml2rfc on them to see if they parse OK. -func TestRFC3(t *testing.T) { // currently broken because of xml2rfc --debug foo - dir := "rfc" - testFiles := []string{ +var ( + testFiles = []string{ "2100.md", "3514.md", + "5841.md", "7511.md", + "8341.md", } + dir = "rfc" + doXMLRFC = false // xml2rfc is broken as we need a very new version of it (in travis) +) +// TestRFC3 parses the RFC in the rfc/ directory and runs xml2rfc on them to see if they parse OK. +func TestRFC3(t *testing.T) { for _, f := range testFiles { base := f[:len(f)-3] @@ -39,22 +45,39 @@ func TestRFC3(t *testing.T) { // currently broken because of xml2rfc --debug foo } // TestRFC2 parses the RFC in the rfc/ directory and runs xml2rfc on them to see if they parse OK. -func TestRFC2(t *testing.T) { // currently broken because of xml2rfc --debug foo - dir := "rfc" - testFiles := []string{ - "2100.md", - "3514.md", - "7511.md", +func TestRFC2(t *testing.T) { + for _, f := range testFiles { + base := f[:len(f)-3] + + opts := xml2.RendererOptions{ + Flags: xml2.CommonFlags | xml2.XMLFragment, + } + renderer := xml2.NewRenderer(opts) + doRenderTest(t, dir, base, renderer) } +} +// TestHTML parses the RFC in the rfc/ directory to HTMl. +func TestHTML(t *testing.T) { for _, f := range testFiles { base := f[:len(f)-3] - opts2 := xml2.RendererOptions{ - Flags: xml2.CommonFlags | xml2.XMLFragment, + opts := html.RendererOptions{ + RenderNodeHook: mhtml.RenderHook, } - renderer2 := xml2.NewRenderer(opts2) - doRenderTest(t, dir, base, renderer2) + renderer := html.NewRenderer(opts) + doRenderTest(t, dir, base, renderer) + } +} + +// TestMarkdown parses the RFC in the rfc/ directory to markdown. +func TestMarkdown(t *testing.T) { + for _, f := range testFiles { + base := f[:len(f)-3] + + opts := mmarkdown.RendererOptions{} + renderer := mmarkdown.NewRenderer(opts) + doRenderTest(t, dir, base, renderer) } } @@ -68,7 +91,7 @@ func doRenderTest(t *testing.T, dir, basename string, renderer markdown.Renderer init := mparser.NewInitial(filename) - p := parser.NewWithExtensions(Extensions) + p := parser.NewWithExtensions(mparser.Extensions) p.Opts = parser.ParserOptions{ ParserHook: func(data []byte) (ast.Node, []byte, int) { node, data, consumed := mparser.Hook(data) @@ -81,10 +104,13 @@ func doRenderTest(t *testing.T, dir, basename string, renderer markdown.Renderer } doc := markdown.Parse(input, p) - addBibliography(doc) - addIndex(doc) + mparser.AddBibliography(doc) + mparser.AddIndex(doc) rfcdata := markdown.Render(doc, renderer) + if !doXMLRFC { + return + } switch renderer.(type) { case *xml.Renderer: