Browse Source

done: flags, i/o, basic regex

todo: reg_link, shortcodes, testing
main
gryffyn 1 year ago
parent
commit
5235dfeda4
Signed by: gryffyn GPG Key ID: 6948DD6514D02BEF
  1. 5
      go.mod
  2. 81
      md2gm.go

5
go.mod

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

81
md2gm.go

@ -0,0 +1,81 @@
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}`)
// deletes hugo and html tags
gm := reg_html.ReplaceAllString(md, "${1}")
gm = reg_hugo.ReplaceAllString(gm, "${1}")
gm = reg_cite.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() {
}
Loading…
Cancel
Save