| 1 | package HTML::TabbedExamples::Generate; | ||
| 2 | $HTML::TabbedExamples::Generate::VERSION = '0.2.0'; | ||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 2 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 2 | ▶ | use MooX 'late'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 2 | ▶ | use CGI (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | use Text::VimColor; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | |||
| 13 | use Carp (); | ||
| 14 | |||
| 15 | has _default_syntax => | ||
| 16 | ( is => 'ro', isa => 'Str', init_arg => 'default_syntax' ); | ||
| 17 | has _main_pre_css_classes => ( | ||
| 18 | is => 'ro', | ||
| 19 | isa => 'ArrayRef[Str]', | ||
| 20 | default => sub { | ||
| 21 | return [qw(code)]; | ||
| 22 | }, | ||
| 23 | init_arg => 'main_pre_css_classes', | ||
| 24 | ); | ||
| 25 | |||
| 26 | sub _calc_post_code | ||
| 27 | { | ||
| 28 | my $self = shift; | ||
| 29 | my $ex_spec = shift; | ||
| 30 | |||
| 31 | my $pre_code = $ex_spec->{code}; | ||
| 32 | |||
| 33 | if ( $ex_spec->{no_syntax} ) | ||
| 34 | { | ||
| 35 | return "<pre>\n" . CGI::escapeHTML($pre_code) . "</pre>\n"; | ||
| 36 | } | ||
| 37 | else | ||
| 38 | { | ||
| 39 | my $syntax = ( $ex_spec->{syntax} || $self->_default_syntax ); | ||
| 40 | |||
| 41 | my $code = <<"EOF"; | ||
| 42 | #!/usr/bin/perl | ||
| 43 | |||
| 44 | use strict; | ||
| 45 | use warnings; | ||
| 46 | |||
| 47 | $pre_code | ||
| 48 | EOF | ||
| 49 | |||
| 50 | my $tvc = Text::VimColor->new( | ||
| 51 | string => \$code, | ||
| 52 | filetype => $syntax, | ||
| 53 | ); | ||
| 54 | |||
| 55 | return qq|<pre class="| | ||
| 56 | . join( ' ', | ||
| 57 | map { CGI::escapeHTML($_) } @{ $self->_main_pre_css_classes }, | ||
| 58 | $syntax ) | ||
| 59 | . qq|">\n| | ||
| 60 | . ( $tvc->html() =~ s{(class=")syn}{$1}gr ) | ||
| 61 | . qq|\n</pre>\n|; | ||
| 62 | |||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | sub render | ||
| 67 | { | ||
| 68 | my ( $self, $args ) = @_; | ||
| 69 | |||
| 70 | my $examples = $args->{'examples'}; | ||
| 71 | my $id_base = $args->{'id_base'}; | ||
| 72 | |||
| 73 | my $ret_string = ''; | ||
| 74 | |||
| 75 | my @lis; | ||
| 76 | my @codes; | ||
| 77 | |||
| 78 | foreach my $ex_spec (@$examples) | ||
| 79 | { | ||
| 80 | my $id = $id_base . '__' . $ex_spec->{id}; | ||
| 81 | my $label = $ex_spec->{label}; | ||
| 82 | |||
| 83 | my $esc_id = CGI::escapeHTML($id); | ||
| 84 | my $esc_label = CGI::escapeHTML($label); | ||
| 85 | |||
| 86 | my $post_code = $self->_calc_post_code($ex_spec); | ||
| 87 | |||
| 88 | push @lis, qq[<li><a href="#$esc_id">$esc_label</a></li>\n]; | ||
| 89 | push @codes, qq[<div id="$esc_id">$post_code</div>]; | ||
| 90 | } | ||
| 91 | |||
| 92 | return | ||
| 93 | qq{<div class="tabs">\n} | ||
| 94 | . qq{<ul>\n} | ||
| 95 | . join( "\n", @lis ) | ||
| 96 | . qq{\n</ul>\n} | ||
| 97 | . join( "\n", @codes ) | ||
| 98 | . qq{</div>\n}; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub html_with_title | ||
| 102 | { | ||
| 103 | my ( $self, $args ) = @_; | ||
| 104 | |||
| 105 | my $id_base = $args->{'id_base'} | ||
| 106 | or Carp::confess("id_base not specified."); | ||
| 107 | my $title = $args->{'title'} | ||
| 108 | or Carp::confess("title not specified."); | ||
| 109 | |||
| 110 | return | ||
| 111 | qq[<h3 id="] | ||
| 112 | . CGI::escapeHTML($id_base) . qq[">] | ||
| 113 | . CGI::escapeHTML($title) | ||
| 114 | . qq[</h3>\n\n] | ||
| 115 | . $self->render($args); | ||
| 116 | } | ||
| 117 | |||
| 118 | 1; | ||
| 119 | |||
| 120 | __END__ | ||
| 121 | |||
| 122 | =pod | ||
| 123 | |||
| 124 | =encoding utf-8 | ||
| 125 | |||
| 126 | =head1 VERSION | ||
| 127 | |||
| 128 | version 0.2.0 | ||
| 129 | |||
| 130 | =head1 SYNOPSIS | ||
| 131 | |||
| 132 | use HTML::TabbedExamples::Generate; | ||
| 133 | |||
| 134 | use strict; | ||
| 135 | use warnings; | ||
| 136 | |||
| 137 | # Examples generator: | ||
| 138 | my $ex_gen = HTML::TabbedExamples::Generate->new( | ||
| 139 | { | ||
| 140 | default_syntax => 'perl', | ||
| 141 | } | ||
| 142 | ); | ||
| 143 | |||
| 144 | print $ex_gen->html_with_title( | ||
| 145 | { | ||
| 146 | title => "Copying a file", | ||
| 147 | id_base => "copying_a_file", | ||
| 148 | examples => | ||
| 149 | [ | ||
| 150 | { | ||
| 151 | id => "io_all", | ||
| 152 | label => "IO-All", | ||
| 153 | code => <<'EOF', | ||
| 154 | use IO::All; | ||
| 155 | |||
| 156 | my ($source_filename, $dest_filename) = @_; | ||
| 157 | io->file($source_filename) > io->file($dest_filename); | ||
| 158 | EOF | ||
| 159 | |||
| 160 | }, | ||
| 161 | { | ||
| 162 | id => "core", | ||
| 163 | label => "Core Perl", | ||
| 164 | code => <<'EOF', | ||
| 165 | use File::Copy qw(copy); | ||
| 166 | |||
| 167 | my ($source_filename, $dest_filename) = @_; | ||
| 168 | |||
| 169 | copy($source_filename, $dest_filename); | ||
| 170 | EOF | ||
| 171 | }, | ||
| 172 | ], | ||
| 173 | } | ||
| 174 | ); | ||
| 175 | |||
| 176 | print $ex_gen->html_with_title( | ||
| 177 | { | ||
| 178 | title => "Overwriting a file with text", | ||
| 179 | id_base => "overwrite_a_file", | ||
| 180 | examples => | ||
| 181 | [ | ||
| 182 | { | ||
| 183 | id => "io_all", | ||
| 184 | label => "IO-All", | ||
| 185 | code => <<'EOF', | ||
| 186 | use IO::All; | ||
| 187 | |||
| 188 | io->file("output.txt")->utf8->print("Hello World!\n"); | ||
| 189 | EOF | ||
| 190 | |||
| 191 | }, | ||
| 192 | { | ||
| 193 | id => "core", | ||
| 194 | label => "Core Perl", | ||
| 195 | code => <<'EOF', | ||
| 196 | use autodie; | ||
| 197 | |||
| 198 | open my $out, '>:encoding(utf8)', "output.txt"; | ||
| 199 | print {$out} "Hello World!\n"; | ||
| 200 | close($out); | ||
| 201 | EOF | ||
| 202 | }, | ||
| 203 | ], | ||
| 204 | } | ||
| 205 | ); | ||
| 206 | |||
| 207 | =head1 NAME | ||
| 208 | |||
| 209 | HTML::TabbedExamples::Generate - generate syntax-highlighted examples for | ||
| 210 | codes with a markup compatible with jQueryUI's tab widgets. | ||
| 211 | |||
| 212 | =head1 METHODS | ||
| 213 | |||
| 214 | =head2 my $obj = HTML::TabbedExamples::Generate->new({ default_syntax => "perl", main_pre_css_classes => [qw( code my_example1 )]}) | ||
| 215 | |||
| 216 | Returns a new object with a default_syntax for the examples, and some classes | ||
| 217 | to add for the main pre tag. Default C<'main_pre_css_classes'> value is only | ||
| 218 | the class C<'code'>. | ||
| 219 | |||
| 220 | =head2 my $html_markup_string = $obj->render({ examples => \@examples, id_base => 'my_example_id'}) | ||
| 221 | |||
| 222 | Renders some examples. C<'id_base'> is the prefix to prepend to the classes’ | ||
| 223 | IDs. C<'examples'> points to an array-ref of examples, which are hash-refs | ||
| 224 | cotnaining: | ||
| 225 | |||
| 226 | =over 4 | ||
| 227 | |||
| 228 | =item * label | ||
| 229 | |||
| 230 | The tab's label. | ||
| 231 | |||
| 232 | =item * no_syntax | ||
| 233 | |||
| 234 | If true, disable syntax highlighting. | ||
| 235 | |||
| 236 | =item * syntax | ||
| 237 | |||
| 238 | If true, override the default_syntax | ||
| 239 | |||
| 240 | =back | ||
| 241 | |||
| 242 | =head2 my $html_markup_string = $obj->html_with_title({ title => 'My title to escape', %ARGS }) | ||
| 243 | |||
| 244 | Same arguments as render() with the addition of C<'title'> which is a title | ||
| 245 | to be placed inside an C<< <h3>..</h3> >> tag, which will be escaped. | ||
| 246 | Returns the complete markup. | ||
| 247 | |||
| 248 | =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan | ||
| 249 | |||
| 250 | =head1 SUPPORT | ||
| 251 | |||
| 252 | =head2 Websites | ||
| 253 | |||
| 254 | The following websites have more information about this module, and may be of help to you. As always, | ||
| 255 | in addition to those websites please use your favorite search engine to discover more resources. | ||
| 256 | |||
| 257 | =over 4 | ||
| 258 | |||
| 259 | =item * | ||
| 260 | |||
| 261 | MetaCPAN | ||
| 262 | |||
| 263 | A modern, open-source CPAN search engine, useful to view POD in HTML format. | ||
| 264 | |||
| 265 | L<https://metacpan.org/release/HTML-TabbedExamples-Generate> | ||
| 266 | |||
| 267 | =item * | ||
| 268 | |||
| 269 | Search CPAN | ||
| 270 | |||
| 271 | The default CPAN search engine, useful to view POD in HTML format. | ||
| 272 | |||
| 273 | L<http://search.cpan.org/dist/HTML-TabbedExamples-Generate> | ||
| 274 | |||
| 275 | =item * | ||
| 276 | |||
| 277 | RT: CPAN's Bug Tracker | ||
| 278 | |||
| 279 | The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN. | ||
| 280 | |||
| 281 | L<https://rt.cpan.org/Public/Dist/Display.html?Name=HTML-TabbedExamples-Generate> | ||
| 282 | |||
| 283 | =item * | ||
| 284 | |||
| 285 | AnnoCPAN | ||
| 286 | |||
| 287 | The AnnoCPAN is a website that allows community annotations of Perl module documentation. | ||
| 288 | |||
| 289 | L<http://annocpan.org/dist/HTML-TabbedExamples-Generate> | ||
| 290 | |||
| 291 | =item * | ||
| 292 | |||
| 293 | CPAN Ratings | ||
| 294 | |||
| 295 | The CPAN Ratings is a website that allows community ratings and reviews of Perl modules. | ||
| 296 | |||
| 297 | L<http://cpanratings.perl.org/d/HTML-TabbedExamples-Generate> | ||
| 298 | |||
| 299 | =item * | ||
| 300 | |||
| 301 | CPANTS | ||
| 302 | |||
| 303 | The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution. | ||
| 304 | |||
| 305 | L<http://cpants.cpanauthors.org/dist/HTML-TabbedExamples-Generate> | ||
| 306 | |||
| 307 | =item * | ||
| 308 | |||
| 309 | CPAN Testers | ||
| 310 | |||
| 311 | The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions. | ||
| 312 | |||
| 313 | L<http://www.cpantesters.org/distro/H/HTML-TabbedExamples-Generate> | ||
| 314 | |||
| 315 | =item * | ||
| 316 | |||
| 317 | CPAN Testers Matrix | ||
| 318 | |||
| 319 | The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms. | ||
| 320 | |||
| 321 | L<http://matrix.cpantesters.org/?dist=HTML-TabbedExamples-Generate> | ||
| 322 | |||
| 323 | =item * | ||
| 324 | |||
| 325 | CPAN Testers Dependencies | ||
| 326 | |||
| 327 | The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution. | ||
| 328 | |||
| 329 | L<http://deps.cpantesters.org/?module=HTML::TabbedExamples::Generate> | ||
| 330 | |||
| 331 | =back | ||
| 332 | |||
| 333 | =head2 Bugs / Feature Requests | ||
| 334 | |||
| 335 | Please report any bugs or feature requests by email to C<bug-html-tabbedexamples-generate at rt.cpan.org>, or through | ||
| 336 | the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=HTML-TabbedExamples-Generate>. You will be automatically notified of any | ||
| 337 | progress on the request by the system. | ||
| 338 | |||
| 339 | =head2 Source Code | ||
| 340 | |||
| 341 | The code is open to the world, and available for you to hack on. Please feel free to browse it and play | ||
| 342 | with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull | ||
| 343 | from your repository :) | ||
| 344 | |||
| 345 | L<https://github.com/shlomif/perl-html-tabbedexamples-generate> | ||
| 346 | |||
| 347 | git clone git://github.com/shlomif/perl-html-tabbedexamples-generate.git | ||
| 348 | |||
| 349 | =head1 AUTHOR | ||
| 350 | |||
| 351 | Shlomi Fish <shlomif@cpan.org> | ||
| 352 | |||
| 353 | =head1 BUGS | ||
| 354 | |||
| 355 | Please report any bugs or feature requests on the bugtracker website | ||
| 356 | L<https://github.com/shlomif/perl-html-tabbedexamples-generate/issues> | ||
| 357 | |||
| 358 | When submitting a bug or request, please include a test-file or a | ||
| 359 | patch to an existing test-file that illustrates the bug or desired | ||
| 360 | feature. | ||
| 361 | |||
| 362 | =head1 COPYRIGHT AND LICENSE | ||
| 363 | |||
| 364 | This software is Copyright (c) 2013 by Shlomi Fish. | ||
| 365 | |||
| 366 | This is free software, licensed under: | ||
| 367 | |||
| 368 | The MIT (X11) License | ||
| 369 | |||
| 370 | =cut | ||