improve lists and remove hugo front matter

This commit is contained in:
dre 2021-07-04 20:45:02 +08:00
parent 6e059224fd
commit 2d78589836
4 changed files with 26 additions and 9 deletions

View file

@ -23,11 +23,11 @@ Usage of ./md2gmi:
go get github.com/n0x1m/md2gmi
cat file.md | md2gmi
md2gmi -in file.md -out file.gmi
md2gmi -i file.md -o file.gmi
The top part of this readme parses from
```md
```markdown
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
@ -42,7 +42,9 @@ protocol](https://gemini.circumlunar.space/) and the [protocol
spec](https://gemini.circumlunar.space/docs/specification.gmi).
```
```md
to
```markdown
Convert Markdown to Gemini gemtext[1] 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.
=> https://gemini.circumlunar.space/docs/gemtext.gmi 1: gemtext

View file

@ -134,7 +134,9 @@ func main() {
FormatLinks(
FormatHeadings(
RemoveComments(
preproc.Process(source.Output()),
RemoveFrontMatter(
preproc.Process(source.Output()),
),
),
),
),

View file

@ -39,7 +39,6 @@ func (m *fsm) Process(in chan WorkItem) chan WorkItem {
continue
}
// fmt.Printf("i preproc '%v'\n", string(b.Payload()))
m.state = m.state(m, b.Payload())
m.sync()
}
@ -49,9 +48,7 @@ func (m *fsm) Process(in chan WorkItem) chan WorkItem {
func (m *fsm) sync() {
if len(m.sendBuffer) > 0 {
//m.sendBuffer = bytes.TrimSpace(m.sendBuffer)
m.sendBuffer = append(m.sendBuffer, '\n')
//fmt.Printf("o preproc '%v'\n", string(m.sendBuffer))
m.out <- New(m.i, m.sendBuffer)
m.sendBuffer = m.sendBuffer[:0]
m.i += 1
@ -60,7 +57,6 @@ func (m *fsm) sync() {
func (m *fsm) blockFlush() {
// blockBuffer to sendbuffer
//fmt.Println("block ", string(m.blockBuffer))
m.sendBuffer = append(m.sendBuffer, m.blockBuffer...)
m.blockBuffer = m.blockBuffer[:0]
@ -80,7 +76,7 @@ func isTerminated(data []byte) bool {
}
func handleList(data []byte) ([]byte, bool) {
re := regexp.MustCompile(`([ ]*[-*])`)
re := regexp.MustCompile(`^([ ]*[-*^]{1,1})[^*-]`)
sub := re.FindSubmatch(data)
// if lists, collapse to single level
if len(sub) > 1 {

17
proc.go
View file

@ -54,6 +54,23 @@ func RemoveComments(in chan WorkItem) chan WorkItem {
return out
}
func RemoveFrontMatter(in chan WorkItem) chan WorkItem {
out := make(chan WorkItem)
go func() {
re := regexp.MustCompile(`---.*---`)
for b := range in {
data := b.Payload()
for _, match := range re.FindAllSubmatch(data, -1) {
data = bytes.Replace(data, match[0], []byte(""), 1)
}
out <- New(b.Index(), append(bytes.TrimSpace(data), '\n'))
//out <- New(b.Index(), data)
}
close(out)
}()
return out
}
func FormatHeadings(in chan WorkItem) chan WorkItem {
out := make(chan WorkItem)
go func() {