| 1 | package HTML::TabbedExamples::Generate; | ||
| 2 | $HTML::TabbedExamples::Generate::VERSION = '0.2.1'; | ||
| 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 | 0 | ▶ | 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__ | ||