This repository has been archived on 2020-11-14. You can view files and clone it, but cannot push or open issues or pull requests.
go-gemtext/render.go

70 lines
1.8 KiB
Go

package gemtext
import (
"fmt"
"path/filepath"
"strings"
)
// addPrefix adds the prefix "pref" to a path if it is absolute
// this is to allow protocol-independent absolute links.
func addPrefix(pref string, link string) string {
if filepath.IsAbs(link) {
return filepath.Join(pref, link)
} else {
return 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) {
if obj.Type != LINK {
return "", fmt.Errorf("Invalid Link")
} else if obj.Path == "" {
return "", fmt.Errorf("Invalid Link")
}
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
}