Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5 #8

Merged
merged 4 commits into from Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .golangci.yml
@@ -0,0 +1,4 @@
linters:
disable:
# no, we won't check every invocation of (io.Writer).Write()
- errcheck
11 changes: 5 additions & 6 deletions cmd/gmnhg/main.go
Expand Up @@ -80,9 +80,9 @@ import (
)

const (
defaultTemplate = "single"
indexMdFilename = "_index.gmi.md"
indexFilename = "index.gmi"
defaultPageTemplate = "single"
indexMdFilename = "_index.gmi.md"
indexFilename = "index.gmi"
)

const (
Expand All @@ -94,7 +94,6 @@ const (

var (
tmplNameRegex = regexp.MustCompile(templateBase + `([\w-_ /]+)\.gotmpl`)
contentNameRegex = regexp.MustCompile(contentBase + `([\w-_ ]+)\.md`)
topLevelPostRegex = regexp.MustCompile(contentBase + `([\w-_ ]+)/([\w-_ ]+)\.md`)
)

Expand Down Expand Up @@ -201,7 +200,7 @@ func main() {
}

// render posts to Gemtext and collect top level posts data
posts := make(map[string]*post, 0)
posts := make(map[string]*post)
topLevelPosts := make(map[string][]*post)
if err := filepath.Walk(contentBase, func(path string, info os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -252,7 +251,7 @@ func main() {
}

var singleTemplate = defaultSingleTemplate
if tmpl, hasTmpl := templates["single"]; hasTmpl {
if tmpl, hasTmpl := templates[defaultPageTemplate]; hasTmpl {
singleTemplate = tmpl
}

Expand Down
32 changes: 28 additions & 4 deletions internal/gemini/renderer.go
Expand Up @@ -18,6 +18,7 @@
package gemini

import (
"bytes"
"fmt"
"io"
"regexp"
Expand All @@ -32,6 +33,7 @@ var (
lineBreak = []byte{'\n'}
space = []byte{' '}
linkPrefix = []byte("=> ")
quoteBrPrefix = []byte("\n> ")
quotePrefix = []byte("> ")
itemPrefix = []byte("* ")
itemIndent = []byte{'\t'}
Expand Down Expand Up @@ -86,9 +88,16 @@ func (r Renderer) blockquote(w io.Writer, node *ast.BlockQuote, entering bool) {
// TODO: Renderer.blockquote: needs support for subnode rendering;
// ideally to be merged with paragraph
if entering {
if para, ok := node.Children[0].(*ast.Paragraph); ok {
w.Write(quotePrefix)
r.text(w, para)
if node := node.AsContainer(); node != nil {
for _, child := range node.Children {
w.Write(quotePrefix)
r.blockquoteText(w, child)
// double linebreak to ensure Gemini clients don't merge
// quotes; gomarkdown assumes separate blockquotes are
// paragraphs of the same blockquote while we don't
w.Write(lineBreak)
w.Write(lineBreak)
}
}
}
}
Expand Down Expand Up @@ -137,6 +146,7 @@ func (r Renderer) paragraph(w io.Writer, node *ast.Paragraph, entering bool) (no
}
linksOnly := func() bool {
for _, child := range children {
// TODO: simplify
if _, ok := child.(*ast.Link); ok {
continue
}
Expand Down Expand Up @@ -251,6 +261,21 @@ func (r Renderer) text(w io.Writer, node ast.Node) {
}
}

// TODO: this really should've been unified with text(), but having two
// extra params for prefix/line breaks is not neat
func (r Renderer) blockquoteText(w io.Writer, node ast.Node) {
if node := node.AsLeaf(); node != nil {
// pad every line break with blockquote symbol
w.Write([]byte(bytes.ReplaceAll(node.Literal, lineBreak, quoteBrPrefix)))
return
}
if node := node.AsContainer(); node != nil {
for _, child := range node.Children {
r.blockquoteText(w, child)
}
}
}

func extractText(node ast.Node) string {
if node := node.AsLeaf(); node != nil {
return strings.ReplaceAll(string(node.Literal), "\n", " ")
Expand Down Expand Up @@ -329,7 +354,6 @@ func (r Renderer) RenderNode(w io.Writer, node ast.Node, entering bool) ast.Walk
switch node := node.(type) {
case *ast.BlockQuote:
r.blockquote(w, node, entering)
noNewLine = false
case *ast.Heading:
r.heading(w, node, entering)
noNewLine = false
Expand Down