improve readability

This commit is contained in:
dre 2021-07-06 01:06:41 +08:00
parent 0944f4dbd5
commit 36b63421ed
4 changed files with 23 additions and 11 deletions

View file

@ -16,10 +16,12 @@ func RemoveComments(in chan pipe.StreamItem) chan pipe.StreamItem {
for b := range in {
data := b.Payload()
touched := false
for _, match := range re.FindAllSubmatch(data, -1) {
data = bytes.Replace(data, match[0], []byte(""), 1)
touched = true
}
if touched && len(bytes.TrimSpace(data)) == 0 {
continue
}

View file

@ -92,12 +92,15 @@ this is code too
`
)
func source(in string) func() chan pipe.StreamItem {
func source(t *testing.T, in string) func() chan pipe.StreamItem {
t.Helper()
return func() chan pipe.StreamItem {
data := make(chan pipe.StreamItem, len(strings.Split(in, "\n")))
for _, line := range strings.Split(in, "\n") {
data <- pipe.NewItem(0, []byte(line))
}
close(data)
return data
@ -105,11 +108,14 @@ func source(in string) func() chan pipe.StreamItem {
}
func sink(t *testing.T, expected string) func(dest chan pipe.StreamItem) {
t.Helper()
return func(dest chan pipe.StreamItem) {
var data []byte
for in := range dest {
data = append(data, in.Payload()...)
}
if string(data) != expected {
t.Errorf("mismatch, expected '%s' but was '%s'", expected, data)
}
@ -117,17 +123,21 @@ func sink(t *testing.T, expected string) func(dest chan pipe.StreamItem) {
}
func TestPreproc(t *testing.T) {
t.Parallel()
s := pipe.New()
s.Use(mdproc.Preproc())
s.Handle(source(input), sink(t, preproc))
s.Handle(source(t, input), sink(t, preproc))
}
func TestMd2Gmi(t *testing.T) {
t.Parallel()
s := pipe.New()
s.Use(mdproc.Preproc())
s.Use(mdproc.RemoveFrontMatter)
s.Use(mdproc.RemoveComments)
s.Use(mdproc.FormatHeadings)
s.Use(mdproc.FormatLinks)
s.Handle(source(input), sink(t, gmi))
s.Handle(source(t, input), sink(t, gmi))
}

View file

@ -103,8 +103,8 @@ func normal(m *fsm, data []byte) stateFn {
if len(bytes.TrimSpace(data)) == 0 {
return normal
}
if data, isList := handleList(data); isList {
//m.blockBuffer = append(data, '\n')
m.blockBuffer = append(m.blockBuffer, data...)
m.blockFlush()

View file

@ -19,26 +19,26 @@ func (s *StreamItem) Context() context.Context {
func NewItem(index int, payload []byte) StreamItem {
var buf bytes.Buffer
w := StreamItem{index: index}
s := StreamItem{index: index}
if err := gob.NewEncoder(&buf).Encode(payload); err != nil {
// assert no broken pipes
panic(err)
}
w.payload = buf.Bytes()
s.payload = buf.Bytes()
return w
return s
}
func (w *StreamItem) Index() int {
return w.index
func (s *StreamItem) Index() int {
return s.index
}
func (w *StreamItem) Payload() []byte {
func (s *StreamItem) Payload() []byte {
var dec []byte
buf := bytes.NewReader(w.payload)
buf := bytes.NewReader(s.payload)
if err := gob.NewDecoder(buf).Decode(&dec); err != nil {
// assert no broken pipes