| 1 | #!/usr/local/bin/perl | ||
| 2 | #!perl | ||
| 3 | |||
| 4 | # Converts Perl to Code-folded HTML using PPI::HTML::CodeFolder | ||
| 5 | |||
| 6 | use File::Slurp (); | ||
| 7 | use PPI; | ||
| 8 | use PPI::HTML::CodeFolder; | ||
| 9 | use CSS::Tiny; | ||
| 10 | use Getopt::Long qw(:config no_ignore_case permute); | ||
| 11 | use File::Path; | ||
| 12 | use File::Spec::Functions qw(splitpath); | ||
| 13 | use Module::Mapper; | ||
| 14 | use Pod::Usage; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | |||
| 19 | my $all = 0; | ||
| 20 | my $bare = 0; | ||
| 21 | my $closeimg = 'closedbook.gif'; | ||
| 22 | my $nocomments = 0; | ||
| 23 | my $help = 0; | ||
| 24 | my $csspath = undef; | ||
| 25 | my $noheredocs = 0; | ||
| 26 | my $noimports = 0; | ||
| 27 | my $jspath = undef; | ||
| 28 | my $jstoc = 0; | ||
| 29 | my $minlines = 4; | ||
| 30 | my $noexpand = 0; | ||
| 31 | my $output = ''; | ||
| 32 | my $openimg = 'openbook.gif'; | ||
| 33 | my $project = 0; | ||
| 34 | my $nopod = 0; | ||
| 35 | my $rootimg = 'globe.gif'; | ||
| 36 | my $root = ''; | ||
| 37 | my $title = 'My Project'; | ||
| 38 | my $noicons = 0; | ||
| 39 | my $verbose; | ||
| 40 | my @srclibs = (); | ||
| 41 | my @exes = (); | ||
| 42 | my @modules = (); | ||
| 43 | my $imgpath = undef; | ||
| 44 | my $useinc = undef; | ||
| 45 | |||
| 46 | GetOptions( | ||
| 47 | 'a|all' => \$all, | ||
| 48 | 'b|bare' => \$bare, | ||
| 49 | 'c|closeimg=s' => \$closeimg, | ||
| 50 | 'C|nocomments' => \$nocomments, | ||
| 51 | 'e|exes=s' => sub { push @exes, $_[1]; }, | ||
| 52 | 'h|help' => \$help, | ||
| 53 | 'H|noheredocs' => \$noheredocs, | ||
| 54 | 'i|imgpath=s' => \$imgpath, | ||
| 55 | 'I|noimports' => \$noimports, | ||
| 56 | 'j|jspath=s' => \$jspath, | ||
| 57 | 'J|jstoc' => \$jstoc, | ||
| 58 | 'm|minlines=s' => \$minlines, | ||
| 59 | 'M|module=s' => sub { push @modules, $_[1]; }, | ||
| 60 | 'O|openimg=s' => \$openimg, | ||
| 61 | 'o|out|output=s' => \$output, | ||
| 62 | 'p|project' => \$project, | ||
| 63 | 'P|nopod' => \$nopod, | ||
| 64 | 'r|rootimg=s' => \$rootimg, | ||
| 65 | 's|csspath=s' => \$csspath, | ||
| 66 | 't|title=s' => \$title, | ||
| 67 | 'u|useinc' => \$useinc, | ||
| 68 | 'v|verbose' => \$verbose, | ||
| 69 | 'x|noexpand' => \$noexpand, | ||
| 70 | 'z|noicons' => \$noicons, | ||
| 71 | '<>' => sub { push @srclibs, @_; } | ||
| 72 | ); | ||
| 73 | |||
| 74 | pod2usage(1) if $help; | ||
| 75 | |||
| 76 | if ($jstoc) { | ||
| 77 | eval "require HTML::ListToTree;"; | ||
| 78 | die "Can't generate Javascript TOC: $@" if $@; | ||
| 79 | } | ||
| 80 | |||
| 81 | $jspath ||= "$output/js"; | ||
| 82 | $csspath ||= "$output/css"; | ||
| 83 | $imgpath ||= "$output/img"; | ||
| 84 | $output ||= './ppicf'; | ||
| 85 | |||
| 86 | my %srcs = (); | ||
| 87 | my %subdirs = (); | ||
| 88 | |||
| 89 | $project = shift @srclibs || '.' | ||
| 90 | if $project; | ||
| 91 | |||
| 92 | my $modules = find_sources( | ||
| 93 | All => $all, | ||
| 94 | Scripts => \@exes, | ||
| 95 | Modules => \@modules, | ||
| 96 | Libs => \@srclibs, | ||
| 97 | UseINC => $useinc, | ||
| 98 | Output => $output, | ||
| 99 | Project => $project, | ||
| 100 | Verbose => ($verbose ? sub { print $_[0]; } : undef), | ||
| 101 | ); | ||
| 102 | |||
| 103 | die $@ unless $modules; | ||
| 104 | |||
| 105 | # | ||
| 106 | # create full directory trees for output; we trim any | ||
| 107 | # std. prefix directories from Config,as well as any volume name | ||
| 108 | # and then map the source file to its path-qualified target output | ||
| 109 | # | ||
| 110 | my %outdirs = (); | ||
| 111 | foreach my $dir (values %$modules) { | ||
| 112 | my ($volume, $subdir, $file) = splitpath( $dir->[1] ); | ||
| 113 | $outdirs{"$volume$subdir"} = 1 if $subdir; | ||
| 114 | } | ||
| 115 | # | ||
| 116 | # save scripts | ||
| 117 | # | ||
| 118 | my $scripts = listScripts($modules); | ||
| 119 | |||
| 120 | print "Creating directory $_...\n" and | ||
| 121 | mkpath $_ | ||
| 122 | foreach (keys %outdirs); | ||
| 123 | |||
| 124 | my %tagcolors = ( | ||
| 125 | cast => '#339999', | ||
| 126 | comment => '#008080', | ||
| 127 | core => '#FF0000', | ||
| 128 | double => '#999999', | ||
| 129 | heredoc => '#FF0000', | ||
| 130 | heredoc_content => '#FF0000', | ||
| 131 | heredoc_terminator => '#FF0000', | ||
| 132 | interpolate => '#999999', | ||
| 133 | keyword => '#0000FF', | ||
| 134 | line_number => '#666666', | ||
| 135 | literal => '#999999', | ||
| 136 | magic => '#0099FF', | ||
| 137 | match => '#9900FF', | ||
| 138 | number => '#990000', | ||
| 139 | operator => '#DD7700', | ||
| 140 | pod => '#008080', | ||
| 141 | pragma => '#990000', | ||
| 142 | regex => '#9900FF', | ||
| 143 | single => '#999999', | ||
| 144 | substitute => '#9900FF', | ||
| 145 | transliterate => '#9900FF', | ||
| 146 | word => '#999999', | ||
| 147 | ); | ||
| 148 | |||
| 149 | $noexpand = 1 if $bare; | ||
| 150 | my $line_nums = (!$bare); | ||
| 151 | |||
| 152 | # Create the PPI::HTML object | ||
| 153 | my $HTML = PPI::HTML::CodeFolder->new( | ||
| 154 | line_numbers => $line_nums, | ||
| 155 | page => 1, | ||
| 156 | colors => \%tagcolors, | ||
| 157 | verbose => $verbose, | ||
| 158 | fold => { | ||
| 159 | Abbreviate => 1, | ||
| 160 | POD => (!$nopod), | ||
| 161 | Comments => (!$nocomments), | ||
| 162 | Expandable => (!$noexpand), | ||
| 163 | Heredocs => (!$noheredocs), | ||
| 164 | Imports => (!$noimports), | ||
| 165 | MinFoldLines => $minlines, | ||
| 166 | Javascript => "$jspath/ppicf.js", | ||
| 167 | Stylesheet => "$csspath/ppicf.css", | ||
| 168 | }, | ||
| 169 | ) | ||
| 170 | or die "Failed to create HTML syntax highlighter"; | ||
| 171 | |||
| 172 | foreach (values %$modules) { | ||
| 173 | print "Scanning $_->[0]...\n" if $verbose; | ||
| 174 | my $Document = PPI::Document->new( $_->[0] ) | ||
| 175 | or die "File '$_->[0]' could not be loaded as a document"; | ||
| 176 | |||
| 177 | # Process the file | ||
| 178 | print "Code folding $_->[0]...\n" if $verbose; | ||
| 179 | $_->[1] .= '.html'; | ||
| 180 | my $content = $HTML->html( $Document, $_->[1], $scripts->{$_->[0]} ) | ||
| 181 | or die "Failed to generate HTML for $_->[0]"; | ||
| 182 | |||
| 183 | print "Writing $_->[1]...\n" if $verbose; | ||
| 184 | File::Slurp::write_file( $_->[1], $content ); | ||
| 185 | print "$_->[0] done\n" if $verbose; | ||
| 186 | } | ||
| 187 | # | ||
| 188 | # save JS and CSS | ||
| 189 | # | ||
| 190 | mkpath $jspath; | ||
| 191 | File::Slurp::write_file( "$jspath/ppicf.js", $HTML->foldJavascript()); | ||
| 192 | |||
| 193 | mkpath $csspath; | ||
| 194 | File::Slurp::write_file( "$csspath/ppicf.css", $HTML->foldCSS()); | ||
| 195 | # | ||
| 196 | # generate TOC | ||
| 197 | # | ||
| 198 | unless ($bare) { | ||
| 199 | if ($jstoc) { | ||
| 200 | my $toc = $HTML->getTOC("$output/toc.html", Order => [ sort values %$scripts ] ); | ||
| 201 | ($toc) = ($toc=~/^.*<\!-- INDEX BEGIN -->(.+)<\!-- INDEX END/s); | ||
| 202 | my $tree = HTML::ListToTree->new(Text => $title, Link => '', Source => $toc) | ||
| 203 | or die $@; | ||
| 204 | |||
| 205 | mkpath $imgpath; | ||
| 206 | |||
| 207 | $tree->writeJavascript("$jspath/tree.js"); | ||
| 208 | $tree->writeCSS("$csspath/tree.css"); | ||
| 209 | $tree->writeIcons($imgpath); | ||
| 210 | |||
| 211 | $csspath = pathAdjust($output, $csspath); | ||
| 212 | $jspath = pathAdjust($output, $jspath); | ||
| 213 | my $widget = $tree->render( | ||
| 214 | CloseIcon => $closeimg, | ||
| 215 | CSSPath => "$csspath/tree.css", | ||
| 216 | IconPath => pathAdjust($output, $imgpath), | ||
| 217 | JSPath => "$jspath/tree.js", | ||
| 218 | UseIcons => (!$noicons), | ||
| 219 | OpenIcon => $openimg, | ||
| 220 | RootIcon => $rootimg, | ||
| 221 | ) or die $@; | ||
| 222 | |||
| 223 | die "Can't open $output/toc.html: $!" | ||
| 224 | unless open(OUTF, ">$output/toc.html"); | ||
| 225 | print OUTF $widget; | ||
| 226 | close OUTF; | ||
| 227 | |||
| 228 | } | ||
| 229 | else { | ||
| 230 | $HTML->writeTOC($output, Order => [ sort values %$scripts ]); | ||
| 231 | } | ||
| 232 | $HTML->writeFrameContainer($output, $title, $root) or die $@; | ||
| 233 | } | ||
| 234 | print "Saved HTML to $output\n" if $verbose; | ||
| 235 | |||
| 236 | sub listScripts { | ||
| 237 | my $modules = shift; | ||
| 238 | my %scripts = (); | ||
| 239 | |||
| 240 | foreach (values %$modules) { | ||
| 241 | next if (substr($_->[0], -3) eq '.pm'); | ||
| 242 | my @parts = split /[\/\\]/, $_->[0]; | ||
| 243 | $scripts{$_->[0]} = $parts[-1]; | ||
| 244 | } | ||
| 245 | return \%scripts; | ||
| 246 | } | ||
| 247 | |||
| 248 | sub pathAdjust { | ||
| 249 | my ($path, $jspath) = @_; | ||
| 250 | return $jspath | ||
| 251 | unless (substr($jspath, 0, 2) eq './') && (substr($path, 0, 2) eq './'); | ||
| 252 | # | ||
| 253 | # relative path, adjust as needed from current base | ||
| 254 | # | ||
| 255 | my @parts = split /\//, $path; | ||
| 256 | my @jsparts = split /\//, $jspath; | ||
| 257 | shift @parts; | ||
| 258 | shift @jsparts; # remove the relative head | ||
| 259 | my $prefix = ''; | ||
| 260 | shift @parts, | ||
| 261 | shift @jsparts | ||
| 262 | while @parts && @jsparts && ($parts[0] eq $jsparts[0]); | ||
| 263 | return ('../' x scalar @parts) . join('/', @jsparts) | ||
| 264 | } | ||
| 265 | |||
| 266 | =pod | ||
| 267 | |||
| 268 | =head1 NAME | ||
| 269 | |||
| 270 | ppihtmlcf | ||
| 271 | |||
| 272 | =head1 SYNOPSIS | ||
| 273 | |||
| 274 | ppihtmlcf [options] <source-path> [ <source-path> ... ] | ||
| 275 | |||
| 276 | Options: | ||
| 277 | -a|-all process all files in subdirectories | ||
| 278 | -b|-bare no TOC, no frame container, no fold expansion, | ||
| 279 | no line numbers | ||
| 280 | -c|-closeimg <filename> image file used for HTML::ListToTree closed | ||
| 281 | nodes; default 'closedbook.gif' | ||
| 282 | -C|-nocomments don't fold comments | ||
| 283 | -e|-exe <scriptpath> process a script; may be used multiple times. | ||
| 284 | <scriptpath> is a fully qualified pathname | ||
| 285 | -h|-help display this help and exit | ||
| 286 | -H|-noheredocs don't fold heredocs | ||
| 287 | -i|-imgpath <path> path to image directory for ProjectDocs and | ||
| 288 | HTML::ListToTree; default <output root>/img | ||
| 289 | -I|-noimports don't fold imports | ||
| 290 | -j|-jspath <path> path to Javascript files; defaults to | ||
| 291 | <output root>/js | ||
| 292 | -J|-jstoc use Javascript tree for table of contents (via | ||
| 293 | HTML::ListToTree); default is HTML list | ||
| 294 | -m|-minlines <lines> minimum consecutive line for a fold | ||
| 295 | -M|-module <pkgname> process a module/namespace; may be specified | ||
| 296 | multiple times. Names are specified in Perl | ||
| 297 | package format, and may be top level | ||
| 298 | namespaces (for -a processing) | ||
| 299 | -O|-openimg <filename> image file used for open tree nodes; | ||
| 300 | default 'openbook.gif' | ||
| 301 | -o|-out|-output <path> target root directory path for generated | ||
| 302 | documents; default './ppicf' | ||
| 303 | -p|-project project mode: scan ./bin and ./lib of first | ||
| 304 | <source-path> for all modules/scripts; | ||
| 305 | any add'l <source-path>s are ignored; | ||
| 306 | if no <source-path>, uses './' | ||
| 307 | -P|-nopod don't fold POD | ||
| 308 | -r|-rootimg <filename> image file used for root of HTML::ListToTree | ||
| 309 | tree; default is openimg | ||
| 310 | -s|-csspath <path> path to CSS files for PPI::CodeFolder and | ||
| 311 | HTML::ListToTree; default <output root>/css | ||
| 312 | -t|-title <title> project title | ||
| 313 | -u|-useinc include @INC when scanning for modules | ||
| 314 | -v|-verbose enable diagnostic messages | ||
| 315 | -x|-noexpand no fold expansion | ||
| 316 | -z|-noicons no icons in HTML::ListToTree tree widget; | ||
| 317 | default is icons on | ||
| 318 | |||
| 319 | E.g.: | ||
| 320 | |||
| 321 | ppihtmlcf -a -J -o ./ppicf -p -t "My Project" | ||
| 322 | |||
| 323 | processes the local ./lib (for modules) and ./bin (for scripts) as a project | ||
| 324 | with a Javascript TOC. | ||
| 325 | |||
| 326 | ppihtmlcf -a -J -o ./ppicf -u -t "My Project" -M PPI ./lib | ||
| 327 | |||
| 328 | processes all modules in the PPI namespace within either ./lib, or in @INC | ||
| 329 | with a Javascript TOC. | ||
| 330 | |||
| 331 | =head1 DESCRIPTION | ||
| 332 | |||
| 333 | Converts Perl source code to codefolded, syntax highlighted HTML via | ||
| 334 | L<PPI::HTML::CodeFolder>, with an associated framer container and either | ||
| 335 | an HTML table of contents, or a Javascripted tree TOC (via L<HTML::ListToTree>). | ||
| 336 | |||
| 337 | The following Javascript and stylesheet files are written to the specified | ||
| 338 | jspath and csspath directories: | ||
| 339 | |||
| 340 | ppicf.js: codefolder Javascript | ||
| 341 | ppicf.css: codefolder stylesheet | ||
| 342 | tree.js: HTML::ListToTree widget Javascript (if -jstoc specified) | ||
| 343 | tree.css: HTML::ListToTree widget stylesheet (if -jstoc specified) | ||
| 344 | |||
| 345 |