From 179a20e0dbd59eaadf37e3ffc48fde9d97051c43 Mon Sep 17 00:00:00 2001 From: Timur Demin Date: Sun, 8 Nov 2020 17:40:16 +0500 Subject: [PATCH] Provide a public APIThis also introduces a simple program, md2gmn, that is meant for testingthe renderer on Markdown files. Nonetheless, it can be used as astandalone tool as well. --- cmd/gmnhg/main.go | 52 +--------------------------------------------- cmd/md2gmn/main.go | 35 +++++++++++++++++++++++++++++++ go.mod | 5 +---- go.sum | 2 -- render.go | 16 ++++++++++++++ 5 files changed, 53 insertions(+), 57 deletions(-) create mode 100644 cmd/md2gmn/main.go create mode 100644 render.go diff --git a/cmd/gmnhg/main.go b/cmd/gmnhg/main.go index 51543b5..67fb969 100644 --- a/cmd/gmnhg/main.go +++ b/cmd/gmnhg/main.go @@ -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") } diff --git a/cmd/md2gmn/main.go b/cmd/md2gmn/main.go new file mode 100644 index 0000000..0bf33f5 --- /dev/null +++ b/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)) +} diff --git a/go.mod b/go.mod index 17174ca..8f09c36 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index 88392a2..6b74de0 100644 --- a/go.sum +++ b/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= diff --git a/render.go b/render.go new file mode 100644 index 0000000..513d0c3 --- /dev/null +++ b/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 +}