mirror of https://github.com/docker/cli.git
vendor: cpuguy83/go-md2man/v2 v2.0.1
full diff: https://github.com/cpuguy83/go-md2man/compare/v2.0.0...v2.0.1
- Fix handling multiple definition descriptions
- Fix inline markup causing table cells to split
- Remove escaping tilde character (prevents tildes (`~`) from disappearing).
- Do not escape dash, underscore, and ampersand (prevents ampersands (`&`) from disappearing).
- Ignore unknown HTML tags to prevent noisy warnings
With this, generating manpages becomes a lot less noisy; no more of these:
WARNING: go-md2man does not handle node type HTMLSpan
WARNING: go-md2man does not handle node type HTMLSpan
WARNING: go-md2man does not handle node type HTMLSpan
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit 13e8225007
)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
f63cb8b97e
commit
260ba1a8a2
|
@ -8,7 +8,7 @@ github.com/containerd/continuity efbc4488d8fe1bdc16bde3b2d299
|
||||||
github.com/containerd/cgroups 0b889c03f102012f1d93a97ddd3ef71cd6f4f510
|
github.com/containerd/cgroups 0b889c03f102012f1d93a97ddd3ef71cd6f4f510
|
||||||
github.com/containerd/typeurl cd3ce7159eae562a4f60ceff37dada11a939d247 # v1.0.1
|
github.com/containerd/typeurl cd3ce7159eae562a4f60ceff37dada11a939d247 # v1.0.1
|
||||||
github.com/coreos/etcd d57e8b8d97adfc4a6c224fe116714bf1a1f3beb9 # v3.3.12
|
github.com/coreos/etcd d57e8b8d97adfc4a6c224fe116714bf1a1f3beb9 # v3.3.12
|
||||||
github.com/cpuguy83/go-md2man/v2 f79a8a8ca69da163eee19ab442bedad7a35bba5a # v2.0.0
|
github.com/cpuguy83/go-md2man/v2 b1ec32e02fe539480dc03e3bf381c20066e7c6cc # v2.0.1
|
||||||
github.com/creack/pty 2a38352e8b4d7ab6c336eef107e42a55e72e7fbc # v1.1.11
|
github.com/creack/pty 2a38352e8b4d7ab6c336eef107e42a55e72e7fbc # v1.1.11
|
||||||
github.com/davecgh/go-spew 8991bc29aa16c548c550c7ff78260e27b9ab7c73 # v1.1.1
|
github.com/davecgh/go-spew 8991bc29aa16c548c550c7ff78260e27b9ab7c73 # v1.1.1
|
||||||
github.com/docker/compose-on-kubernetes 78e6a00beda64ac8ccb9fec787e601fe2ce0d5bb # v0.5.0-alpha1
|
github.com/docker/compose-on-kubernetes 78e6a00beda64ac8ccb9fec787e601fe2ce0d5bb # v0.5.0-alpha1
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
module github.com/cpuguy83/go-md2man/v2
|
module github.com/cpuguy83/go-md2man/v2
|
||||||
|
|
||||||
go 1.12
|
go 1.11
|
||||||
|
|
||||||
require (
|
require github.com/russross/blackfriday/v2 v2.1.0
|
||||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
|
||||||
github.com/russross/blackfriday/v2 v2.0.1
|
|
||||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
|
||||||
)
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ type roffRenderer struct {
|
||||||
extensions blackfriday.Extensions
|
extensions blackfriday.Extensions
|
||||||
listCounters []int
|
listCounters []int
|
||||||
firstHeader bool
|
firstHeader bool
|
||||||
defineTerm bool
|
firstDD bool
|
||||||
listDepth int
|
listDepth int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,7 +42,8 @@ const (
|
||||||
quoteCloseTag = "\n.RE\n"
|
quoteCloseTag = "\n.RE\n"
|
||||||
listTag = "\n.RS\n"
|
listTag = "\n.RS\n"
|
||||||
listCloseTag = "\n.RE\n"
|
listCloseTag = "\n.RE\n"
|
||||||
arglistTag = "\n.TP\n"
|
dtTag = "\n.TP\n"
|
||||||
|
dd2Tag = "\n"
|
||||||
tableStart = "\n.TS\nallbox;\n"
|
tableStart = "\n.TS\nallbox;\n"
|
||||||
tableEnd = ".TE\n"
|
tableEnd = ".TE\n"
|
||||||
tableCellStart = "T{\n"
|
tableCellStart = "T{\n"
|
||||||
|
@ -90,7 +91,7 @@ func (r *roffRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
|
||||||
|
|
||||||
switch node.Type {
|
switch node.Type {
|
||||||
case blackfriday.Text:
|
case blackfriday.Text:
|
||||||
r.handleText(w, node, entering)
|
escapeSpecialChars(w, node.Literal)
|
||||||
case blackfriday.Softbreak:
|
case blackfriday.Softbreak:
|
||||||
out(w, crTag)
|
out(w, crTag)
|
||||||
case blackfriday.Hardbreak:
|
case blackfriday.Hardbreak:
|
||||||
|
@ -150,40 +151,21 @@ func (r *roffRenderer) RenderNode(w io.Writer, node *blackfriday.Node, entering
|
||||||
out(w, codeCloseTag)
|
out(w, codeCloseTag)
|
||||||
case blackfriday.Table:
|
case blackfriday.Table:
|
||||||
r.handleTable(w, node, entering)
|
r.handleTable(w, node, entering)
|
||||||
case blackfriday.TableCell:
|
|
||||||
r.handleTableCell(w, node, entering)
|
|
||||||
case blackfriday.TableHead:
|
case blackfriday.TableHead:
|
||||||
case blackfriday.TableBody:
|
case blackfriday.TableBody:
|
||||||
case blackfriday.TableRow:
|
case blackfriday.TableRow:
|
||||||
// no action as cell entries do all the nroff formatting
|
// no action as cell entries do all the nroff formatting
|
||||||
return blackfriday.GoToNext
|
return blackfriday.GoToNext
|
||||||
|
case blackfriday.TableCell:
|
||||||
|
r.handleTableCell(w, node, entering)
|
||||||
|
case blackfriday.HTMLSpan:
|
||||||
|
// ignore other HTML tags
|
||||||
default:
|
default:
|
||||||
fmt.Fprintln(os.Stderr, "WARNING: go-md2man does not handle node type "+node.Type.String())
|
fmt.Fprintln(os.Stderr, "WARNING: go-md2man does not handle node type "+node.Type.String())
|
||||||
}
|
}
|
||||||
return walkAction
|
return walkAction
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *roffRenderer) handleText(w io.Writer, node *blackfriday.Node, entering bool) {
|
|
||||||
var (
|
|
||||||
start, end string
|
|
||||||
)
|
|
||||||
// handle special roff table cell text encapsulation
|
|
||||||
if node.Parent.Type == blackfriday.TableCell {
|
|
||||||
if len(node.Literal) > 30 {
|
|
||||||
start = tableCellStart
|
|
||||||
end = tableCellEnd
|
|
||||||
} else {
|
|
||||||
// end rows that aren't terminated by "tableCellEnd" with a cr if end of row
|
|
||||||
if node.Parent.Next == nil && !node.Parent.IsHeader {
|
|
||||||
end = crTag
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out(w, start)
|
|
||||||
escapeSpecialChars(w, node.Literal)
|
|
||||||
out(w, end)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *roffRenderer) handleHeading(w io.Writer, node *blackfriday.Node, entering bool) {
|
func (r *roffRenderer) handleHeading(w io.Writer, node *blackfriday.Node, entering bool) {
|
||||||
if entering {
|
if entering {
|
||||||
switch node.Level {
|
switch node.Level {
|
||||||
|
@ -230,15 +212,20 @@ func (r *roffRenderer) handleItem(w io.Writer, node *blackfriday.Node, entering
|
||||||
if node.ListFlags&blackfriday.ListTypeOrdered != 0 {
|
if node.ListFlags&blackfriday.ListTypeOrdered != 0 {
|
||||||
out(w, fmt.Sprintf(".IP \"%3d.\" 5\n", r.listCounters[len(r.listCounters)-1]))
|
out(w, fmt.Sprintf(".IP \"%3d.\" 5\n", r.listCounters[len(r.listCounters)-1]))
|
||||||
r.listCounters[len(r.listCounters)-1]++
|
r.listCounters[len(r.listCounters)-1]++
|
||||||
|
} else if node.ListFlags&blackfriday.ListTypeTerm != 0 {
|
||||||
|
// DT (definition term): line just before DD (see below).
|
||||||
|
out(w, dtTag)
|
||||||
|
r.firstDD = true
|
||||||
} else if node.ListFlags&blackfriday.ListTypeDefinition != 0 {
|
} else if node.ListFlags&blackfriday.ListTypeDefinition != 0 {
|
||||||
// state machine for handling terms and following definitions
|
// DD (definition description): line that starts with ": ".
|
||||||
// since blackfriday does not distinguish them properly, nor
|
//
|
||||||
// does it seperate them into separate lists as it should
|
// We have to distinguish between the first DD and the
|
||||||
if !r.defineTerm {
|
// subsequent ones, as there should be no vertical
|
||||||
out(w, arglistTag)
|
// whitespace between the DT and the first DD.
|
||||||
r.defineTerm = true
|
if r.firstDD {
|
||||||
|
r.firstDD = false
|
||||||
} else {
|
} else {
|
||||||
r.defineTerm = false
|
out(w, dd2Tag)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
out(w, ".IP \\(bu 2\n")
|
out(w, ".IP \\(bu 2\n")
|
||||||
|
@ -261,28 +248,41 @@ func (r *roffRenderer) handleTable(w io.Writer, node *blackfriday.Node, entering
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *roffRenderer) handleTableCell(w io.Writer, node *blackfriday.Node, entering bool) {
|
func (r *roffRenderer) handleTableCell(w io.Writer, node *blackfriday.Node, entering bool) {
|
||||||
var (
|
|
||||||
start, end string
|
|
||||||
)
|
|
||||||
if node.IsHeader {
|
|
||||||
start = codespanTag
|
|
||||||
end = codespanCloseTag
|
|
||||||
}
|
|
||||||
if entering {
|
if entering {
|
||||||
|
var start string
|
||||||
if node.Prev != nil && node.Prev.Type == blackfriday.TableCell {
|
if node.Prev != nil && node.Prev.Type == blackfriday.TableCell {
|
||||||
out(w, "\t"+start)
|
start = "\t"
|
||||||
} else {
|
|
||||||
out(w, start)
|
|
||||||
}
|
}
|
||||||
|
if node.IsHeader {
|
||||||
|
start += codespanTag
|
||||||
|
} else if nodeLiteralSize(node) > 30 {
|
||||||
|
start += tableCellStart
|
||||||
|
}
|
||||||
|
out(w, start)
|
||||||
} else {
|
} else {
|
||||||
// need to carriage return if we are at the end of the header row
|
var end string
|
||||||
if node.IsHeader && node.Next == nil {
|
if node.IsHeader {
|
||||||
end = end + crTag
|
end = codespanCloseTag
|
||||||
|
} else if nodeLiteralSize(node) > 30 {
|
||||||
|
end = tableCellEnd
|
||||||
|
}
|
||||||
|
if node.Next == nil && end != tableCellEnd {
|
||||||
|
// Last cell: need to carriage return if we are at the end of the
|
||||||
|
// header row and content isn't wrapped in a "tablecell"
|
||||||
|
end += crTag
|
||||||
}
|
}
|
||||||
out(w, end)
|
out(w, end)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func nodeLiteralSize(node *blackfriday.Node) int {
|
||||||
|
total := 0
|
||||||
|
for n := node.FirstChild; n != nil; n = n.FirstChild {
|
||||||
|
total += len(n.Literal)
|
||||||
|
}
|
||||||
|
return total
|
||||||
|
}
|
||||||
|
|
||||||
// because roff format requires knowing the column count before outputting any table
|
// because roff format requires knowing the column count before outputting any table
|
||||||
// data we need to walk a table tree and count the columns
|
// data we need to walk a table tree and count the columns
|
||||||
func countColumns(node *blackfriday.Node) int {
|
func countColumns(node *blackfriday.Node) int {
|
||||||
|
@ -309,15 +309,6 @@ func out(w io.Writer, output string) {
|
||||||
io.WriteString(w, output) // nolint: errcheck
|
io.WriteString(w, output) // nolint: errcheck
|
||||||
}
|
}
|
||||||
|
|
||||||
func needsBackslash(c byte) bool {
|
|
||||||
for _, r := range []byte("-_&\\~") {
|
|
||||||
if c == r {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func escapeSpecialChars(w io.Writer, text []byte) {
|
func escapeSpecialChars(w io.Writer, text []byte) {
|
||||||
for i := 0; i < len(text); i++ {
|
for i := 0; i < len(text); i++ {
|
||||||
// escape initial apostrophe or period
|
// escape initial apostrophe or period
|
||||||
|
@ -328,7 +319,7 @@ func escapeSpecialChars(w io.Writer, text []byte) {
|
||||||
// directly copy normal characters
|
// directly copy normal characters
|
||||||
org := i
|
org := i
|
||||||
|
|
||||||
for i < len(text) && !needsBackslash(text[i]) {
|
for i < len(text) && text[i] != '\\' {
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
if i > org {
|
if i > org {
|
||||||
|
|
Loading…
Reference in New Issue