add support for (IMG) macro

trunk
vilmibm 2023-04-13 05:25:31 +00:00
parent 8f39c5a1e9
commit 34b1a118a3
1 changed files with 30 additions and 7 deletions

37
main.go
View File

@ -17,10 +17,15 @@ func _main(o opts) error {
return fmt.Errorf("could not read from stdin: %w", err) return fmt.Errorf("could not read from stdin: %w", err)
} }
r := regexp.MustCompile(`\(LINK ([^ ]+?) (.+?)\)`) // TODO replace imgs
mms := r.FindAllSubmatch(t, -1)
if mms == nil { linkRE := regexp.MustCompile(`\(LINK ([^ ]+?) (.+?)\)`)
linkMatches := linkRE.FindAllSubmatch(t, -1)
imgRE := regexp.MustCompile(`\(IMG ([^ ]+?) (.+?)\)`)
imgMatches := imgRE.FindAllSubmatch(t, -1)
if linkMatches == nil && imgMatches == nil {
fmt.Fprintf(o.Out, string(t)) fmt.Fprintf(o.Out, string(t))
return nil return nil
} }
@ -28,10 +33,10 @@ func _main(o opts) error {
output := string(t) output := string(t)
footer := "" footer := ""
for ix, ms := range mms { for ix, lm := range linkMatches {
rawLink := string(ms[0]) rawLink := string(lm[0])
link := string(ms[1]) link := string(lm[1])
title := string(ms[2]) title := string(lm[2])
switch o.Mode { switch o.Mode {
case "html": case "html":
output = strings.ReplaceAll(output, rawLink, output = strings.ReplaceAll(output, rawLink,
@ -49,6 +54,24 @@ func _main(o opts) error {
} }
} }
for ix, im := range imgMatches {
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":
output = strings.ReplaceAll(output, rawImg, fmt.Sprintf("%s[%d]", alt, ix))
linkType := "p"
footer += fmt.Sprintf("%s[%d]: %s %s\n", linkType, ix, alt, src)
case "gemini":
output = strings.ReplaceAll(output, rawImg, fmt.Sprintf("%s[%d]", alt, ix))
footer += fmt.Sprintf("=> %s [%d]: %s\n", src, ix, alt)
}
}
if footer != "" { if footer != "" {
output = output + "\n\n" + footer output = output + "\n\n" + footer
} }