Making it better
This commit is contained in:
parent
457a9b6309
commit
b19b5a829b
39
vimdoc.go
39
vimdoc.go
@ -26,6 +26,7 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
@ -48,17 +49,32 @@ type header struct {
|
||||
}
|
||||
|
||||
type vimDoc struct {
|
||||
rootHead *header
|
||||
lastHead *header
|
||||
filename string
|
||||
title string
|
||||
desc string
|
||||
cols int
|
||||
tabs int
|
||||
tocPos int
|
||||
lists []*list
|
||||
rootHead *header
|
||||
lastHead *header
|
||||
}
|
||||
|
||||
func VimDocRenderer(cols, tabs int) blackfriday.Renderer {
|
||||
return &vimDoc{title: "test", cols: cols, tabs: tabs, tocPos: -1}
|
||||
func VimDocRenderer(filename, desc string, cols, tabs int) blackfriday.Renderer {
|
||||
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() {
|
||||
@ -98,11 +114,12 @@ func (v *vimDoc) buildTag(header []byte) []byte {
|
||||
|
||||
func (v *vimDoc) writeStraddle(out *bytes.Buffer, left, right []byte, trim int) {
|
||||
padding := v.cols - (len(left) + len(right)) + trim
|
||||
if padding <= 0 {
|
||||
padding = 1
|
||||
}
|
||||
|
||||
out.Write(left)
|
||||
if padding > 0 {
|
||||
out.WriteString(strings.Repeat(" ", padding))
|
||||
}
|
||||
out.Write(right)
|
||||
out.WriteString("\n")
|
||||
}
|
||||
@ -347,9 +364,13 @@ func (v *vimDoc) NormalText(out *bytes.Buffer, text []byte) {
|
||||
}
|
||||
|
||||
// Header and footer
|
||||
func (*vimDoc) DocumentHeader(out *bytes.Buffer) {
|
||||
// unimplemented
|
||||
log.Println("DocumentHeader is a stub")
|
||||
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")
|
||||
}
|
||||
}
|
||||
|
||||
func (v *vimDoc) DocumentFooter(out *bytes.Buffer) {
|
||||
|
40
vimdown.go
40
vimdown.go
@ -33,7 +33,7 @@ import (
|
||||
)
|
||||
|
||||
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, "Parameters:\n")
|
||||
flag.PrintDefaults()
|
||||
@ -42,44 +42,32 @@ func usage() {
|
||||
func main() {
|
||||
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")
|
||||
desc := flag.String("desc", "", "short description of extension")
|
||||
flag.Usage = usage
|
||||
flag.Parse()
|
||||
|
||||
args := flag.Args()
|
||||
|
||||
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:
|
||||
if len(args) < 2 {
|
||||
flag.Usage()
|
||||
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
|
||||
output := blackfriday.Markdown(input, renderer, extensions)
|
||||
|
||||
var file *os.File
|
||||
switch len(args) {
|
||||
case 2:
|
||||
if file, err = os.Create(args[1]); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error creating %s: %v", args[1], err)
|
||||
os.Exit(-1)
|
||||
file, err := os.Create(args[1])
|
||||
if err != nil {
|
||||
log.Fatalf("error: unable to write to file %s", args[1])
|
||||
}
|
||||
defer file.Close()
|
||||
default:
|
||||
file = os.Stdout
|
||||
}
|
||||
|
||||
if _, err = file.Write(output); err != nil {
|
||||
log.Fatal("error writing output")
|
||||
if _, err := file.Write(output); err != nil {
|
||||
log.Fatal("error: unable to write output")
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user