Skip to content

Commit

Permalink
Provide a public API
Browse files Browse the repository at this point in the history
This also introduces a simple program, md2gmn, that is meant for testing
the renderer on Markdown files. Nonetheless, it can be used as
a standalone tool as well.
  • Loading branch information
tdemin committed Nov 11, 2020
1 parent 507ec6a commit 4d80d36
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 57 deletions.
52 changes: 1 addition & 51 deletions cmd/gmnhg/main.go
Expand Up @@ -3,56 +3,6 @@
// TODO: it is yet to actually do that.
package main

import (
"fmt"

"git.tdem.in/tdemin/gmnhg/internal/gemini"
"github.com/davecgh/go-spew/spew"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/parser"
)

var text = `
# Some document
This is some markdown [text](https://tdem.in). This is some more text.
![This is some image](https://tdem.in/favicon.ico)
[This is some full-blown link.](https://tdem.in/nyaa)
This is some more plain text. More of it!
+ Unordered list item
+ Another list item
* Indented list item.
* Another one.
+ Third.
1. Ordered list item.
2. Another one.
* and another inset list.
* text.
3. Yay.
` + "```" + `
some preformatted text
another line of preformatted text
more lines of preformatted text
` + "```" + `
## Subheading 2
More text!
> Some weird blockquote. More text.
> More quote text.
`

func main() {
ast := markdown.Parse([]byte(text), parser.NewWithExtensions(parser.CommonExtensions))
spew.Dump(ast)
geminiContent := markdown.Render(ast, gemini.NewRenderer())
fmt.Printf("---\noriginal:\n---\n%s---\ngemini:\n---\n%s", text, geminiContent)
println("in development")
}
35 changes: 35 additions & 0 deletions cmd/md2gmn/main.go
@@ -0,0 +1,35 @@
// md2gmn converts Markdown text files to text/gemini.
package main

import (
"flag"
"io/ioutil"
"os"

gemini "git.tdem.in/tdemin/gmnhg"
)

func main() {
var (
input string
file *os.File
)
flag.StringVar(&input, "f", "", "input file")
flag.Parse()

if input != "" {
var err error
file, err = os.Open(input)
if err != nil {
panic(err)
}
} else {
file = os.Stdin
}
text, err := ioutil.ReadAll(file)
if err != nil {
panic(err)
}

os.Stdout.Write(gemini.RenderMarkdown(text))
}
5 changes: 1 addition & 4 deletions go.mod
Expand Up @@ -2,7 +2,4 @@ module git.tdem.in/tdemin/gmnhg

go 1.15

require (
github.com/davecgh/go-spew v1.1.1
github.com/gomarkdown/markdown v0.0.0-20201024011455-45c732cc8a6b
)
require github.com/gomarkdown/markdown v0.0.0-20201024011455-45c732cc8a6b
2 changes: 0 additions & 2 deletions go.sum
@@ -1,5 +1,3 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gomarkdown/markdown v0.0.0-20201024011455-45c732cc8a6b h1:Om9FdD4lzIJELyJxwr9EWSjaG6GMUNS3iebnhrGevhI=
github.com/gomarkdown/markdown v0.0.0-20201024011455-45c732cc8a6b/go.mod h1:aii0r/K0ZnHv7G0KF7xy1v0A7s2Ljrb5byB7MO5p6TU=
golang.org/dl v0.0.0-20190829154251-82a15e2f2ead/go.mod h1:IUMfjQLJQd4UTqG1Z90tenwKoCX93Gn3MAQJMOSBsDQ=
16 changes: 16 additions & 0 deletions render.go
@@ -0,0 +1,16 @@
package gemini

import (
"git.tdem.in/tdemin/gmnhg/internal/gemini"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/parser"
)

// RenderMarkdown converts Markdown text to text/gemini using gomarkdown.
//
// gomarkdown doesn't return any errors, nor does this function.
func RenderMarkdown(md []byte) (geminiText []byte) {
ast := markdown.Parse(md, parser.NewWithExtensions(parser.CommonExtensions))
geminiContent := markdown.Render(ast, gemini.NewRenderer())
return geminiContent
}

0 comments on commit 4d80d36

Please sign in to comment.