| 1 | # | ||
| 2 | # This file is part of Markdown::Publish. | ||
| 3 | # | ||
| 4 | # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>. | ||
| 5 | # | ||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||
| 8 | # | ||
| 9 | # Full license text is available at: | ||
| 10 | # | ||
| 11 | # <http://dev.perl.org/licenses/> | ||
| 12 | # | ||
| 13 | package Markdown::Publish; | ||
| 14 | |||
| 15 | |||
| 16 | # Compiler pragma and package variables | ||
| 17 | # | ||
| 18 | use strict qw(vars); | ||
| 19 | use vars qw($VERSION $AUTHORITY); | ||
| 20 | use warnings; | ||
| 21 | |||
| 22 | |||
| 23 | # Core and external packages | ||
| 24 | # | ||
| 25 | use Cwd qw(abs_path getcwd); | ||
| 26 | use File::Basename qw(basename); | ||
| 27 | use File::Copy qw(copy); | ||
| 28 | use File::Find (); | ||
| 29 | use File::Path qw(make_path); | ||
| 30 | use File::Spec; | ||
| 31 | use File::Temp qw(tempdir); | ||
| 32 | use IPC::Run3 qw(run3); | ||
| 33 | use JSON::PP qw(decode_json encode_json); | ||
| 34 | use Markdown::Publish::Constant; | ||
| 35 | |||
| 36 | |||
| 37 | # Version information | ||
| 38 | # | ||
| 39 | $AUTHORITY='cpan:ASPEER'; | ||
| 40 | $VERSION='1.004'; | ||
| 41 | |||
| 42 | |||
| 43 | # Supported publication actions | ||
| 44 | # | ||
| 45 | my %ACTION=map {$_ => 1} qw(build serve gh gh-push cloudflare); | ||
| 46 | |||
| 47 | |||
| 48 | # Short names for the publisher classes supplied by this distribution | ||
| 49 | # | ||
| 50 | my %module_alias=( | ||
| 51 | mkdocs => 'Markdown::Publish::MkDocs', | ||
| 52 | vitepress => 'Markdown::Publish::VitePress', | ||
| 53 | docusaurus => 'Markdown::Publish::Docusaurus', | ||
| 54 | starlight => 'Markdown::Publish::Starlight' | ||
| 55 | ); | ||
| 56 | |||
| 57 | |||
| 58 | # Done | ||
| 59 | # | ||
| 60 | 1; | ||
| 61 | |||
| 62 | |||
| 63 | #====================================================================================================================== | ||
| 64 | |||
| 65 | sub new { | ||
| 66 | |||
| 67 | my ($class, $opt_hr)=@_; | ||
| 68 | $opt_hr={} unless defined($opt_hr); | ||
| 69 | die "publication configuration must be a hash reference\n" | ||
| 70 | unless ref($opt_hr) eq 'HASH'; | ||
| 71 | if ($class eq __PACKAGE__) { | ||
| 72 | if (exists($opt_hr->{'config_file'})) { | ||
| 73 | die "config_file cannot be combined with inline publication settings\n" | ||
| 74 | unless keys(%{$opt_hr})==1; | ||
| 75 | return $class->load_config($opt_hr->{'config_file'}); | ||
| 76 | } | ||
| 77 | my $publisher=defined($ENV{'MARKDOWN_PUBLISH_MODULE'}) ? | ||
| 78 | $ENV{'MARKDOWN_PUBLISH_MODULE'} : | ||
| 79 | (exists($opt_hr->{'module'}) ? $opt_hr->{'module'} : $MARKDOWN_PUBLISH_MODULE); | ||
| 80 | die "publication module is not defined\n" | ||
| 81 | unless defined($publisher) && length($publisher); | ||
| 82 | $publisher=$module_alias{lc($publisher)} || $publisher; | ||
| 83 | (my $publisher_fn=$publisher)=~s{::}{/}g; | ||
| 84 | my $loaded=eval { | ||
| 85 | require "$publisher_fn.pm"; | ||
| 86 | 1; | ||
| 87 | }; | ||
| 88 | die "unable to load publication module $publisher: $@" | ||
| 89 | unless $loaded; | ||
| 90 | die "$publisher is not a Markdown::Publish subclass\n" | ||
| 91 | unless $publisher->isa(__PACKAGE__); | ||
| 92 | return $publisher->new($opt_hr); | ||
| 93 | } | ||
| 94 | my $self=bless({%{$opt_hr}}, $class); | ||
| 95 | return $self; | ||
| 96 | |||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub load_config { | ||
| 101 | |||
| 102 | |||
| 103 | # Read a standalone JSON configuration and accept either the complete | ||
| 104 | # metadata extension or its publish section. | ||
| 105 | # | ||
| 106 | my ($class, $config_fn)=@_; | ||
| 107 | open(my $config_fh, '<', $config_fn) || | ||
| 108 | die "unable to read publication configuration $config_fn: $!\n"; | ||
| 109 | local $/=undef; | ||
| 110 | my $json=<$config_fh>; | ||
| 111 | close($config_fh) || | ||
| 112 | die "unable to close publication configuration $config_fn: $!\n"; | ||
| 113 | my $config_hr=decode_json($json); | ||
| 114 | die "publication configuration in $config_fn must be an object\n" | ||
| 115 | unless ref($config_hr) eq 'HASH'; | ||
| 116 | |||
| 117 | if (ref($config_hr->{'x_documentation'}) eq 'HASH') { | ||
| 118 | $config_hr=$config_hr->{'x_documentation'}; | ||
| 119 | } | ||
| 120 | if (ref($config_hr->{'publish'}) eq 'HASH') { | ||
| 121 | $config_hr=$config_hr->{'publish'}; | ||
| 122 | } | ||
| 123 | return $class->new($config_hr); | ||
| 124 | |||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | sub option { | ||
| 129 | |||
| 130 | my ($self, $name, $default)=@_; | ||
| 131 | return $self->{$name} if exists($self->{$name}); | ||
| 132 | return $default; | ||
| 133 | |||
| 134 | } | ||
| 135 | |||
| 136 | |||
| 137 | sub configuration_files { | ||
| 138 | |||
| 139 | my ($self)=@_; | ||
| 140 | my $config_fn=$self->option('config', undef); | ||
| 141 | my $extend_fn=$self->option('config_extend', undef); | ||
| 142 | my $has_config=defined($config_fn) && length($config_fn); | ||
| 143 | my $has_extend=defined($extend_fn) && length($extend_fn); | ||
| 144 | die "config and config_extend cannot be combined\n" | ||
| 145 | if $has_config && $has_extend; | ||
| 146 | return ($has_config ? $config_fn : undef, | ||
| 147 | $has_extend ? $extend_fn : undef); | ||
| 148 | |||
| 149 | } | ||
| 150 | |||
| 151 | |||
| 152 | sub configuration_context { | ||
| 153 | |||
| 154 | my ($self, $pages_ar, $navigation_ar)=@_; | ||
| 155 | $pages_ar=[] unless defined($pages_ar); | ||
| 156 | $navigation_ar=[] unless defined($navigation_ar); | ||
| 157 | return { | ||
| 158 | name => $self->option('name', 'Documentation'), | ||
| 159 | base => $self->site_base('/'), | ||
| 160 | output => $self->option('output', $MARKDOWN_PUBLISH_OUTPUT_DN), | ||
| 161 | pages => [@{$pages_ar}], | ||
| 162 | navigation => [map {{%{$_}}} @{$navigation_ar}] | ||
| 163 | }; | ||
| 164 | |||
| 165 | } | ||
| 166 | |||
| 167 | |||
| 168 | sub site_base { | ||
| 169 | |||
| 170 | my ($self, $default)=@_; | ||
| 171 | my $base=exists($self->{'base'}) ? $self->{'base'} : $default; | ||
| 172 | return undef unless defined($base); | ||
| 173 | die "publication base must start and end with / and contain no empty or dot segments\n" | ||
| 174 | if ref($base) || !length($base) || $base!~m{\A/} || $base!~m{/\z} || | ||
| 175 | $base=~m{//} || $base=~m{(?:\A|/)\.\.?/} || $base=~m{[\s?#]}; | ||
| 176 | return $base; | ||
| 177 | |||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | sub github_repository_name { | ||
| 182 | |||
| 183 | my ($self)=@_; | ||
| 184 | my $remote; | ||
| 185 | eval {$remote=$self->command('git', 'config', '--get', 'remote.origin.url')}; | ||
| 186 | if (defined($remote) && length($remote)) { | ||
| 187 | $remote=~s/[\r\n]+\z//; | ||
| 188 | $remote=~s{[/\\]+\z}{}; | ||
| 189 | $remote=~s{\.git\z}{}i; | ||
| 190 | if ($remote=~m{([^/\\:]+)\z}) { | ||
| 191 | my $repository=$1; | ||
| 192 | return $repository if $repository=~/\A[A-Za-z0-9._-]+\z/; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | my $root_dn=$self->command('git', 'rev-parse', '--show-toplevel'); | ||
| 196 | $root_dn=~s/[\r\n]+\z//; | ||
| 197 | my $repository=basename($root_dn); | ||
| 198 | die "unable to determine GitHub repository name\n" | ||
| 199 | unless defined($repository) && $repository=~/\A[A-Za-z0-9._-]+\z/; | ||
| 200 | return $repository; | ||
| 201 | |||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | sub github_base { | ||
| 206 | |||
| 207 | my ($self)=@_; | ||
| 208 | my $repository=$self->github_repository_name(); | ||
| 209 | return '/' if $repository=~/\.github\.io\z/i; | ||
| 210 | return "/$repository/"; | ||
| 211 | |||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | sub github_site_base { | ||
| 216 | |||
| 217 | my ($self)=@_; | ||
| 218 | return $self->site_base('/') if exists($self->{'base'}); | ||
| 219 | return $self->github_base(); | ||
| 220 | |||
| 221 | } | ||
| 222 | |||
| 223 | |||
| 224 | sub validate_action { | ||
| 225 | |||
| 226 | my ($self, $action)=@_; | ||
| 227 | die "unknown publication action: $action\n" | ||
| 228 | unless defined($action) && $ACTION{$action}; | ||
| 229 | return 1; | ||
| 230 | |||
| 231 | } | ||
| 232 | |||
| 233 | |||
| 234 | sub run { | ||
| 235 | |||
| 236 | my ($self, $action)=@_; | ||
| 237 | $self->validate_action($action); | ||
| 238 | return $self->build() if $action eq 'build'; | ||
| 239 | return $self->serve() if $action eq 'serve'; | ||
| 240 | return $self->publish_gh() if $action eq 'gh'; | ||
| 241 | return $self->publish_gh_push() if $action eq 'gh-push'; | ||
| 242 | return $self->publish_cloudflare(); | ||
| 243 | |||
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | sub command { | ||
| 248 | |||
| 249 | my ($self, @command)=@_; | ||
| 250 | my ($output, $error); | ||
| 251 | run3(\@command, \undef, \$output, \$error); | ||
| 252 | die "command failed (@command): $error\n" if $?; | ||
| 253 | return $output; | ||
| 254 | |||
| 255 | } | ||
| 256 | |||
| 257 | |||
| 258 | sub system_command { | ||
| 259 | |||
| 260 | my ($self, @command)=@_; | ||
| 261 | system(@command); | ||
| 262 | die "command failed (@command)\n" if $?; | ||
| 263 | return 1; | ||
| 264 | |||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | sub system_in_dir { | ||
| 269 | |||
| 270 | my ($self, $dir, @command)=@_; | ||
| 271 | my $cwd=getcwd(); | ||
| 272 | chdir($dir) || die "unable to chdir $dir: $!\n"; | ||
| 273 | my $ok=eval {$self->system_command(@command); 1}; | ||
| 274 | my $error=$@; | ||
| 275 | chdir($cwd) || die "unable to chdir $cwd: $!\n"; | ||
| 276 | die $error unless $ok; | ||
| 277 | return 1; | ||
| 278 | |||
| 279 | } | ||
| 280 | |||
| 281 | |||
| 282 | sub write_file { | ||
| 283 | |||
| 284 | my ($self, $fn, $text)=@_; | ||
| 285 | my (undef, $parent_dn)=File::Spec->splitpath($fn); | ||
| 286 | make_path($parent_dn) if length($parent_dn) && !-d $parent_dn; | ||
| 287 | open(my $output_fh, '>', $fn) || die "unable to write $fn: $!\n"; | ||
| 288 | print {$output_fh} $text; | ||
| 289 | close($output_fh) || die "unable to close $fn: $!\n"; | ||
| 290 | return 1; | ||
| 291 | |||
| 292 | } | ||
| 293 | |||
| 294 | |||
| 295 | sub copy_tree { | ||
| 296 | |||
| 297 | my ($self, $source_dn, $target_dn)=@_; | ||
| 298 | return unless -d $source_dn; | ||
| 299 | File::Find::find({ | ||
| 300 | no_chdir => 1, | ||
| 301 | wanted => sub { | ||
| 302 | my $fn=$File::Find::name; | ||
| 303 | return if -l $fn; | ||
| 304 | my $relative=File::Spec->abs2rel($fn, $source_dn); | ||
| 305 | my $output_fn=File::Spec->catfile($target_dn, $relative); | ||
| 306 | if (-d $fn) { | ||
| 307 | make_path($output_fn); | ||
| 308 | } | ||
| 309 | elsif (-f $fn) { | ||
| 310 | copy($fn, $output_fn) || die "unable to copy $fn: $!\n"; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | }, $source_dn); | ||
| 314 | return 1; | ||
| 315 | |||
| 316 | } | ||
| 317 | |||
| 318 | |||
| 319 | sub source_directories { | ||
| 320 | |||
| 321 | |||
| 322 | # An explicit source list is exact. Without one, doc is the publication | ||
| 323 | # boundary whenever it exists; module sidecars are only a fallback. | ||
| 324 | # | ||
| 325 | my ($self)=@_; | ||
| 326 | if (exists($self->{'sources'})) { | ||
| 327 | die "publication sources must be an array reference\n" | ||
| 328 | unless ref($self->{'sources'}) eq 'ARRAY'; | ||
| 329 | return [@{$self->{'sources'}}]; | ||
| 330 | } | ||
| 331 | return ['doc'] if -d 'doc'; | ||
| 332 | my @source_dn=grep {-d $_} qw(lib bin); | ||
| 333 | warn "doc directory not found; publishing module and executable sidecars\n" | ||
| 334 | if @source_dn; | ||
| 335 | return \@source_dn; | ||
| 336 | |||
| 337 | } | ||
| 338 | |||
| 339 | |||
| 340 | sub target_filename { | ||
| 341 | |||
| 342 | my ($self, $source_dn, $fn)=@_; | ||
| 343 | my $relative=File::Spec->abs2rel($fn, $source_dn); | ||
| 344 | if ($source_dn eq 'lib') { | ||
| 345 | $relative=~s/\.pm\.md$/.md/; | ||
| 346 | $relative=~s{[/\\]}{_}g; | ||
| 347 | return File::Spec->catfile('modules', $relative); | ||
| 348 | } | ||
| 349 | if ($source_dn eq 'bin') { | ||
| 350 | return File::Spec->catfile('utilities', $relative); | ||
| 351 | } | ||
| 352 | return $relative; | ||
| 353 | |||
| 354 | } | ||
| 355 | |||
| 356 | |||
| 357 | sub copy_markdown_tree { | ||
| 358 | |||
| 359 | my ($self, $source_dn, $docs_dn)=@_; | ||
| 360 | return unless -d $source_dn; | ||
| 361 | File::Find::find({ | ||
| 362 | no_chdir => 1, | ||
| 363 | preprocess => sub {sort @_}, | ||
| 364 | wanted => sub { | ||
| 365 | my $fn=$File::Find::name; | ||
| 366 | return if -l $fn; | ||
| 367 | return unless -f $fn && $fn=~/\.md$/; | ||
| 368 | my $target_fn=File::Spec->catfile($source_dn, | ||
| 369 | File::Spec->abs2rel($fn, $source_dn)); | ||
| 370 | my $output_fn=File::Spec->catfile($docs_dn, $target_fn); | ||
| 371 | die "publication page already exists: $target_fn\n" if -e $output_fn; | ||
| 372 | my (undef, $parent_dn)=File::Spec->splitpath($output_fn); | ||
| 373 | make_path($parent_dn); | ||
| 374 | copy($fn, $output_fn) || die "unable to copy $fn: $!\n"; | ||
| 375 | } | ||
| 376 | }, $source_dn); | ||
| 377 | return 1; | ||
| 378 | |||
| 379 | } | ||
| 380 | |||
| 381 | |||
| 382 | sub promote_home { | ||
| 383 | |||
| 384 | # Keep the first page's original path available for authored links. | ||
| 385 | # Only a top-level page can be copied to index without rebasing its links. | ||
| 386 | # | ||
| 387 | my ($self, $docs_dn, $pages_ar)=@_; | ||
| 388 | return unless @{$pages_ar}; | ||
| 389 | my $has_index=grep {$_ eq 'index.md'} @{$pages_ar}; | ||
| 390 | return if $has_index || $pages_ar->[0]=~m{[/\\]}; | ||
| 391 | my $first_fn=File::Spec->catfile($docs_dn, $pages_ar->[0]); | ||
| 392 | my $index_fn=File::Spec->catfile($docs_dn, 'index.md'); | ||
| 393 | copy($first_fn, $index_fn) || die "unable to copy $first_fn: $!\n"; | ||
| 394 | $pages_ar->[0]='index.md'; | ||
| 395 | return 1; | ||
| 396 | |||
| 397 | } | ||
| 398 | |||
| 399 | |||
| 400 | sub prepare_docs { | ||
| 401 | |||
| 402 | |||
| 403 | # Assemble the configured source roots and mirror lib/bin Markdown when | ||
| 404 | # doc is selected. Nested documents remain linkable but outside generated | ||
| 405 | # navigation. | ||
| 406 | # | ||
| 407 | my ($self)=@_; | ||
| 408 | my $temporary_dn=abs_path(tempdir(CLEANUP => 1)); | ||
| 409 | my $docs_dn=File::Spec->catdir($temporary_dn, 'docs'); | ||
| 410 | make_path($docs_dn); | ||
| 411 | my @pages; | ||
| 412 | my $source_dn_ar=$self->source_directories(); | ||
| 413 | |||
| 414 | foreach my $source_dn (@{$source_dn_ar}) { | ||
| 415 | die "publication source directory not found: $source_dn\n" | ||
| 416 | unless -d $source_dn; | ||
| 417 | File::Find::find({ | ||
| 418 | no_chdir => 1, | ||
| 419 | preprocess => sub {sort @_}, | ||
| 420 | wanted => sub { | ||
| 421 | my $fn=$File::Find::name; | ||
| 422 | if (-d $fn && $fn=~m{[/\\](?:build|example|examples|mkdocs|node_modules|site|t)$}) { | ||
| 423 | $File::Find::prune=1; | ||
| 424 | return; | ||
| 425 | } | ||
| 426 | return unless -f $fn && !-l $fn && $fn=~/\.md$/; | ||
| 427 | my $target_fn=$self->target_filename($source_dn, $fn); | ||
| 428 | my $output_fn=File::Spec->catfile($docs_dn, $target_fn); | ||
| 429 | my (undef, $parent_dn)=File::Spec->splitpath($output_fn); | ||
| 430 | make_path($parent_dn); | ||
| 431 | my $navigation_page=$source_dn eq 'lib' || $source_dn eq 'bin' || | ||
| 432 | $target_fn!~m{[/\\]}; | ||
| 433 | |||
| 434 | if ($source_dn eq 'doc' && $navigation_page) { | ||
| 435 | open(my $input_fh, '<', $fn) || die "unable to read $fn: $!\n"; | ||
| 436 | local $/=undef; | ||
| 437 | my $markdown=<$input_fh>; | ||
| 438 | close($input_fh) || die "unable to close $fn: $!\n"; | ||
| 439 | my $split_hr=$self->split($target_fn, $markdown); | ||
| 440 | if (keys(%{$split_hr}) > 1) { | ||
| 441 | foreach my $page (@{$self->{'page_order'}}) { | ||
| 442 | my $page_fn=File::Spec->catfile($docs_dn, $page); | ||
| 443 | $self->write_file($page_fn, $split_hr->{$page}); | ||
| 444 | push(@pages, $page); | ||
| 445 | } | ||
| 446 | return; | ||
| 447 | } | ||
| 448 | } | ||
| 449 | copy($fn, $output_fn) || die "unable to copy $fn: $!\n"; | ||
| 450 | push(@pages, $target_fn) if $navigation_page; | ||
| 451 | } | ||
| 452 | }, $source_dn); | ||
| 453 | } | ||
| 454 | |||
| 455 | die "no Markdown documents discovered in publication sources\n" unless @pages; | ||
| 456 | if (grep {$_ eq 'doc'} @{$source_dn_ar}) { | ||
| 457 | $self->copy_markdown_tree('lib', $docs_dn); | ||
| 458 | $self->copy_markdown_tree('bin', $docs_dn); | ||
| 459 | } | ||
| 460 | # Keep a home page without placing its generated link list in navigation. | ||
| 461 | # | ||
| 462 | unless (-f File::Spec->catfile($docs_dn, 'index.md')) { | ||
| 463 | my $index="# Documentation\n\n"; | ||
| 464 | $index.="- [$_]($_)\n" foreach @pages; | ||
| 465 | $self->write_file(File::Spec->catfile($docs_dn, 'index.md'), $index); | ||
| 466 | } | ||
| 467 | |||
| 468 | foreach my $source_dn (@{$source_dn_ar}) { | ||
| 469 | $self->copy_tree(File::Spec->catdir($source_dn, 'images'), | ||
| 470 | File::Spec->catdir($docs_dn, 'images')); | ||
| 471 | $self->copy_tree(File::Spec->catdir($source_dn, 'assets'), | ||
| 472 | File::Spec->catdir($docs_dn, 'assets')); | ||
| 473 | } | ||
| 474 | return ($temporary_dn, $docs_dn, \@pages); | ||
| 475 | |||
| 476 | } | ||
| 477 | |||
| 478 | |||
| 479 | sub split { | ||
| 480 | |||
| 481 | my ($self, $fn, $markdown)=@_; | ||
| 482 | (my $stem=$fn)=~s/\.md$//; | ||
| 483 | my (%pages, %anchor, %chapter_anchor, @order); | ||
| 484 | my ($page, $fence, $length, $preamble)=('', '', 0, ''); | ||
| 485 | foreach my $line (split(/(?<=\n)/, $markdown)) { | ||
| 486 | if (!$fence && $line=~/^ {0,3}(`{3,}|~{3,})/) { | ||
| 487 | $fence=substr($1, 0, 1); | ||
| 488 | $length=length($1); | ||
| 489 | } | ||
| 490 | elsif ($fence && $line=~/^ {0,3}\Q$fence\E{$length,}\s*$/) { | ||
| 491 | $fence=''; | ||
| 492 | } | ||
| 493 | elsif (!$fence && $line=~/^#\s+(.+?)\s*$/) { | ||
| 494 | my $title=$1; | ||
| 495 | my ($id)=$title=~/\{#([^}]+)\}/; | ||
| 496 | unless ($id) { | ||
| 497 | $id=lc($title); | ||
| 498 | $id=~s/[^a-z0-9]+/-/g; | ||
| 499 | $id=~s/^-|-$//g; | ||
| 500 | } | ||
| 501 | die "empty or unsafe chapter ID in $fn\n" | ||
| 502 | unless $id && $id=~/^[\w.-]+$/; | ||
| 503 | $page="$stem--$id.md"; | ||
| 504 | die "duplicate chapter ID $id in $fn\n" if exists($pages{$page}); | ||
| 505 | $pages{$page}=$preamble; | ||
| 506 | $preamble=''; | ||
| 507 | $chapter_anchor{$id}=1; | ||
| 508 | push(@order, $page); | ||
| 509 | } | ||
| 510 | $anchor{$1}=$page if !$fence && $line=~/\{#([^}]+)\}/ && $page; | ||
| 511 | if ($page) { | ||
| 512 | $pages{$page}.=$line; | ||
| 513 | } | ||
| 514 | else { | ||
| 515 | $preamble.=$line; | ||
| 516 | } | ||
| 517 | } | ||
| 518 | |||
| 519 | foreach my $current_page (@order) { | ||
| 520 | my $text=''; | ||
| 521 | my ($current_fence, $current_length)=('', 0); | ||
| 522 | foreach my $line (split(/(?<=\n)/, $pages{$current_page})) { | ||
| 523 | if (!$current_fence && $line=~/^ {0,3}(`{3,}|~{3,})/) { | ||
| 524 | $current_fence=substr($1, 0, 1); | ||
| 525 | $current_length=length($1); | ||
| 526 | } | ||
| 527 | elsif ($current_fence && $line=~/^ {0,3}\Q$current_fence\E{$current_length,}\s*$/) { | ||
| 528 | $current_fence=''; | ||
| 529 | } | ||
| 530 | elsif (!$current_fence) { | ||
| 531 | foreach my $id (keys(%anchor)) { | ||
| 532 | my (undef, undef, $target)=File::Spec->splitpath($anchor{$id}); | ||
| 533 | my $link=$target.($chapter_anchor{$id} ? '' : "#$id"); | ||
| 534 | $line=~s{\]\(\#\Q$id\E\)}{]($link)}g; | ||
| 535 | } | ||
| 536 | } | ||
| 537 | $text.=$line; | ||
| 538 | } | ||
| 539 | $pages{$current_page}=$text; | ||
| 540 | } | ||
| 541 | $self->{'page_order'}=\@order; | ||
| 542 | return \%pages; | ||
| 543 | |||
| 544 | } | ||
| 545 | |||
| 546 | |||
| 547 | sub normalize_node_admonitions { | ||
| 548 | |||
| 549 | my ($self, $markdown)=@_; | ||
| 550 | my @output; | ||
| 551 | my @line=split(/(?<=\n)/, $markdown); | ||
| 552 | for (my $index=0; $index<@line; $index++) { | ||
| 553 | if ($line[$index]=~/^!!!\s+(\w+).*?\n?$/) { | ||
| 554 | my $kind=$self->admonition_type($1); | ||
| 555 | push(@output, ":::$kind\n"); | ||
| 556 | while ($index + 1 < @line && $line[$index + 1]=~/^(?: |\s*$)/) { | ||
| 557 | $index++; | ||
| 558 | my $admonition_line=$line[$index]; | ||
| 559 | $admonition_line=~s/^ //; | ||
| 560 | push(@output, $admonition_line); | ||
| 561 | } | ||
| 562 | push(@output, ":::\n"); | ||
| 563 | next; | ||
| 564 | } | ||
| 565 | push(@output, $line[$index]); | ||
| 566 | } | ||
| 567 | return join('', @output); | ||
| 568 | |||
| 569 | } | ||
| 570 | |||
| 571 | |||
| 572 | sub admonition_type { | ||
| 573 | |||
| 574 | my ($self, $kind)=@_; | ||
| 575 | return $kind; | ||
| 576 | |||
| 577 | } | ||
| 578 | |||
| 579 | |||
| 580 | sub normalize_node_definition_lists { | ||
| 581 | |||
| 582 | my ($self, $markdown)=@_; | ||
| 583 | my @line=split(/(?<=\n)/, $markdown); | ||
| 584 | my @output; | ||
| 585 | my ($fence, $length)=('', 0); | ||
| 586 | for (my $index=0; $index<@line; $index++) { | ||
| 587 | if (!$fence && $line[$index]=~/^ {0,3}(`{3,}|~{3,})/) { | ||
| 588 | $fence=substr($1, 0, 1); | ||
| 589 | $length=length($1); | ||
| 590 | } | ||
| 591 | elsif ($fence && $line[$index]=~/^ {0,3}\Q$fence\E{$length,}\s*$/) { | ||
| 592 | $fence=''; | ||
| 593 | } | ||
| 594 | |||
| 595 | # Pandoc definition lists are not portable to the Node renderers. | ||
| 596 | # Their two-space continuation indent is also the content indent for | ||
| 597 | # the equivalent CommonMark list item, so the remaining block can be | ||
| 598 | # retained verbatim. | ||
| 599 | # | ||
| 600 | if (!$fence && $index + 2 < @line && $line[$index]!~/^\s*$/ && | ||
| 601 | $line[$index + 1]=~/^\s*$/ && $line[$index + 2]=~/^:\s+(.*)$/) { | ||
| 602 | my $term=$line[$index]; | ||
| 603 | my $description=$1; | ||
| 604 | my $newline=$line[$index + 2]=~/\n\z/ ? "\n" : ''; | ||
| 605 | $term=~s/\r?\n\z//; | ||
| 606 | $description=~s/\r?\n\z//; | ||
| 607 | push(@output, "- **$term**\n\n $description$newline"); | ||
| 608 | $index+=2; | ||
| 609 | next; | ||
| 610 | } | ||
| 611 | push(@output, $line[$index]); | ||
| 612 | } | ||
| 613 | return join('', @output); | ||
| 614 | |||
| 615 | } | ||
| 616 | |||
| 617 | |||
| 618 | sub normalize_node_attributes { | ||
| 619 | |||
| 620 | my ($self, $markdown)=@_; | ||
| 621 | my @output; | ||
| 622 | my ($fence, $length)=('', 0); | ||
| 623 | foreach my $line (split(/(?<=\n)/, $markdown)) { | ||
| 624 | if ($fence) { | ||
| 625 | if ($line=~/^ {0,3}\Q$fence\E{$length,}\s*$/) { | ||
| 626 | $fence=''; | ||
| 627 | } | ||
| 628 | push(@output, $line); | ||
| 629 | next; | ||
| 630 | } | ||
| 631 | |||
| 632 | # Retain an authored code-block ID as an adjacent HTML anchor and | ||
| 633 | # pass its first class as the conventional fenced-code language. | ||
| 634 | # | ||
| 635 | if ($line=~/^( {0,3})(`{3,}|~{3,})[ \t]*\{([^}\r\n]+)\}[ \t]*(\r?\n)?$/) { | ||
| 636 | my ($indent, $delimiter, $attributes, $newline)=($1, $2, $3, $4 || ''); | ||
| 637 | my ($id)=$attributes=~/(?:^|\s)#([\w.-]+)/; | ||
| 638 | my ($language)=$attributes=~/(?:^|\s)\.([\w+-]+)/; | ||
| 639 | push(@output, "$indent<a id=\"$id\"></a>$newline") if defined($id); | ||
| 640 | push(@output, $indent.$delimiter.(defined($language) ? $language : '').$newline); | ||
| 641 | $fence=substr($delimiter, 0, 1); | ||
| 642 | $length=length($delimiter); | ||
| 643 | next; | ||
| 644 | } | ||
| 645 | if ($line=~/^ {0,3}(`{3,}|~{3,})/) { | ||
| 646 | $fence=substr($1, 0, 1); | ||
| 647 | $length=length($1); | ||
| 648 | push(@output, $line); | ||
| 649 | next; | ||
| 650 | } | ||
| 651 | |||
| 652 | # Docusaurus and Starlight do not accept Pandoc heading attributes. | ||
| 653 | # A raw anchor preserves the stable identifiers used by split links. | ||
| 654 | # | ||
| 655 | if ($self->heading_anchor_required() && | ||
| 656 | $line=~/^( {0,3})(#{1,6}[^\r\n]*?)\s+\{#([\w.-]+)(?:\s+[^}]*)?\}[ \t]*(\r?\n)?$/) { | ||
| 657 | push(@output, "$1<a id=\"$3\"></a>".($4 || '').$1.$2.($4 || '')); | ||
| 658 | next; | ||
| 659 | } | ||
| 660 | |||
| 661 | # Attribute blocks on inline links and images otherwise become | ||
| 662 | # visible text in one or more of the Node renderers. | ||
| 663 | # | ||
| 664 | $line=~s{((?:!)?\[[^\]]*\]\([^\)\r\n]+\))\{[^\}\r\n]+\}}{$1}g; | ||
| 665 | push(@output, $line); | ||
| 666 | } | ||
| 667 | return join('', @output); | ||
| 668 | |||
| 669 | } | ||
| 670 | |||
| 671 | |||
| 672 | sub heading_anchor_required { | ||
| 673 | |||
| 674 | return 0; | ||
| 675 | |||
| 676 | } | ||
| 677 | |||
| 678 | |||
| 679 | |||
| 680 | |||
| 681 | sub markdown_title { | ||
| 682 | |||
| 683 | my ($self, $fn, $markdown)=@_; | ||
| 684 | my $title; | ||
| 685 | |||
| 686 | # Prefer authored frontmatter, accepting the simple quoted and unquoted | ||
| 687 | # title forms used by the supported documentation engines. | ||
| 688 | # | ||
| 689 | if ($markdown=~/\A---[ \t]*\r?\n(.*?)^---[ \t]*\r?\n/ms) { | ||
| 690 | my $frontmatter=$1; | ||
| 691 | ($title)=$frontmatter=~/^title:[ \t]*(.*?)[ \t]*\r?$/m; | ||
| 692 | if (defined($title) && $title=~/\A"/) { | ||
| 693 | my $decoded=eval {decode_json($title)}; | ||
| 694 | $title=$decoded if defined($decoded) && !ref($decoded); | ||
| 695 | } | ||
| 696 | elsif (defined($title) && $title=~/\A'(.*)'\z/s) { | ||
| 697 | $title=$1; | ||
| 698 | $title=~s/''/'/g; | ||
| 699 | } | ||
| 700 | } | ||
| 701 | |||
| 702 | # Otherwise use the first level-one heading outside a fenced example. | ||
| 703 | # | ||
| 704 | unless (defined($title) && length($title)) { | ||
| 705 | my ($fence, $length)=('', 0); | ||
| 706 | foreach my $line (split(/(?<=\n)/, $markdown)) { | ||
| 707 | if (!$fence && $line=~/^ {0,3}(`{3,}|~{3,})/) { | ||
| 708 | $fence=substr($1, 0, 1); | ||
| 709 | $length=length($1); | ||
| 710 | } | ||
| 711 | elsif ($fence && $line=~/^ {0,3}\Q$fence\E{$length,}\s*$/) { | ||
| 712 | $fence=''; | ||
| 713 | } | ||
| 714 | elsif (!$fence && $line=~/^#\s+(.+?)\s*$/) { | ||
| 715 | $title=$1; | ||
| 716 | $title=~s/\s+\{#[^}]+\}\s*$//; | ||
| 717 | $title=~s/[ \t]+#+[ \t]*$//; | ||
| 718 | last; | ||
| 719 | } | ||
| 720 | } | ||
| 721 | } | ||
| 722 | |||
| 723 | # A filename-derived label is a last resort for headingless documents. | ||
| 724 | # | ||
| 725 | unless (defined($title) && length($title)) { | ||
| 726 | (undef, undef, $title)=File::Spec->splitpath($fn); | ||
| 727 | $title=~s/\.md$//; | ||
| 728 | $title=~s/[-_]+/ /g; | ||
| 729 | $title=join(' ', map {ucfirst($_)} split(/\s+/, $title)); | ||
| 730 | } | ||
| 731 | $title=~s/\s+\{#[^}]+\}\s*$//; | ||
| 732 | return $title; | ||
| 733 | |||
| 734 | } | ||
| 735 | |||
| 736 | |||
| 737 | sub title_frontmatter { | ||
| 738 | |||
| 739 | my ($self, $fn, $markdown)=@_; | ||
| 740 | my $title=$self->markdown_title($fn, $markdown); | ||
| 741 | if ($markdown=~/\A---[ \t]*\r?\n(.*?)^---[ \t]*\r?\n/ms) { | ||
| 742 | my $frontmatter=$1; | ||
| 743 | return $markdown if $frontmatter=~/^title:[ \t]*/m; | ||
| 744 | my $title_line='title: '.encode_json($title)."\n"; | ||
| 745 | $markdown=~s/\A(---[ \t]*\r?\n)/$1$title_line/; | ||
| 746 | return $markdown; | ||
| 747 | } | ||
| 748 | return "---\ntitle: ".encode_json($title)."\n---\n\n$markdown"; | ||
| 749 | |||
| 750 | } | ||
| 751 | |||
| 752 | |||
| 753 | sub navigation { | ||
| 754 | |||
| 755 | my ($self, $docs_dn, $pages_ar)=@_; | ||
| 756 | my @navigation; | ||
| 757 | foreach my $page (@{$pages_ar}) { | ||
| 758 | my $fn=File::Spec->catfile($docs_dn, $page); | ||
| 759 | open(my $input_fh, '<', $fn) || die "unable to read $fn: $!\n"; | ||
| 760 | local $/=undef; | ||
| 761 | my $markdown=<$input_fh>; | ||
| 762 | close($input_fh) || die "unable to close $fn: $!\n"; | ||
| 763 | (my $id=$page)=~s/\.md$//; | ||
| 764 | $id=~s{\\}{/}g; | ||
| 765 | push(@navigation, { | ||
| 766 | file => $page, | ||
| 767 | id => $id, | ||
| 768 | title => $self->markdown_title($page, $markdown) | ||
| 769 | }); | ||
| 770 | } | ||
| 771 | return \@navigation; | ||
| 772 | |||
| 773 | } | ||
| 774 | |||
| 775 | |||
| 776 | sub normalize_node_markdown { | ||
| 777 | |||
| 778 | my ($self, $source_dn)=@_; | ||
| 779 | File::Find::find({ | ||
| 780 | no_chdir => 1, | ||
| 781 | wanted => sub { | ||
| 782 | my $fn=$File::Find::name; | ||
| 783 | return unless -f $fn && $fn=~/\.md$/; | ||
| 784 | open(my $input_fh, '<', $fn) || die "unable to read $fn: $!\n"; | ||
| 785 | local $/=undef; | ||
| 786 | my $markdown=<$input_fh>; | ||
| 787 | close($input_fh) || die "unable to close $fn: $!\n"; | ||
| 788 | $markdown=$self->normalize_node_definition_lists($markdown); | ||
| 789 | $markdown=$self->normalize_backend_markdown($fn, $markdown); | ||
| 790 | $markdown=$self->normalize_node_attributes($markdown); | ||
| 791 | $markdown=$self->normalize_node_admonitions($markdown); | ||
| 792 | $self->write_file($fn, $markdown); | ||
| 793 | } | ||
| 794 | }, $source_dn); | ||
| 795 | return 1; | ||
| 796 | |||
| 797 | } | ||
| 798 | |||
| 799 | |||
| 800 | sub normalize_backend_markdown { | ||
| 801 | |||
| 802 | my ($self, $fn, $markdown)=@_; | ||
| 803 | return $markdown; | ||
| 804 | |||
| 805 | } | ||
| 806 | |||
| 807 | |||
| 808 | sub npm_install { | ||
| 809 | |||
| 810 | my ($self, $site_dn)=@_; | ||
| 811 | my $npm=$self->option('npm', 'npm'); | ||
| 812 | my $engine=ref($self); | ||
| 813 | $engine=~s/^.*:://; | ||
| 814 | print STDERR "Installing $engine npm dependencies...\n"; | ||
| 815 | my @quiet=defined($MARKDOWN_PUBLISH_NPM_VERBOSE) && | ||
| 816 | $MARKDOWN_PUBLISH_NPM_VERBOSE eq '1' ? () : ('--silent'); | ||
| 817 | $self->system_in_dir($site_dn, $npm, 'install', @quiet); | ||
| 818 | print STDERR "$engine npm dependencies installed.\n"; | ||
| 819 | return 1; | ||
| 820 | |||
| 821 | } | ||
| 822 | |||
| 823 | |||
| 824 | sub build { | ||
| 825 | |||
| 826 | die "publication backend must implement build()\n"; | ||
| 827 | |||
| 828 | } | ||
| 829 | |||
| 830 | |||
| 831 | sub serve { | ||
| 832 | |||
| 833 | die "publication backend must implement serve()\n"; | ||
| 834 | |||
| 835 | } | ||
| 836 | |||
| 837 | |||
| 838 | sub publish_gh { | ||
| 839 | |||
| 840 | |||
| 841 | # Build first, then replace only the disposable publication worktree. | ||
| 842 | # Leave the resulting branch local so pushing remains an explicit Git | ||
| 843 | # operation controlled by the repository's configured upstream. | ||
| 844 | # | ||
| 845 | my ($self)=@_; | ||
| 846 | my $branch=$self->option('branch', $MARKDOWN_PUBLISH_BRANCH); | ||
| 847 | my $base=$self->github_site_base(); | ||
| 848 | local $self->{'base'}=$base; | ||
| 849 | my $site_dn=$self->build(); | ||
| 850 | $self->command('git', 'check-ref-format', '--branch', $branch); | ||
| 851 | my $temporary_dn=abs_path(tempdir(CLEANUP => 1)); | ||
| 852 | my $work_dn=File::Spec->catdir($temporary_dn, 'pages'); | ||
| 853 | my $exists=eval { | ||
| 854 | $self->command('git', 'rev-parse', '--verify', "refs/heads/$branch"); | ||
| 855 | 1; | ||
| 856 | }; | ||
| 857 | $self->command('git', 'worktree', 'add', ($exists ? () : '--detach'), $work_dn, | ||
| 858 | $exists ? $branch : 'HEAD'); | ||
| 859 | my $ok=eval { | ||
| 860 | $self->command('git', '-C', $work_dn, 'checkout', '--orphan', $branch) | ||
| 861 | unless $exists; | ||
| 862 | $self->command('git', '-C', $work_dn, 'rm', '-r', '-f', '--ignore-unmatch', '.'); | ||
| 863 | $self->copy_tree($site_dn, $work_dn); | ||
| 864 | $self->write_file(File::Spec->catfile($work_dn, '.nojekyll'), ''); | ||
| 865 | $self->command('git', '-C', $work_dn, 'add', '.'); | ||
| 866 | my $changed=$exists ? | ||
| 867 | length($self->command('git', '-C', $work_dn, 'diff', '--cached', '--name-only')) : 1; | ||
| 868 | $self->command('git', '-C', $work_dn, 'commit', '-m', 'Update documentation') | ||
| 869 | if $changed; | ||
| 870 | 1; | ||
| 871 | }; | ||
| 872 | my $error=$@; | ||
| 873 | $self->command('git', 'worktree', 'remove', '--force', $work_dn); | ||
| 874 | die $error unless $ok; | ||
| 875 | return $branch; | ||
| 876 | |||
| 877 | } | ||
| 878 | |||
| 879 | |||
| 880 | sub publish_gh_push { | ||
| 881 | |||
| 882 | |||
| 883 | # Keep local branch creation in publish_gh(), then make the remote side | ||
| 884 | # effect explicit by pushing only the resulting branch to origin. | ||
| 885 | # | ||
| 886 | my ($self)=@_; | ||
| 887 | my $branch=$self->publish_gh(); | ||
| 888 | $self->command('git', 'push', 'origin', $branch); | ||
| 889 | return $branch; | ||
| 890 | |||
| 891 | } | ||
| 892 | |||
| 893 | |||
| 894 | sub publish_cloudflare { | ||
| 895 | |||
| 896 | |||
| 897 | # The site generator owns the build; Wrangler deploys only the resulting | ||
| 898 | # static assets using an explicit or conventional root configuration. | ||
| 899 | # | ||
| 900 | my ($self)=@_; | ||
| 901 | my $cloudflare_hr=exists($self->{'cloudflare'}) ? $self->{'cloudflare'} : {}; | ||
| 902 | die "cloudflare publication configuration must be a hash reference\n" | ||
| 903 | unless ref($cloudflare_hr) eq 'HASH'; | ||
| 904 | my $config_fn; | ||
| 905 | if (exists($cloudflare_hr->{'config'})) { | ||
| 906 | $config_fn=$cloudflare_hr->{'config'}; | ||
| 907 | die "cloudflare publication requires a Wrangler configuration file\n" | ||
| 908 | unless defined($config_fn) && length($config_fn); | ||
| 909 | } | ||
| 910 | else { | ||
| 911 | foreach my $candidate_fn (qw(wrangler.jsonc wrangler.json)) { | ||
| 912 | if (-f $candidate_fn) { | ||
| 913 | $config_fn=$candidate_fn; | ||
| 914 | last; | ||
| 915 | } | ||
| 916 | } | ||
| 917 | die "cloudflare publication requires cloudflare.config or wrangler.jsonc/wrangler.json in the project root\n" | ||
| 918 | unless defined($config_fn); | ||
| 919 | } | ||
| 920 | die "Wrangler configuration not found: $config_fn\n" unless -f $config_fn; | ||
| 921 | $config_fn=abs_path($config_fn); | ||
| 922 | my $wrangler=$cloudflare_hr->{'wrangler'} || 'wrangler'; | ||
| 923 | |||
| 924 | my $site_dn=$self->build(); | ||
| 925 | die "built site directory not found: $site_dn\n" unless -d $site_dn; | ||
| 926 | $site_dn=abs_path($site_dn); | ||
| 927 | my @command=($wrangler, 'deploy', '--config', $config_fn, | ||
| 928 | '--assets', $site_dn); | ||
| 929 | push(@command, '--env', $cloudflare_hr->{'environment'}) | ||
| 930 | if defined($cloudflare_hr->{'environment'}) && length($cloudflare_hr->{'environment'}); | ||
| 931 | $self->system_command(@command); | ||
| 932 | return $site_dn; | ||
| 933 | |||
| 934 | } | ||
| 935 | |||
| 936 | |||
| 937 | __END__ |