reduce go program in scope to just do the link processing

trunk
vilmibm 2022-05-28 00:30:51 +01:00
parent 1fb18f9f9f
commit 4fc74f5222
1 changed files with 70 additions and 0 deletions

70
main.go 100644
View File

@ -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("<a href=\"%s\">%s</a>", 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)
}
}