| 1 | package Text::MarkdownAdoc::Inline; | ||||||||||||
| 2 | |||||||||||||
| 3 | 17 | ▶ | use 5.016; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 17 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 17 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | #=========================================================================== | ||||||||||||
| 8 | # Inline formatting processor | ||||||||||||
| 9 | #=========================================================================== | ||||||||||||
| 10 | |||||||||||||
| 11 | sub new | ||||||||||||
| 12 | 186 | ▶ | { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 13 | 186 | my ($class, %opts) = @_; | |||||||||||
| 14 | |||||||||||||
| 15 | 186 | my $self = { | |||||||||||
| 16 | options => {%opts}, | ||||||||||||
| 17 | has_math => 0, | ||||||||||||
| 18 | }; | ||||||||||||
| 19 | |||||||||||||
| 20 | 186 | bless $self, $class; | |||||||||||
| 21 | 186 | return $self; | |||||||||||
| 22 | } | ||||||||||||
| 23 | |||||||||||||
| 24 | sub process | ||||||||||||
| 25 | 243 | ▶ | { | ||||||||||
|
Subroutine
process: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 26 | 243 | my ($self, $text) = @_; | |||||||||||
| 27 | |||||||||||||
| 28 | 243 | ▶ | my $input = $text // ''; | ||||||||||
|
Condition50%1/2
$text // ""
MC/DC0%0/1
$text // ""
$text
Truth table50%1/2
A
A= $text
| |||||||||||||
| 29 | 243 | ▶ | return '' if $input eq ''; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | |||||||||||||
| 31 | # Processing order: | ||||||||||||
| 32 | # 1. Handle backslash escapes (protect escaped chars BEFORE code spans) | ||||||||||||
| 33 | # 2. Extract code spans (protect from further processing) | ||||||||||||
| 34 | # 3. Inline math $...$ (after code spans so $ in code is safe) | ||||||||||||
| 35 | # 4. Inline links (must come before reference-style; exclude image prefix) | ||||||||||||
| 36 | # 5. Footnotes → placeholders (must come BEFORE reference-style links | ||||||||||||
| 37 | # because [^label] would otherwise be consumed by the [ref] shorthand regex) | ||||||||||||
| 38 | # 6. Reference-style links → placeholders | ||||||||||||
| 39 | # 7. Images (protect [alt] from reference link processing) | ||||||||||||
| 40 | # 8. Autolinks | ||||||||||||
| 41 | # 9. Bold, strikethrough → placeholders | ||||||||||||
| 42 | # 10. Italic (process inside placeholders, then main text) | ||||||||||||
| 43 | # 11. Resolve bold/strikethrough placeholders (AFTER italic) | ||||||||||||
| 44 | # 12. HTML entities | ||||||||||||
| 45 | # 13. Inline HTML | ||||||||||||
| 46 | # 14. Smart quotes | ||||||||||||
| 47 | # 15. Typographic symbols (em dash, ellipsis) | ||||||||||||
| 48 | # 16. Clean up PROT markers | ||||||||||||
| 49 | # 17. Restore code spans and protected content | ||||||||||||
| 50 | |||||||||||||
| 51 | 243 | my @protected; | |||||||||||
| 52 | 243 | $input = _protect_escapes($input, \@protected); | |||||||||||
| 53 | |||||||||||||
| 54 | 243 | my @code_spans; | |||||||||||
| 55 | 243 | $input = _extract_code_spans($input, \@code_spans); | |||||||||||
| 56 | |||||||||||||
| 57 | 243 | $input = _convert_inline_links($input); | |||||||||||
| 58 | 243 | $input = _convert_footnotes($input); | |||||||||||
| 59 | 243 | $input = _convert_reference_links($input); | |||||||||||
| 60 | 243 | $input = _convert_inline_math($input, $self); | |||||||||||
| 61 | 243 | $input = _convert_images($input); | |||||||||||
| 62 | 243 | $input = _convert_autolinks($input); | |||||||||||
| 63 | 243 | $input = _convert_bold_and_strikethrough($input); | |||||||||||
| 64 | 243 | $input = _convert_italic($input); | |||||||||||
| 65 | 243 | $input = _resolve_bold_and_strikethrough($input); | |||||||||||
| 66 | 243 | $input = _convert_html_entities($input); | |||||||||||
| 67 | 243 | $input = _convert_inline_html($input); | |||||||||||
| 68 | 243 | $input = _convert_smart_quotes($input); | |||||||||||
| 69 | 243 | $input = _convert_typographic_symbols($input); | |||||||||||
| 70 | |||||||||||||
| 71 | 243 | $input = _cleanup_prot($input); | |||||||||||
| 72 | 243 | $input = _restore_protected($input, \@protected); | |||||||||||
| 73 | 243 | $input = _restore_code_spans($input, \@code_spans); | |||||||||||
| 74 | |||||||||||||
| 75 | 243 | return $input; | |||||||||||
| 76 | } | ||||||||||||
| 77 | |||||||||||||
| 78 | #=========================================================================== | ||||||||||||
| 79 | # Backslash escapes — protect from further processing (BEFORE code spans) | ||||||||||||
| 80 | #=========================================================================== | ||||||||||||
| 81 | |||||||||||||
| 82 | sub _protect_escapes | ||||||||||||
| 83 | 243 | ▶ | { | ||||||||||
|
Subroutine
_protect_escapes: called CC=1 SCAR=0.0
| |||||||||||||
| 84 | 243 | my ($text, $stash) = @_; | |||||||||||
| 85 | |||||||||||||
| 86 | # Protect backslash-escaped punctuation characters | ||||||||||||
| 87 | 243 | $text =~ s{\\([!"#\$%&'()*+,\-./:;<=>?@\[\\\]^_`{|}~])}{ | |||||||||||
| 88 | 6 | push @$stash, $1; | |||||||||||
| 89 | 6 | "\x01ESC" . ($#$stash) . "\x01"; | |||||||||||
| 90 | }eg; | ||||||||||||
| 91 | |||||||||||||
| 92 | 243 | return $text; | |||||||||||
| 93 | } | ||||||||||||
| 94 | |||||||||||||
| 95 | sub _restore_protected | ||||||||||||
| 96 | 243 | ▶ | { | ||||||||||
|
Subroutine
_restore_protected: called CC=1 SCAR=0.0
| |||||||||||||
| 97 | 243 | my ($text, $stash) = @_; | |||||||||||
| 98 | |||||||||||||
| 99 | 243 | $text =~ s{\x01ESC(\d+)\x01}{ | |||||||||||
| 100 | 6 | my $idx = $1; | |||||||||||
| 101 | 6 | $stash->[$idx]; | |||||||||||
| 102 | }eg; | ||||||||||||
| 103 | |||||||||||||
| 104 | 243 | return $text; | |||||||||||
| 105 | } | ||||||||||||
| 106 | |||||||||||||
| 107 | #=========================================================================== | ||||||||||||
| 108 | # Code span extraction (AFTER escapes) | ||||||||||||
| 109 | #=========================================================================== | ||||||||||||
| 110 | |||||||||||||
| 111 | sub _extract_code_spans | ||||||||||||
| 112 | 243 | ▶ | { | ||||||||||
|
Subroutine
_extract_code_spans: called CC=1 SCAR=0.0
| |||||||||||||
| 113 | 243 | my ($text, $stash) = @_; | |||||||||||
| 114 | |||||||||||||
| 115 | 243 | $text =~ s{ | |||||||||||
| 116 | (`{1,}) # opening backticks (capture count) | ||||||||||||
| 117 | (.+?) # code content (non-greedy) | ||||||||||||
| 118 | \1 # closing backticks (same count) | ||||||||||||
| 119 | }{ | ||||||||||||
| 120 | 11 | my $code = $2; | |||||||||||
| 121 | 11 | push @$stash, $code; | |||||||||||
| 122 | 11 | "\x01CODE" . ($#$stash) . "\x01"; | |||||||||||
| 123 | }xesg; | ||||||||||||
| 124 | |||||||||||||
| 125 | 243 | return $text; | |||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | sub _restore_code_spans | ||||||||||||
| 129 | 243 | ▶ | { | ||||||||||
|
Subroutine
_restore_code_spans: called CC=1 SCAR=0.0
| |||||||||||||
| 130 | 243 | my ($text, $stash) = @_; | |||||||||||
| 131 | |||||||||||||
| 132 | 243 | $text =~ s{\x01CODE(\d+)\x01}{ | |||||||||||
| 133 | 11 | my $idx = $1; | |||||||||||
| 134 | 11 | '`' . $stash->[$idx] . '`'; | |||||||||||
| 135 | }eg; | ||||||||||||
| 136 | |||||||||||||
| 137 | 243 | return $text; | |||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | #=========================================================================== | ||||||||||||
| 141 | # Inline links — must process BEFORE reference-style links | ||||||||||||
| 142 | #=========================================================================== | ||||||||||||
| 143 | |||||||||||||
| 144 | sub _convert_inline_links | ||||||||||||
| 145 | 243 | ▶ | { | ||||||||||
|
Subroutine
_convert_inline_links: called CC=1 SCAR=0.0
| |||||||||||||
| 146 | 243 | my ($text) = @_; | |||||||||||
| 147 | |||||||||||||
| 148 | # Match [text](url) or [text](url "title") | ||||||||||||
| 149 | # Must NOT be preceded by ! (image syntax) | ||||||||||||
| 150 | 243 | $text =~ s{ | |||||||||||
| 151 | (?<!\!) # not an image | ||||||||||||
| 152 | \[ | ||||||||||||
| 153 | ( (?:\\.|[^\]])*? ) # link text (allow escaped ]) | ||||||||||||
| 154 | \] | ||||||||||||
| 155 | \( | ||||||||||||
| 156 | [ \t]* | ||||||||||||
| 157 | ( [^)\s]+ ) # URL (no spaces) | ||||||||||||
| 158 | (?: [ \t]+ "([^"]*)" )? # optional title | ||||||||||||
| 159 | [ \t]* | ||||||||||||
| 160 | \) | ||||||||||||
| 161 | }{ | ||||||||||||
| 162 | 10 | my $link_text = $1; | |||||||||||
| 163 | 10 | my $url = $2; | |||||||||||
| 164 | |||||||||||||
| 165 | # Unescape brackets in link text | ||||||||||||
| 166 | 10 | $link_text =~ s/\\\[/[/g; | |||||||||||
| 167 | 10 | $link_text =~ s/\\\]/]/g; | |||||||||||
| 168 | |||||||||||||
| 169 | 10 | _format_link($link_text, $url); | |||||||||||
| 170 | }xeg; | ||||||||||||
| 171 | |||||||||||||
| 172 | 243 | return $text; | |||||||||||
| 173 | } | ||||||||||||
| 174 | |||||||||||||
| 175 | sub _format_link | ||||||||||||
| 176 | 10 | ▶ | { | ||||||||||
|
Subroutine
_format_link: called CC=4 SCAR=13.9
| |||||||||||||
| 177 | 10 | my ($text, $url) = @_; | |||||||||||
| 178 | |||||||||||||
| 179 | # Anchor link: [text](#anchor) → deferred xref placeholder | ||||||||||||
| 180 | 10 | ▶ | if ($url =~ m/^#(.+)/) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 181 | { | ||||||||||||
| 182 | 4 | my $anchor = $1; | |||||||||||
| 183 | 4 | return "\x00XREF:$anchor:$text\x00"; | |||||||||||
| 184 | } | ||||||||||||
| 185 | |||||||||||||
| 186 | # .md file → .adoc xref | ||||||||||||
| 187 | 6 | ▶ | if ($url =~ m/^(.+)\.md$/i) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 188 | { | ||||||||||||
| 189 | 1 | my $base = $1; | |||||||||||
| 190 | 1 | return "xref:$base.adoc\x01PROT$text\x01PROT"; | |||||||||||
| 191 | } | ||||||||||||
| 192 | |||||||||||||
| 193 | # URL-only link where text equals URL → bare URL | ||||||||||||
| 194 | 5 | ▶ | if ($text eq $url) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 195 | { | ||||||||||||
| 196 | 1 | return $url; | |||||||||||
| 197 | } | ||||||||||||
| 198 | |||||||||||||
| 199 | # Standard link: [text](url) → url[text] | ||||||||||||
| 200 | 4 | return "$url\x01PROT$text\x01PROT"; | |||||||||||
| 201 | } | ||||||||||||
| 202 | |||||||||||||
| 203 | #=========================================================================== | ||||||||||||
| 204 | # Reference-style links → placeholders | ||||||||||||
| 205 | #=========================================================================== | ||||||||||||
| 206 | |||||||||||||
| 207 | # Placeholder layout: \x00REFLINK:<label>\x02<display>\x02<form>\x00 | ||||||||||||
| 208 | # The form is "s" (shorthand), "c" (collapsed) or "f" followed by the raw | ||||||||||||
| 209 | # label (full). The raw label carries no brackets here so that the shorthand | ||||||||||||
| 210 | # pass cannot mistake it for a reference of its own. | ||||||||||||
| 211 | # Refs.pm uses it to restore the source text when no definition exists, | ||||||||||||
| 212 | # because brackets without a definition are literal text. | ||||||||||||
| 213 | sub _convert_reference_links | ||||||||||||
| 214 | 243 | ▶ | { | ||||||||||
|
Subroutine
_convert_reference_links: called CC=3 SCAR=11.0
| |||||||||||||
| 215 | 243 | my ($text) = @_; | |||||||||||
| 216 | |||||||||||||
| 217 | # [text][ref] → placeholder | ||||||||||||
| 218 | 243 | $text =~ s{ | |||||||||||
| 219 | \[ | ||||||||||||
| 220 | ( (?:\\.|[^\]])*? ) # link text | ||||||||||||
| 221 | \] | ||||||||||||
| 222 | \[ | ||||||||||||
| 223 | ( [^\]]*? ) # reference label (may be empty) | ||||||||||||
| 224 | \] | ||||||||||||
| 225 | }{ | ||||||||||||
| 226 | 4 | my $link_text = $1; | |||||||||||
| 227 | 4 | my $ref_label = $2; | |||||||||||
| 228 | 4 | ▶ | my $form = $ref_label eq q{} ? q{c} : "f$ref_label"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 229 | |||||||||||||
| 230 | 4 | $link_text =~ s/\\\[/[/g; | |||||||||||
| 231 | 4 | $link_text =~ s/\\\]/]/g; | |||||||||||
| 232 | |||||||||||||
| 233 | 4 | ▶ | if ($ref_label eq '') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 234 | 2 | $ref_label = $link_text; | |||||||||||
| 235 | } | ||||||||||||
| 236 | |||||||||||||
| 237 | 4 | my $normalized = lc($ref_label); | |||||||||||
| 238 | 4 | $normalized =~ s/\s+/ /g; | |||||||||||
| 239 | 4 | $normalized =~ s/^\s+|\s+$//g; | |||||||||||
| 240 | |||||||||||||
| 241 | 4 | "\x00REFLINK:$normalized\x02$link_text\x02$form\x00"; | |||||||||||
| 242 | }xeg; | ||||||||||||
| 243 | |||||||||||||
| 244 | # [ref] (shorthand, but NOT followed by '(' which would be an inline link) | ||||||||||||
| 245 | 243 | $text =~ s{ | |||||||||||
| 246 | (?<!\!) # not an image | ||||||||||||
| 247 | \[ | ||||||||||||
| 248 | ( [^\[\]\(]+? ) # reference label (no brackets or parens) | ||||||||||||
| 249 | \] | ||||||||||||
| 250 | (?!\() # NOT followed by '(' (inline link) | ||||||||||||
| 251 | }{ | ||||||||||||
| 252 | 8 | my $ref_label = $1; | |||||||||||
| 253 | |||||||||||||
| 254 | 8 | my $normalized = lc($ref_label); | |||||||||||
| 255 | 8 | $normalized =~ s/\s+/ /g; | |||||||||||
| 256 | 8 | $normalized =~ s/^\s+|\s+$//g; | |||||||||||
| 257 | |||||||||||||
| 258 | 8 | "\x00REFLINK:$normalized\x02$ref_label\x02s\x00"; | |||||||||||
| 259 | }xeg; | ||||||||||||
| 260 | |||||||||||||
| 261 | 243 | return $text; | |||||||||||
| 262 | } | ||||||||||||
| 263 | |||||||||||||
| 264 | #=========================================================================== | ||||||||||||
| 265 | # Inline images — protect [alt] from reference link processing | ||||||||||||
| 266 | #=========================================================================== | ||||||||||||
| 267 | |||||||||||||
| 268 | sub _convert_images | ||||||||||||
| 269 | 243 | ▶ | { | ||||||||||
|
Subroutine
_convert_images: called CC=2 SCAR=10.1
| |||||||||||||
| 270 | 243 | my ($text) = @_; | |||||||||||
| 271 | |||||||||||||
| 272 | # Reference-style images: ![alt][ref] | ||||||||||||
| 273 | 243 | $text =~ s{ | |||||||||||
| 274 | !\[ | ||||||||||||
| 275 | ( (?:\\.|[^\]])*? ) # alt text | ||||||||||||
| 276 | \] | ||||||||||||
| 277 | \[ | ||||||||||||
| 278 | ( [^\]]*? ) # reference label | ||||||||||||
| 279 | \] | ||||||||||||
| 280 | }{ | ||||||||||||
| 281 | 0 | my $alt = $1; | |||||||||||
| 282 | 0 | my $label = $2; | |||||||||||
| 283 | |||||||||||||
| 284 | 0 | $alt =~ s/\\\[/[/g; | |||||||||||
| 285 | 0 | $alt =~ s/\\\]/]/g; | |||||||||||
| 286 | |||||||||||||
| 287 | 0 | ▶ | if ($label eq '') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 288 | 0 | $label = $alt; | |||||||||||
| 289 | } | ||||||||||||
| 290 | |||||||||||||
| 291 | 0 | my $normalized = lc($label); | |||||||||||
| 292 | 0 | $normalized =~ s/\s+/ /g; | |||||||||||
| 293 | 0 | $normalized =~ s/^\s+|\s+$//g; | |||||||||||
| 294 | |||||||||||||
| 295 | 0 | "\x00REFIMG:$normalized:$alt\x00"; | |||||||||||
| 296 | }xeg; | ||||||||||||
| 297 | |||||||||||||
| 298 | # Inline images:  or  | ||||||||||||
| 299 | 243 | $text =~ s{ | |||||||||||
| 300 | !\[ | ||||||||||||
| 301 | ( (?:\\.|[^\]])*? ) # alt text | ||||||||||||
| 302 | \] | ||||||||||||
| 303 | \( | ||||||||||||
| 304 | [ \t]* | ||||||||||||
| 305 | ( [^)\s]+ ) # URL | ||||||||||||
| 306 | (?: [ \t]+ "([^"]*)" )? # optional title | ||||||||||||
| 307 | [ \t]* | ||||||||||||
| 308 | \) | ||||||||||||
| 309 | }{ | ||||||||||||
| 310 | 2 | my $alt = $1; | |||||||||||
| 311 | 2 | my $src = $2; | |||||||||||
| 312 | |||||||||||||
| 313 | 2 | $alt =~ s/\\\[/[/g; | |||||||||||
| 314 | 2 | $alt =~ s/\\\]/]/g; | |||||||||||
| 315 | |||||||||||||
| 316 | # Protect the [alt] part from reference link processing | ||||||||||||
| 317 | 2 | "image:$src\x01PROT$alt\x01PROT"; | |||||||||||
| 318 | }xeg; | ||||||||||||
| 319 | |||||||||||||
| 320 | 243 | return $text; | |||||||||||
| 321 | } | ||||||||||||
| 322 | |||||||||||||
| 323 | #=========================================================================== | ||||||||||||
| 324 | # Footnotes → placeholders for deferred resolution in Refs.pm | ||||||||||||
| 325 | #=========================================================================== | ||||||||||||
| 326 | |||||||||||||
| 327 | sub _convert_footnotes | ||||||||||||
| 328 | 243 | ▶ | { | ||||||||||
|
Subroutine
_convert_footnotes: called CC=1 SCAR=0.0
| |||||||||||||
| 329 | 243 | my ($text) = @_; | |||||||||||
| 330 | |||||||||||||
| 331 | # [^label] → \x00FOOTNOTE:label\x00 | ||||||||||||
| 332 | 243 | $text =~ s{\[\^([^\]]+)\]}{ | |||||||||||
| 333 | 11 | my $label = $1; | |||||||||||
| 334 | 11 | "\x00FOOTNOTE:$label\x00"; | |||||||||||
| 335 | }xeg; | ||||||||||||
| 336 | |||||||||||||
| 337 | 243 | return $text; | |||||||||||
| 338 | } | ||||||||||||
| 339 | |||||||||||||
| 340 | #=========================================================================== | ||||||||||||
| 341 | # Autolinks | ||||||||||||
| 342 | #=========================================================================== | ||||||||||||
| 343 | |||||||||||||
| 344 | sub _convert_autolinks | ||||||||||||
| 345 | 243 | ▶ | { | ||||||||||
|
Subroutine
_convert_autolinks: called CC=1 SCAR=0.0
| |||||||||||||
| 346 | 243 | my ($text) = @_; | |||||||||||
| 347 | |||||||||||||
| 348 | # <url> → bare URL | ||||||||||||
| 349 | 243 | $text =~ s{<([a-zA-Z][a-zA-Z0-9+.-]*://[^>\s]+)>}{$1}g; | |||||||||||
| 350 | |||||||||||||
| 351 | 243 | return $text; | |||||||||||
| 352 | } | ||||||||||||
| 353 | |||||||||||||
| 354 | #=========================================================================== | ||||||||||||
| 355 | # Bold and strikethrough → placeholders | ||||||||||||
| 356 | #=========================================================================== | ||||||||||||
| 357 | |||||||||||||
| 358 | sub _convert_bold_and_strikethrough | ||||||||||||
| 359 | 243 | ▶ | { | ||||||||||
|
Subroutine
_convert_bold_and_strikethrough: called CC=1 SCAR=0.0
| |||||||||||||
| 360 | 243 | my ($text) = @_; | |||||||||||
| 361 | |||||||||||||
| 362 | # **bold** → placeholder | ||||||||||||
| 363 | 243 | $text =~ s{\*\*(.+?)\*\*}{ | |||||||||||
| 364 | 13 | "\x01BOLD$1\x01"; | |||||||||||
| 365 | }xeg; | ||||||||||||
| 366 | |||||||||||||
| 367 | # __bold__ → placeholder | ||||||||||||
| 368 | 243 | $text =~ s{__(.+?)__}{ | |||||||||||
| 369 | 2 | "\x01BOLD$1\x01"; | |||||||||||
| 370 | }xeg; | ||||||||||||
| 371 | |||||||||||||
| 372 | # ~~strikethrough~~ → placeholder | ||||||||||||
| 373 | 243 | $text =~ s{~~(.+?)~~}{ | |||||||||||
| 374 | 1 | "\x01STRIKE$1\x01"; | |||||||||||
| 375 | }xeg; | ||||||||||||
| 376 | |||||||||||||
| 377 | 243 | return $text; | |||||||||||
| 378 | } | ||||||||||||
| 379 | |||||||||||||
| 380 | #=========================================================================== | ||||||||||||
| 381 | # Italic (process inside placeholders, then main text) | ||||||||||||
| 382 | #=========================================================================== | ||||||||||||
| 383 | |||||||||||||
| 384 | sub _convert_italic | ||||||||||||
| 385 | 243 | ▶ | { | ||||||||||
|
Subroutine
_convert_italic: called CC=1 SCAR=0.0
| |||||||||||||
| 386 | 243 | my ($text) = @_; | |||||||||||
| 387 | |||||||||||||
| 388 | # Process italic inside bold/strikethrough placeholders | ||||||||||||
| 389 | 243 | $text =~ s{\x01BOLD(.+?)\x01}{ | |||||||||||
| 390 | 15 | my $inner = $1; | |||||||||||
| 391 | 15 | $inner = _apply_italic($inner); | |||||||||||
| 392 | 15 | "\x01BOLD$inner\x01"; | |||||||||||
| 393 | }xeg; | ||||||||||||
| 394 | |||||||||||||
| 395 | 243 | $text =~ s{\x01STRIKE(.+?)\x01}{ | |||||||||||
| 396 | 1 | my $inner = $1; | |||||||||||
| 397 | 1 | $inner = _apply_italic($inner); | |||||||||||
| 398 | 1 | "\x01STRIKE$inner\x01"; | |||||||||||
| 399 | }xeg; | ||||||||||||
| 400 | |||||||||||||
| 401 | # Process italic in the main text | ||||||||||||
| 402 | 243 | $text = _apply_italic($text); | |||||||||||
| 403 | |||||||||||||
| 404 | 243 | return $text; | |||||||||||
| 405 | } | ||||||||||||
| 406 | |||||||||||||
| 407 | sub _apply_italic | ||||||||||||
| 408 | 259 | ▶ | { | ||||||||||
|
Subroutine
_apply_italic: called CC=1 SCAR=0.0
| |||||||||||||
| 409 | 259 | my ($text) = @_; | |||||||||||
| 410 | |||||||||||||
| 411 | # *italic* → _text_ (must be bounded by non-word chars) | ||||||||||||
| 412 | 259 | $text =~ s{(?<!\w)\*(.+?)\*(?!\w)}{ | |||||||||||
| 413 | 5 | "_$1_"; | |||||||||||
| 414 | }xeg; | ||||||||||||
| 415 | |||||||||||||
| 416 | # _italic_ → _text_ (must be bounded by non-word chars) | ||||||||||||
| 417 | 259 | $text =~ s{(?<!\w)_(.+?)_(?!\w)}{ | |||||||||||
| 418 | 12 | "_$1_"; | |||||||||||
| 419 | }xeg; | ||||||||||||
| 420 | |||||||||||||
| 421 | 259 | return $text; | |||||||||||
| 422 | } | ||||||||||||
| 423 | |||||||||||||
| 424 | #=========================================================================== | ||||||||||||
| 425 | # Resolve bold/strikethrough placeholders (AFTER italic processing) | ||||||||||||
| 426 | #=========================================================================== | ||||||||||||
| 427 | |||||||||||||
| 428 | sub _resolve_bold_and_strikethrough | ||||||||||||
| 429 | 243 | ▶ | { | ||||||||||
|
Subroutine
_resolve_bold_and_strikethrough: called CC=1 SCAR=0.0
| |||||||||||||
| 430 | 243 | my ($text) = @_; | |||||||||||
| 431 | |||||||||||||
| 432 | 243 | $text =~ s{\x01BOLD(.+?)\x01}{*$1*}g; | |||||||||||
| 433 | 243 | $text =~ s{\x01STRIKE(.+?)\x01}{[.line-through]#$1#}g; | |||||||||||
| 434 | |||||||||||||
| 435 | 243 | return $text; | |||||||||||
| 436 | } | ||||||||||||
| 437 | |||||||||||||
| 438 | #=========================================================================== | ||||||||||||
| 439 | # Clean up PROT markers → [...] | ||||||||||||
| 440 | #=========================================================================== | ||||||||||||
| 441 | |||||||||||||
| 442 | sub _cleanup_prot | ||||||||||||
| 443 | 243 | ▶ | { | ||||||||||
|
Subroutine
_cleanup_prot: called CC=1 SCAR=0.0
| |||||||||||||
| 444 | 243 | my ($text) = @_; | |||||||||||
| 445 | |||||||||||||
| 446 | 243 | $text =~ s{\x01PROT([^\x01]*)\x01PROT}{[$1]}g; | |||||||||||
| 447 | |||||||||||||
| 448 | 243 | return $text; | |||||||||||
| 449 | } | ||||||||||||
| 450 | |||||||||||||
| 451 | #=========================================================================== | ||||||||||||
| 452 | # HTML entities | ||||||||||||
| 453 | #=========================================================================== | ||||||||||||
| 454 | |||||||||||||
| 455 | sub _convert_html_entities | ||||||||||||
| 456 | 243 | ▶ | { | ||||||||||
|
Subroutine
_convert_html_entities: called CC=1 SCAR=0.0
| |||||||||||||
| 457 | 243 | my ($text) = @_; | |||||||||||
| 458 | |||||||||||||
| 459 | # → {nbsp} | ||||||||||||
| 460 | 243 | $text =~ s/ /\{nbsp\}/gi; | |||||||||||
| 461 | |||||||||||||
| 462 | 243 | return $text; | |||||||||||
| 463 | } | ||||||||||||
| 464 | |||||||||||||
| 465 | #=========================================================================== | ||||||||||||
| 466 | # Inline HTML | ||||||||||||
| 467 | #=========================================================================== | ||||||||||||
| 468 | |||||||||||||
| 469 | sub _convert_inline_html | ||||||||||||
| 470 | 243 | ▶ | { | ||||||||||
|
Subroutine
_convert_inline_html: called CC=1 SCAR=0.1
| |||||||||||||
| 471 | 243 | my ($text) = @_; | |||||||||||
| 472 | |||||||||||||
| 473 | # <br> or <br/> → hard line break | ||||||||||||
| 474 | 243 | $text =~ s{<br\s*/?>}{ +}gi; | |||||||||||
| 475 | |||||||||||||
| 476 | # <strong>text</strong> → *text* | ||||||||||||
| 477 | 243 | $text =~ s{<strong>(.+?)</strong>}{*$1*}gi; | |||||||||||
| 478 | |||||||||||||
| 479 | # <b>text</b> → *text* | ||||||||||||
| 480 | 243 | $text =~ s{<b>(.+?)</b>}{*$1*}gi; | |||||||||||
| 481 | |||||||||||||
| 482 | # <em>text</em> → _text_ | ||||||||||||
| 483 | 243 | $text =~ s{<em>(.+?)</em>}{_$1_}gi; | |||||||||||
| 484 | |||||||||||||
| 485 | # <i>text</i> → _text_ | ||||||||||||
| 486 | 243 | $text =~ s{<i>(.+?)</i>}{_$1_}gi; | |||||||||||
| 487 | |||||||||||||
| 488 | # <code>text</code> → `text` | ||||||||||||
| 489 | 243 | $text =~ s{<code>(.+?)</code>}{`$1`}gi; | |||||||||||
| 490 | |||||||||||||
| 491 | # <del>text</del> → [.line-through]#text# | ||||||||||||
| 492 | 243 | $text =~ s{<del>(.+?)</del>}{[.line-through]#$1#}gi; | |||||||||||
| 493 | |||||||||||||
| 494 | # <s>text</s> → [.line-through]#text# | ||||||||||||
| 495 | 243 | $text =~ s{<s>(.+?)</s>}{[.line-through]#$1#}gi; | |||||||||||
| 496 | |||||||||||||
| 497 | # <mark>text</mark> → #text# | ||||||||||||
| 498 | 243 | $text =~ s{<mark>(.+?)</mark>}{#$1#}gi; | |||||||||||
| 499 | |||||||||||||
| 500 | # <sup>text</sup> → ^text^ | ||||||||||||
| 501 | 243 | $text =~ s{<sup>(.+?)</sup>}{^$1^}gi; | |||||||||||
| 502 | |||||||||||||
| 503 | # <sub>text</sub> → ~text~ | ||||||||||||
| 504 | 243 | $text =~ s{<sub>(.+?)</sub>}{~$1~}gi; | |||||||||||
| 505 | |||||||||||||
| 506 | # Unknown inline tags: pass through as +++<tag>+++content+++</tag>+++ | ||||||||||||
| 507 | 243 | $text =~ s{<(/?)([a-zA-Z][a-zA-Z0-9]*)(\s[^>]*)?>}{ | |||||||||||
| 508 | 0 | my $slash = $1; | |||||||||||
| 509 | 0 | my $tag = $2; | |||||||||||
| 510 | 0 | "+++<$slash$tag>+++"; | |||||||||||
| 511 | }xeg; | ||||||||||||
| 512 | |||||||||||||
| 513 | 243 | return $text; | |||||||||||
| 514 | } | ||||||||||||
| 515 | |||||||||||||
| 516 | #=========================================================================== | ||||||||||||
| 517 | # Smart quotes | ||||||||||||
| 518 | #=========================================================================== | ||||||||||||
| 519 | |||||||||||||
| 520 | sub _convert_smart_quotes | ||||||||||||
| 521 | 243 | ▶ | { | ||||||||||
|
Subroutine
_convert_smart_quotes: called CC=1 SCAR=0.0
| |||||||||||||
| 522 | 243 | my ($text) = @_; | |||||||||||
| 523 | |||||||||||||
| 524 | # Unicode smart quotes → AsciiDoc typographic syntax | ||||||||||||
| 525 | 243 | $text =~ s/\x{201C}/"\`/g; | |||||||||||
| 526 | 243 | $text =~ s/\x{201D}/\`"/g; | |||||||||||
| 527 | 243 | $text =~ s/\x{2018}/'\`/g; | |||||||||||
| 528 | 243 | $text =~ s/\x{2019}/\`'/g; | |||||||||||
| 529 | |||||||||||||
| 530 | 243 | return $text; | |||||||||||
| 531 | } | ||||||||||||
| 532 | |||||||||||||
| 533 | #=========================================================================== | ||||||||||||
| 534 | # Inline math — $...$ → stem:[...] | ||||||||||||
| 535 | #=========================================================================== | ||||||||||||
| 536 | |||||||||||||
| 537 | sub _convert_inline_math | ||||||||||||
| 538 | 243 | ▶ | { | ||||||||||
|
Subroutine
_convert_inline_math: called CC=1 SCAR=0.0
| |||||||||||||
| 539 | 243 | my ($text, $self) = @_; | |||||||||||
| 540 | |||||||||||||
| 541 | # $...$ where content does not span lines (single-line math) | ||||||||||||
| 542 | # Must not be preceded by $ (avoid $$ block math) | ||||||||||||
| 543 | # Content must have at least one non-space character | ||||||||||||
| 544 | # Use PROT markers for brackets to avoid ref link consumption | ||||||||||||
| 545 | 243 | $text =~ s{ | |||||||||||
| 546 | (?<!\$) # not preceded by $ (avoid $$) | ||||||||||||
| 547 | \$ | ||||||||||||
| 548 | ( [^\$\n]+? ) # math content (no $ chars, no newlines) | ||||||||||||
| 549 | \$ | ||||||||||||
| 550 | (?!\$) # not followed by $ (avoid $$) | ||||||||||||
| 551 | }{ | ||||||||||||
| 552 | 2 | $self->{has_math} = 1; | |||||||||||
| 553 | 2 | "stem:\x01PROT" . $1 . "\x01PROT"; | |||||||||||
| 554 | }xeg; | ||||||||||||
| 555 | |||||||||||||
| 556 | 243 | return $text; | |||||||||||
| 557 | } | ||||||||||||
| 558 | |||||||||||||
| 559 | #=========================================================================== | ||||||||||||
| 560 | # Typographic symbols | ||||||||||||
| 561 | #=========================================================================== | ||||||||||||
| 562 | |||||||||||||
| 563 | sub _convert_typographic_symbols | ||||||||||||
| 564 | 243 | ▶ | { | ||||||||||
|
Subroutine
_convert_typographic_symbols: called CC=1 SCAR=0.0
| |||||||||||||
| 565 | 243 | my ($text) = @_; | |||||||||||
| 566 | |||||||||||||
| 567 | # --- (em dash in paragraph text) → -- (AsciiDoc em dash) | ||||||||||||
| 568 | # In inline context, --- is not a thematic break, so convert. | ||||||||||||
| 569 | # Code spans are already protected at this point. | ||||||||||||
| 570 | 243 | $text =~ s/---/--/g; | |||||||||||
| 571 | |||||||||||||
| 572 | # ... (ellipsis) passes through unchanged (already ... in AsciiDoc) | ||||||||||||
| 573 | # en dash -- passes through unchanged | ||||||||||||
| 574 | |||||||||||||
| 575 | 243 | return $text; | |||||||||||
| 576 | } | ||||||||||||
| 577 | |||||||||||||
| 578 | 17 | 1; | |||||||||||
| 579 | |||||||||||||
| 580 | __END__ | ||||||||||||