| 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::MkDocs; | ||
| 14 | |||
| 15 | |||
| 16 | # Compiler pragma and package variables | ||
| 17 | # | ||
| 18 | use strict qw(vars); | ||
| 19 | use vars qw($VERSION @ISA); | ||
| 20 | use warnings; | ||
| 21 | |||
| 22 | |||
| 23 | # Parent and supporting packages | ||
| 24 | # | ||
| 25 | use Markdown::Publish (); | ||
| 26 | use Markdown::Publish::Constant; | ||
| 27 | use Cwd qw(abs_path); | ||
| 28 | use File::Spec; | ||
| 29 | use JSON::PP qw(encode_json); | ||
| 30 | |||
| 31 | |||
| 32 | # Inheritance and version information | ||
| 33 | # | ||
| 34 | @ISA=qw(Markdown::Publish); | ||
| 35 | $VERSION='1.004'; | ||
| 36 | |||
| 37 | |||
| 38 | # Done | ||
| 39 | # | ||
| 40 | 1; | ||
| 41 | |||
| 42 | |||
| 43 | #====================================================================================================================== | ||
| 44 | |||
| 45 | |||
| 46 | sub prepare { | ||
| 47 | |||
| 48 | my ($self, $preview)=@_; | ||
| 49 | my ($config_fn, $extend_fn)=$self->configuration_files(); | ||
| 50 | die "config_extend cannot be used with direct MkDocs configuration\n" | ||
| 51 | if defined($extend_fn) && $self->option('config_mode', '') eq 'direct'; | ||
| 52 | $config_fn='mkdocs.yml' | ||
| 53 | if !defined($config_fn) && !defined($extend_fn) && -f 'mkdocs.yml'; | ||
| 54 | if (defined($config_fn) && length($config_fn) && | ||
| 55 | ($self->option('config_mode', '') eq 'direct' || $config_fn eq 'mkdocs.yml')) { | ||
| 56 | die "MkDocs configuration not found: $config_fn\n" unless -f $config_fn; | ||
| 57 | return abs_path($config_fn); | ||
| 58 | } | ||
| 59 | $config_fn=$extend_fn if defined($extend_fn); | ||
| 60 | $config_fn='doc/mkdocs/mkdocs.yml' | ||
| 61 | if !defined($config_fn) && -f 'doc/mkdocs/mkdocs.yml'; | ||
| 62 | |||
| 63 | my ($temporary_dn, $docs_dn, $pages_ar)=$self->prepare_docs(); | ||
| 64 | $self->promote_home($docs_dn, $pages_ar); | ||
| 65 | my $generated_fn=File::Spec->catfile($temporary_dn, 'mkdocs.yml'); | ||
| 66 | my $output_dn=File::Spec->rel2abs($self->option('output', $MARKDOWN_PUBLISH_OUTPUT_DN)); | ||
| 67 | my $config=''; | ||
| 68 | if (defined($config_fn) && length($config_fn)) { | ||
| 69 | die "MkDocs configuration not found: $config_fn\n" unless -f $config_fn; | ||
| 70 | $config.='INHERIT: '.encode_json(abs_path($config_fn))."\n"; | ||
| 71 | } | ||
| 72 | else { | ||
| 73 | $config.='site_name: '.encode_json($self->option('name', 'Documentation'))."\n"; | ||
| 74 | $config.="theme:\n name: material\n"; | ||
| 75 | $config.="markdown_extensions:\n - admonition\n - attr_list\n - def_list\n - footnotes\n - tables\n - pymdownx.superfences\n"; | ||
| 76 | } | ||
| 77 | $config.='docs_dir: '.encode_json(abs_path($docs_dn))."\n"; | ||
| 78 | $config.='site_dir: '.encode_json($output_dn)."\n"; | ||
| 79 | $config.="plugins:\n - search\n" if $preview; | ||
| 80 | $config.="nav:\n"; | ||
| 81 | $config.=' - '.encode_json($_)."\n" foreach @{$pages_ar}; | ||
| 82 | $self->write_file($generated_fn, $config); | ||
| 83 | return $generated_fn; | ||
| 84 | |||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | sub build { | ||
| 89 | |||
| 90 | my ($self)=@_; | ||
| 91 | my $output_dn=File::Spec->rel2abs($self->option('output', $MARKDOWN_PUBLISH_OUTPUT_DN)); | ||
| 92 | my $config_fn=$self->prepare(0); | ||
| 93 | my @command=($self->option('command', 'mkdocs'), 'build'); | ||
| 94 | push(@command, '--strict') if $self->option('strict', 1); | ||
| 95 | push(@command, '-f', $config_fn, '--site-dir', $output_dn); | ||
| 96 | $self->command(@command); | ||
| 97 | return $output_dn; | ||
| 98 | |||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | sub serve { | ||
| 103 | |||
| 104 | my ($self)=@_; | ||
| 105 | my $config_fn=$self->prepare(1); | ||
| 106 | my @command=($self->option('command', 'mkdocs'), 'serve', '-f', $config_fn); | ||
| 107 | my $address=$self->option('address', undef); | ||
| 108 | unless (exists($self->{'address'})) { | ||
| 109 | my $host=$self->option('host', $MARKDOWN_PUBLISH_HOST); | ||
| 110 | my $port=$self->option('port', $MARKDOWN_PUBLISH_PORT); | ||
| 111 | if (defined($host) || defined($port)) { | ||
| 112 | $host='127.0.0.1' unless defined($host); | ||
| 113 | $port=8000 unless defined($port); | ||
| 114 | $address="$host:$port"; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | push(@command, '-a', $address) if defined($address) && length($address); | ||
| 118 | return $self->system_command(@command); | ||
| 119 | |||
| 120 | } | ||
| 121 | __END__ |