simplier links replacement
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Arnas Udovicius 2022-08-23 15:56:17 +03:00
parent 58e868de80
commit 8bbccfcfea
2 changed files with 20 additions and 43 deletions

View file

@ -1,52 +1,19 @@
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net/url"
"regexp"
"sort"
"strings"
"github.com/n0x1m/md2gmi/mdproc"
"github.com/n0x1m/md2gmi/pipe"
)
func source(r io.Reader) pipe.Source {
return func() chan pipe.StreamItem {
data := make(chan pipe.StreamItem)
s := bufio.NewScanner(r)
go func() {
i := 0
for s.Scan() {
data <- pipe.NewItem(i, s.Bytes())
i++
}
close(data)
}()
return data
}
}
func sink(w *bytes.Buffer) pipe.Sink {
return func(dest chan pipe.StreamItem) {
for b := range dest {
_, err := fmt.Fprintf(w, string(b.Payload()))
if err != nil {
fmt.Println(err)
}
}
}
}
func (fileContent *FileContent) FormatContent() string {
content := fileContent.Content
content = strings.Trim(content, "/n")
badChars := make(map[string]string)
badChars["ā"] = "ā"
badChars["ē"] = "ē"
@ -57,13 +24,24 @@ func (fileContent *FileContent) FormatContent() string {
content = strings.ReplaceAll(content, k, v)
}
var writer bytes.Buffer
s := pipe.New()
s.Use(mdproc.Preprocessor())
s.Use(mdproc.FormatLinks)
s.Handle(source(strings.NewReader(content)), sink(&writer))
var buffer []byte
data := []byte(content)
re := regexp.MustCompile(`!?\[([^\]*]*)\]\(([^ ]*)\)`)
return writer.String()
for i, match := range re.FindAllSubmatch(data, -1) {
replaceWithIndex := append(match[1], fmt.Sprintf("[%d]", i+1)...)
data = bytes.Replace(data, match[0], replaceWithIndex, 1)
// append entry to buffer to be added later
link := fmt.Sprintf("=> %s %d: %s\n", match[2], i+1, match[1])
buffer = append(buffer, link...)
}
// append links to that paragraph
if len(buffer) > 0 {
data = append(data, []byte("\n\n---\n")...)
data = append(data, buffer...)
}
return string(data)
}
func (file *TreeFile) CategoriesAsUrl() string {

1
go.mod
View file

@ -5,7 +5,6 @@ go 1.18
require (
git.sr.ht/~adnano/go-gemini v0.2.3
github.com/flosch/pongo2/v6 v6.0.0
github.com/n0x1m/md2gmi v1.0.0
google.golang.org/grpc v1.48.0
google.golang.org/protobuf v1.28.1
)