", n.Index, len(tabs))
for i := range tabs {
checked := ""
if i == selected {
checked = ` checked="checked"`
}
fmt.Fprintf(w, "
\n")
return ast.WalkContinue, nil
}
func renderTab(w util.BufWriter, _ []byte, _ ast.Node, entering bool) (ast.WalkStatus, error) {
if entering {
w.WriteString("
\n")
} else {
w.WriteString("
\n")
}
return ast.WalkContinue, nil
}
func (r docsRenderer) renderKeys(w util.BufWriter, _ []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
n := node.(*keys)
w.WriteString(`
`)
for i, k := range n.Keys {
if i > 0 {
w.WriteString("+")
}
fmt.Fprintf(w, `%s`, html.EscapeString(keyName(k)), html.EscapeString(r.keyLabel(k)))
}
w.WriteString(``)
return ast.WalkSkipChildren, nil
}
// --- Markdown inside HTML blocks ---------------------------------------------
// mdInHTML lets a block of HTML hold Markdown, as md_in_html does: an
// opening tag on a line of its own carrying a markdown attribute,
//
//
// A **paragraph**, a list, anything.
//
//
// becomes the tag without the attribute, a blank line, the Markdown
// (dedented), a blank line and the closing tag. CommonMark ends an HTML block
// at a blank line, so what's between is read as Markdown again. Only for
// sites whose HTML is trusted or sanitized; otherwise the HTML is dropped
// anyway. Tags whose closing tag isn't on a line of its own are left as they
// are.
var (
mdOpenRe = regexp.MustCompile(`^( {0,3})<([a-zA-Z][a-zA-Z0-9]*)\b([^>]*?)\s+markdown(?:=(?:"[^"]*"|'[^']*'|\S+))?([^>]*)>\s*$`)
mdAttrRe = regexp.MustCompile(`\s+markdown(?:=(?:"[^"]*"|'[^']*'|[^\s>]+))?`)
fenceLine = regexp.MustCompile("^\\s*(```|~~~)")
)
func mdInHTML(src []byte) []byte {
if !bytes.Contains(src, []byte("markdown")) {
return src
}
lines := strings.SplitAfter(string(src), "\n")
return []byte(strings.Join(mdInHTMLLines(lines), ""))
}
func mdInHTMLLines(lines []string) []string {
var out []string
inFence := false
for i := 0; i < len(lines); i++ {
line := lines[i]
if fenceLine.MatchString(line) {
inFence = !inFence
}
m := mdOpenRe.FindStringSubmatch(strings.TrimRight(line, "\r\n"))
if inFence || m == nil {
out = append(out, line)
continue
}
tag := strings.ToLower(m[2])
openRe := regexp.MustCompile(`(?i)<` + tag + `\b[^>]*[^/]>|<` + tag + `>`)
closeRe := regexp.MustCompile(`(?i)` + tag + `\s*>`)
depth, end := 1, -1
for j := i + 1; j < len(lines); j++ {
depth += len(openRe.FindAllString(lines[j], -1)) - len(closeRe.FindAllString(lines[j], -1))
if depth == 0 {
if strings.TrimSpace(closeRe.ReplaceAllString(lines[j], "")) == "" {
end = j
}
break
}
}
if end < 0 {
out = append(out, line)
continue
}
inner := mdInHTMLLines(dedent(lines[i+1 : end]))
out = append(out, mdAttrRe.ReplaceAllString(strings.TrimRight(line, "\r\n"), "")+"\n", "\n")
out = append(out, inner...)
if len(inner) > 0 && !strings.HasSuffix(inner[len(inner)-1], "\n") {
out = append(out, "\n")
}
out = append(out, "\n", lines[end])
i = end
}
return out
}
// dedent removes the indentation every non-blank line shares.
func dedent(lines []string) []string {
common := -1
for _, l := range lines {
if strings.TrimSpace(l) == "" {
continue
}
n := len(l) - len(strings.TrimLeft(l, " \t"))
if common < 0 || n < common {
common = n
}
}
if common <= 0 {
return lines
}
out := make([]string, len(lines))
for i, l := range lines {
if len(l) >= common && strings.TrimSpace(l[:common]) == "" {
out[i] = l[common:]
} else {
out[i] = strings.TrimLeft(l, " \t")
}
}
return out
}