More work

This commit is contained in:
Alex Yatskov 2015-08-09 11:35:15 +09:00
parent 40d65a4393
commit bc3067d9f2
2 changed files with 30 additions and 27 deletions

View File

@ -26,27 +26,30 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"log" "log"
"regexp"
"strings" "strings"
"github.com/russross/blackfriday" "github.com/russross/blackfriday"
) )
const ( const (
LIST_STYLE_ORDERED = iota + 1 DEFAULT_NUM_COLUMNS = 80
LIST_STYLE_UNORDERED
) )
type listItem struct { type list struct {
style, index int index int
} }
type vimDoc struct { type vimDoc struct {
lists []*listItem lists []*list
cols, tabsize int
} }
func (v *vimDoc) pushl(style int) { func VimDocRenderer() *vimDoc {
v.lists = append(v.lists, &listItem{style, 1}) return &vimDoc{}
}
func (v *vimDoc) pushl() {
v.lists = append(v.lists, &list{1})
} }
func (v *vimDoc) popl() { func (v *vimDoc) popl() {
@ -57,7 +60,7 @@ func (v *vimDoc) popl() {
v.lists = v.lists[:len(v.lists)-1] v.lists = v.lists[:len(v.lists)-1]
} }
func (v *vimDoc) getl() *listItem { func (v *vimDoc) getl() *list {
if len(v.lists) == 0 { if len(v.lists) == 0 {
log.Fatal("invalid list operation") log.Fatal("invalid list operation")
} }
@ -65,15 +68,13 @@ func (v *vimDoc) getl() *listItem {
return v.lists[len(v.lists)-1] return v.lists[len(v.lists)-1]
} }
func VimDocRenderer() blackfriday.Renderer { func (v *vimDoc) fixup(input []byte) []byte {
return &vimDoc{ r := regexp.MustCompile(`(?m)^\s*([<>])$`)
cols: 80, return r.ReplaceAll(input, []byte("$1"))
tabsize: 4,
}
} }
func (*vimDoc) hrule(out *bytes.Buffer, repeat string) { func (*vimDoc) hrule(out *bytes.Buffer, repeat string) {
out.WriteString(strings.Repeat(repeat, 80)) out.WriteString(strings.Repeat(repeat, DEFAULT_NUM_COLUMNS))
out.WriteString("\n") out.WriteString("\n")
} }
@ -81,7 +82,7 @@ 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 := v.tabsize width := blackfriday.TAB_SIZE_DEFAULT
if index == 0 { if index == 0 {
width -= trim width -= trim
} }
@ -138,12 +139,7 @@ func (v *vimDoc) HRule(out *bytes.Buffer) {
} }
func (v *vimDoc) List(out *bytes.Buffer, text func() bool, flags int) { func (v *vimDoc) List(out *bytes.Buffer, text func() bool, flags int) {
style := LIST_STYLE_UNORDERED v.pushl()
if flags&blackfriday.LIST_TYPE_ORDERED == blackfriday.LIST_TYPE_ORDERED {
style = LIST_STYLE_ORDERED
}
v.pushl(style)
text() text()
v.popl() v.popl()
} }
@ -152,7 +148,7 @@ func (v *vimDoc) ListItem(out *bytes.Buffer, text []byte, flags int) {
marker := out.Len() marker := out.Len()
list := v.getl() list := v.getl()
if list.style == LIST_STYLE_ORDERED { if flags&blackfriday.LIST_TYPE_ORDERED == blackfriday.LIST_TYPE_ORDERED {
out.WriteString(fmt.Sprintf("%d. ", list.index)) out.WriteString(fmt.Sprintf("%d. ", list.index))
list.index++ list.index++
} else { } else {
@ -216,10 +212,14 @@ 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`)
if !r.Match(text) {
out.WriteString("`") out.WriteString("`")
out.Write(text) out.Write(text)
out.WriteString("`") out.WriteString("`")
} }
}
func (*vimDoc) DoubleEmphasis(out *bytes.Buffer, text []byte) { func (*vimDoc) DoubleEmphasis(out *bytes.Buffer, text []byte) {
out.Write(text) out.Write(text)
@ -243,7 +243,8 @@ 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) {
out.Write(tag) // unimplemented
log.Println("StrikeThrough is a stub")
} }
func (*vimDoc) TripleEmphasis(out *bytes.Buffer, text []byte) { func (*vimDoc) TripleEmphasis(out *bytes.Buffer, text []byte) {

View File

@ -38,6 +38,7 @@ func usage() {
fmt.Fprintf(os.Stderr, "Parameters:\n") fmt.Fprintf(os.Stderr, "Parameters:\n")
flag.PrintDefaults() flag.PrintDefaults()
} }
func main() { func main() {
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()
@ -68,6 +69,7 @@ func main() {
extensions |= blackfriday.EXTENSION_SPACE_HEADERS extensions |= blackfriday.EXTENSION_SPACE_HEADERS
output := blackfriday.Markdown(input, renderer, extensions) output := blackfriday.Markdown(input, renderer, extensions)
output = renderer.fixup(output)
var file *os.File var file *os.File
switch len(args) { switch len(args) {