From c23030733fe4cba578b14ad2c1d1292891202562 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Thu, 19 Jan 2023 16:02:27 +0100 Subject: Add support for gemtext posts --- src/gmi/gemtext.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/gmi/gemtext.go (limited to 'src/gmi/gemtext.go') diff --git a/src/gmi/gemtext.go b/src/gmi/gemtext.go new file mode 100644 index 0000000..a1136bd --- /dev/null +++ b/src/gmi/gemtext.go @@ -0,0 +1,73 @@ +package gmi + +import ( + "bufio" + "errors" + "fmt" + "io" + "log" + "path" + "regexp" + "strings" +) + +func hasImgExt(p string) bool { + switch path.Ext(strings.ToLower(p)) { + case ".jpg", ".jpeg", ".png", ".gif", ".svg": + return true + default: + return false + } +} + +// matches `=> dstURL [optional description]` +var linkRegexp = regexp.MustCompile(`^=>\s+(\S+)\s*(.*?)\s*$`) + +// GemtextToMarkdown reads a gemtext formatted body from the Reader and writes +// the markdown version of that body to the Writer. +func GemtextToMarkdown(dst io.Writer, src io.Reader) error { + + bufSrc := bufio.NewReader(src) + + for { + + line, err := bufSrc.ReadString('\n') + if err != nil && !errors.Is(err, io.EOF) { + return fmt.Errorf("reading: %w", err) + } + + last := err == io.EOF + + if match := linkRegexp.FindStringSubmatch(line); len(match) > 0 { + + isImg := hasImgExt(match[1]) + + descr := match[2] + + if descr != "" { + // ok + } else if isImg { + descr = "Image" + } else { + descr = "Link" + } + + log.Printf("descr:%q", descr) + + line = fmt.Sprintf("[%s](%s)\n", descr, match[1]) + + if isImg { + line = "!" + line + } + } + + if _, err := dst.Write([]byte(line)); err != nil { + return fmt.Errorf("writing: %w", err) + } + + if last { + return nil + } + } + +} -- cgit v1.2.3