This commit is contained in:
Alex Yatskov 2015-08-09 14:57:43 +09:00
parent 200ffa63ca
commit cb8b6ed112
2 changed files with 36 additions and 36 deletions

View File

@ -48,10 +48,10 @@ type list struct {
index int index int
} }
type header struct { type heading struct {
text []byte text []byte
level int level int
children []*header children []*heading
} }
type vimDoc struct { type vimDoc struct {
@ -63,8 +63,8 @@ type vimDoc struct {
flags int flags int
tocPos int tocPos int
lists []*list lists []*list
rootHead *header rootHead *heading
lastHead *header lastHead *heading
} }
func VimDocRenderer(filename, desc string, cols, tabs, flags int) blackfriday.Renderer { func VimDocRenderer(filename, desc string, cols, tabs, flags int) blackfriday.Renderer {
@ -113,20 +113,20 @@ func (v *vimDoc) fixupCode(input []byte) []byte {
return r.ReplaceAll(input, []byte("$1")) return r.ReplaceAll(input, []byte("$1"))
} }
func (v *vimDoc) fixupHeader(header []byte) []byte { func (v *vimDoc) fixupHeader(text []byte) []byte {
return bytes.ToUpper(header) return bytes.ToUpper(text)
} }
func (v *vimDoc) buildTag(header []byte) []byte { func (v *vimDoc) buildTag(text []byte) []byte {
if v.flags&FLAG_PASCAL == 0 { if v.flags&FLAG_PASCAL == 0 {
header = bytes.ToLower(header) text = bytes.ToLower(text)
header = bytes.Replace(header, []byte{' '}, []byte{'_'}, -1) text = bytes.Replace(text, []byte{' '}, []byte{'_'}, -1)
} else { } else {
header = bytes.Title(header) text = bytes.Title(text)
header = bytes.Replace(header, []byte{' '}, []byte{}, -1) text = bytes.Replace(text, []byte{' '}, []byte{}, -1)
} }
return []byte(fmt.Sprintf("%s-%s", v.title, header)) return []byte(fmt.Sprintf("%s-%s", v.title, text))
} }
func (v *vimDoc) writeStraddle(out *bytes.Buffer, left, right []byte, trim int) { func (v *vimDoc) writeStraddle(out *bytes.Buffer, left, right []byte, trim int) {
@ -146,13 +146,13 @@ func (v *vimDoc) writeRule(out *bytes.Buffer, repeat string) {
out.WriteString("\n") out.WriteString("\n")
} }
func (v *vimDoc) writeToc(out *bytes.Buffer, h *header, depth int) { func (v *vimDoc) writeToc(out *bytes.Buffer, head *heading, depth int) {
title := fmt.Sprintf("%s%s", strings.Repeat(" ", depth*v.tabs), h.text) title := fmt.Sprintf("%s%s", strings.Repeat(" ", depth*v.tabs), head.text)
link := fmt.Sprintf("|%s|", v.buildTag(h.text)) link := fmt.Sprintf("|%s|", v.buildTag(head.text))
v.writeStraddle(out, []byte(title), []byte(link), 2) v.writeStraddle(out, []byte(title), []byte(link), 2)
for _, c := range h.children { for _, child := range head.children {
v.writeToc(out, c, depth+1) v.writeToc(out, child, depth+1)
} }
} }
@ -204,7 +204,7 @@ func (v *vimDoc) Header(out *bytes.Buffer, text func() bool, level int, id strin
} }
} }
headerPos := out.Len() headingPos := out.Len()
if !text() { if !text() {
out.Truncate(initPos) out.Truncate(initPos)
@ -216,31 +216,31 @@ func (v *vimDoc) Header(out *bytes.Buffer, text func() bool, level int, id strin
} }
var value []byte var value []byte
value = append(value, out.Bytes()[headerPos:]...) value = append(value, out.Bytes()[headingPos:]...)
header := &header{value, level, nil} heading := &heading{value, level, nil}
if v.lastHead == nil { if v.lastHead == nil {
if header.level != 1 { if heading.level != 1 {
log.Println("warning: top-level header in document is not a level 1 header") log.Println("warning: top-level heading in document is not a level 1 heading")
} }
v.rootHead = header v.rootHead = heading
v.lastHead = header v.lastHead = heading
} else { } else {
if v.rootHead.level >= header.level { if v.rootHead.level >= heading.level {
log.Println("warning: found header of higher or equal level to the root header") log.Println("warning: found heading of higher or equal level to the root heading")
} }
if header.level <= v.lastHead.level { if heading.level <= v.lastHead.level {
v.lastHead = header v.lastHead = heading
} else { } else {
v.lastHead.children = append(v.lastHead.children, header) v.lastHead.children = append(v.lastHead.children, heading)
} }
} }
out.Truncate(headerPos) out.Truncate(headingPos)
tag := fmt.Sprintf("*%s*", v.buildTag(header.text)) tag := fmt.Sprintf("*%s*", v.buildTag(heading.text))
v.writeStraddle(out, v.fixupHeader(header.text), []byte(tag), 2) v.writeStraddle(out, v.fixupHeader(heading.text), []byte(tag), 2)
out.WriteString("\n") out.WriteString("\n")
} }
@ -283,7 +283,7 @@ func (*vimDoc) Paragraph(out *bytes.Buffer, text func() bool) {
out.WriteString("\n\n") out.WriteString("\n\n")
} }
func (*vimDoc) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) { func (*vimDoc) Table(out *bytes.Buffer, heading []byte, body []byte, columnData []int) {
// unimplemented // unimplemented
log.Println("warning: Table is a stub") log.Println("warning: Table is a stub")
} }

View File

@ -40,12 +40,12 @@ func usage() {
} }
func main() { func main() {
cols := flag.Int("cols", DEFAULT_NUM_COLUMNS, "number of columns to use for alignment and rules") cols := flag.Int("cols", DEFAULT_NUM_COLUMNS, "number of columns to use for layout")
tabs := flag.Int("tabs", DEFAULT_TAB_SIZE, "size of the tab to use specified as number of spaces") tabs := flag.Int("tabs", DEFAULT_TAB_SIZE, "tab width specified in number of spaces")
notoc := flag.Bool("notoc", false, "do not generate table of contents for headings") notoc := flag.Bool("notoc", false, "do not generate table of contents for headings")
norules := flag.Bool("norules", false, "do not generate horizontal rules above headings") norules := flag.Bool("norules", false, "do not generate horizontal rules above headings")
pascal := flag.Bool("pascal", false, "use PascalCase for abbreviating tags") pascal := flag.Bool("pascal", false, "use PascalCase for abbreviating tags")
desc := flag.String("desc", "", "short description of extension") desc := flag.String("desc", "", "short description of the help file")
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()