From 58e868de808d40b9dcf17f6b885a76fb4b65fc8c Mon Sep 17 00:00:00 2001 From: Arnas Udovicius Date: Tue, 23 Aug 2022 14:58:18 +0300 Subject: [PATCH] try to convert markdown to gemtext --- formatter.go | 46 +++++++++++++++++++++++++++++++++++++++++++++- go.mod | 1 + go.sum | 2 ++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/formatter.go b/formatter.go index 648ab06..154bdb8 100644 --- a/formatter.go +++ b/formatter.go @@ -1,12 +1,50 @@ package main import ( + "bufio" + "bytes" "errors" + "fmt" + "io" "net/url" "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 badChars := make(map[string]string) @@ -19,7 +57,13 @@ func (fileContent *FileContent) FormatContent() string { content = strings.ReplaceAll(content, k, v) } - return content + var writer bytes.Buffer + s := pipe.New() + s.Use(mdproc.Preprocessor()) + s.Use(mdproc.FormatLinks) + s.Handle(source(strings.NewReader(content)), sink(&writer)) + + return writer.String() } func (file *TreeFile) CategoriesAsUrl() string { diff --git a/go.mod b/go.mod index f30c5de..531231e 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ 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 ) diff --git a/go.sum b/go.sum index a4a5144..9f031c1 100644 --- a/go.sum +++ b/go.sum @@ -51,6 +51,8 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/n0x1m/md2gmi v1.0.0 h1:T4FTIjojSutOvyh7okIO7N59sk7KD9XyQMx35JX5jjA= +github.com/n0x1m/md2gmi v1.0.0/go.mod h1:q5iY7/I5QlC5bUrqmkXVA2FR2HOB9+PmqDhpCNHF/vc= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=