gm

an extremely simple gemini browser/formatter
git clone git://bvnf.space/gm.git
Log | Files | Refs | LICENSE

gemtext.go (1569B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
package main

import (
    "strings"
)

// Prettify formats gemtext nicely with escape sequences.
func Prettify(plain *string) *string {
    lines := strings.Split(*plain, "\n")
    outlines := make([]string, 0, len(lines))
    preform := false
    for _, line := range lines {
        outline := line
        line = strings.TrimSpace(line)
        fwspace := strings.Index(line, " ")
        var firstword string
        if fwspace == -1 {
            firstword = line
        } else {
            firstword = line[:fwspace]
        }
        if preform && firstword != "```" {
            outlines = append(outlines, outline)
            continue
        }
        if len(firstword) > 3 {
            firstword = firstword[:3]
        }

        switch firstword {
        case "```":
            // preformatted
            preform = !preform
            continue
        case "#":
            // heading
            outline = "\033[1;31m" + outline + "\033[m"
        case "##":
            // subheading
            outline = "\033[1;32m" + outline + "\033[m"
        case "###":
            // sub-subheading
            outline = "\033[1;33m" + outline + "\033[m"
        case "*":
            // list
            outline = "  •" + line[1:]
        case ">":
            // quote
            outline = "\t\033[3;37m" + outline + "\033[m"
        case "=>":
            // link
            outline = "\033[4;34m" + outline + "\033[m"
        }

        outlines = append(outlines, outline)
    }

    pretty := strings.Join(outlines, "\n")
    return &pretty
}