add context, remove gobs, update readme

This commit is contained in:
dre 2021-07-06 19:36:41 +08:00
parent 4817e23eee
commit 3ded4c947f
2 changed files with 18 additions and 25 deletions

View file

@ -2,12 +2,13 @@
Convert Markdown to Gemini [gemtext](https://gemini.circumlunar.space/docs/gemtext.gmi) markup with
Go. Working with streams and pipes for UNIX like behavior utilizing Go channels. Processing streams
line by line is slightly more complex than it needs to be as I'm playing with channels and state
machines here.
line by line is slightly more complex than it needs to be as I was toying with channels and state
machines.
Internally md2gmi does a 1st pass that constructs the blocks of single lines for gemtext. This is
then streamed to the 2nd pass line by line. The 2nd pass will convert links, fix headings and stream
line by line to the output sink. The sink is either a file or stdout.
Internally md2gmi does a 1st pass that constructs the blocks of single lines for gemtext from one or
multiple lines of an input stream. These blocks are then streamed to the 2nd passes. The 2nd pass
will convert hugo front matters, links, fix headings etc. These stages/passes can be composed and
chained with go pipelines. The output sink is either a file or stdout.
### Usage

View file

@ -1,9 +1,7 @@
package pipe
import (
"bytes"
"context"
"encoding/gob"
)
type StreamItem struct {
@ -17,17 +15,20 @@ func (s *StreamItem) Context() context.Context {
}
func NewItem(index int, payload []byte) StreamItem {
var buf bytes.Buffer
return newItem(context.Background(), index, payload)
}
s := StreamItem{index: index}
func NewItemWithContext(ctx context.Context, index int, payload []byte) StreamItem {
return newItem(ctx, index, payload)
}
if err := gob.NewEncoder(&buf).Encode(payload); err != nil {
// assert no broken pipes
panic(err)
func newItem(ctx context.Context, index int, payload []byte) StreamItem {
s := StreamItem{
ctx: ctx,
index: index,
payload: make([]byte, len(payload)),
}
s.payload = buf.Bytes()
copy(s.payload, payload)
return s
}
@ -36,14 +37,5 @@ func (s *StreamItem) Index() int {
}
func (s *StreamItem) Payload() []byte {
var dec []byte
buf := bytes.NewReader(s.payload)
if err := gob.NewDecoder(buf).Decode(&dec); err != nil {
// assert no broken pipes
panic(err)
}
return dec
return s.payload
}