Work in progress

This commit is contained in:
Alex Yatskov 2015-08-09 13:49:53 +09:00
parent 7b620031e8
commit 7890987d99
2 changed files with 50 additions and 33 deletions

View File

@ -34,6 +34,7 @@ import (
const ( const (
DEFAULT_NUM_COLUMNS = 80 DEFAULT_NUM_COLUMNS = 80
DEFAULT_TAB_SIZE = 4
) )
type list struct { type list struct {
@ -50,12 +51,14 @@ type vimDoc struct {
rootHead *header rootHead *header
lastHead *header lastHead *header
title string title string
cols int
tabs int
tocPos int tocPos int
lists []*list lists []*list
} }
func VimDocRenderer() blackfriday.Renderer { func VimDocRenderer(cols, tabs int) blackfriday.Renderer {
return &vimDoc{title: "test", tocPos: -1} return &vimDoc{title: "test", cols: cols, tabs: tabs, tocPos: -1}
} }
func (v *vimDoc) pushl() { func (v *vimDoc) pushl() {
@ -64,7 +67,7 @@ func (v *vimDoc) pushl() {
func (v *vimDoc) popl() { func (v *vimDoc) popl() {
if len(v.lists) == 0 { if len(v.lists) == 0 {
log.Fatal("invalid list operation") log.Fatal("error: invalid list operation")
} }
v.lists = v.lists[:len(v.lists)-1] v.lists = v.lists[:len(v.lists)-1]
@ -72,7 +75,7 @@ func (v *vimDoc) popl() {
func (v *vimDoc) getl() *list { func (v *vimDoc) getl() *list {
if len(v.lists) == 0 { if len(v.lists) == 0 {
log.Fatal("invalid list operation") log.Fatal("error: invalid list operation")
} }
return v.lists[len(v.lists)-1] return v.lists[len(v.lists)-1]
@ -88,20 +91,30 @@ func (v *vimDoc) fixupHeader(header []byte) []byte {
} }
func (v *vimDoc) buildTag(header []byte) []byte { func (v *vimDoc) buildTag(header []byte) []byte {
return []byte(fmt.Sprintf("%s-%s", v.title, string(bytes.ToLower(header)))) return []byte(fmt.Sprintf("%s-%s", v.title, bytes.ToLower(header)))
} }
func (*vimDoc) writeRule(out *bytes.Buffer, repeat string) { func (v *vimDoc) writeStraddle(out *bytes.Buffer, left, right []byte, trim int) {
out.WriteString(strings.Repeat(repeat, DEFAULT_NUM_COLUMNS)) padding := v.cols - (len(left) + len(right)) + trim
out.Write(left)
if padding > 0 {
out.WriteString(strings.Repeat(" ", padding))
}
out.Write(right)
out.WriteString("\n")
}
func (v *vimDoc) writeRule(out *bytes.Buffer, repeat string) {
out.WriteString(strings.Repeat(repeat, v.cols))
out.WriteString("\n") out.WriteString("\n")
} }
func (v *vimDoc) writeToc(out *bytes.Buffer, h *header, depth int) { func (v *vimDoc) writeToc(out *bytes.Buffer, h *header, depth int) {
out.WriteString(fmt.Sprintf( title := fmt.Sprintf("%s%s:", strings.Repeat(" ", depth*v.tabs), h.text)
"%s%s: |%s|\n", link := fmt.Sprintf("|%s|", v.buildTag(h.text))
strings.Repeat(" ", depth*blackfriday.TAB_SIZE_DEFAULT), v.writeStraddle(out, []byte(title), []byte(link), 2)
string(h.text),
v.buildTag(h.text)))
for _, c := range h.children { for _, c := range h.children {
v.writeToc(out, c, depth+1) v.writeToc(out, c, depth+1)
} }
@ -111,19 +124,17 @@ func (v *vimDoc) format(out *bytes.Buffer, text string, trim int) {
lines := strings.Split(text, "\n") lines := strings.Split(text, "\n")
for index, line := range lines { for index, line := range lines {
width := blackfriday.TAB_SIZE_DEFAULT width := v.tabs
if width >= trim && index == 0 { if width >= trim && index == 0 {
width -= trim width -= trim
} }
if len(line) == 0 { if len(line) > 0 {
continue
}
out.WriteString(strings.Repeat(" ", width)) out.WriteString(strings.Repeat(" ", width))
out.WriteString(line) out.WriteString(line)
out.WriteString("\n") out.WriteString("\n")
} }
}
} }
// Block-level callbacks // Block-level callbacks
@ -190,7 +201,9 @@ func (v *vimDoc) Header(out *bytes.Buffer, text func() bool, level int, id strin
} }
out.Truncate(headerPos) out.Truncate(headerPos)
out.WriteString(fmt.Sprintf("%s\t\t*%s*\n\n", v.fixupHeader(header.text), v.buildTag(header.text))) tag := fmt.Sprintf("*%s*", v.buildTag(header.text))
v.writeStraddle(out, v.fixupHeader(header.text), []byte(tag), 2)
out.WriteString("\n")
} }
func (v *vimDoc) HRule(out *bytes.Buffer) { func (v *vimDoc) HRule(out *bytes.Buffer) {
@ -216,53 +229,55 @@ func (v *vimDoc) ListItem(out *bytes.Buffer, text []byte, flags int) {
v.format(out, string(text), out.Len()-marker) v.format(out, string(text), out.Len()-marker)
if flags&blackfriday.LIST_ITEM_END_OF_LIST == blackfriday.LIST_ITEM_END_OF_LIST { if flags&blackfriday.LIST_ITEM_END_OF_LIST != 0 {
out.WriteString("\n") 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()
if !text() { if !text() {
out.Truncate(marker) out.Truncate(marker)
return return
} }
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, header []byte, body []byte, columnData []int) {
// unimplemented // unimplemented
log.Println("Table is a stub") log.Println("warning: Table is a stub")
} }
func (*vimDoc) TableRow(out *bytes.Buffer, text []byte) { func (*vimDoc) TableRow(out *bytes.Buffer, text []byte) {
// unimplemented // unimplemented
log.Println("TableRow is a stub") log.Println("warning: 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 // unimplemented
log.Println("TableHeaderCell is a stub") log.Println("warning: 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 // unimplemented
log.Println("TableCell is a stub") log.Println("warning: TableCell is a stub")
} }
func (*vimDoc) Footnotes(out *bytes.Buffer, text func() bool) { func (*vimDoc) Footnotes(out *bytes.Buffer, text func() bool) {
// unimplemented // unimplemented
log.Println("Footnotes is a stub") log.Println("warning: 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) {
// unimplemented // unimplemented
log.Println("FootnoteItem is a stub") log.Println("warning: FootnoteItem is a stub")
} }
func (*vimDoc) TitleBlock(out *bytes.Buffer, text []byte) { func (*vimDoc) TitleBlock(out *bytes.Buffer, text []byte) {
// unimplemented // unimplemented
log.Println("TitleBlock is a stub") log.Println("warning: TitleBlock is a stub")
} }
// Span-level callbacks // Span-level callbacks
@ -273,7 +288,7 @@ func (*vimDoc) AutoLink(out *bytes.Buffer, link []byte, kind int) {
func (*vimDoc) CodeSpan(out *bytes.Buffer, text []byte) { func (*vimDoc) CodeSpan(out *bytes.Buffer, text []byte) {
r := regexp.MustCompile(`\s`) r := regexp.MustCompile(`\s`)
// vim does not properly highlight spaces in code spans // vim does not correctly highlight space-delimited words in code spans
if !r.Match(text) { if !r.Match(text) {
out.WriteString("`") out.WriteString("`")
out.Write(text) out.Write(text)
@ -303,7 +318,7 @@ func (*vimDoc) Link(out *bytes.Buffer, link []byte, title []byte, content []byte
func (*vimDoc) RawHtmlTag(out *bytes.Buffer, tag []byte) { func (*vimDoc) RawHtmlTag(out *bytes.Buffer, tag []byte) {
// unimplemented // unimplemented
log.Println("StrikeThrough is a stub") log.Println("warning: StrikeThrough is a stub")
} }
func (*vimDoc) TripleEmphasis(out *bytes.Buffer, text []byte) { func (*vimDoc) TripleEmphasis(out *bytes.Buffer, text []byte) {
@ -312,12 +327,12 @@ func (*vimDoc) TripleEmphasis(out *bytes.Buffer, text []byte) {
func (*vimDoc) StrikeThrough(out *bytes.Buffer, text []byte) { func (*vimDoc) StrikeThrough(out *bytes.Buffer, text []byte) {
// unimplemented // unimplemented
log.Println("StrikeThrough is a stub") log.Println("warning: StrikeThrough is a stub")
} }
func (*vimDoc) FootnoteRef(out *bytes.Buffer, ref []byte, id int) { func (*vimDoc) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
// unimplemented // unimplemented
log.Println("FootnoteRef is a stub") log.Println("warning: FootnoteRef is a stub")
} }
// Low-level callbacks // Low-level callbacks

View File

@ -40,6 +40,8 @@ func usage() {
} }
func main() { func main() {
cols := flag.Int("cols", 80, "number of columns to use for alignment and rules")
tabs := flag.Int("tabs", 4, "size of the tab to use specified as number of spaces")
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()
@ -61,7 +63,7 @@ func main() {
os.Exit(-1) os.Exit(-1)
} }
renderer := VimDocRenderer() renderer := VimDocRenderer(*cols, *tabs)
extensions := blackfriday.EXTENSION_FENCED_CODE | blackfriday.EXTENSION_NO_INTRA_EMPHASIS | blackfriday.EXTENSION_SPACE_HEADERS extensions := blackfriday.EXTENSION_FENCED_CODE | blackfriday.EXTENSION_NO_INTRA_EMPHASIS | blackfriday.EXTENSION_SPACE_HEADERS
output := blackfriday.Markdown(input, renderer, extensions) output := blackfriday.Markdown(input, renderer, extensions)