-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation on the shape of the markdown AST produced by the blackfriday parser is rather sparse. Add a decorator for renderers which dumps the visited AST nodes to stderr, interleaved with the renderer output, to help with debugging the roff renderer. Signed-off-by: Cory Snider <[email protected]>
- Loading branch information
Showing
2 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package md2man | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/russross/blackfriday/v2" | ||
) | ||
|
||
func fmtListFlags(flags blackfriday.ListType) string { | ||
knownFlags := []struct { | ||
name string | ||
flag blackfriday.ListType | ||
}{ | ||
{"ListTypeOrdered", blackfriday.ListTypeOrdered}, | ||
{"ListTypeDefinition", blackfriday.ListTypeDefinition}, | ||
{"ListTypeTerm", blackfriday.ListTypeTerm}, | ||
{"ListItemContainsBlock", blackfriday.ListItemContainsBlock}, | ||
{"ListItemBeginningOfList", blackfriday.ListItemBeginningOfList}, | ||
{"ListItemEndOfList", blackfriday.ListItemEndOfList}, | ||
} | ||
|
||
var f []string | ||
for _, kf := range knownFlags { | ||
if flags&kf.flag != 0 { | ||
f = append(f, kf.name) | ||
flags &^= kf.flag | ||
} | ||
} | ||
if flags != 0 { | ||
f = append(f, fmt.Sprintf("Unknown(%#x)", flags)) | ||
} | ||
return strings.Join(f, "|") | ||
} | ||
|
||
type debugDecorator struct { | ||
blackfriday.Renderer | ||
} | ||
|
||
func depth(node *blackfriday.Node) int { | ||
d := 0 | ||
for n := node.Parent; n != nil; n = n.Parent { | ||
d++ | ||
} | ||
return d | ||
} | ||
|
||
func (d *debugDecorator) RenderNode(w io.Writer, node *blackfriday.Node, entering bool) blackfriday.WalkStatus { | ||
fmt.Fprintf(os.Stderr, "%s%s %v %v\n", | ||
strings.Repeat(" ", depth(node)), | ||
map[bool]string{true: "+", false: "-"}[entering], | ||
node, | ||
fmtListFlags(node.ListFlags)) | ||
var b strings.Builder | ||
status := d.Renderer.RenderNode(io.MultiWriter(&b, w), node, entering) | ||
if b.Len() > 0 { | ||
fmt.Fprintf(os.Stderr, ">> %q\n", b.String()) | ||
} | ||
return status | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
package md2man | ||
|
||
import ( | ||
"os" | ||
"strconv" | ||
|
||
"github.com/russross/blackfriday/v2" | ||
) | ||
|
||
// Render converts a markdown document into a roff formatted document. | ||
func Render(doc []byte) []byte { | ||
renderer := NewRoffRenderer() | ||
var r blackfriday.Renderer = renderer | ||
if v, _ := strconv.ParseBool(os.Getenv("MD2MAN_DEBUG")); v { | ||
r = &debugDecorator{Renderer: r} | ||
} | ||
|
||
return blackfriday.Run(doc, | ||
[]blackfriday.Option{ | ||
blackfriday.WithRenderer(renderer), | ||
blackfriday.WithRenderer(r), | ||
blackfriday.WithExtensions(renderer.GetExtensions()), | ||
}...) | ||
} |