Provide a public API
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.
This commit is contained in:
parent
507ec6ad88
commit
4d80d36f00
5 changed files with 53 additions and 57 deletions
|
@ -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
cmd/md2gmn/main.go
Normal file
35
cmd/md2gmn/main.go
Normal file
|
@ -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
go.mod
5
go.mod
|
@ -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
go.sum
2
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
render.go
Normal file
16
render.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue