Parse: take an io.Reader argument instead of a string
This commit is contained in:
parent
ea9008f19c
commit
fc11ba4282
@ -5,13 +5,11 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/knieriem/markdown"
|
"github.com/knieriem/markdown"
|
||||||
"io/ioutil"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var b []byte
|
|
||||||
|
|
||||||
flag.Usage = func() {
|
flag.Usage = func() {
|
||||||
fmt.Fprintf(os.Stderr, "Usage: %s [FILE]\n", os.Args[0])
|
fmt.Fprintf(os.Stderr, "Usage: %s [FILE]\n", os.Args[0])
|
||||||
flag.PrintDefaults()
|
flag.PrintDefaults()
|
||||||
@ -21,10 +19,14 @@ func main() {
|
|||||||
optDlists := flag.Bool("dlists", false, "support definitions lists")
|
optDlists := flag.Bool("dlists", false, "support definitions lists")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
r := os.Stdin
|
||||||
if flag.NArg() > 0 {
|
if flag.NArg() > 0 {
|
||||||
b, _ = ioutil.ReadFile(flag.Arg(0))
|
f, err := os.Open(flag.Arg(0))
|
||||||
} else {
|
if err != nil {
|
||||||
b, _ = ioutil.ReadAll(os.Stdin)
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
r = f
|
||||||
}
|
}
|
||||||
|
|
||||||
e := markdown.Extensions{
|
e := markdown.Extensions{
|
||||||
@ -36,7 +38,7 @@ func main() {
|
|||||||
startPProf()
|
startPProf()
|
||||||
defer stopPProf()
|
defer stopPProf()
|
||||||
|
|
||||||
doc := markdown.Parse(string(b), e)
|
doc := markdown.Parse(r, e)
|
||||||
w := bufio.NewWriter(os.Stdout)
|
w := bufio.NewWriter(os.Stdout)
|
||||||
doc.WriteHtml(w)
|
doc.WriteHtml(w)
|
||||||
w.Flush()
|
w.Flush()
|
||||||
|
9
doc.go
9
doc.go
@ -3,17 +3,16 @@ A translation of peg-markdown [1] into Go.
|
|||||||
|
|
||||||
Usage example:
|
Usage example:
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
md "markdown"
|
md "github.com/knieriem/markdown"
|
||||||
"os"
|
"os"
|
||||||
"io/ioutil"
|
|
||||||
"bufio"
|
"bufio"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
b, _ := ioutil.ReadAll(os.Stdin)
|
doc := md.Parse(os.Stdin, md.Extensions{Smart: true})
|
||||||
|
|
||||||
doc := md.Parse(string(b), md.Extensions{Smart: true})
|
|
||||||
|
|
||||||
w := bufio.NewWriter(os.Stdout)
|
w := bufio.NewWriter(os.Stdout)
|
||||||
doc.WriteHtml(w)
|
doc.WriteHtml(w)
|
||||||
|
29
markdown.go
29
markdown.go
@ -21,6 +21,7 @@ package markdown
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -35,7 +36,7 @@ type Extensions struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Parse converts a Markdown document into a tree for later output processing.
|
// Parse converts a Markdown document into a tree for later output processing.
|
||||||
func Parse(text string, ext Extensions) *Doc {
|
func Parse(r io.Reader, ext Extensions) *Doc {
|
||||||
d := new(Doc)
|
d := new(Doc)
|
||||||
d.extension = ext
|
d.extension = ext
|
||||||
|
|
||||||
@ -43,7 +44,7 @@ func Parse(text string, ext Extensions) *Doc {
|
|||||||
d.parser.Doc = d
|
d.parser.Doc = d
|
||||||
d.parser.Init()
|
d.parser.Init()
|
||||||
|
|
||||||
s := preformat(text)
|
s := preformat(r)
|
||||||
|
|
||||||
d.parseRule(ruleReferences, s)
|
d.parseRule(ruleReferences, s)
|
||||||
if ext.Notes {
|
if ext.Notes {
|
||||||
@ -109,21 +110,27 @@ const (
|
|||||||
/* preformat - allocate and copy text buffer while
|
/* preformat - allocate and copy text buffer while
|
||||||
* performing tab expansion.
|
* performing tab expansion.
|
||||||
*/
|
*/
|
||||||
func preformat(text string) (s string) {
|
func preformat(r io.Reader) (s string) {
|
||||||
charstotab := TABSTOP
|
charstotab := TABSTOP
|
||||||
i0 := 0
|
buf := make([]byte, 32768)
|
||||||
|
|
||||||
b := bytes.NewBuffer(make([]byte, 0, len(text)+256))
|
b := bytes.NewBuffer(make([]byte, 0, 32768))
|
||||||
for i, _ := range text {
|
for {
|
||||||
switch text[i] {
|
n, err := r.Read(buf)
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
i0 := 0
|
||||||
|
for i := range buf[:n] {
|
||||||
|
switch buf[i] {
|
||||||
case '\t':
|
case '\t':
|
||||||
b.WriteString(text[i0:i])
|
b.Write(buf[i0:i])
|
||||||
for ; charstotab > 0; charstotab-- {
|
for ; charstotab > 0; charstotab-- {
|
||||||
b.WriteByte(' ')
|
b.WriteByte(' ')
|
||||||
}
|
}
|
||||||
i0 = i + 1
|
i0 = i + 1
|
||||||
case '\n':
|
case '\n':
|
||||||
b.WriteString(text[i0 : i+1])
|
b.Write(buf[i0 : i+1])
|
||||||
i0 = i + 1
|
i0 = i + 1
|
||||||
charstotab = TABSTOP
|
charstotab = TABSTOP
|
||||||
default:
|
default:
|
||||||
@ -133,7 +140,9 @@ func preformat(text string) (s string) {
|
|||||||
charstotab = TABSTOP
|
charstotab = TABSTOP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
b.WriteString(text[i0:])
|
b.Write(buf[i0:n])
|
||||||
|
}
|
||||||
|
|
||||||
b.WriteString("\n\n")
|
b.WriteString("\n\n")
|
||||||
return b.String()
|
return b.String()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user