md2vim/vimdoc.go

420 lines
9.4 KiB
Go
Raw Normal View History

2015-08-07 10:18:13 +00:00
/*
2021-06-13 19:00:35 +00:00
* Copyright (c) 2015-2021 Alex Yatskov <alex@foosoft.net>
2015-08-07 10:18:13 +00:00
*
* 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-19 08:31:10 +00:00
"strconv"
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-11 05:56:14 +00:00
defNumCols = 80
defTabSize = 4
2015-08-07 11:25:00 +00:00
)
2015-08-09 05:37:09 +00:00
const (
2015-08-11 05:56:14 +00:00
flagNoToc = 1 << iota
flagNoRules
flagPascal
2015-08-09 05:37:09 +00:00
)
2015-08-09 02:35:15 +00:00
type list struct {
index int
2015-08-07 11:25:00 +00:00
}
2015-08-09 05:57:43 +00:00
type heading struct {
2015-08-19 07:39:28 +00:00
text []byte
level int
2015-08-09 04:12:17 +00:00
}
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-19 07:39:28 +00:00
headings []*heading
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]
2015-08-11 05:56:14 +00:00
if flags&flagPascal == 0 {
2015-08-09 05:37:09 +00:00
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
}
2015-08-19 07:39:28 +00:00
func (v *vimDoc) fixupCodeTags(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-19 07:39:28 +00:00
func (v *vimDoc) buildHelpTag(text []byte) []byte {
2015-08-11 05:56:14 +00:00
if v.flags&flagPascal == 0 {
2015-08-09 05:57:43 +00:00
text = bytes.ToLower(text)
text = bytes.Replace(text, []byte{' '}, []byte{'_'}, -1)
2015-08-09 05:37:09 +00:00
} else {
2015-08-09 05:57:43 +00:00
text = bytes.Title(text)
text = bytes.Replace(text, []byte{' '}, []byte{}, -1)
2015-08-09 05:37:09 +00:00
}
2015-08-09 05:57:43 +00:00
return []byte(fmt.Sprintf("%s-%s", v.title, text))
2015-08-09 04:12:17 +00:00
}
2015-08-19 08:31:10 +00:00
func (v *vimDoc) buildChapters(h *heading) []byte {
index := -1
2015-08-19 08:34:01 +00:00
{
for i, curr := range v.headings {
if curr == h {
index = i
break
}
2015-08-19 08:31:10 +00:00
}
2015-08-19 08:34:01 +00:00
if index < 0 {
log.Fatal("heading not found")
}
2015-08-19 08:31:10 +00:00
}
var chapters []int
{
level := h.level
siblings := 1
for i := index - 1; i >= 0; i-- {
curr := v.headings[i]
if curr.level == level {
siblings++
} else if curr.level < level {
chapters = append(chapters, siblings)
level = curr.level
siblings = 1
}
}
chapters = append(chapters, siblings)
}
var out bytes.Buffer
for i := len(chapters) - 1; i >= 0; i-- {
out.WriteString(strconv.Itoa(chapters[i]))
out.WriteString(".")
}
return out.Bytes()
2015-08-19 07:39:28 +00:00
}
func (v *vimDoc) writeSplitText(out *bytes.Buffer, left, right []byte, repeat string, trim int) {
2015-08-09 04:49:53 +00:00
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-15 08:15:59 +00:00
out.WriteString(strings.Repeat(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-19 07:39:28 +00:00
func (v *vimDoc) writeToc(out *bytes.Buffer) {
for _, h := range v.headings {
2015-08-19 08:31:10 +00:00
title := fmt.Sprintf("%s%s %s", strings.Repeat(" ", (h.level-1)*v.tabs), v.buildChapters(h), h.text)
2015-08-19 07:39:28 +00:00
link := fmt.Sprintf("|%s|", v.buildHelpTag(h.text))
v.writeSplitText(out, []byte(title), []byte(link), ".", 2)
2015-08-09 04:12:17 +00:00
}
}
2015-08-19 07:39:28 +00:00
func (v *vimDoc) writeIndent(out *bytes.Buffer, text string, trim int) {
2015-08-08 10:58:23 +00:00
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-19 07:39:28 +00:00
v.writeIndent(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-19 07:39:28 +00:00
v.writeIndent(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-19 07:39:28 +00:00
v.writeIndent(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-11 05:56:14 +00:00
if v.flags&flagNoRules == 0 {
2015-08-09 05:37:09 +00:00
switch level {
case 1:
v.writeRule(out, "=")
case 2:
v.writeRule(out, "-")
}
2015-08-08 03:09:12 +00:00
}
2015-08-09 05:57:43 +00:00
headingPos := 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-19 07:39:28 +00:00
var temp []byte
temp = append(temp, out.Bytes()[headingPos:]...)
2015-08-19 08:45:08 +00:00
out.Truncate(headingPos)
2015-08-09 04:12:17 +00:00
2015-08-19 07:39:28 +00:00
h := &heading{temp, level}
v.headings = append(v.headings, h)
2015-08-09 04:12:17 +00:00
2015-08-19 07:39:28 +00:00
tag := fmt.Sprintf("*%s*", v.buildHelpTag(h.text))
2015-08-19 09:54:33 +00:00
v.writeSplitText(out, bytes.ToUpper(h.text), []byte(tag), " ", 2)
2015-08-09 04:49:53 +00:00
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-19 06:10:17 +00:00
v.lists = append(v.lists, &list{1})
2015-08-07 11:25:00 +00:00
text()
2015-08-19 06:10:17 +00:00
v.lists = v.lists[:len(v.lists)-1]
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-19 06:10:17 +00:00
list := v.lists[len(v.lists)-1]
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-19 07:39:28 +00:00
v.writeIndent(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
}
2015-08-09 05:57:43 +00:00
func (*vimDoc) Table(out *bytes.Buffer, heading []byte, body []byte, columnData []int) {
2015-08-08 03:09:12 +00:00
// unimplemented
2015-08-19 08:36:11 +00:00
log.Println("Table is unimplemented")
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-19 08:36:11 +00:00
log.Println("TableRow is unimplemented")
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-19 08:36:11 +00:00
log.Println("TableHeaderCell is unimplemented")
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-19 08:36:11 +00:00
log.Println("TableCell is unimplemented")
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-19 08:36:11 +00:00
log.Println("Footnotes is unimplemented")
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-19 08:36:11 +00:00
log.Println("FootnoteItem is unimplemented")
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-19 08:36:11 +00:00
log.Println("TitleBlock is unimplemented")
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) {
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-19 08:36:11 +00:00
log.Println("StrikeThrough is unimplemented")
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-19 08:36:11 +00:00
log.Println("StrikeThrough is unimplemented")
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-19 08:36:11 +00:00
log.Println("FootnoteRef is unimplemented")
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 {
2021-06-13 19:04:53 +00:00
parts := strings.Split(v.desc, "\\n")
for i, part := range parts {
if i == 0 {
v.writeSplitText(out, []byte(v.filename), []byte(part), " ", 0)
} else {
out.WriteString(part)
}
out.WriteString("\n")
}
2015-08-09 05:18:09 +00:00
} else {
out.WriteString(v.filename)
out.WriteString("\n")
}
2015-08-09 05:42:25 +00:00
out.WriteString("\n")
2015-08-19 08:45:08 +00:00
v.tocPos = out.Len()
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-11 05:56:14 +00:00
if v.tocPos > 0 && v.flags&flagNoToc == 0 {
2015-08-09 04:58:39 +00:00
temp.Write(out.Bytes()[:v.tocPos])
2015-08-19 08:45:08 +00:00
v.writeRule(&temp, "=")
2015-08-19 09:59:18 +00:00
title := []byte("Contents")
2015-08-19 09:54:33 +00:00
tag := fmt.Sprintf("*%s*", v.buildHelpTag(title))
v.writeSplitText(&temp, bytes.ToUpper(title), []byte(tag), " ", 2)
2015-08-19 08:45:08 +00:00
temp.WriteString("\n")
2015-08-19 07:39:28 +00:00
v.writeToc(&temp)
2015-08-09 04:58:39 +00:00
temp.WriteString("\n")
2015-08-19 08:45:08 +00:00
2015-08-09 04:58:39 +00:00
temp.Write(out.Bytes()[v.tocPos:])
} else {
temp.ReadFrom(out)
}
2015-08-09 04:12:17 +00:00
out.Reset()
2015-08-19 07:39:28 +00:00
out.Write(v.fixupCodeTags(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
}