| 1 | # | ||
| 2 | # This file is part of Docbook::Convert. | ||
| 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 | |||
| 14 | |||
| 15 | # | ||
| 16 | # | ||
| 17 | package Docbook::Convert::Markdown::Util; | ||
| 18 | |||
| 19 | |||
| 20 | # Pragma | ||
| 21 | # | ||
| 22 | use strict qw(vars); | ||
| 23 | use vars qw($VERSION $AUTOLOAD); | ||
| 24 | use warnings; | ||
| 25 | no warnings qw(uninitialized); | ||
| 26 | |||
| 27 | |||
| 28 | # External modules | ||
| 29 | # | ||
| 30 | use Docbook::Convert::Constant; | ||
| 31 | use Data::Dumper; | ||
| 32 | use HTML::Entities qw(decode_entities); | ||
| 33 | |||
| 34 | |||
| 35 | # Version information in a format suitable for CPAN etc. Must be | ||
| 36 | # all on one line | ||
| 37 | # | ||
| 38 | $VERSION='1.012'; | ||
| 39 | |||
| 40 | |||
| 41 | #=================================================================================================== | ||
| 42 | |||
| 43 | |||
| 44 | sub _anchor { | ||
| 45 | |||
| 46 | my ($self, $id)=@_; | ||
| 47 | my $anchor=qq(<a name="$id"></a>); | ||
| 48 | return $anchor; | ||
| 49 | |||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub _anchor_fix { | ||
| 54 | |||
| 55 | # Nothing to fix in markdown | ||
| 56 | # | ||
| 57 | my ($self, $output)=@_; | ||
| 58 | return $output; | ||
| 59 | |||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | sub _bold { | ||
| 64 | my ($self, $text)=@_; | ||
| 65 | return $self->{'_plaintext'} ? $text : "**$text**"; | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | sub _code { | ||
| 70 | my ($self, $text)=@_; | ||
| 71 | $text=decode_entities($text); | ||
| 72 | $text=~s/\`//g; | ||
| 73 | my $fence='`'; | ||
| 74 | $text=~s/\s+\Q<**SBR**>\E\s+/` $CR`/g; | ||
| 75 | return $self->{'_plaintext'} ? $text : "${fence}${text}${fence}"; | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | sub _email { | ||
| 80 | my ($self, $email)=@_; | ||
| 81 | return $self->{'_plaintext'} ? $email : "<$email>"; | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | sub _h1 { | ||
| 86 | my ($self, $text)=@_; | ||
| 87 | return $self->{'_plaintext'} ? $text : "# $text #"; | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | sub _h2 { | ||
| 92 | my ($self, $text)=@_; | ||
| 93 | return $self->{'_plaintext'} ? $text : "## $text ##"; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub _h3 { | ||
| 98 | my ($self, $text)=@_; | ||
| 99 | return $self->{'_plaintext'} ? $text : "### $text ###"; | ||
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | sub _h4 { | ||
| 104 | my ($self, $text)=@_; | ||
| 105 | return $self->{'_plaintext'} ? $text : "#### $text ####"; | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | sub _quote { | ||
| 110 | my ($self, $text)=@_; | ||
| 111 | my $quote; | ||
| 112 | my @line=($text=~/^(.*)$/gm); | ||
| 113 | foreach my $line (@line) { | ||
| 114 | chomp($line); | ||
| 115 | $quote.="> ${line}${CR}"; | ||
| 116 | } | ||
| 117 | $quote.=">${CR}"; | ||
| 118 | return $quote; | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | sub _image { | ||
| 123 | my ($self, $url, $alt_text, $title, $attr_hr)=@_; | ||
| 124 | if ((my $width=$attr_hr->{'width'}) && !$NO_HTML) { | ||
| 125 | |||
| 126 | # MD generally can't do width - convert to HTML link | ||
| 127 | return $self->_image_html($url, $alt_text, $title, $attr_hr); | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | my $md_link=$self->_link($url, $alt_text, $title, $attr_hr); | ||
| 131 | return "!${md_link}"; | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | sub _image_html { | ||
| 137 | my ($self, $url, $alt_text, $title, $attr_hr)=@_; | ||
| 138 | my $width=$attr_hr->{'width'}; | ||
| 139 | $width && ($width=qq(width="$width")); | ||
| 140 | my $html=<<HERE; | ||
| 141 | <p><img src="$url" alt="$alt_text" $width /></p> | ||
| 142 | HERE | ||
| 143 | return $html | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | sub _italic { | ||
| 148 | my ($self, $text)=@_; | ||
| 149 | return $self->{'_plaintext'} ? $text : "*$text*"; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | sub _link { | ||
| 154 | my ($self, $url, $text, $title)=@_; | ||
| 155 | |||
| 156 | #print "url $url\n"; | ||
| 157 | if ($self->{'_plaintext'}) { | ||
| 158 | return $url | ||
| 159 | } | ||
| 160 | elsif ($title) { | ||
| 161 | return "[$text]($url \"$title\")"; | ||
| 162 | } | ||
| 163 | else { | ||
| 164 | return "[$text]($url)"; | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | |||
| 169 | sub _list { | ||
| 170 | my $self=shift; | ||
| 171 | my $text=shift; | ||
| 172 | return "+ $text"; | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | sub _list_begin { | ||
| 177 | return undef; | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | sub _list_end { | ||
| 182 | return undef; | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | sub _list_item { | ||
| 187 | my ($self, $text)=@_; | ||
| 188 | return $text; | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | sub _listitem_join { | ||
| 193 | &_variablelist_join(@_); | ||
| 194 | } | ||
| 195 | |||
| 196 | |||
| 197 | sub _prefix { | ||
| 198 | return undef; | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | sub _strikethrough { | ||
| 203 | my ($self, $text)=@_; | ||
| 204 | return $self->{'_plaintext'} ? $text : "~~$text~~"; | ||
| 205 | } | ||
| 206 | |||
| 207 | |||
| 208 | sub _suffix { | ||
| 209 | return undef; | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | sub _variablelist_join { | ||
| 214 | return "${CR2}${SP4}"; | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | # Experimental | ||
| 219 | sub sluggify { | ||
| 220 | my ($self, $title)=@_; | ||
| 221 | my $ix=(($self->{'_split_ix'}||=0)++); | ||
| 222 | chomp $title; | ||
| 223 | die if $title=~/^#\s*$/; | ||
| 224 | $title =~ s/^#+\s*//; # Remove leading '#' and whitespace | ||
| 225 | $title =~ s/[^A-Za-z0-9]+/_/g; # Replace non-alphanumeric chars with underscores | ||
| 226 | $title =~ s/^_+|_+$//g; # Trim leading/trailing underscores | ||
| 227 | $title = lc($title); | ||
| 228 | $title = sprintf('%0.2d_%s.md', $ix, $title); | ||
| 229 | return $title; | ||
| 230 | } | ||
| 231 | |||
| 232 | 1; | ||
| 233 | __END__ |