| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "html" |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | "unicode" |
| 8 | "unicode/utf8" |
| 9 | |
| 10 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 11 | ) |
| 12 | |
| 13 | // The word diff inside a changed block (level-up §4.5): words, the space |
| 14 | // between them, and whole links, code spans and embeds, compared by |
| 15 | // what was typed and drawn escaped. |
| 16 | |
| 17 | // token is one word of a block: Key is what is compared (a link's |
| 18 | // source), and HTML is how it is drawn, escaped. Text marks a word of |
| 19 | // running text, where emphasis is drawn. |
| 20 | type token struct { |
| 21 | Key string |
| 22 | HTML string |
| 23 | Text bool |
| 24 | } |
| 25 | |
| 26 | // wordRun is a run of tokens kept (Tag ""), removed ("del") or added |
| 27 | // ("ins"). |
| 28 | type wordRun struct { |
| 29 | Tag string |
| 30 | Tokens []token |
| 31 | } |
| 32 | |
| 33 | // tokensOf splits a block's text into words, the spaces between them, |
| 34 | // and whole links, code spans and embeds. |
| 35 | func tokensOf(block markup.Block) []token { |
| 36 | if block.Kind == markup.Code { |
| 37 | return wordsIn(markup.Words(block), false) |
| 38 | } |
| 39 | var out []token |
| 40 | for _, span := range markup.Spans(markup.Strip(block)) { |
| 41 | switch span.Kind { |
| 42 | case markup.Text: |
| 43 | out = append(out, wordsIn(span.Text, true)...) |
| 44 | case markup.WikiLink: |
| 45 | shown := span.Text |
| 46 | if shown == "" { |
| 47 | shown = span.Target |
| 48 | } |
| 49 | out = append(out, token{Key: span.Source, |
| 50 | HTML: "<span mapa-diff-link>" + html.EscapeString(shown) + |
| 51 | "</span>"}) |
| 52 | case markup.Image: |
| 53 | // One token, id and all: an image replaced under the same |
| 54 | // alt text shows as changed (images §2.1). |
| 55 | out = append(out, token{Key: span.Source, |
| 56 | HTML: "<span mapa-diff-image>[image: " + |
| 57 | html.EscapeString(span.Text) + "]</span>"}) |
| 58 | case markup.InCode: |
| 59 | out = append(out, token{Key: span.Source, |
| 60 | HTML: "<code>" + html.EscapeString(span.Text) + "</code>"}) |
| 61 | default: |
| 62 | out = append(out, token{Key: span.Source, |
| 63 | HTML: html.EscapeString(span.Text)}) |
| 64 | } |
| 65 | } |
| 66 | return out |
| 67 | } |
| 68 | |
| 69 | // wordsIn is text as words and the runs of space between them. In |
| 70 | // running text a line break is drawn as <br>; code keeps it, in <pre>. |
| 71 | func wordsIn(text string, running bool) []token { |
| 72 | var out []token |
| 73 | spaceAt := func(at int) (bool, int) { |
| 74 | char, width := utf8.DecodeRuneInString(text[at:]) |
| 75 | return unicode.IsSpace(char), width |
| 76 | } |
| 77 | start := 0 |
| 78 | for start < len(text) { |
| 79 | space, _ := spaceAt(start) |
| 80 | end := start |
| 81 | for end < len(text) { |
| 82 | here, width := spaceAt(end) |
| 83 | if here != space { |
| 84 | break |
| 85 | } |
| 86 | end += width |
| 87 | } |
| 88 | run := text[start:end] |
| 89 | drawn := html.EscapeString(run) |
| 90 | if running { |
| 91 | drawn = strings.ReplaceAll(drawn, "\n", "<br>") |
| 92 | } |
| 93 | out = append(out, token{Key: run, HTML: drawn, Text: running}) |
| 94 | start = end |
| 95 | } |
| 96 | return out |
| 97 | } |
| 98 | |
| 99 | // maxWordCells bounds the word diff's table: two blocks longer than |
| 100 | // this together are shown as removed and added whole. |
| 101 | const maxWordCells = 250000 |
| 102 | |
| 103 | // diffWords is to against from as runs: kept, removed and added. |
| 104 | func diffWords(from, to []token) []wordRun { |
| 105 | if len(from)*len(to) > maxWordCells { |
| 106 | return []wordRun{{Tag: "del", Tokens: from}, |
| 107 | {Tag: "ins", Tokens: to}} |
| 108 | } |
| 109 | matched := lcs(len(from), len(to), func(row, column int) bool { |
| 110 | return from[row].Key == to[column].Key |
| 111 | }) |
| 112 | var runs []wordRun |
| 113 | add := func(tag string, tokens ...token) { |
| 114 | if len(tokens) == 0 { |
| 115 | return |
| 116 | } |
| 117 | if last := len(runs) - 1; last >= 0 && runs[last].Tag == tag { |
| 118 | runs[last].Tokens = append(runs[last].Tokens, tokens...) |
| 119 | return |
| 120 | } |
| 121 | runs = append(runs, wordRun{Tag: tag, Tokens: tokens}) |
| 122 | } |
| 123 | next := 0 |
| 124 | for index, target := range matched { |
| 125 | if target < 0 { |
| 126 | add("del", from[index]) |
| 127 | continue |
| 128 | } |
| 129 | add("ins", to[next:target]...) |
| 130 | add("", to[target]) |
| 131 | next = target + 1 |
| 132 | } |
| 133 | add("ins", to[next:]...) |
| 134 | return runs |
| 135 | } |
| 136 | |
| 137 | // similar reports two blocks whose words are alike enough to diff word |
| 138 | // by word: at least two fifths of their words in common. |
| 139 | func similar(from, to []token) bool { |
| 140 | from, to = wordsOnly(from), wordsOnly(to) |
| 141 | if len(from)+len(to) == 0 { |
| 142 | return true |
| 143 | } |
| 144 | if len(from)*len(to) > maxWordCells { |
| 145 | return false |
| 146 | } |
| 147 | common := 0 |
| 148 | matched := lcs(len(from), len(to), func(row, column int) bool { |
| 149 | return from[row].Key == to[column].Key |
| 150 | }) |
| 151 | for _, target := range matched { |
| 152 | if target >= 0 { |
| 153 | common++ |
| 154 | } |
| 155 | } |
| 156 | return common*5 >= len(from)+len(to) |
| 157 | } |
| 158 | |
| 159 | func wordsOnly(tokens []token) []token { |
| 160 | var out []token |
| 161 | for _, word := range tokens { |
| 162 | if strings.TrimSpace(word.Key) != "" { |
| 163 | out = append(out, word) |
| 164 | } |
| 165 | } |
| 166 | return out |
| 167 | } |
| 168 | |
| 169 | // drawRuns draws runs of tokens, each removed or added run in its tag. |
| 170 | func drawRuns(runs []wordRun) string { |
| 171 | var out strings.Builder |
| 172 | for _, run := range runs { |
| 173 | if len(run.Tokens) == 0 { |
| 174 | continue |
| 175 | } |
| 176 | if run.Tag != "" { |
| 177 | out.WriteString("<" + run.Tag + ">") |
| 178 | } |
| 179 | out.WriteString(drawTokens(run.Tokens)) |
| 180 | if run.Tag != "" { |
| 181 | out.WriteString("</" + run.Tag + ">") |
| 182 | } |
| 183 | } |
| 184 | return out.String() |
| 185 | } |
| 186 | |
| 187 | var ( |
| 188 | strongText = regexp.MustCompile(`\*\*(\S(?:[^*]*?\S)?)\*\*`) |
| 189 | emphasisText = regexp.MustCompile(`\*(\S(?:[^*]*?\S)?)\*`) |
| 190 | ) |
| 191 | |
| 192 | // drawTokens joins tokens, drawing emphasis inside each stretch of |
| 193 | // running text, as the page's renderer does. Emphasis that crosses a |
| 194 | // change, a link or code shows its asterisks. |
| 195 | func drawTokens(tokens []token) string { |
| 196 | var out, text strings.Builder |
| 197 | flush := func() { |
| 198 | drawn := strongText.ReplaceAllString(text.String(), |
| 199 | "<strong>$1</strong>") |
| 200 | out.WriteString(emphasisText.ReplaceAllString(drawn, "<em>$1</em>")) |
| 201 | text.Reset() |
| 202 | } |
| 203 | for _, word := range tokens { |
| 204 | if word.Text { |
| 205 | text.WriteString(word.HTML) |
| 206 | continue |
| 207 | } |
| 208 | flush() |
| 209 | out.WriteString(word.HTML) |
| 210 | } |
| 211 | flush() |
| 212 | return out.String() |
| 213 | } |
| 214 | |