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)
}
r := regexp.MustCompile(`\(LINK ([^ ]+?) (.+?)\)`)
mms := r.FindAllSubmatch(t, -1)
// TODO replace imgs
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))
return nil
}
@ -28,10 +33,10 @@ func _main(o opts) error {
output := string(t)
footer := ""
for ix, ms := range mms {
rawLink := string(ms[0])
link := string(ms[1])
title := string(ms[2])
for ix, lm := range linkMatches {
rawLink := string(lm[0])
link := string(lm[1])
title := string(lm[2])
switch o.Mode {
case "html":
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 != "" {
output = output + "\n\n" + footer
}