2022-05-27 23:30:51 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-04-13 05:25:31 +00:00
|
|
|
// TODO replace imgs
|
2022-05-27 23:30:51 +00:00
|
|
|
|
2023-04-13 05:25:31 +00:00
|
|
|
linkRE := regexp.MustCompile(`\(LINK ([^ ]+?) (.+?)\)`)
|
|
|
|
linkMatches := linkRE.FindAllSubmatch(t, -1)
|
|
|
|
|
|
|
|
imgRE := regexp.MustCompile(`\(IMG ([^ ]+?) (.+?)\)`)
|
|
|
|
imgMatches := imgRE.FindAllSubmatch(t, -1)
|
|
|
|
|
|
|
|
if linkMatches == nil && imgMatches == nil {
|
2022-05-27 23:30:51 +00:00
|
|
|
fmt.Fprintf(o.Out, string(t))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
output := string(t)
|
2022-05-28 00:00:51 +00:00
|
|
|
footer := ""
|
2022-05-27 23:30:51 +00:00
|
|
|
|
2023-04-13 05:41:21 +00:00
|
|
|
linkIx := 0
|
|
|
|
|
2023-04-13 05:25:31 +00:00
|
|
|
for ix, lm := range linkMatches {
|
2023-04-13 05:41:21 +00:00
|
|
|
linkIx = ix
|
2023-04-13 05:25:31 +00:00
|
|
|
rawLink := string(lm[0])
|
|
|
|
link := string(lm[1])
|
|
|
|
title := string(lm[2])
|
2022-05-27 23:30:51 +00:00
|
|
|
switch o.Mode {
|
|
|
|
case "html":
|
2022-05-28 00:00:51 +00:00
|
|
|
output = strings.ReplaceAll(output, rawLink,
|
|
|
|
fmt.Sprintf("<a href=\"%s\">%s</a>", link, title))
|
2022-05-27 23:30:51 +00:00
|
|
|
case "gopher":
|
2022-05-28 00:00:51 +00:00
|
|
|
output = strings.ReplaceAll(output, rawLink, fmt.Sprintf("%s[%d]", title, ix))
|
|
|
|
linkType := "i"
|
|
|
|
if strings.HasPrefix(link, "http") {
|
|
|
|
linkType = "h"
|
|
|
|
}
|
|
|
|
footer += fmt.Sprintf("%s[%d]: %s %s\n", linkType, ix, title, link)
|
2022-05-27 23:30:51 +00:00
|
|
|
case "gemini":
|
2022-05-28 00:06:06 +00:00
|
|
|
output = strings.ReplaceAll(output, rawLink, fmt.Sprintf("%s[%d]", title, ix))
|
|
|
|
footer += fmt.Sprintf("=> %s [%d]: %s\n", link, ix, title)
|
2022-05-27 23:30:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 05:25:31 +00:00
|
|
|
for ix, im := range imgMatches {
|
2023-04-13 05:41:21 +00:00
|
|
|
linkIx += ix
|
2023-04-13 05:25:31 +00:00
|
|
|
rawImg := string(im[0])
|
|
|
|
src := string(im[1])
|
|
|
|
alt := string(im[2])
|
|
|
|
switch o.Mode {
|
|
|
|
case "html":
|
|
|
|
output = strings.ReplaceAll(output, rawImg,
|
|
|
|
fmt.Sprintf("<img src=\"%s\" alt=\"%s\"/>", src, alt))
|
|
|
|
case "gopher":
|
2023-04-13 05:41:21 +00:00
|
|
|
output = strings.ReplaceAll(output, rawImg, fmt.Sprintf("%s[%d]", alt, linkIx))
|
2023-04-13 05:25:31 +00:00
|
|
|
linkType := "p"
|
2023-04-13 05:41:21 +00:00
|
|
|
footer += fmt.Sprintf("%s[%d]: %s %s\n", linkType, linkIx, alt, src)
|
2023-04-13 05:25:31 +00:00
|
|
|
case "gemini":
|
2023-04-13 05:41:21 +00:00
|
|
|
output = strings.ReplaceAll(output, rawImg, fmt.Sprintf("%s[%d]", alt, linkIx))
|
|
|
|
footer += fmt.Sprintf("=> %s [%d]: %s\n", src, linkIx, alt)
|
2023-04-13 05:25:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 00:00:51 +00:00
|
|
|
if footer != "" {
|
|
|
|
output = output + "\n\n" + footer
|
|
|
|
}
|
|
|
|
|
2022-05-27 23:30:51 +00:00
|
|
|
fmt.Fprintf(o.Out, output)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type opts struct {
|
|
|
|
In io.Reader
|
|
|
|
Out io.Writer
|
|
|
|
Mode string
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|