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