Small Go tool to convert Github markdown (plus HTML and Hugo shortcodes) to Gemini text files.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.7 KiB

package main
import (
"fmt"
"github.com/urfave/cli/v2"
"io/ioutil"
"log"
"os"
"regexp"
)
func ifile(filename string) (string, error) {
content, err := ioutil.ReadFile(filename)
str := string(content)
return str, err
}
func ofile(filename, str string) error {
err := ioutil.WriteFile(filename, []byte(str), os.FileMode.Perm(0777))
return err
}
func md2gm(c *cli.Context) error {
// opens file for writing
md, err := ifile(c.String("input"))
if err != nil {
log.Fatal("File could not be opened.")
}
// defines regexes and compiles them
reg_html := regexp.MustCompile(`<.+?>.+?<\/.+?>`)
reg_link := regexp.MustCompile(`\[(?P<name>.+?)\]\((?P<url>.+?)\)`)
reg_cite := regexp.MustCompile(`\[\^.+?\]\(.+?\)`)
reg_hugo := regexp.MustCompile(`(?s)\+{3}.+?\+{3}`)
reg_shcd := regexp.MustCompile(`{{(.*<.*>.*|.*%.*%.*)}}\n?`)
// deletes hugo and html tags
gm := reg_html.ReplaceAllString(md, "${1}")
gm = reg_hugo.ReplaceAllString(gm, "${1}")
gm = reg_cite.ReplaceAllString(gm, "${1}")
gm = reg_shcd.ReplaceAllString(gm, "${1}")
// redoes links
reg_link_matches := reg_link.FindStringSubmatch("[Mosh](https://mosh.org)")
i_url := reg_link.SubexpIndex("url")
i_name := reg_link.SubexpIndex("name")
print("\n=> " + reg_link_matches[i_url] + " " + reg_link_matches[i_name] + "\n")
}
func runapp() {
app := cli.NewApp()
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "input",
Value: "input",
Usage: "input markdown file",
Required: true,
},
}
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "output",
Value: "output",
Usage: "output gemtext file",
Required: true,
},
}
app.Action = md2gm
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func main() {
}