| 1 | package GraphViz2::Marpa::Demo; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use warnings qw(FATAL utf8); # Fatalize encoding glitches. | ||
| 6 | |||
| 7 | use Config; | ||
| 8 | |||
| 9 | use Date::Format; # For time2str(). | ||
| 10 | use Date::Simple; | ||
| 11 | |||
| 12 | use File::Spec; | ||
| 13 | |||
| 14 | use GraphViz2::Marpa::Config; | ||
| 15 | |||
| 16 | use HTML::Entities::Interpolate; | ||
| 17 | |||
| 18 | use Moo; | ||
| 19 | |||
| 20 | use Path::Tiny; | ||
| 21 | |||
| 22 | use Text::Xslate 'mark_raw'; | ||
| 23 | |||
| 24 | use Types::Standard qw/HashRef Int/; | ||
| 25 | |||
| 26 | has authortest => | ||
| 27 | ( | ||
| 28 | default => sub{return 0}, | ||
| 29 | is => 'rw', | ||
| 30 | isa => Int, | ||
| 31 | required => 0, | ||
| 32 | ); | ||
| 33 | |||
| 34 | has config => | ||
| 35 | ( | ||
| 36 | default => sub{return GraphViz2::Marpa::Config -> new -> config}, | ||
| 37 | is => 'rw', | ||
| 38 | isa => HashRef, | ||
| 39 | required => 0, | ||
| 40 | ); | ||
| 41 | |||
| 42 | our $VERSION = '2.12'; | ||
| 43 | |||
| 44 | # ------------------------------------------------ | ||
| 45 | |||
| 46 | sub generate_demo_environment | ||
| 47 | { | ||
| 48 | my($self) = @_; | ||
| 49 | |||
| 50 | my(@environment); | ||
| 51 | |||
| 52 | # mark_raw() is needed because of the HTML tag <a>. | ||
| 53 | |||
| 54 | push @environment, | ||
| 55 | {left => 'Author', right => mark_raw(qq|<a href="http://savage.net.au/">Ron Savage</a>|)}, | ||
| 56 | {left => 'Date', right => Date::Simple -> today}, | ||
| 57 | {left => 'OS', right => 'Debian V 7'}, | ||
| 58 | {left => 'Perl', right => $Config{version} }; | ||
| 59 | |||
| 60 | return \@environment; | ||
| 61 | |||
| 62 | } # End of generate_demo_environment. | ||
| 63 | |||
| 64 | # ----------------------------------------------- | ||
| 65 | |||
| 66 | sub generate_demo_index | ||
| 67 | { | ||
| 68 | my($self) = @_; | ||
| 69 | my($format) = 'svg'; | ||
| 70 | my($data_dir_name) = 'data'; | ||
| 71 | my($html_dir_name) = 'html'; | ||
| 72 | |||
| 73 | if ($self -> authortest) | ||
| 74 | { | ||
| 75 | $data_dir_name = "xt/author/$data_dir_name"; | ||
| 76 | $html_dir_name = "xt/author/$html_dir_name"; | ||
| 77 | } | ||
| 78 | |||
| 79 | my(@dot_file) = $self -> get_files($data_dir_name, 'gv'); | ||
| 80 | |||
| 81 | my(@content); | ||
| 82 | my($dot_file); | ||
| 83 | my($image_file, %image_file); | ||
| 84 | my($object_file); | ||
| 85 | |||
| 86 | for my $file_name (@dot_file) | ||
| 87 | { | ||
| 88 | $dot_file = "$file_name.gv"; | ||
| 89 | $image_file = path("$file_name.$format") -> basename; | ||
| 90 | $image_file = File::Spec -> catfile($html_dir_name, $image_file); | ||
| 91 | @content = map{$Entitize{$_} } path($dot_file) -> lines_utf8; | ||
| 92 | $object_file = './' . path("$file_name.svg") -> basename; | ||
| 93 | $image_file{$file_name} = | ||
| 94 | { | ||
| 95 | image_file => -e $image_file ? $image_file : '', | ||
| 96 | image_size => -e $image_file ? -s $image_file : 0, | ||
| 97 | input => $dot_file, | ||
| 98 | input_bytes => 'byte' . (-s $dot_file == 1 ? '' : 's'), | ||
| 99 | input_size => -s $dot_file, | ||
| 100 | object_file => $object_file, | ||
| 101 | output => -e $image_file && -s $image_file ? $image_file : '', | ||
| 102 | output_bytes => 'byte' . (-e $image_file && -s $image_file == 1 ? '' : 's'), | ||
| 103 | output_size => -s $image_file, | ||
| 104 | raw => join('<br />', @content), | ||
| 105 | }; | ||
| 106 | } | ||
| 107 | |||
| 108 | my($config) = $self -> config; | ||
| 109 | my($templater) = Text::Xslate -> new | ||
| 110 | ( | ||
| 111 | input_layer => '', | ||
| 112 | path => $$config{template_path}, | ||
| 113 | ); | ||
| 114 | my($count) = 0; | ||
| 115 | my($index) = $templater -> render | ||
| 116 | ( | ||
| 117 | 'graphviz2.marpa.index.tx', | ||
| 118 | { | ||
| 119 | authortest => $self -> authortest, | ||
| 120 | data => | ||
| 121 | [ | ||
| 122 | map | ||
| 123 | { | ||
| 124 | { | ||
| 125 | count => ++$count, | ||
| 126 | image_file => mark_raw($image_file{$_}{image_file}), | ||
| 127 | image_size => $image_file{$_}{image_size}, | ||
| 128 | input => mark_raw($image_file{$_}{input}), | ||
| 129 | input_bytes => $image_file{$_}{input_bytes}, | ||
| 130 | input_size => mark_raw($image_file{$_}{input_size}), | ||
| 131 | object_file => $image_file{$_}{object_file}, | ||
| 132 | output => mark_raw($image_file{$_}{output}), | ||
| 133 | output_bytes => $image_file{$_}{output_bytes}, | ||
| 134 | output_size => $image_file{$_}{output_size}, | ||
| 135 | raw => mark_raw($image_file{$_}{raw}), | ||
| 136 | } | ||
| 137 | } @dot_file | ||
| 138 | ], | ||
| 139 | default_css => "$$config{css_url}/default.css", | ||
| 140 | environment => $self -> generate_demo_environment, | ||
| 141 | fancy_table_css => "$$config{css_url}/fancy.table.css", | ||
| 142 | version => $VERSION, | ||
| 143 | } | ||
| 144 | ); | ||
| 145 | |||
| 146 | print "Finished rendering. \n"; | ||
| 147 | |||
| 148 | my($file_name) = File::Spec -> catfile($html_dir_name, 'index.html'); | ||
| 149 | |||
| 150 | open(my $fh, '>:encoding(utf-8)', $file_name); | ||
| 151 | print $fh $index; | ||
| 152 | close $fh; | ||
| 153 | |||
| 154 | print "Wrote: $file_name\n"; | ||
| 155 | |||
| 156 | # Return 0 for success and 1 for failure. | ||
| 157 | |||
| 158 | return 0; | ||
| 159 | |||
| 160 | } # End of generate_demo_index. | ||
| 161 | |||
| 162 | # ------------------------------------------------ | ||
| 163 | |||
| 164 | sub get_files | ||
| 165 | { | ||
| 166 | my($self, $dir_name, $type) = @_; | ||
| 167 | |||
| 168 | return (sort map{s/\.$type//; $_} grep{/\.$type$/} path($dir_name) -> children); | ||
| 169 | |||
| 170 | } # End of get_files. | ||
| 171 | |||
| 172 | # -------------------------------------------------- | ||
| 173 | |||
| 174 | sub justify | ||
| 175 | { | ||
| 176 | my($self, $s) = @_; | ||
| 177 | my($width) = 20; | ||
| 178 | |||
| 179 | return $s . ' ' x ($width - length $s); | ||
| 180 | |||
| 181 | } # End of justify. | ||
| 182 | |||
| 183 | # ----------------------------------------------- | ||
| 184 | |||
| 185 | 1; | ||
| 186 | |||
| 187 | =pod | ||
| 188 | |||
| 189 | =head1 NAME | ||
| 190 | |||
| 191 | C<GraphViz2::Marpa::Demo> - A demo page generator for C<GraphViz2::Marpa> | ||
| 192 | |||
| 193 | =head1 Synopsis | ||
| 194 | |||
| 195 | See L<GraphViz2::Marpa/Synopsis>. | ||
| 196 | |||
| 197 | =head1 Description | ||
| 198 | |||
| 199 | L<GraphViz2::Marpa> provides a Marpa-based parser for Graphviz C<dot> files, | ||
| 200 | and this module helps generate the demo page. | ||
| 201 | |||
| 202 | This module is really only of interest to the author. | ||
| 203 | |||
| 204 | =head1 Distributions | ||
| 205 | |||
| 206 | This module is available as a Unix-style distro (*.tgz). | ||
| 207 | |||
| 208 | See L<http://savage.net.au/Perl-modules/html/installing-a-module.html> | ||
| 209 | for help on unpacking and installing distros. | ||
| 210 | |||
| 211 | =head1 Installation | ||
| 212 | |||
| 213 | Install L<GraphViz2::Marpa> as you would for any C<Perl> module: | ||
| 214 | |||
| 215 | Run: | ||
| 216 | |||
| 217 | cpanm GraphViz2::Marpa | ||
| 218 | |||
| 219 | or run: | ||
| 220 | |||
| 221 | sudo cpan GraphViz2::Marpa | ||
| 222 | |||
| 223 | or unpack the distro, and then either: | ||
| 224 | |||
| 225 | perl Build.PL | ||
| 226 | ./Build | ||
| 227 | ./Build test | ||
| 228 | sudo ./Build install | ||
| 229 | |||
| 230 | or: | ||
| 231 | |||
| 232 | perl Makefile.PL | ||
| 233 | make (or dmake or nmake) | ||
| 234 | make test | ||
| 235 | make install | ||
| 236 | |||
| 237 | =head1 Constructor and Initialization | ||
| 238 | |||
| 239 | =head2 Calling new() | ||
| 240 | |||
| 241 | C<new()> is called as C<< my($obj) = GraphViz2::Marpa::Demo -> new(k1 => v1, k2 => v2, ...) >>. | ||
| 242 | |||
| 243 | It returns a new object of type C<GraphViz2::Marpa::Demo>. | ||
| 244 | |||
| 245 | Key-value pairs accepted in the parameter list: | ||
| 246 | |||
| 247 | =over 4 | ||
| 248 | |||
| 249 | =item o authortest => $Boolean | ||
| 250 | |||
| 251 | This allows generate.demo.pl to control whether it's processing data/ ot xt/author/data/. | ||
| 252 | |||
| 253 | =back | ||
| 254 | |||
| 255 | =head1 Methods | ||
| 256 | |||
| 257 | =head2 generate_demo_environment() | ||
| 258 | |||
| 259 | Called by generate_demo_index(). | ||
| 260 | |||
| 261 | Generates a table to be inserted into html/index.html. | ||
| 262 | |||
| 263 | See scripts/generate.demo.pl. | ||
| 264 | |||
| 265 | =head2 generate_demo_index() | ||
| 266 | |||
| 267 | Generates html/index.html. | ||
| 268 | |||
| 269 | Does not run any programs to generate other files, e.g. html/*.svg. See scripts/generate.demo.sh for that. | ||
| 270 | |||
| 271 | =head2 get_files($dir_name, $type) | ||
| 272 | |||
| 273 | Returns a sorted list of files of type (extension) $type from directory $dir_name. | ||
| 274 | |||
| 275 | =head2 justify($string) | ||
| 276 | |||
| 277 | Right justify the $string in a field of 20 spaces. | ||
| 278 | |||
| 279 | =head1 Machine-Readable Change Log | ||
| 280 | |||
| 281 | The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>. | ||
| 282 | |||
| 283 | =head1 Version Numbers | ||
| 284 | |||
| 285 | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | ||
| 286 | |||
| 287 | =head1 Repository | ||
| 288 | |||
| 289 | L<https://github.com/ronsavage/GraphViz2-Marpa> | ||
| 290 | |||
| 291 | =head1 Support | ||
| 292 | |||
| 293 | Email the author, or log a bug on RT: | ||
| 294 | |||
| 295 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=GraphViz2::Marpa>. | ||
| 296 | |||
| 297 | =head1 Author | ||
| 298 | |||
| 299 | L<GraphViz2::Marpa> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2012. | ||
| 300 | |||
| 301 | Home page: L<http://savage.net.au/index.html>. | ||
| 302 | |||
| 303 | =head1 Copyright | ||
| 304 | |||
| 305 | Australian copyright (c) 2012, Ron Savage. | ||
| 306 | |||
| 307 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 308 | you can redistribute them and/or modify them under the terms of | ||
| 309 | The Perl License, a copy of which is available at: | ||
| 310 | http://dev.perl.org/licenses/ | ||
| 311 | |||
| 312 | =cut |