md2vim/vimdoc.go

223 lines
5.0 KiB
Go
Raw Normal View History

2015-08-07 10:18:13 +00:00
/*
* Copyright (c) 2015 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
"bytes"
2015-08-07 11:25:00 +00:00
"fmt"
2015-08-07 10:18:13 +00:00
"log"
2015-08-07 11:25:00 +00:00
"strings"
2015-08-07 10:18:13 +00:00
"github.com/russross/blackfriday"
)
2015-08-07 11:25:00 +00:00
const (
LIST_STYLE_ORDERED = iota + 1
LIST_STYLE_UNORDERED
)
func indent(text []byte) []byte {
return nil
}
type listCtx struct {
style, index int
}
2015-08-07 10:18:13 +00:00
type vimDoc struct {
2015-08-07 11:25:00 +00:00
lists []*listCtx
}
func (v *vimDoc) pushList(style int) {
v.lists = append(v.lists, &listCtx{style, 1})
}
func (v *vimDoc) popList() {
if len(v.lists) == 0 {
log.Fatal("invalid list operation")
}
v.lists = v.lists[:len(v.lists)-1]
}
func (v *vimDoc) getList() *listCtx {
if len(v.lists) == 0 {
log.Fatal("invalid list operation")
}
return v.lists[len(v.lists)-1]
2015-08-07 10:18:13 +00:00
}
func VimDocRenderer() blackfriday.Renderer {
return &vimDoc{}
}
// Block-level callbacks
func (*vimDoc) BlockCode(out *bytes.Buffer, text []byte, lang string) {
2015-08-07 11:25:00 +00:00
out.WriteString(">\n")
out.Write(text)
out.WriteString("<\n")
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) BlockQuote(out *bytes.Buffer, text []byte) {
2015-08-07 11:25:00 +00:00
out.WriteString(">\n")
out.Write(text)
out.WriteString("<\n")
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) BlockHtml(out *bytes.Buffer, text []byte) {
2015-08-07 11:25:00 +00:00
out.WriteString(">\n")
out.Write(text)
out.WriteString("<\n")
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) Header(out *bytes.Buffer, text func() bool, level int, id string) {
2015-08-07 11:25:00 +00:00
if text() {
out.WriteString(" ~\n")
}
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) HRule(out *bytes.Buffer) {
2015-08-07 11:25:00 +00:00
out.WriteString(strings.Repeat("=", 80))
2015-08-07 10:18:13 +00:00
}
2015-08-07 11:25:00 +00:00
func (v *vimDoc) List(out *bytes.Buffer, text func() bool, flags int) {
style := LIST_STYLE_UNORDERED
if flags&blackfriday.LIST_TYPE_ORDERED == blackfriday.LIST_TYPE_ORDERED {
style = LIST_STYLE_ORDERED
}
v.pushList(style)
text()
v.popList()
2015-08-07 10:18:13 +00:00
}
2015-08-07 11:25:00 +00:00
func (v *vimDoc) ListItem(out *bytes.Buffer, text []byte, flags int) {
list := v.getList()
out.WriteString(fmt.Sprintf("\n%d.", list.index))
out.Write(text)
list.index++
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) Paragraph(out *bytes.Buffer, text func() bool) {
2015-08-07 11:25:00 +00:00
marker := out.Len()
out.WriteString("\n")
if !text() {
out.Truncate(marker)
return
}
out.WriteString("\n")
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
}
func (*vimDoc) TableRow(out *bytes.Buffer, text []byte) {
}
func (*vimDoc) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) {
}
func (*vimDoc) TableCell(out *bytes.Buffer, text []byte, flags int) {
}
func (*vimDoc) Footnotes(out *bytes.Buffer, text func() bool) {
2015-08-07 11:25:00 +00:00
text()
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
2015-08-07 11:25:00 +00:00
out.Write(text)
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) TitleBlock(out *bytes.Buffer, text []byte) {
2015-08-07 11:25:00 +00:00
out.Write(text)
2015-08-07 10:18:13 +00:00
}
// Span-level callbacks
func (*vimDoc) AutoLink(out *bytes.Buffer, link []byte, kind int) {
2015-08-07 11:25:00 +00:00
out.Write(link)
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) CodeSpan(out *bytes.Buffer, text []byte) {
2015-08-07 11:25:00 +00:00
out.Write(text)
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) DoubleEmphasis(out *bytes.Buffer, text []byte) {
2015-08-07 11:25:00 +00:00
out.Write(text)
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) Emphasis(out *bytes.Buffer, text []byte) {
2015-08-07 11:25:00 +00:00
out.Write(text)
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
2015-08-07 11:25:00 +00:00
// not implemented
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) LineBreak(out *bytes.Buffer) {
2015-08-07 11:25:00 +00:00
out.WriteString("\n")
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
2015-08-07 11:25:00 +00:00
out.WriteString(fmt.Sprintf("%s (%s)", content, link))
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) RawHtmlTag(out *bytes.Buffer, tag []byte) {
2015-08-07 11:25:00 +00:00
out.Write(tag)
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) TripleEmphasis(out *bytes.Buffer, text []byte) {
2015-08-07 11:25:00 +00:00
out.Write(text)
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) StrikeThrough(out *bytes.Buffer, text []byte) {
2015-08-07 11:25:00 +00:00
out.Write(text)
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
2015-08-07 11:25:00 +00:00
// not implemented
2015-08-07 10:18:13 +00:00
}
// Low-level callbacks
func (*vimDoc) Entity(out *bytes.Buffer, entity []byte) {
2015-08-07 11:25:00 +00:00
out.Write(entity)
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) NormalText(out *bytes.Buffer, text []byte) {
2015-08-07 11:25:00 +00:00
out.Write(text)
2015-08-07 10:18:13 +00:00
}
// Header and footer
func (*vimDoc) DocumentHeader(out *bytes.Buffer) {
2015-08-07 11:25:00 +00:00
// not implemented
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) DocumentFooter(out *bytes.Buffer) {
2015-08-07 11:25:00 +00:00
// not implemented
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) GetFlags() int {
return 0
}