| 1 | // Package highlight kleurt een bronbestand per regel. |
| 2 | package highlight |
| 3 | |
| 4 | import ( |
| 5 | "html" |
| 6 | "path" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // Lines geeft per regel HTML. Tekst uit het bestand is geëscaped. |
| 11 | func Lines(filename, src string) []string { |
| 12 | lang := langOf(filename) |
| 13 | inBlock := false |
| 14 | raw := strings.Split(src, "\n") |
| 15 | if len(raw) > 0 && raw[len(raw)-1] == "" && strings.HasSuffix(src, "\n") { |
| 16 | raw = raw[:len(raw)-1] |
| 17 | } |
| 18 | out := make([]string, 0, len(raw)) |
| 19 | for _, line := range raw { |
| 20 | var htmlLine string |
| 21 | htmlLine, inBlock = paint(lang, line, inBlock) |
| 22 | out = append(out, htmlLine) |
| 23 | } |
| 24 | return out |
| 25 | } |
| 26 | |
| 27 | func langOf(filename string) string { |
| 28 | switch strings.ToLower(path.Ext(filename)) { |
| 29 | case ".go": |
| 30 | return "go" |
| 31 | case ".js", ".mjs": |
| 32 | return "js" |
| 33 | case ".ts": |
| 34 | return "ts" |
| 35 | case ".py": |
| 36 | return "py" |
| 37 | case ".rs": |
| 38 | return "rs" |
| 39 | case ".c", ".h", ".cpp": |
| 40 | return "c" |
| 41 | case ".rb": |
| 42 | return "rb" |
| 43 | case ".sh", ".bash": |
| 44 | return "sh" |
| 45 | case ".yml", ".yaml": |
| 46 | return "yml" |
| 47 | case ".toml": |
| 48 | return "toml" |
| 49 | case ".json": |
| 50 | return "json" |
| 51 | case ".css": |
| 52 | return "css" |
| 53 | case ".sql": |
| 54 | return "sql" |
| 55 | case ".html", ".xml": |
| 56 | return "html" |
| 57 | case ".md": |
| 58 | return "md" |
| 59 | default: |
| 60 | base := strings.ToLower(path.Base(filename)) |
| 61 | if base == "dockerfile" || strings.HasSuffix(base, ".dockerfile") { |
| 62 | return "sh" |
| 63 | } |
| 64 | return "" |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | var keywords = map[string]map[string]bool{ |
| 69 | "go": words("func package import return if else for var const type struct map range switch case default break continue go defer select chan interface nil true false"), |
| 70 | "js": words("function return if else for var const let class new this null undefined true false import export async await"), |
| 71 | "ts": words("function return if else for var const let class new this null undefined true false import export async await type interface"), |
| 72 | "py": words("def class return if else elif for while import from as none true false and or not in with lambda try except"), |
| 73 | "rs": words("fn let mut pub struct enum impl trait use mod return if else for while match loop true false self"), |
| 74 | "c": words("if else for while return struct enum typedef void int char const static"), |
| 75 | "rb": words("def class end if else elsif do module require return nil true false"), |
| 76 | "sh": words("if then else fi for while do done case esac function return"), |
| 77 | "sql": words("select from where insert update delete create table join left right on as and or not null"), |
| 78 | } |
| 79 | |
| 80 | func words(s string) map[string]bool { |
| 81 | m := map[string]bool{} |
| 82 | for _, w := range strings.Fields(s) { |
| 83 | m[w] = true |
| 84 | } |
| 85 | return m |
| 86 | } |
| 87 | |
| 88 | func hashComment(lang string) bool { |
| 89 | switch lang { |
| 90 | case "py", "sh", "yml", "toml", "rb": |
| 91 | return true |
| 92 | } |
| 93 | return false |
| 94 | } |
| 95 | |
| 96 | func blockComment(lang string) bool { |
| 97 | switch lang { |
| 98 | case "go", "js", "ts", "rs", "c", "css", "sql": |
| 99 | return true |
| 100 | } |
| 101 | return false |
| 102 | } |
| 103 | |
| 104 | func paint(lang, line string, inBlock bool) (string, bool) { |
| 105 | var b strings.Builder |
| 106 | i := 0 |
| 107 | for i < len(line) { |
| 108 | rest := line[i:] |
| 109 | if inBlock { |
| 110 | end := strings.Index(rest, "*/") |
| 111 | if end < 0 { |
| 112 | b.WriteString(`<span class="hl-c">`) |
| 113 | b.WriteString(html.EscapeString(rest)) |
| 114 | b.WriteString("</span>") |
| 115 | return b.String(), true |
| 116 | } |
| 117 | b.WriteString(`<span class="hl-c">`) |
| 118 | b.WriteString(html.EscapeString(rest[:end+2])) |
| 119 | b.WriteString("</span>") |
| 120 | i += end + 2 |
| 121 | inBlock = false |
| 122 | continue |
| 123 | } |
| 124 | if strings.HasPrefix(rest, "//") || (hashComment(lang) && strings.HasPrefix(rest, "#")) || (lang == "sql" && strings.HasPrefix(rest, "--")) { |
| 125 | b.WriteString(`<span class="hl-c">`) |
| 126 | b.WriteString(html.EscapeString(rest)) |
| 127 | b.WriteString("</span>") |
| 128 | return b.String(), false |
| 129 | } |
| 130 | if blockComment(lang) && strings.HasPrefix(rest, "/*") { |
| 131 | inBlock = true |
| 132 | continue |
| 133 | } |
| 134 | if rest[0] == '"' || rest[0] == '\'' || (rest[0] == '`' && lang == "go") { |
| 135 | q := rest[0] |
| 136 | j := 1 |
| 137 | for j < len(rest) { |
| 138 | if rest[j] == '\\' && q != '`' { |
| 139 | j += 2 |
| 140 | continue |
| 141 | } |
| 142 | if rest[j] == q { |
| 143 | j++ |
| 144 | break |
| 145 | } |
| 146 | j++ |
| 147 | } |
| 148 | if j > len(rest) { |
| 149 | j = len(rest) |
| 150 | } |
| 151 | b.WriteString(`<span class="hl-s">`) |
| 152 | b.WriteString(html.EscapeString(rest[:j])) |
| 153 | b.WriteString("</span>") |
| 154 | i += j |
| 155 | continue |
| 156 | } |
| 157 | if rest[0] >= '0' && rest[0] <= '9' { |
| 158 | j := 1 |
| 159 | for j < len(rest) && ((rest[j] >= '0' && rest[j] <= '9') || rest[j] == '.') { |
| 160 | j++ |
| 161 | } |
| 162 | b.WriteString(`<span class="hl-n">`) |
| 163 | b.WriteString(html.EscapeString(rest[:j])) |
| 164 | b.WriteString("</span>") |
| 165 | i += j |
| 166 | continue |
| 167 | } |
| 168 | if isIdent(rest[0]) { |
| 169 | j := 1 |
| 170 | for j < len(rest) && (isIdent(rest[j]) || (rest[j] >= '0' && rest[j] <= '9')) { |
| 171 | j++ |
| 172 | } |
| 173 | word := rest[:j] |
| 174 | if keywords[lang][word] { |
| 175 | b.WriteString(`<span class="hl-k">`) |
| 176 | b.WriteString(html.EscapeString(word)) |
| 177 | b.WriteString("</span>") |
| 178 | } else { |
| 179 | b.WriteString(html.EscapeString(word)) |
| 180 | } |
| 181 | i += j |
| 182 | continue |
| 183 | } |
| 184 | b.WriteString(html.EscapeString(rest[:1])) |
| 185 | i++ |
| 186 | } |
| 187 | return b.String(), inBlock |
| 188 | } |
| 189 | |
| 190 | func isIdent(c byte) bool { |
| 191 | return c == '_' || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') |
| 192 | } |