simplier links replacement
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
58e868de80
commit
8bbccfcfea
2 changed files with 20 additions and 43 deletions
62
formatter.go
62
formatter.go
|
@ -1,52 +1,19 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"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 {
|
func (fileContent *FileContent) FormatContent() string {
|
||||||
content := fileContent.Content
|
content := fileContent.Content
|
||||||
|
content = strings.Trim(content, "/n")
|
||||||
|
|
||||||
badChars := make(map[string]string)
|
badChars := make(map[string]string)
|
||||||
badChars["ā"] = "ā"
|
badChars["ā"] = "ā"
|
||||||
badChars["ē"] = "ē"
|
badChars["ē"] = "ē"
|
||||||
|
@ -57,13 +24,24 @@ func (fileContent *FileContent) FormatContent() string {
|
||||||
content = strings.ReplaceAll(content, k, v)
|
content = strings.ReplaceAll(content, k, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
var writer bytes.Buffer
|
var buffer []byte
|
||||||
s := pipe.New()
|
data := []byte(content)
|
||||||
s.Use(mdproc.Preprocessor())
|
re := regexp.MustCompile(`!?\[([^\]*]*)\]\(([^ ]*)\)`)
|
||||||
s.Use(mdproc.FormatLinks)
|
|
||||||
s.Handle(source(strings.NewReader(content)), sink(&writer))
|
|
||||||
|
|
||||||
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 {
|
func (file *TreeFile) CategoriesAsUrl() string {
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -5,7 +5,6 @@ go 1.18
|
||||||
require (
|
require (
|
||||||
git.sr.ht/~adnano/go-gemini v0.2.3
|
git.sr.ht/~adnano/go-gemini v0.2.3
|
||||||
github.com/flosch/pongo2/v6 v6.0.0
|
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/grpc v1.48.0
|
||||||
google.golang.org/protobuf v1.28.1
|
google.golang.org/protobuf v1.28.1
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue