md2vim/vimdoc.go

414 lines
9.4 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-09 05:18:09 +00:00
"path"
2015-08-09 02:35:15 +00:00
"regexp"
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 (
2015-08-09 02:35:15 +00:00
DEFAULT_NUM_COLUMNS = 80
2015-08-09 04:49:53 +00:00
DEFAULT_TAB_SIZE = 4
2015-08-07 11:25:00 +00:00
)
2015-08-09 05:37:09 +00:00
const (
FLAG_NO_TOC = 1 << iota
FLAG_NO_RULES
FLAG_PASCAL
)
2015-08-09 02:35:15 +00:00
type list struct {
index int
2015-08-07 11:25:00 +00:00
}
2015-08-09 04:12:17 +00:00
type header struct {
text []byte
level int
children []*header
}
2015-08-07 10:18:13 +00:00
type vimDoc struct {
2015-08-09 05:18:09 +00:00
filename string
2015-08-09 04:12:17 +00:00
title string
2015-08-09 05:18:09 +00:00
desc string
2015-08-09 04:49:53 +00:00
cols int
tabs int
2015-08-09 05:37:09 +00:00
flags int
2015-08-09 04:12:17 +00:00
tocPos int
lists []*list
2015-08-09 05:18:09 +00:00
rootHead *header
lastHead *header
2015-08-07 11:25:00 +00:00
}
2015-08-09 05:37:09 +00:00
func VimDocRenderer(filename, desc string, cols, tabs, flags int) blackfriday.Renderer {
2015-08-09 05:18:09 +00:00
filename = path.Base(filename)
title := filename
if index := strings.LastIndex(filename, "."); index > -1 {
2015-08-09 05:37:09 +00:00
title = filename[:index]
if flags&FLAG_PASCAL == 0 {
title = strings.ToLower(title)
}
2015-08-09 05:18:09 +00:00
}
return &vimDoc{
filename: filename,
title: title,
desc: desc,
cols: cols,
tabs: tabs,
2015-08-09 05:37:09 +00:00
flags: flags,
2015-08-09 05:18:09 +00:00
tocPos: -1}
2015-08-09 02:35:15 +00:00
}
func (v *vimDoc) pushl() {
v.lists = append(v.lists, &list{1})
2015-08-07 11:25:00 +00:00
}
2015-08-08 10:58:23 +00:00
func (v *vimDoc) popl() {
2015-08-07 11:25:00 +00:00
if len(v.lists) == 0 {
2015-08-09 04:49:53 +00:00
log.Fatal("error: invalid list operation")
2015-08-07 11:25:00 +00:00
}
v.lists = v.lists[:len(v.lists)-1]
}
2015-08-09 02:35:15 +00:00
func (v *vimDoc) getl() *list {
2015-08-07 11:25:00 +00:00
if len(v.lists) == 0 {
2015-08-09 04:49:53 +00:00
log.Fatal("error: invalid list operation")
2015-08-07 11:25:00 +00:00
}
return v.lists[len(v.lists)-1]
2015-08-07 10:18:13 +00:00
}
2015-08-09 04:12:17 +00:00
func (v *vimDoc) fixupCode(input []byte) []byte {
2015-08-09 02:35:15 +00:00
r := regexp.MustCompile(`(?m)^\s*([<>])$`)
return r.ReplaceAll(input, []byte("$1"))
2015-08-07 10:18:13 +00:00
}
2015-08-09 04:12:17 +00:00
func (v *vimDoc) fixupHeader(header []byte) []byte {
return bytes.ToUpper(header)
}
func (v *vimDoc) buildTag(header []byte) []byte {
2015-08-09 05:37:09 +00:00
if v.flags&FLAG_PASCAL == 0 {
header = bytes.ToLower(header)
header = bytes.Replace(header, []byte{' '}, []byte{'_'}, -1)
} else {
header = bytes.ToTitle(header)
header = bytes.Replace(header, []byte{' '}, []byte{}, -1)
}
2015-08-09 04:58:39 +00:00
return []byte(fmt.Sprintf("%s-%s", v.title, header))
2015-08-09 04:12:17 +00:00
}
2015-08-09 04:49:53 +00:00
func (v *vimDoc) writeStraddle(out *bytes.Buffer, left, right []byte, trim int) {
padding := v.cols - (len(left) + len(right)) + trim
2015-08-09 05:18:09 +00:00
if padding <= 0 {
padding = 1
}
2015-08-09 04:49:53 +00:00
out.Write(left)
2015-08-09 05:18:09 +00:00
out.WriteString(strings.Repeat(" ", padding))
2015-08-09 04:49:53 +00:00
out.Write(right)
out.WriteString("\n")
}
func (v *vimDoc) writeRule(out *bytes.Buffer, repeat string) {
out.WriteString(strings.Repeat(repeat, v.cols))
2015-08-08 03:09:12 +00:00
out.WriteString("\n")
}
2015-08-09 04:12:17 +00:00
func (v *vimDoc) writeToc(out *bytes.Buffer, h *header, depth int) {
2015-08-09 04:58:39 +00:00
title := fmt.Sprintf("%s%s", strings.Repeat(" ", depth*v.tabs), h.text)
2015-08-09 04:49:53 +00:00
link := fmt.Sprintf("|%s|", v.buildTag(h.text))
v.writeStraddle(out, []byte(title), []byte(link), 2)
2015-08-09 04:12:17 +00:00
for _, c := range h.children {
v.writeToc(out, c, depth+1)
}
}
2015-08-08 10:58:23 +00:00
func (v *vimDoc) format(out *bytes.Buffer, text string, trim int) {
lines := strings.Split(text, "\n")
for index, line := range lines {
2015-08-09 04:49:53 +00:00
width := v.tabs
2015-08-09 04:12:17 +00:00
if width >= trim && index == 0 {
2015-08-08 10:58:23 +00:00
width -= trim
}
2015-08-09 04:49:53 +00:00
if len(line) > 0 {
out.WriteString(strings.Repeat(" ", width))
out.WriteString(line)
out.WriteString("\n")
2015-08-08 10:58:23 +00:00
}
}
2015-08-08 03:09:12 +00:00
}
2015-08-07 10:18:13 +00:00
// Block-level callbacks
2015-08-08 03:09:12 +00:00
func (v *vimDoc) BlockCode(out *bytes.Buffer, text []byte, lang string) {
2015-08-07 11:25:00 +00:00
out.WriteString(">\n")
2015-08-08 10:58:23 +00:00
v.format(out, string(text), 0)
2015-08-08 03:09:12 +00:00
out.WriteString("<\n\n")
2015-08-07 10:18:13 +00:00
}
2015-08-08 03:09:12 +00:00
func (v *vimDoc) BlockQuote(out *bytes.Buffer, text []byte) {
2015-08-07 11:25:00 +00:00
out.WriteString(">\n")
2015-08-08 10:58:23 +00:00
v.format(out, string(text), 0)
2015-08-08 03:09:12 +00:00
out.WriteString("<\n\n")
2015-08-07 10:18:13 +00:00
}
2015-08-08 03:09:12 +00:00
func (v *vimDoc) BlockHtml(out *bytes.Buffer, text []byte) {
2015-08-07 11:25:00 +00:00
out.WriteString(">\n")
2015-08-08 10:58:23 +00:00
v.format(out, string(text), 0)
2015-08-08 03:09:12 +00:00
out.WriteString("<\n\n")
2015-08-07 10:18:13 +00:00
}
2015-08-08 03:09:12 +00:00
func (v *vimDoc) Header(out *bytes.Buffer, text func() bool, level int, id string) {
2015-08-09 04:12:17 +00:00
initPos := out.Len()
2015-08-08 03:09:12 +00:00
2015-08-09 05:37:09 +00:00
if v.flags&FLAG_NO_RULES == 0 {
switch level {
case 1:
v.writeRule(out, "=")
case 2:
v.writeRule(out, "-")
}
2015-08-08 03:09:12 +00:00
}
2015-08-09 04:12:17 +00:00
headerPos := out.Len()
2015-08-08 03:09:12 +00:00
if !text() {
2015-08-09 04:12:17 +00:00
out.Truncate(initPos)
2015-08-08 03:09:12 +00:00
return
2015-08-07 11:25:00 +00:00
}
2015-08-08 03:09:12 +00:00
2015-08-09 04:12:17 +00:00
if v.tocPos == -1 && v.rootHead != nil {
v.tocPos = initPos
}
var value []byte
value = append(value, out.Bytes()[headerPos:]...)
header := &header{value, level, nil}
if v.lastHead == nil {
if header.level != 1 {
log.Println("warning: top-level header in document is not a level 1 header")
}
v.rootHead = header
v.lastHead = header
} else {
if v.rootHead.level >= header.level {
log.Println("warning: found header of higher or equal level to the root header")
}
if header.level <= v.lastHead.level {
v.lastHead = header
} else {
v.lastHead.children = append(v.lastHead.children, header)
}
}
out.Truncate(headerPos)
2015-08-09 04:49:53 +00:00
tag := fmt.Sprintf("*%s*", v.buildTag(header.text))
v.writeStraddle(out, v.fixupHeader(header.text), []byte(tag), 2)
out.WriteString("\n")
2015-08-07 10:18:13 +00:00
}
2015-08-08 03:09:12 +00:00
func (v *vimDoc) HRule(out *bytes.Buffer) {
2015-08-09 04:12:17 +00:00
v.writeRule(out, "-")
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) {
2015-08-09 02:35:15 +00:00
v.pushl()
2015-08-07 11:25:00 +00:00
text()
2015-08-08 10:58:23 +00:00
v.popl()
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) {
2015-08-08 10:58:23 +00:00
marker := out.Len()
2015-08-08 03:09:12 +00:00
2015-08-08 10:58:23 +00:00
list := v.getl()
2015-08-09 02:35:15 +00:00
if flags&blackfriday.LIST_TYPE_ORDERED == blackfriday.LIST_TYPE_ORDERED {
2015-08-08 03:09:12 +00:00
out.WriteString(fmt.Sprintf("%d. ", list.index))
list.index++
} else {
out.WriteString("* ")
}
2015-08-08 10:58:23 +00:00
v.format(out, string(text), out.Len()-marker)
2015-08-08 03:09:12 +00:00
2015-08-09 04:49:53 +00:00
if flags&blackfriday.LIST_ITEM_END_OF_LIST != 0 {
2015-08-08 03:09:12 +00:00
out.WriteString("\n")
}
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()
2015-08-09 04:49:53 +00:00
2015-08-07 11:25:00 +00:00
if !text() {
out.Truncate(marker)
return
}
2015-08-09 04:49:53 +00:00
2015-08-08 03:09:12 +00:00
out.WriteString("\n\n")
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
2015-08-08 03:09:12 +00:00
// unimplemented
2015-08-09 04:49:53 +00:00
log.Println("warning: Table is a stub")
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) TableRow(out *bytes.Buffer, text []byte) {
2015-08-08 03:09:12 +00:00
// unimplemented
2015-08-09 04:49:53 +00:00
log.Println("warning: TableRow is a stub")
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) {
2015-08-08 03:09:12 +00:00
// unimplemented
2015-08-09 04:49:53 +00:00
log.Println("warning: TableHeaderCell is a stub")
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) TableCell(out *bytes.Buffer, text []byte, flags int) {
2015-08-08 03:09:12 +00:00
// unimplemented
2015-08-09 04:49:53 +00:00
log.Println("warning: TableCell is a stub")
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) Footnotes(out *bytes.Buffer, text func() bool) {
2015-08-08 03:09:12 +00:00
// unimplemented
2015-08-09 04:49:53 +00:00
log.Println("warning: Footnotes is a stub")
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
2015-08-08 03:09:12 +00:00
// unimplemented
2015-08-09 04:49:53 +00:00
log.Println("warning: FootnoteItem is a stub")
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) TitleBlock(out *bytes.Buffer, text []byte) {
2015-08-08 03:09:12 +00:00
// unimplemented
2015-08-09 04:49:53 +00:00
log.Println("warning: TitleBlock is a stub")
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-09 02:35:15 +00:00
r := regexp.MustCompile(`\s`)
2015-08-09 04:49:53 +00:00
// vim does not correctly highlight space-delimited words in code spans
2015-08-09 02:35:15 +00:00
if !r.Match(text) {
out.WriteString("`")
out.Write(text)
out.WriteString("`")
}
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-09 04:12:17 +00:00
// cannot view images in vim
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-09 02:35:15 +00:00
// unimplemented
2015-08-09 04:49:53 +00:00
log.Println("warning: StrikeThrough is a stub")
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-08 03:09:12 +00:00
// unimplemented
2015-08-09 04:49:53 +00:00
log.Println("warning: StrikeThrough is a stub")
2015-08-07 10:18:13 +00:00
}
func (*vimDoc) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
2015-08-08 03:09:12 +00:00
// unimplemented
2015-08-09 04:49:53 +00:00
log.Println("warning: FootnoteRef is a stub")
2015-08-07 10:18:13 +00:00
}
// Low-level callbacks
2015-08-08 03:09:12 +00:00
func (v *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
}
2015-08-08 03:09:12 +00:00
func (v *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
2015-08-09 05:18:09 +00:00
func (v *vimDoc) DocumentHeader(out *bytes.Buffer) {
if len(v.desc) > 0 {
v.writeStraddle(out, []byte(v.filename), []byte(v.desc), 0)
} else {
out.WriteString(v.filename)
out.WriteString("\n")
}
2015-08-07 10:18:13 +00:00
}
2015-08-09 04:12:17 +00:00
func (v *vimDoc) DocumentFooter(out *bytes.Buffer) {
var temp bytes.Buffer
2015-08-09 05:37:09 +00:00
if v.tocPos > 0 && v.flags&FLAG_NO_TOC == 0 {
2015-08-09 04:58:39 +00:00
temp.Write(out.Bytes()[:v.tocPos])
v.writeToc(&temp, v.rootHead, 0)
temp.WriteString("\n")
temp.Write(out.Bytes()[v.tocPos:])
} else {
temp.ReadFrom(out)
}
2015-08-09 04:12:17 +00:00
out.Reset()
out.Write(v.fixupCode(temp.Bytes()))
2015-08-07 10:18:13 +00:00
}
2015-08-09 05:37:09 +00:00
func (v *vimDoc) GetFlags() int {
return v.flags
2015-08-07 10:18:13 +00:00
}