add preproc and proc
This commit is contained in:
parent
a7316fff66
commit
5eaedce5e6
3 changed files with 65 additions and 17 deletions
20
main.go
20
main.go
|
@ -23,7 +23,7 @@ func reader(in string) (io.Reader, error) {
|
|||
|
||||
func writer(out string) (io.Writer, error) {
|
||||
if out != "" {
|
||||
file, err := os.Open(out)
|
||||
file, err := os.Create(out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ type ir struct {
|
|||
r io.Reader
|
||||
}
|
||||
|
||||
func NewIr(r io.Reader) *ir {
|
||||
func InputStream(r io.Reader) *ir {
|
||||
return &ir{r: r}
|
||||
}
|
||||
|
||||
|
@ -62,7 +62,7 @@ type ow struct {
|
|||
w io.Writer
|
||||
}
|
||||
|
||||
func NewOw(w io.Writer) *ow {
|
||||
func OutputStream(w io.Writer) *ow {
|
||||
return &ow{w: w}
|
||||
}
|
||||
|
||||
|
@ -75,8 +75,8 @@ func (m *ow) Input(data chan []byte) {
|
|||
func main() {
|
||||
var in, out string
|
||||
|
||||
flag.StringVar(&in, "in", "", "specify a .md (Markdown) file to read from, otherwise stdin (default)")
|
||||
flag.StringVar(&out, "out", "", "specify a .gmi (gemtext) file to write to, otherwise stdout (default)")
|
||||
flag.StringVar(&in, "f", "", "specify a .md (Markdown) file to read from, otherwise stdin (default)")
|
||||
flag.StringVar(&out, "o", "", "specify a .gmi (gemtext) file to write to, otherwise stdout (default)")
|
||||
flag.Parse()
|
||||
|
||||
r, err := reader(in)
|
||||
|
@ -91,9 +91,11 @@ func main() {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
source := NewIr(r)
|
||||
sink := NewOw(w)
|
||||
preproc := NewParser()
|
||||
source := InputStream(r)
|
||||
sink := OutputStream(w)
|
||||
preproc := NewPreproc()
|
||||
proc := NewProc()
|
||||
|
||||
sink.Input(preproc.Parse(source.Output()))
|
||||
//sink.Input(preproc.Process(source.Output()))
|
||||
sink.Input(proc.Process(preproc.Process(source.Output())))
|
||||
}
|
||||
|
|
|
@ -14,11 +14,11 @@ type fsm struct {
|
|||
pending []byte
|
||||
}
|
||||
|
||||
func NewParser() *fsm {
|
||||
func NewPreproc() *fsm {
|
||||
return &fsm{}
|
||||
}
|
||||
|
||||
func (m *fsm) Parse(in chan []byte) chan []byte {
|
||||
func (m *fsm) Process(in chan []byte) chan []byte {
|
||||
m.out = make(chan []byte)
|
||||
go func() {
|
||||
for m.state = normal; m.state != nil; {
|
||||
|
@ -90,7 +90,6 @@ func normal(m *fsm, data []byte) stateFn {
|
|||
return paragraph
|
||||
}
|
||||
// TODO
|
||||
// find links
|
||||
// collapse lists
|
||||
m.out <- append(data, '\n')
|
||||
|
||||
|
@ -124,8 +123,3 @@ func paragraph(m *fsm, data []byte) stateFn {
|
|||
m.buffer = append(m.buffer, []byte(" ")...)
|
||||
return paragraph
|
||||
}
|
||||
|
||||
func link(m *fsm, data []byte) stateFn {
|
||||
|
||||
return link
|
||||
}
|
52
proc.go
Normal file
52
proc.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package main
|
||||
|
||||
// state function
|
||||
type stateFn2 func(*proc, []byte) stateFn2
|
||||
|
||||
// state machine
|
||||
type proc struct {
|
||||
state stateFn2
|
||||
out chan []byte
|
||||
|
||||
// combining multiple input lines
|
||||
buffer []byte
|
||||
// if we have a termination rule to abide, e.g. implied code fences
|
||||
pending []byte
|
||||
}
|
||||
|
||||
func NewProc() *proc {
|
||||
return &proc{}
|
||||
}
|
||||
|
||||
func (m *proc) Process(in chan []byte) chan []byte {
|
||||
m.out = make(chan []byte)
|
||||
go func() {
|
||||
for m.state = line; m.state != nil; {
|
||||
b, ok := <-in
|
||||
if !ok {
|
||||
m.flush()
|
||||
close(m.out)
|
||||
m.state = nil
|
||||
continue
|
||||
}
|
||||
m.state = m.state(m, b)
|
||||
}
|
||||
}()
|
||||
return m.out
|
||||
}
|
||||
|
||||
func (m *proc) flush() {
|
||||
if len(m.pending) > 0 {
|
||||
m.out <- append(m.pending, '\n')
|
||||
m.pending = m.pending[:0]
|
||||
}
|
||||
}
|
||||
|
||||
func line(m *proc, data []byte) stateFn2 {
|
||||
// TODO
|
||||
// find links
|
||||
// collapse lists
|
||||
m.out <- data
|
||||
|
||||
return line
|
||||
}
|
Loading…
Reference in a new issue