diff --git a/main.go b/main.go new file mode 100644 index 0000000..8bb7532 --- /dev/null +++ b/main.go @@ -0,0 +1,70 @@ +package main + +import ( + _ "embed" + "flag" + "fmt" + "io" + "io/ioutil" + "os" + "regexp" + "strings" +) + +func _main(o opts) error { + t, err := ioutil.ReadAll(o.In) + if err != nil { + return fmt.Errorf("could not read from stdin: %w", err) + } + + r := regexp.MustCompile(`\(LINK ([^ ]+?) (.+?)\)`) + mms := r.FindAllSubmatch(t, -1) + + if mms == nil { + fmt.Fprintf(o.Out, string(t)) + return nil + } + + output := string(t) + + for _, ms := range mms { + switch o.Mode { + case "html": + output = strings.ReplaceAll(output, string(ms[0]), + fmt.Sprintf("%s", string(ms[1]), string(ms[2]))) + case "gopher": + break + case "gemini": + break + } + } + + fmt.Fprintf(o.Out, output) + + return nil +} + +type opts struct { + In io.Reader + Out io.Writer + Mode string +} + +func main() { + // TODO mode flag + var modeFlag = flag.String("mode", "", "one of html, gopher, gemini.") + flag.Parse() + if *modeFlag == "" || (*modeFlag != "html" && *modeFlag != "gopher" && *modeFlag != "gemini") { + fmt.Fprintln(os.Stderr, "--mode must be specified and one of: html, gopher, gemini") + os.Exit(1) + } + o := opts{ + In: os.Stdin, + Out: os.Stdout, + Mode: *modeFlag, + } + if err := _main(o); err != nil { + fmt.Fprintln(os.Stderr, err.Error()) + os.Exit(2) + } +}