Skip to content
This repository was archived by the owner on Nov 23, 2026. It is now read-only.

Commit f7a45aa

Browse files
committed
Escape :: between words so pandoc keeps it as text
1 parent 8d3fea1 commit f7a45aa

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

‎admin/filters/underscores.php‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,44 @@
33
declare(strict_types=1);
44

55
/**
6-
* Escapes the underscore that starts a path segment, which pandoc 3.13 reads as an RST inline target.
6+
* Escapes the underscore that starts a path segment and the `::` between two words, which pandoc 3.13 reads as an RST inline target and as a link.
77
*/
88
function before_filter_underscores(string $data, string $folder): ?string
99
{
10-
return preg_replace_callback('/(``.+?``)|\S+/', function (array $matches): string {
10+
$lines = explode("\n", $data);
11+
12+
foreach (array_keys($lines) as $i) {
13+
$escaped = escape_inline_targets($lines[$i]);
14+
$added = strlen($escaped) - strlen($lines[$i]);
15+
$lines[$i] = $escaped;
16+
17+
if ($added > 0 && isset($lines[$i + 1]) && is_adornment($lines[$i + 1])) {
18+
$lines[$i + 1] .= str_repeat($lines[$i + 1][0], $added);
19+
20+
if ($i > 0 && is_adornment($lines[$i - 1]) && $lines[$i - 1][0] === $lines[$i + 1][0] && ($i === 1 || trim($lines[$i - 2]) === '')) {
21+
$lines[$i - 1] .= str_repeat($lines[$i - 1][0], $added);
22+
}
23+
}
24+
}
25+
26+
return implode("\n", $lines);
27+
}
28+
29+
function escape_inline_targets(string $line): string
30+
{
31+
return preg_replace_callback('/(`[^`]+`|``.+?``)|[^\s`]+/', function (array $matches): string {
1132
if (isset($matches[1]) || str_contains($matches[0], '://') || str_contains($matches[0], '](')) {
1233
return $matches[0];
1334
}
1435

15-
return preg_replace('#(?<=/)_(?=\w)#', '\\\\_', $matches[0]);
16-
}, $data);
36+
return preg_replace(['#(?<=/)_(?=\w)#', '/(?<=\w)::(?=\w)/'], ['\\\\_', '\\\\::'], $matches[0]);
37+
}, $line);
38+
}
39+
40+
/**
41+
* Whether `$line` is a run of one punctuation character, as under an RST section title.
42+
*/
43+
function is_adornment(string $line): bool
44+
{
45+
return preg_match('/^([!-\/:-@\[-`{-~])\1+$/', $line) === 1;
1746
}

‎admin/how_to_use_filters.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ all before filters on every page, then pandoc on every page, then all after filt
2727
| `doc_link` | Converts `:doc:`, `:ref:` and `:php:*:` roles, external links and `\|version\|` | Unescapes the link syntax and converts `title-ref` and label spans |
2828
| `class_reference` | Converts `php:namespace`, `php:class`, `php:interface`, `php:trait`, `php:method`, `php:function`, `php:attr`, `php:const` and their fields into headings and lists | Unescapes `\#` |
2929
| `list_indentation` | Adds the blank line that pandoc needs between a directive and a list that starts its body | Re-indents nested lists by four spaces per level and adds the blank line that Python-Markdown needs before a list |
30-
| `underscores` | Escapes the underscore that starts a path segment, such as `tests/_support`, which pandoc 3.13 reads as an inline target | |
30+
| `underscores` | Escapes the underscore that starts a path segment, such as `tests/_support`, and the `::` between two words, such as `News::index()`, which pandoc 3.13 reads as an inline target and as a link | |
3131
| `images` | | Converts image tags to Markdown |
3232
| `alerts` | | Converts `> [!NOTE]` alerts and unwraps the blockquotes left by indented RST text |
3333
| `dollar_sign` | | Unescapes `\$` |

0 commit comments

Comments
 (0)