Making it better
This commit is contained in:
parent
457a9b6309
commit
b19b5a829b
41
vimdoc.go
41
vimdoc.go
@ -26,6 +26,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -48,17 +49,32 @@ type header struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type vimDoc struct {
|
type vimDoc struct {
|
||||||
rootHead *header
|
filename string
|
||||||
lastHead *header
|
|
||||||
title string
|
title string
|
||||||
|
desc string
|
||||||
cols int
|
cols int
|
||||||
tabs int
|
tabs int
|
||||||
tocPos int
|
tocPos int
|
||||||
lists []*list
|
lists []*list
|
||||||
|
rootHead *header
|
||||||
|
lastHead *header
|
||||||
}
|
}
|
||||||
|
|
||||||
func VimDocRenderer(cols, tabs int) blackfriday.Renderer {
|
func VimDocRenderer(filename, desc string, cols, tabs int) blackfriday.Renderer {
|
||||||
return &vimDoc{title: "test", cols: cols, tabs: tabs, tocPos: -1}
|
filename = path.Base(filename)
|
||||||
|
title := filename
|
||||||
|
|
||||||
|
if index := strings.LastIndex(filename, "."); index > -1 {
|
||||||
|
title = strings.ToLower(filename[:index])
|
||||||
|
}
|
||||||
|
|
||||||
|
return &vimDoc{
|
||||||
|
filename: filename,
|
||||||
|
title: title,
|
||||||
|
desc: desc,
|
||||||
|
cols: cols,
|
||||||
|
tabs: tabs,
|
||||||
|
tocPos: -1}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *vimDoc) pushl() {
|
func (v *vimDoc) pushl() {
|
||||||
@ -98,11 +114,12 @@ func (v *vimDoc) buildTag(header []byte) []byte {
|
|||||||
|
|
||||||
func (v *vimDoc) writeStraddle(out *bytes.Buffer, left, right []byte, trim int) {
|
func (v *vimDoc) writeStraddle(out *bytes.Buffer, left, right []byte, trim int) {
|
||||||
padding := v.cols - (len(left) + len(right)) + trim
|
padding := v.cols - (len(left) + len(right)) + trim
|
||||||
|
if padding <= 0 {
|
||||||
|
padding = 1
|
||||||
|
}
|
||||||
|
|
||||||
out.Write(left)
|
out.Write(left)
|
||||||
if padding > 0 {
|
out.WriteString(strings.Repeat(" ", padding))
|
||||||
out.WriteString(strings.Repeat(" ", padding))
|
|
||||||
}
|
|
||||||
out.Write(right)
|
out.Write(right)
|
||||||
out.WriteString("\n")
|
out.WriteString("\n")
|
||||||
}
|
}
|
||||||
@ -347,9 +364,13 @@ func (v *vimDoc) NormalText(out *bytes.Buffer, text []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Header and footer
|
// Header and footer
|
||||||
func (*vimDoc) DocumentHeader(out *bytes.Buffer) {
|
func (v *vimDoc) DocumentHeader(out *bytes.Buffer) {
|
||||||
// unimplemented
|
if len(v.desc) > 0 {
|
||||||
log.Println("DocumentHeader is a stub")
|
v.writeStraddle(out, []byte(v.filename), []byte(v.desc), 0)
|
||||||
|
} else {
|
||||||
|
out.WriteString(v.filename)
|
||||||
|
out.WriteString("\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *vimDoc) DocumentFooter(out *bytes.Buffer) {
|
func (v *vimDoc) DocumentFooter(out *bytes.Buffer) {
|
||||||
|
42
vimdown.go
42
vimdown.go
@ -33,7 +33,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func usage() {
|
func usage() {
|
||||||
fmt.Fprintf(os.Stderr, "Usage: %s [options] [input] [output]\n", os.Args[0])
|
fmt.Fprintf(os.Stderr, "Usage: %s [options] input output\n", os.Args[0])
|
||||||
fmt.Fprintf(os.Stderr, "http://foosoft.net/projects/vimdown/\n\n")
|
fmt.Fprintf(os.Stderr, "http://foosoft.net/projects/vimdown/\n\n")
|
||||||
fmt.Fprintf(os.Stderr, "Parameters:\n")
|
fmt.Fprintf(os.Stderr, "Parameters:\n")
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
@ -42,44 +42,32 @@ func usage() {
|
|||||||
func main() {
|
func main() {
|
||||||
cols := flag.Int("cols", 80, "number of columns to use for alignment and rules")
|
cols := flag.Int("cols", 80, "number of columns to use for alignment and rules")
|
||||||
tabs := flag.Int("tabs", 4, "size of the tab to use specified as number of spaces")
|
tabs := flag.Int("tabs", 4, "size of the tab to use specified as number of spaces")
|
||||||
|
desc := flag.String("desc", "", "short description of extension")
|
||||||
flag.Usage = usage
|
flag.Usage = usage
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
|
if len(args) < 2 {
|
||||||
var input []byte
|
|
||||||
var err error
|
|
||||||
switch len(args) {
|
|
||||||
case 0:
|
|
||||||
if input, err = ioutil.ReadAll(os.Stdin); err != nil {
|
|
||||||
log.Fatal("error reading from stdin")
|
|
||||||
}
|
|
||||||
case 1, 2:
|
|
||||||
if input, err = ioutil.ReadFile(args[0]); err != nil {
|
|
||||||
log.Fatalf("error reading from %s", args[0])
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer := VimDocRenderer(*cols, *tabs)
|
input, err := ioutil.ReadFile(args[0])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("error: unable to read from file %s", args[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
renderer := VimDocRenderer(args[1], *desc, *cols, *tabs)
|
||||||
extensions := blackfriday.EXTENSION_FENCED_CODE | blackfriday.EXTENSION_NO_INTRA_EMPHASIS | blackfriday.EXTENSION_SPACE_HEADERS
|
extensions := blackfriday.EXTENSION_FENCED_CODE | blackfriday.EXTENSION_NO_INTRA_EMPHASIS | blackfriday.EXTENSION_SPACE_HEADERS
|
||||||
output := blackfriday.Markdown(input, renderer, extensions)
|
output := blackfriday.Markdown(input, renderer, extensions)
|
||||||
|
|
||||||
var file *os.File
|
file, err := os.Create(args[1])
|
||||||
switch len(args) {
|
if err != nil {
|
||||||
case 2:
|
log.Fatalf("error: unable to write to file %s", args[1])
|
||||||
if file, err = os.Create(args[1]); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Error creating %s: %v", args[1], err)
|
|
||||||
os.Exit(-1)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
default:
|
|
||||||
file = os.Stdout
|
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
if _, err = file.Write(output); err != nil {
|
if _, err := file.Write(output); err != nil {
|
||||||
log.Fatal("error writing output")
|
log.Fatal("error: unable to write output")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user