diff --git a/vimdoc.go b/vimdoc.go index 6464f28..9c504f5 100644 --- a/vimdoc.go +++ b/vimdoc.go @@ -25,6 +25,7 @@ package main import ( "bytes" "fmt" + "go/doc" "log" "strings" @@ -40,16 +41,16 @@ func indent(text []byte) []byte { return nil } -type listCtx struct { +type listItem struct { style, index int } type vimDoc struct { - lists []*listCtx + lists []*listItem } func (v *vimDoc) pushList(style int) { - v.lists = append(v.lists, &listCtx{style, 1}) + v.lists = append(v.lists, &listItem{style, 1}) } func (v *vimDoc) popList() { @@ -60,7 +61,7 @@ func (v *vimDoc) popList() { v.lists = v.lists[:len(v.lists)-1] } -func (v *vimDoc) getList() *listCtx { +func (v *vimDoc) getList() *listItem { if len(v.lists) == 0 { log.Fatal("invalid list operation") } @@ -72,33 +73,54 @@ func VimDocRenderer() blackfriday.Renderer { return &vimDoc{} } +func (*vimDoc) hrule(out *bytes.Buffer, repeat string) { + out.WriteString(strings.Repeat(repeat, 80)) + out.WriteString("\n") +} + +func (*vimDoc) formatText(out *bytes.Buffer, text string, level int) { + doc.ToText(out, string(text), strings.Repeat(" ", 4*level), "", 80) +} + // Block-level callbacks -func (*vimDoc) BlockCode(out *bytes.Buffer, text []byte, lang string) { +func (v *vimDoc) BlockCode(out *bytes.Buffer, text []byte, lang string) { out.WriteString(">\n") - out.Write(text) - out.WriteString("<\n") + v.formatText(out, string(text), 1) + out.WriteString("<\n\n") } -func (*vimDoc) BlockQuote(out *bytes.Buffer, text []byte) { +func (v *vimDoc) BlockQuote(out *bytes.Buffer, text []byte) { out.WriteString(">\n") - out.Write(text) - out.WriteString("<\n") + v.formatText(out, string(text), 1) + out.WriteString("<\n\n") } -func (*vimDoc) BlockHtml(out *bytes.Buffer, text []byte) { +func (v *vimDoc) BlockHtml(out *bytes.Buffer, text []byte) { out.WriteString(">\n") - out.Write(text) - out.WriteString("<\n") + v.formatText(out, string(text), 1) + out.WriteString("<\n\n") } -func (*vimDoc) Header(out *bytes.Buffer, text func() bool, level int, id string) { - if text() { - out.WriteString(" ~\n") +func (v *vimDoc) Header(out *bytes.Buffer, text func() bool, level int, id string) { + marker := out.Len() + + switch level { + case 1: + v.hrule(out, "=") + case 2: + v.hrule(out, "-") } + + if !text() { + out.Truncate(marker) + return + } + + out.WriteString(" ~\n\n") } -func (*vimDoc) HRule(out *bytes.Buffer) { - out.WriteString(strings.Repeat("=", 80)) +func (v *vimDoc) HRule(out *bytes.Buffer) { + v.hrule(out, "-") } func (v *vimDoc) List(out *bytes.Buffer, text func() bool, flags int) { @@ -106,52 +128,72 @@ func (v *vimDoc) List(out *bytes.Buffer, text func() bool, flags int) { if flags&blackfriday.LIST_TYPE_ORDERED == blackfriday.LIST_TYPE_ORDERED { style = LIST_STYLE_ORDERED } + v.pushList(style) - text() - v.popList() } func (v *vimDoc) ListItem(out *bytes.Buffer, text []byte, flags int) { list := v.getList() - out.WriteString(fmt.Sprintf("\n%d.", list.index)) + + if list.style == LIST_STYLE_ORDERED { + out.WriteString(fmt.Sprintf("%d. ", list.index)) + list.index++ + } else { + out.WriteString("* ") + } + out.Write(text) - list.index++ + + out.WriteString("\n") + if flags&blackfriday.LIST_ITEM_END_OF_LIST == blackfriday.LIST_ITEM_END_OF_LIST { + out.WriteString("\n") + } } func (*vimDoc) Paragraph(out *bytes.Buffer, text func() bool) { marker := out.Len() - out.WriteString("\n") if !text() { out.Truncate(marker) return } - out.WriteString("\n") + out.WriteString("\n\n") } func (*vimDoc) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) { + // unimplemented + log.Println("Table is a stub") } func (*vimDoc) TableRow(out *bytes.Buffer, text []byte) { + // unimplemented + log.Println("TableRow is a stub") } func (*vimDoc) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) { + // unimplemented + log.Println("TableHeaderCell is a stub") } func (*vimDoc) TableCell(out *bytes.Buffer, text []byte, flags int) { + // unimplemented + log.Println("TableCell is a stub") } func (*vimDoc) Footnotes(out *bytes.Buffer, text func() bool) { - text() + // unimplemented + log.Println("Footnotes is a stub") } func (*vimDoc) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) { - out.Write(text) + // unimplemented + log.Println("FootnoteItem is a stub") } func (*vimDoc) TitleBlock(out *bytes.Buffer, text []byte) { - out.Write(text) + // unimplemented + log.Println("TitleBlock is a stub") } // Span-level callbacks @@ -172,7 +214,8 @@ func (*vimDoc) Emphasis(out *bytes.Buffer, text []byte) { } func (*vimDoc) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) { - // not implemented + // unimplemented + log.Println("Image is a stub") } func (*vimDoc) LineBreak(out *bytes.Buffer) { @@ -192,29 +235,35 @@ func (*vimDoc) TripleEmphasis(out *bytes.Buffer, text []byte) { } func (*vimDoc) StrikeThrough(out *bytes.Buffer, text []byte) { - out.Write(text) + // unimplemented + log.Println("StrikeThrough is a stub") } func (*vimDoc) FootnoteRef(out *bytes.Buffer, ref []byte, id int) { - // not implemented + // unimplemented + log.Println("FootnoteRef is a stub") } // Low-level callbacks -func (*vimDoc) Entity(out *bytes.Buffer, entity []byte) { +func (v *vimDoc) Entity(out *bytes.Buffer, entity []byte) { out.Write(entity) + // v.formatText(out, string(entity), 0) } -func (*vimDoc) NormalText(out *bytes.Buffer, text []byte) { +func (v *vimDoc) NormalText(out *bytes.Buffer, text []byte) { out.Write(text) + // v.formatText(out, string(text), 0) } // Header and footer func (*vimDoc) DocumentHeader(out *bytes.Buffer) { - // not implemented + // unimplemented + log.Println("DocumentHeader is a stub") } func (*vimDoc) DocumentFooter(out *bytes.Buffer) { - // not implemented + // unimplemented + log.Println("DocumentFooter is a stub") } func (*vimDoc) GetFlags() int { diff --git a/vimdown.go b/vimdown.go index 27737fe..3f9d1be 100644 --- a/vimdown.go +++ b/vimdown.go @@ -63,11 +63,8 @@ func main() { renderer := VimDocRenderer() extensions := 0 - extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS - extensions |= blackfriday.EXTENSION_TABLES extensions |= blackfriday.EXTENSION_FENCED_CODE - extensions |= blackfriday.EXTENSION_AUTOLINK - extensions |= blackfriday.EXTENSION_STRIKETHROUGH + extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS extensions |= blackfriday.EXTENSION_SPACE_HEADERS output := blackfriday.Markdown(input, renderer, extensions)