| 1 | package markup |
| 2 | |
| 3 | import ( |
| 4 | "html" |
| 5 | "regexp" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | // Images (images spec §2):  is an inline span |
| 11 | // wherever links are parsed, never inside code. Alt text holds no [, ] |
| 12 | // or line break, and at most MaxAlt bytes; there is no title and no |
| 13 | // size. The address is relative, so it never names a host. |
| 14 | |
| 15 | // MaxAlt is the longest alt text, in bytes of UTF-8. |
| 16 | const MaxAlt = 1000 |
| 17 | |
| 18 | // ImagePrefix is where every image is served. |
| 19 | const ImagePrefix = "/i/" |
| 20 | |
| 21 | var imageID = regexp.MustCompile(`^[0-9a-z]{1,13}$`) |
| 22 | |
| 23 | // IsImageID is a public id's shape: base 36, at most 13 characters. |
| 24 | func IsImageID(id string) bool { return imageID.MatchString(id) } |
| 25 | |
| 26 | // imageSpan reads  at text[index], or reports width 0. |
| 27 | func imageSpan(scanned *scan, index int) (Span, int) { |
| 28 | text := scanned.text |
| 29 | end := scanned.next(stopLabel, index+2) |
| 30 | if end < 0 || text[end] != ']' || end+1 >= len(text) || |
| 31 | text[end+1] != '(' || end-index-2 > MaxAlt { |
| 32 | return Span{}, 0 |
| 33 | } |
| 34 | if open := scanned.next(stopOpen, index+2); open >= 0 && open < end { |
| 35 | return Span{}, 0 |
| 36 | } |
| 37 | closer := scanned.next(stopAddress, end+2) |
| 38 | if closer < 0 || text[closer] != ')' { |
| 39 | return Span{}, 0 |
| 40 | } |
| 41 | address := text[end+2 : closer] |
| 42 | id, ok := strings.CutPrefix(address, ImagePrefix) |
| 43 | if !ok || !IsImageID(id) { |
| 44 | return Span{}, 0 |
| 45 | } |
| 46 | return Span{Kind: Image, Text: text[index+2 : end], Target: id, |
| 47 | URL: address}, closer + 1 - index |
| 48 | } |
| 49 | |
| 50 | // ImageResolver answers, for the person reading and the page being |
| 51 | // drawn, whether an image is shown and at what size. A Resolver that is |
| 52 | // not one draws every image as not available. |
| 53 | type ImageResolver interface { |
| 54 | Image(id string) ImageAnswer |
| 55 | } |
| 56 | |
| 57 | // ImageAnswer is one image's state: available, with its width and |
| 58 | // height after orientation, or not. |
| 59 | type ImageAnswer struct { |
| 60 | Available bool |
| 61 | Width, Height int |
| 62 | // Link, when set, makes the image a link there: History's old |
| 63 | // revisions link each image to its Remove confirm page (§3.3). The |
| 64 | // editor never draws one. |
| 65 | Link string |
| 66 | } |
| 67 | |
| 68 | // imageHTML draws one image as the editor's schema does: an <img> with |
| 69 | // its box reserved, or the fixed box that says it is not available. A |
| 70 | // lone image (the block's only span) is drawn as a block (class alone). |
| 71 | func imageHTML(span Span, resolver Resolver, alone bool) string { |
| 72 | var answer ImageAnswer |
| 73 | if images, ok := resolver.(ImageResolver); ok { |
| 74 | answer = images.Image(span.Target) |
| 75 | } |
| 76 | class := "" |
| 77 | if alone { |
| 78 | class = ` class="alone"` |
| 79 | } |
| 80 | if !answer.Available { |
| 81 | return `<span mapa-image="unavailable"` + class + |
| 82 | `>Image not available</span>` |
| 83 | } |
| 84 | drawn := `<img src="` + html.EscapeString(span.URL) + `" alt="` + |
| 85 | html.EscapeString(span.Text) + `" width="` + |
| 86 | strconv.Itoa(answer.Width) + `" height="` + |
| 87 | strconv.Itoa(answer.Height) + |
| 88 | `" loading="lazy" decoding="async"` + class + `>` |
| 89 | if answer.Link == "" { |
| 90 | return drawn |
| 91 | } |
| 92 | return `<a href="` + html.EscapeString(answer.Link) + |
| 93 | `" mapa-image-remove title="Remove this image from Mapa">` + |
| 94 | drawn + `</a>` |
| 95 | } |
| 96 | |
| 97 | // ImageRef is one image in a body: its public id and alt text. |
| 98 | type ImageRef struct{ ID, Alt string } |
| 99 | |
| 100 | // Images is every image in body, outside code, in order. |
| 101 | func Images(body string) []ImageRef { |
| 102 | var out []ImageRef |
| 103 | for _, block := range Parse(body) { |
| 104 | if block.Kind == Code { |
| 105 | continue |
| 106 | } |
| 107 | for _, span := range Spans(Strip(block)) { |
| 108 | if span.Kind == Image { |
| 109 | out = append(out, ImageRef{ID: span.Target, Alt: span.Text}) |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | return out |
| 114 | } |
| 115 | |
| 116 | // ImageIDs is the set of image ids body names. |
| 117 | func ImageIDs(body string) map[string]bool { |
| 118 | out := map[string]bool{} |
| 119 | for _, ref := range Images(body) { |
| 120 | out[ref.ID] = true |
| 121 | } |
| 122 | return out |
| 123 | } |
| 124 | |
| 125 | // RewriteImages changes each image span through change, which answers |
| 126 | // the span's new source and whether to change it, touching nothing |
| 127 | // else in body. |
| 128 | func RewriteImages(body string, |
| 129 | change func(Span) (string, bool)) string { |
| 130 | blocks := Parse(body) |
| 131 | edits := map[int]string{} |
| 132 | for index, block := range blocks { |
| 133 | if block.Kind == Code { |
| 134 | continue |
| 135 | } |
| 136 | var out strings.Builder |
| 137 | edited := false |
| 138 | for _, span := range Spans(block.Source) { |
| 139 | if span.Kind == Image { |
| 140 | if source, ok := change(span); ok { |
| 141 | out.WriteString(source) |
| 142 | edited = true |
| 143 | continue |
| 144 | } |
| 145 | } |
| 146 | out.WriteString(span.Source) |
| 147 | } |
| 148 | if edited { |
| 149 | edits[index] = out.String() |
| 150 | } |
| 151 | } |
| 152 | return Replace(body, blocks, edits) |
| 153 | } |
| 154 | |
| 155 | // ReplaceImageIDs points each image named in ids at its new id. |
| 156 | func ReplaceImageIDs(body string, ids map[string]string) string { |
| 157 | return RewriteImages(body, func(span Span) (string, bool) { |
| 158 | fresh, ok := ids[span.Target] |
| 159 | return "", ok |
| 160 | }) |
| 161 | } |
| 162 | |
| 163 | // ImagesAsAlt is body with every image written as its alt text: what a |
| 164 | // sibling app sees (§2.1), so no image id leaves Mapa. |
| 165 | func ImagesAsAlt(body string) string { |
| 166 | return RewriteImages(body, func(span Span) (string, bool) { |
| 167 | return span.Text, true |
| 168 | }) |
| 169 | } |
| 170 | |