Browse Source

0.1.0 release

main v0.1.0
gryffyn 10 months ago
parent
commit
84e321c517
Signed by: gryffyn GPG Key ID: 6948DD6514D02BEF
  1. 2
      .gitignore
  2. 2
      LICENSE
  3. 2
      README.md
  4. 4
      go.mod
  5. 83
      md2gm.go
  6. 73
      md2gmi.go

2
.gitignore

@ -90,3 +90,5 @@ fabric.properties
# vendor/
go.sum
md2gm

2
LICENSE

@ -1,4 +1,4 @@
MIT License Copyright (c) <year> <copyright holders>
MIT License Copyright (c) 2020 gryffyn
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

2
README.md

@ -1,3 +1,3 @@
# md2gm
# md2gmi
Small Go tool to convert Github markdown (plus HTML and Hugo shortcodes) to Gemini text files.

4
go.mod

@ -1,5 +1,5 @@
module git.neveris.one/gryffyn/md2gm
module git.neveris.one/gryffyn/md2gmi
go 1.15
require github.com/urfave/cli/v2 v2.3.0
require github.com/alexflint/go-arg v1.3.0

83
md2gm.go

@ -1,83 +0,0 @@
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() {
}

73
md2gmi.go

@ -0,0 +1,73 @@
// md2gmi 0.1.0
// gryffyn[at]neveris[dot]one
package main
import (
"io/ioutil"
"os"
"regexp"
"github.com/alexflint/go-arg"
)
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(0644))
return err
}
func md2gmi(in, out string) error {
// opens file for writing
md, err := ifile(in)
// defines regexes and compiles them
reg_html := regexp.MustCompile(`<.+?>.+?<\/.+?>`)
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_0 := reg_hugo.ReplaceAllString(gm, "${1}")
gm_1 := reg_cite.ReplaceAllString(gm_0, "${1}")
gm_2 := reg_shcd.ReplaceAllString(gm_1, "${1}")
// re-does links
reg_link := regexp.MustCompile(`\[(?P<name>.+?)\]\((?P<url>.+?)\)`)
res := reg_link.ReplaceAllStringFunc(gm_2, func(m string) string {
parts := reg_link.FindStringSubmatch(m)
return "\n=> " + parts[2] + " " + parts[1] + "\n"
})
err = ofile(out, res)
return err
}
func main() {
var args struct {
Input string `arg:"positional" help:"input markdown file (.md)"`
Output string `arg:"positional" help:"output gemtext file (.gmi)"`
V bool `arg:"-V" help:"Version"`
}
parse := arg.MustParse(&args)
if args.Input == "" && args.Output == "" && args.V {
println("md2gmi 0.1.0\nby gryffyn")
return
} else if args.Input == "" && args.Output == "" && !(args.V) {
parse.Fail("You must provide INPUT and OUTPUT.")
} else if args.Output == "" && !(args.V) {
parse.Fail("You must provide INPUT and OUTPUT.")
}
err := md2gmi(args.Input, args.Output)
if err != nil {
panic(err)
}
}
Loading…
Cancel
Save