finish gemtext renderer for now
This commit is contained in:
parent
e14436a8e9
commit
d24c5846c4
55
render.go
55
render.go
|
@ -3,6 +3,7 @@ package gemtext
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// addPrefix adds the prefix "pref" to a path if it is absolute
|
// addPrefix adds the prefix "pref" to a path if it is absolute
|
||||||
|
@ -17,11 +18,53 @@ func addPrefix(pref string, link string) string {
|
||||||
|
|
||||||
// RenderLink takes a GemtextObject and returns a gemini link as a string or an error if the object is not a link.
|
// RenderLink takes a GemtextObject and returns a gemini link as a string or an error if the object is not a link.
|
||||||
func RenderLink(obj GemtextObject) (string, error) {
|
func RenderLink(obj GemtextObject) (string, error) {
|
||||||
if obj.Type != LINK {
|
if obj.Type != LINK {
|
||||||
return "", fmt.Errorf("Not a link!")
|
return "", fmt.Errorf("Invalid Link")
|
||||||
} else {
|
} else if obj.Path == "" {
|
||||||
str := fmt.Sprintf("=> %s %s",obj.Path, obj.Text)
|
return "", fmt.Errorf("Invalid Link")
|
||||||
return str,nil
|
}
|
||||||
}
|
str := fmt.Sprintf("=> %s %s",obj.Path, obj.Text)
|
||||||
|
return str,nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RenderHeading takes a GemtextObject and returns a gemini heading as a string or an error if the object is not a heading.
|
||||||
|
func RenderHeading(obj GemtextObject) (string, error) {
|
||||||
|
if obj.Type != HEADING {
|
||||||
|
return "", fmt.Errorf("Invalid Heading (Not a Heading)")
|
||||||
|
} else if obj.Level > 3 {
|
||||||
|
return "", fmt.Errorf("Invalid Heading (Invalid level)")
|
||||||
|
}
|
||||||
|
return strings.Repeat("#", obj.Level) + " " + obj.Text, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RenderGemtext takes a GemtextPage and renders it into Gemtext.
|
||||||
|
func RenderGemtext(p GemtextPage) (string, error) {
|
||||||
|
str := ""
|
||||||
|
for _, o := range p {
|
||||||
|
switch {
|
||||||
|
case o.Type == TEXT:
|
||||||
|
str += o.Text + "\n"
|
||||||
|
case o.Type == LINK:
|
||||||
|
l, err := RenderLink(o)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
str += l + "\n"
|
||||||
|
case o.Type == PREFORMATTED_TOGGLE:
|
||||||
|
str += "```" + o.Text + "\n"
|
||||||
|
case o.Type == PREFORMATTED_TEXT:
|
||||||
|
str += o.Text + "\n"
|
||||||
|
case o.Type == HEADING:
|
||||||
|
l, err := RenderHeading(o)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
str += l + "\n"
|
||||||
|
case o.Type == LIST:
|
||||||
|
str += "* " + o.Text + "\n"
|
||||||
|
case o.Type == QUOTE:
|
||||||
|
str += ">" + o.Text + "\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return str, nil
|
||||||
|
}
|
|
@ -0,0 +1,48 @@
|
||||||
|
package gemtext
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
type PrefixCase struct {
|
||||||
|
Prefix string
|
||||||
|
Link string
|
||||||
|
Want string
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddPrefix(t *testing.T) {
|
||||||
|
cases := []PrefixCase{
|
||||||
|
PrefixCase{Prefix: "", Link: "/test/", Want: "/test"},
|
||||||
|
PrefixCase{Prefix:"https:itwont.work/", Link: "/test/stuff.gmi", Want: "https:itwont.work/test/stuff.gmi"},
|
||||||
|
PrefixCase{Prefix:"https:itwont.work/", Link: "test/stuff.gmi", Want: "test/stuff.gmi"},
|
||||||
|
PrefixCase{Prefix:"", Link: "", Want: ""},
|
||||||
|
PrefixCase{Prefix:"https:itwont.work/", Link: "", Want: ""},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
got := addPrefix(c.Prefix,c.Link)
|
||||||
|
if got != c.Want {
|
||||||
|
t.Errorf("Case %#v, Got %#v", c, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type LineRenderCase struct {
|
||||||
|
In GemtextObject
|
||||||
|
Want string
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRenderHeading(t *testing.T) {
|
||||||
|
cases := []LineRenderCase{
|
||||||
|
LineRenderCase{In: GemtextObject{Type: HEADING, Level: 1, Text: "Hello"}, Want:"# Hello"}, // I'm not adding a Literal to these cases.
|
||||||
|
LineRenderCase{In: GemtextObject{Type: HEADING, Level: 2, Text: ""}, Want:"## "},
|
||||||
|
LineRenderCase{In: GemtextObject{Type: HEADING, Level: 2, Text: "Hello"}, Want:"## Hello"},
|
||||||
|
LineRenderCase{In: GemtextObject{Type: HEADING, Level: 3, Text: "This contains spaces"}, Want:"### This contains spaces"},
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
got, _ := RenderHeading(c.In)
|
||||||
|
if got != c.Want {
|
||||||
|
t.Errorf("Case %#v, Got %#v", c, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue