This commit is contained in:
Alex Yatskov 2015-08-08 12:09:12 +09:00
parent 2289cfa305
commit 76c8629326
2 changed files with 84 additions and 38 deletions

117
vimdoc.go
View File

@ -25,6 +25,7 @@ package main
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"go/doc"
"log" "log"
"strings" "strings"
@ -40,16 +41,16 @@ func indent(text []byte) []byte {
return nil return nil
} }
type listCtx struct { type listItem struct {
style, index int style, index int
} }
type vimDoc struct { type vimDoc struct {
lists []*listCtx lists []*listItem
} }
func (v *vimDoc) pushList(style int) { 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() { func (v *vimDoc) popList() {
@ -60,7 +61,7 @@ func (v *vimDoc) popList() {
v.lists = v.lists[:len(v.lists)-1] v.lists = v.lists[:len(v.lists)-1]
} }
func (v *vimDoc) getList() *listCtx { func (v *vimDoc) getList() *listItem {
if len(v.lists) == 0 { if len(v.lists) == 0 {
log.Fatal("invalid list operation") log.Fatal("invalid list operation")
} }
@ -72,33 +73,54 @@ func VimDocRenderer() blackfriday.Renderer {
return &vimDoc{} 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 // 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.WriteString(">\n")
out.Write(text) v.formatText(out, string(text), 1)
out.WriteString("<\n") 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.WriteString(">\n")
out.Write(text) v.formatText(out, string(text), 1)
out.WriteString("<\n") 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.WriteString(">\n")
out.Write(text) v.formatText(out, string(text), 1)
out.WriteString("<\n") out.WriteString("<\n\n")
} }
func (*vimDoc) Header(out *bytes.Buffer, text func() bool, level int, id string) { func (v *vimDoc) Header(out *bytes.Buffer, text func() bool, level int, id string) {
if text() { marker := out.Len()
out.WriteString(" ~\n")
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) { func (v *vimDoc) HRule(out *bytes.Buffer) {
out.WriteString(strings.Repeat("=", 80)) v.hrule(out, "-")
} }
func (v *vimDoc) List(out *bytes.Buffer, text func() bool, flags int) { 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 { if flags&blackfriday.LIST_TYPE_ORDERED == blackfriday.LIST_TYPE_ORDERED {
style = LIST_STYLE_ORDERED style = LIST_STYLE_ORDERED
} }
v.pushList(style) v.pushList(style)
text() text()
v.popList() v.popList()
} }
func (v *vimDoc) ListItem(out *bytes.Buffer, text []byte, flags int) { func (v *vimDoc) ListItem(out *bytes.Buffer, text []byte, flags int) {
list := v.getList() list := v.getList()
out.WriteString(fmt.Sprintf("\n%d.", list.index))
out.Write(text) if list.style == LIST_STYLE_ORDERED {
out.WriteString(fmt.Sprintf("%d. ", list.index))
list.index++ list.index++
} else {
out.WriteString("* ")
}
out.Write(text)
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) { func (*vimDoc) Paragraph(out *bytes.Buffer, text func() bool) {
marker := out.Len() marker := out.Len()
out.WriteString("\n")
if !text() { if !text() {
out.Truncate(marker) out.Truncate(marker)
return return
} }
out.WriteString("\n") out.WriteString("\n\n")
} }
func (*vimDoc) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) { 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) { 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) { 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) { 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) { 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) { 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) { func (*vimDoc) TitleBlock(out *bytes.Buffer, text []byte) {
out.Write(text) // unimplemented
log.Println("TitleBlock is a stub")
} }
// Span-level callbacks // 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) { 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) { 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) { 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) { func (*vimDoc) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
// not implemented // unimplemented
log.Println("FootnoteRef is a stub")
} }
// Low-level callbacks // Low-level callbacks
func (*vimDoc) Entity(out *bytes.Buffer, entity []byte) { func (v *vimDoc) Entity(out *bytes.Buffer, entity []byte) {
out.Write(entity) 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) out.Write(text)
// v.formatText(out, string(text), 0)
} }
// Header and footer // Header and footer
func (*vimDoc) DocumentHeader(out *bytes.Buffer) { func (*vimDoc) DocumentHeader(out *bytes.Buffer) {
// not implemented // unimplemented
log.Println("DocumentHeader is a stub")
} }
func (*vimDoc) DocumentFooter(out *bytes.Buffer) { func (*vimDoc) DocumentFooter(out *bytes.Buffer) {
// not implemented // unimplemented
log.Println("DocumentFooter is a stub")
} }
func (*vimDoc) GetFlags() int { func (*vimDoc) GetFlags() int {

View File

@ -63,11 +63,8 @@ func main() {
renderer := VimDocRenderer() renderer := VimDocRenderer()
extensions := 0 extensions := 0
extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
extensions |= blackfriday.EXTENSION_TABLES
extensions |= blackfriday.EXTENSION_FENCED_CODE extensions |= blackfriday.EXTENSION_FENCED_CODE
extensions |= blackfriday.EXTENSION_AUTOLINK extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
extensions |= blackfriday.EXTENSION_STRIKETHROUGH
extensions |= blackfriday.EXTENSION_SPACE_HEADERS extensions |= blackfriday.EXTENSION_SPACE_HEADERS
output := blackfriday.Markdown(input, renderer, extensions) output := blackfriday.Markdown(input, renderer, extensions)