| 1 | package MarpaX::Languages::Dash::Utils; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use warnings qw(FATAL utf8); # Fatalize encoding glitches. | ||
| 6 | |||
| 7 | use Config; | ||
| 8 | |||
| 9 | use Date::Simple; | ||
| 10 | |||
| 11 | use File::Slurper 'read_text'; | ||
| 12 | use File::Spec; | ||
| 13 | |||
| 14 | use MarpaX::Languages::Dash::Config; | ||
| 15 | use MarpaX::Languages::Dash::Filer; | ||
| 16 | |||
| 17 | use HTML::Entities::Interpolate; | ||
| 18 | |||
| 19 | use Marpa::R2; | ||
| 20 | |||
| 21 | use Moo; | ||
| 22 | |||
| 23 | use Text::Xslate 'mark_raw'; | ||
| 24 | |||
| 25 | use Types::Standard qw/HashRef/; | ||
| 26 | |||
| 27 | has config => | ||
| 28 | ( | ||
| 29 | default => sub{return MarpaX::Languages::Dash::Config -> new -> config}, | ||
| 30 | is => 'rw', | ||
| 31 | isa => HashRef, | ||
| 32 | required => 0, | ||
| 33 | ); | ||
| 34 | |||
| 35 | |||
| 36 | our $VERSION = '1.00'; | ||
| 37 | |||
| 38 | # ------------------------------------------------ | ||
| 39 | |||
| 40 | sub generate_demo_environment | ||
| 41 | { | ||
| 42 | my($self) = @_; # Chomp only works in list context (or with array_ref). | ||
| 43 | my($debian) = read_text '/etc/debian_version'; | ||
| 44 | |||
| 45 | my(@environment); | ||
| 46 | |||
| 47 | # mark_raw() is needed because of the HTML tag <a>. | ||
| 48 | |||
| 49 | push @environment, | ||
| 50 | {left => 'Author', right => mark_raw(qq|<a href="http://savage.net.au/">Ron Savage</a>|)}, | ||
| 51 | {left => 'Date', right => Date::Simple -> today}, | ||
| 52 | {left => 'OS', right => "Debian V $debian"}, | ||
| 53 | {left => 'Perl', right => $Config{version} }; | ||
| 54 | |||
| 55 | return \@environment; | ||
| 56 | |||
| 57 | } # End of generate_demo_environment. | ||
| 58 | |||
| 59 | # ----------------------------------------------- | ||
| 60 | |||
| 61 | sub generate_demo_index | ||
| 62 | { | ||
| 63 | my($self) = @_; | ||
| 64 | my($data_dir_name) = 'data'; | ||
| 65 | my($html_dir_name) = 'html'; | ||
| 66 | my(%data_file) = MarpaX::Languages::Dash::Filer -> new -> get_files($data_dir_name, 'dash'); | ||
| 67 | |||
| 68 | my($html_name); | ||
| 69 | my($line, @line); | ||
| 70 | my($name); | ||
| 71 | |||
| 72 | for my $key (sort keys %data_file) | ||
| 73 | { | ||
| 74 | $name = "$data_dir_name/$key.dash"; | ||
| 75 | $line = read_text $name; | ||
| 76 | @line = split(/\n/, $line); | ||
| 77 | $html_name = "$html_dir_name/$key.svg"; | ||
| 78 | |||
| 79 | $data_file{$key} = | ||
| 80 | { | ||
| 81 | dash => join('<br />', map{$Entitize{$_} || ''} @line), | ||
| 82 | input => $name, | ||
| 83 | output => -e $html_name ? $html_name : 'None', | ||
| 84 | title => $line[0], | ||
| 85 | }; | ||
| 86 | } | ||
| 87 | |||
| 88 | my(@key) = sort keys %data_file; | ||
| 89 | my($config) = $self -> config; | ||
| 90 | my($templater) = Text::Xslate -> new | ||
| 91 | ( | ||
| 92 | input_layer => '', | ||
| 93 | path => $$config{template_path}, | ||
| 94 | ); | ||
| 95 | |||
| 96 | my($count) = 0; | ||
| 97 | my($index) = $templater -> render | ||
| 98 | ( | ||
| 99 | 'marpax.languages.dash.tx', | ||
| 100 | { | ||
| 101 | default_css => "$$config{css_url}/default.css", | ||
| 102 | data => | ||
| 103 | [ | ||
| 104 | map | ||
| 105 | { | ||
| 106 | { | ||
| 107 | count => ++$count, | ||
| 108 | dash => mark_raw($data_file{$_}{dash}), | ||
| 109 | image => "./$_.svg", | ||
| 110 | input => mark_raw($data_file{$_}{input}), | ||
| 111 | output => mark_raw($data_file{$_}{output}), | ||
| 112 | title => mark_raw($data_file{$_}{title}), | ||
| 113 | }; | ||
| 114 | } @key | ||
| 115 | ], | ||
| 116 | date => Date::Simple -> today, | ||
| 117 | environment => $self -> generate_demo_environment, | ||
| 118 | fancy_table_css => "$$config{css_url}/fancy.table.css", | ||
| 119 | marpa_version => $Marpa::R2::VERSION, | ||
| 120 | version => $VERSION, | ||
| 121 | } | ||
| 122 | ); | ||
| 123 | my($file_name) = File::Spec -> catfile($html_dir_name, 'index.html'); | ||
| 124 | |||
| 125 | open(my $fh, '>', $file_name); | ||
| 126 | print $fh $index; | ||
| 127 | close $fh; | ||
| 128 | |||
| 129 | print "Wrote $file_name\n"; | ||
| 130 | |||
| 131 | # Return 0 for success and 1 for failure. | ||
| 132 | |||
| 133 | return 0; | ||
| 134 | |||
| 135 | } # End of generate_demo_index. | ||
| 136 | |||
| 137 | # ----------------------------------------------- | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | =pod | ||
| 142 | |||
| 143 | =head1 NAME | ||
| 144 | |||
| 145 | L<MarpaX::Languages::Dash::Utils> - Utils used by MarpaX::Languages::Dash | ||
| 146 | |||
| 147 | =head1 Synopsis | ||
| 148 | |||
| 149 | See scripts/generate.index.pl. | ||
| 150 | |||
| 151 | Note: scripts/generate.index.pl outputs to a directory called 'html' in the 'current' directory. | ||
| 152 | |||
| 153 | See L<the demo page|http://savage.net.au/Perl-modules/html/marpax.languages.dash/> for sample output. | ||
| 154 | |||
| 155 | =head1 Description | ||
| 156 | |||
| 157 | Some utils to simplify testing. | ||
| 158 | |||
| 159 | End-users do not need to call the methods in this module. | ||
| 160 | |||
| 161 | =head1 Distributions | ||
| 162 | |||
| 163 | This module is available as a Unix-style distro (*.tgz). | ||
| 164 | |||
| 165 | See L<http://savage.net.au/Perl-modules/html/installing-a-module.html> | ||
| 166 | for help on unpacking and installing distros. | ||
| 167 | |||
| 168 | =head1 Installation | ||
| 169 | |||
| 170 | Install L<MarpaX::Languages::Dash> as you would for any C<Perl> module: | ||
| 171 | |||
| 172 | Run: | ||
| 173 | |||
| 174 | cpanm MarpaX::Languages::Dash | ||
| 175 | |||
| 176 | or run: | ||
| 177 | |||
| 178 | sudo cpan MarpaX::Languages::Dash | ||
| 179 | |||
| 180 | or unpack the distro, and then either: | ||
| 181 | |||
| 182 | perl Build.PL | ||
| 183 | ./Build | ||
| 184 | ./Build test | ||
| 185 | sudo ./Build install | ||
| 186 | |||
| 187 | or: | ||
| 188 | |||
| 189 | perl Makefile.PL | ||
| 190 | make (or dmake or nmake) | ||
| 191 | make test | ||
| 192 | make install | ||
| 193 | |||
| 194 | =head1 Constructor and Initialization | ||
| 195 | |||
| 196 | =head2 Calling new() | ||
| 197 | |||
| 198 | C<new()> is called as C<< my($obj) = MarpaX::Languages::Dash::Utils -> new(k1 => v1, k2 => v2, ...) >>. | ||
| 199 | |||
| 200 | It returns a new object of type C<MarpaX::Languages::Dash::Utils>. | ||
| 201 | |||
| 202 | Key-value pairs accepted in the parameter list: | ||
| 203 | |||
| 204 | =over 4 | ||
| 205 | |||
| 206 | =item o (none) | ||
| 207 | |||
| 208 | =back | ||
| 209 | |||
| 210 | =head1 Methods | ||
| 211 | |||
| 212 | =head2 generate_demo_environment() | ||
| 213 | |||
| 214 | Returns a hashref of OS, etc, values. | ||
| 215 | |||
| 216 | Keys are C<left> and C<right>, to suit C<htdocs/assets/templates/marpax/languages/dash/fancy.table.tx>. | ||
| 217 | |||
| 218 | C<*.tx> files are used by L<Text::Xslate>. | ||
| 219 | |||
| 220 | Called by L</generate_demo_index()>. | ||
| 221 | |||
| 222 | =head2 generate_demo_index() | ||
| 223 | |||
| 224 | Calls L<MarpaX::Languages::Dash::Filer/get_files($dir_name, $type)> and L</generate_demo_environment()>. | ||
| 225 | |||
| 226 | Writes C<html/index.html>. | ||
| 227 | |||
| 228 | See scripts/generate.index.pl. | ||
| 229 | |||
| 230 | =head1 Version Numbers | ||
| 231 | |||
| 232 | Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. | ||
| 233 | |||
| 234 | =head1 Machine-Readable Change Log | ||
| 235 | |||
| 236 | The file Changes was converted into Changelog.ini by L<Module::Metadata::Changes>. | ||
| 237 | |||
| 238 | =head1 Support | ||
| 239 | |||
| 240 | Email the author, or log a bug on RT: | ||
| 241 | |||
| 242 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=MarpaX::Languages::Dash>. | ||
| 243 | |||
| 244 | =head1 Author | ||
| 245 | |||
| 246 | L<MarpaX::Languages::Dash> was written by Ron Savage I<E<lt>ron@savage.net.auE<gt>> in 2013. | ||
| 247 | |||
| 248 | Home page: L<http://savage.net.au/index.html>. | ||
| 249 | |||
| 250 | =head1 Copyright | ||
| 251 | |||
| 252 | Australian copyright (c) 2013, Ron Savage. | ||
| 253 | |||
| 254 | All Programs of mine are 'OSI Certified Open Source Software'; | ||
| 255 | you can redistribute them and/or modify them under the terms of | ||
| 256 | The Artistic License, a copy of which is available at: | ||
| 257 | http://www.opensource.org/licenses/index.html | ||
| 258 | |||
| 259 | =cut |