Skip to content

Commit

Permalink
renderer/html: fix outputting references (#47)
Browse files Browse the repository at this point in the history
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 <miek@miek.nl>
  • Loading branch information
miekg committed Jan 2, 2019
1 parent 03f9873 commit e16365c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
5 changes: 4 additions & 1 deletion render/mhtml/hook.go
Expand Up @@ -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, `<dt class="bibliography-cite" id="`+string(bib.Anchor)+`">`+fmt.Sprintf("[%s]", bib.Anchor)+"</dt>\n")
io.WriteString(w, `<dd>`)
defer io.WriteString(w, "</dd>\n")
if bib.Reference == nil {
return
}
io.WriteString(w, `<span class="bibliography-author">`+bib.Reference.Front.Author.Fullname+"</span>\n")
io.WriteString(w, `<span class="bibliography-title">`+bib.Reference.Front.Title+"</span>\n")
if bib.Reference.Format != nil && bib.Reference.Format.Target != "" {
Expand All @@ -107,7 +111,6 @@ func bibliographyItem(w io.Writer, bib *mast.BibliographyItem, entering bool) {
if bib.Reference.Front.Date.Year != "" {
io.WriteString(w, `<date class="bibliography-date">`+bib.Reference.Front.Date.Year+"</date>\n")
}
io.WriteString(w, "</dd>\n")
}

func firstSubItem(node ast.Node) bool {
Expand Down
68 changes: 47 additions & 21 deletions rfc_test.go
@@ -1,5 +1,3 @@
// +build xml2rfc

package main

import (
Expand All @@ -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]

Expand All @@ -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)
}
}

Expand All @@ -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)
Expand All @@ -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:
Expand Down

0 comments on commit e16365c

Please sign in to comment.