| 1 |
package md2man |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"os" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/russross/blackfriday/v2" |
| 10 |
) |
| 11 |
|
| 12 |
func fmtListFlags(flags blackfriday.ListType) string { |
| 13 |
knownFlags := []struct { |
| 14 |
name string |
| 15 |
flag blackfriday.ListType |
| 16 |
}{ |
| 17 |
{"ListTypeOrdered", blackfriday.ListTypeOrdered}, |
| 18 |
{"ListTypeDefinition", blackfriday.ListTypeDefinition}, |
| 19 |
{"ListTypeTerm", blackfriday.ListTypeTerm}, |
| 20 |
{"ListItemContainsBlock", blackfriday.ListItemContainsBlock}, |
| 21 |
{"ListItemBeginningOfList", blackfriday.ListItemBeginningOfList}, |
| 22 |
{"ListItemEndOfList", blackfriday.ListItemEndOfList}, |
| 23 |
} |
| 24 |
|
| 25 |
var f []string |
| 26 |
for _, kf := range knownFlags { |
| 27 |
if flags&kf.flag != 0 { |
| 28 |
f = append(f, kf.name) |
| 29 |
flags &^= kf.flag |
| 30 |
} |
| 31 |
} |
| 32 |
if flags != 0 { |
| 33 |
f = append(f, fmt.Sprintf("Unknown(%#x)", flags)) |
| 34 |
} |
| 35 |
return strings.Join(f, "|") |
| 36 |
} |
| 37 |
|
| 38 |
type debugDecorator struct { |
| 39 |
blackfriday.Renderer |
| 40 |
} |
| 41 |
|
| 42 |
func depth(node *blackfriday.Node) int { |
| 43 |
d := 0 |
| 44 |
for n := node.Parent; n != nil; n = n.Parent { |
| 45 |
d++ |
| 46 |
} |
| 47 |
return d |
| 48 |
} |
| 49 |
|
| 50 |
func (d *debugDecorator) RenderNode(w io.Writer, node *blackfriday.Node, entering bool) blackfriday.WalkStatus { |
| 51 |
fmt.Fprintf(os.Stderr, "%s%s %v %v\n", |
| 52 |
strings.Repeat(" ", depth(node)), |
| 53 |
map[bool]string{true: "+", false: "-"}[entering], |
| 54 |
node, |
| 55 |
fmtListFlags(node.ListFlags)) |
| 56 |
var b strings.Builder |
| 57 |
status := d.Renderer.RenderNode(io.MultiWriter(&b, w), node, entering) |
| 58 |
if b.Len() > 0 { |
| 59 |
fmt.Fprintf(os.Stderr, ">> %q\n", b.String()) |
| 60 |
} |
| 61 |
return status |
| 62 |
} |