* Find any hyperlinks in a given line. * * @param line The line to search for hyperlinks. * @param line_index The index of the line. */
| 223 | * @param line_index The index of the line. |
| 224 | */ |
| 225 | void TextfileWindow::FindHyperlinksInMarkdown(Line &line, size_t line_index) |
| 226 | { |
| 227 | std::string::const_iterator last_match_end = line.text.cbegin(); |
| 228 | std::string fixed_line; |
| 229 | StringBuilder builder(fixed_line); |
| 230 | |
| 231 | std::sregex_iterator matcher{ line.text.cbegin(), line.text.cend(), _markdown_link_regex}; |
| 232 | while (matcher != std::sregex_iterator()) { |
| 233 | std::smatch match = *matcher; |
| 234 | |
| 235 | Hyperlink &link = this->links.emplace_back(line_index, 0, 0, match[2].str()); |
| 236 | |
| 237 | HyperlinkType link_type = ClassifyHyperlink(link.destination, this->trusted); |
| 238 | StringControlCode link_colour; |
| 239 | switch (link_type) { |
| 240 | case HyperlinkType::Internal: |
| 241 | link_colour = SCC_GREEN; |
| 242 | break; |
| 243 | case HyperlinkType::Web: |
| 244 | link_colour = SCC_LTBLUE; |
| 245 | break; |
| 246 | case HyperlinkType::File: |
| 247 | link_colour = SCC_LTBROWN; |
| 248 | break; |
| 249 | default: |
| 250 | /* Don't make other link types fancy as they aren't handled (yet). */ |
| 251 | link_colour = SCC_CONTROL_END; |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | if (link_colour != SCC_CONTROL_END) { |
| 256 | /* Format the link to look like a link. */ |
| 257 | builder += std::string_view(last_match_end, match[0].first); |
| 258 | link.begin = fixed_line.length(); |
| 259 | builder.PutUtf8(SCC_PUSH_COLOUR); |
| 260 | builder.PutUtf8(link_colour); |
| 261 | builder += match[1].str(); |
| 262 | link.end = fixed_line.length(); |
| 263 | builder.PutUtf8(SCC_POP_COLOUR); |
| 264 | last_match_end = match[0].second; |
| 265 | } |
| 266 | |
| 267 | /* Find next link. */ |
| 268 | ++matcher; |
| 269 | } |
| 270 | if (last_match_end == line.text.cbegin()) return; // nothing found |
| 271 | |
| 272 | /* Add remaining text on line. */ |
| 273 | fixed_line += std::string(last_match_end, line.text.cend()); |
| 274 | |
| 275 | /* Overwrite original line text with "fixed" line text. */ |
| 276 | line.text = std::move(fixed_line); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Get the hyperlink at the given position. |
no test coverage detected