374 lines
8.6 KiB
Go
374 lines
8.6 KiB
Go
/*
|
|
* 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"
|
|
"fmt"
|
|
"log"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/russross/blackfriday"
|
|
)
|
|
|
|
const (
|
|
DEFAULT_NUM_COLUMNS = 80
|
|
DEFAULT_TAB_SIZE = 4
|
|
)
|
|
|
|
type list struct {
|
|
index int
|
|
}
|
|
|
|
type header struct {
|
|
text []byte
|
|
level int
|
|
children []*header
|
|
}
|
|
|
|
type vimDoc struct {
|
|
rootHead *header
|
|
lastHead *header
|
|
title string
|
|
cols int
|
|
tabs int
|
|
tocPos int
|
|
lists []*list
|
|
}
|
|
|
|
func VimDocRenderer(cols, tabs int) blackfriday.Renderer {
|
|
return &vimDoc{title: "test", cols: cols, tabs: tabs, tocPos: -1}
|
|
}
|
|
|
|
func (v *vimDoc) pushl() {
|
|
v.lists = append(v.lists, &list{1})
|
|
}
|
|
|
|
func (v *vimDoc) popl() {
|
|
if len(v.lists) == 0 {
|
|
log.Fatal("error: invalid list operation")
|
|
}
|
|
|
|
v.lists = v.lists[:len(v.lists)-1]
|
|
}
|
|
|
|
func (v *vimDoc) getl() *list {
|
|
if len(v.lists) == 0 {
|
|
log.Fatal("error: invalid list operation")
|
|
}
|
|
|
|
return v.lists[len(v.lists)-1]
|
|
}
|
|
|
|
func (v *vimDoc) fixupCode(input []byte) []byte {
|
|
r := regexp.MustCompile(`(?m)^\s*([<>])$`)
|
|
return r.ReplaceAll(input, []byte("$1"))
|
|
}
|
|
|
|
func (v *vimDoc) fixupHeader(header []byte) []byte {
|
|
return bytes.ToUpper(header)
|
|
}
|
|
|
|
func (v *vimDoc) buildTag(header []byte) []byte {
|
|
header = bytes.ToLower(header)
|
|
header = bytes.Replace(header, []byte{' '}, []byte{'_'}, -1)
|
|
return []byte(fmt.Sprintf("%s-%s", v.title, header))
|
|
}
|
|
|
|
func (v *vimDoc) writeStraddle(out *bytes.Buffer, left, right []byte, trim int) {
|
|
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")
|
|
}
|
|
|
|
func (v *vimDoc) writeToc(out *bytes.Buffer, h *header, depth int) {
|
|
title := fmt.Sprintf("%s%s", strings.Repeat(" ", depth*v.tabs), h.text)
|
|
link := fmt.Sprintf("|%s|", v.buildTag(h.text))
|
|
v.writeStraddle(out, []byte(title), []byte(link), 2)
|
|
|
|
for _, c := range h.children {
|
|
v.writeToc(out, c, depth+1)
|
|
}
|
|
}
|
|
|
|
func (v *vimDoc) format(out *bytes.Buffer, text string, trim int) {
|
|
lines := strings.Split(text, "\n")
|
|
|
|
for index, line := range lines {
|
|
width := v.tabs
|
|
if width >= trim && index == 0 {
|
|
width -= trim
|
|
}
|
|
|
|
if len(line) > 0 {
|
|
out.WriteString(strings.Repeat(" ", width))
|
|
out.WriteString(line)
|
|
out.WriteString("\n")
|
|
}
|
|
}
|
|
}
|
|
|
|
// Block-level callbacks
|
|
func (v *vimDoc) BlockCode(out *bytes.Buffer, text []byte, lang string) {
|
|
out.WriteString(">\n")
|
|
v.format(out, string(text), 0)
|
|
out.WriteString("<\n\n")
|
|
}
|
|
|
|
func (v *vimDoc) BlockQuote(out *bytes.Buffer, text []byte) {
|
|
out.WriteString(">\n")
|
|
v.format(out, string(text), 0)
|
|
out.WriteString("<\n\n")
|
|
}
|
|
|
|
func (v *vimDoc) BlockHtml(out *bytes.Buffer, text []byte) {
|
|
out.WriteString(">\n")
|
|
v.format(out, string(text), 0)
|
|
out.WriteString("<\n\n")
|
|
}
|
|
|
|
func (v *vimDoc) Header(out *bytes.Buffer, text func() bool, level int, id string) {
|
|
initPos := out.Len()
|
|
|
|
switch level {
|
|
case 1:
|
|
v.writeRule(out, "=")
|
|
case 2:
|
|
v.writeRule(out, "-")
|
|
}
|
|
|
|
headerPos := out.Len()
|
|
|
|
if !text() {
|
|
out.Truncate(initPos)
|
|
return
|
|
}
|
|
|
|
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)
|
|
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) {
|
|
v.writeRule(out, "-")
|
|
}
|
|
|
|
func (v *vimDoc) List(out *bytes.Buffer, text func() bool, flags int) {
|
|
v.pushl()
|
|
text()
|
|
v.popl()
|
|
}
|
|
|
|
func (v *vimDoc) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
|
marker := out.Len()
|
|
|
|
list := v.getl()
|
|
if flags&blackfriday.LIST_TYPE_ORDERED == blackfriday.LIST_TYPE_ORDERED {
|
|
out.WriteString(fmt.Sprintf("%d. ", list.index))
|
|
list.index++
|
|
} else {
|
|
out.WriteString("* ")
|
|
}
|
|
|
|
v.format(out, string(text), out.Len()-marker)
|
|
|
|
if flags&blackfriday.LIST_ITEM_END_OF_LIST != 0 {
|
|
out.WriteString("\n")
|
|
}
|
|
}
|
|
|
|
func (*vimDoc) Paragraph(out *bytes.Buffer, text func() bool) {
|
|
marker := out.Len()
|
|
|
|
if !text() {
|
|
out.Truncate(marker)
|
|
return
|
|
}
|
|
|
|
out.WriteString("\n\n")
|
|
}
|
|
|
|
func (*vimDoc) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) {
|
|
// unimplemented
|
|
log.Println("warning: Table is a stub")
|
|
}
|
|
|
|
func (*vimDoc) TableRow(out *bytes.Buffer, text []byte) {
|
|
// unimplemented
|
|
log.Println("warning: TableRow is a stub")
|
|
}
|
|
|
|
func (*vimDoc) TableHeaderCell(out *bytes.Buffer, text []byte, flags int) {
|
|
// unimplemented
|
|
log.Println("warning: TableHeaderCell is a stub")
|
|
}
|
|
|
|
func (*vimDoc) TableCell(out *bytes.Buffer, text []byte, flags int) {
|
|
// unimplemented
|
|
log.Println("warning: TableCell is a stub")
|
|
}
|
|
|
|
func (*vimDoc) Footnotes(out *bytes.Buffer, text func() bool) {
|
|
// unimplemented
|
|
log.Println("warning: Footnotes is a stub")
|
|
}
|
|
|
|
func (*vimDoc) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
|
|
// unimplemented
|
|
log.Println("warning: FootnoteItem is a stub")
|
|
}
|
|
|
|
func (*vimDoc) TitleBlock(out *bytes.Buffer, text []byte) {
|
|
// unimplemented
|
|
log.Println("warning: TitleBlock is a stub")
|
|
}
|
|
|
|
// Span-level callbacks
|
|
func (*vimDoc) AutoLink(out *bytes.Buffer, link []byte, kind int) {
|
|
out.Write(link)
|
|
}
|
|
|
|
func (*vimDoc) CodeSpan(out *bytes.Buffer, text []byte) {
|
|
r := regexp.MustCompile(`\s`)
|
|
|
|
// vim does not correctly highlight space-delimited words in code spans
|
|
if !r.Match(text) {
|
|
out.WriteString("`")
|
|
out.Write(text)
|
|
out.WriteString("`")
|
|
}
|
|
}
|
|
|
|
func (*vimDoc) DoubleEmphasis(out *bytes.Buffer, text []byte) {
|
|
out.Write(text)
|
|
}
|
|
|
|
func (*vimDoc) Emphasis(out *bytes.Buffer, text []byte) {
|
|
out.Write(text)
|
|
}
|
|
|
|
func (*vimDoc) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
|
|
// cannot view images in vim
|
|
}
|
|
|
|
func (*vimDoc) LineBreak(out *bytes.Buffer) {
|
|
out.WriteString("\n")
|
|
}
|
|
|
|
func (*vimDoc) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
|
|
out.WriteString(fmt.Sprintf("%s (%s)", content, link))
|
|
}
|
|
|
|
func (*vimDoc) RawHtmlTag(out *bytes.Buffer, tag []byte) {
|
|
// unimplemented
|
|
log.Println("warning: StrikeThrough is a stub")
|
|
}
|
|
|
|
func (*vimDoc) TripleEmphasis(out *bytes.Buffer, text []byte) {
|
|
out.Write(text)
|
|
}
|
|
|
|
func (*vimDoc) StrikeThrough(out *bytes.Buffer, text []byte) {
|
|
// unimplemented
|
|
log.Println("warning: StrikeThrough is a stub")
|
|
}
|
|
|
|
func (*vimDoc) FootnoteRef(out *bytes.Buffer, ref []byte, id int) {
|
|
// unimplemented
|
|
log.Println("warning: FootnoteRef is a stub")
|
|
}
|
|
|
|
// Low-level callbacks
|
|
func (v *vimDoc) Entity(out *bytes.Buffer, entity []byte) {
|
|
out.Write(entity)
|
|
}
|
|
|
|
func (v *vimDoc) NormalText(out *bytes.Buffer, text []byte) {
|
|
out.Write(text)
|
|
}
|
|
|
|
// Header and footer
|
|
func (*vimDoc) DocumentHeader(out *bytes.Buffer) {
|
|
// unimplemented
|
|
log.Println("DocumentHeader is a stub")
|
|
}
|
|
|
|
func (v *vimDoc) DocumentFooter(out *bytes.Buffer) {
|
|
var temp bytes.Buffer
|
|
|
|
if v.tocPos > 0 {
|
|
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)
|
|
}
|
|
|
|
out.Reset()
|
|
out.Write(v.fixupCode(temp.Bytes()))
|
|
}
|
|
|
|
func (*vimDoc) GetFlags() int {
|
|
return 0
|
|
}
|