From 34b1a118a355108426c28475489ac1cd0967d521 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Thu, 13 Apr 2023 05:25:31 +0000 Subject: [PATCH] add support for (IMG) macro --- main.go | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 6c0004b..76dee50 100644 --- a/main.go +++ b/main.go @@ -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("\"%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 }