| 1 | package App::unbelievable; | ||||||||||||
| 2 | |||||||||||||
| 3 | # Documentation {{{1 | ||||||||||||
| 4 | |||||||||||||
| 5 | =encoding utf-8 | ||||||||||||
| 6 | |||||||||||||
| 7 | =head1 NAME | ||||||||||||
| 8 | |||||||||||||
| 9 | App::unbelievable - Dancer2 static site generator | ||||||||||||
| 10 | |||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||
| 12 | |||||||||||||
| 13 | In your Dancer2 app: | ||||||||||||
| 14 | |||||||||||||
| 15 | use App::unbelievable; # Pulls in Dancer2 | ||||||||||||
| 16 | # your routes here | ||||||||||||
| 17 | unbelievable; # At EOF, fills in the rest of the routes. | ||||||||||||
| 18 | |||||||||||||
| 19 | Then: | ||||||||||||
| 20 | |||||||||||||
| 21 | $ unbelievable build # Make the HTML | ||||||||||||
| 22 | $ unbelievable serve # Run a local development server | ||||||||||||
| 23 | |||||||||||||
| 24 | App::unbelievable makes a Dancer2 application into a static site generator. | ||||||||||||
| 25 | App::unbelievable adds routes for C</> and C</**> that will render Markdown | ||||||||||||
| 26 | files in C<content/>. The L<unbelievable> script generates static HTML | ||||||||||||
| 27 | and other assets into C<_built/>. | ||||||||||||
| 28 | |||||||||||||
| 29 | =cut | ||||||||||||
| 30 | |||||||||||||
| 31 | # }}}1 | ||||||||||||
| 32 | |||||||||||||
| 33 | 1 | ▶ | use version 0.77; our $VERSION = version->declare('v0.0.5'); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 34 | |||||||||||||
| 35 | 1 | ▶ | use App::unbelievable::Util; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 36 | |||||||||||||
| 37 | 1 | ▶ | use File::Slurp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 38 | 1 | ▶ | use File::Spec; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 39 | 1 | ▶ | use JSON; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 40 | 1 | ▶ | use Plack::Builder; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 41 | 1 | ▶ | use Syntax::Highlight::Engine::Kate; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 42 | 1 | ▶ | use Syntax::Highlight::Engine::Kate::All; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 43 | 1 | ▶ | use Text::FrontMatter::YAML; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 44 | 1 | ▶ | use Text::MultiMarkdown 'markdown'; # TODO someday? - Text::Markup | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | # But see https://github.com/theory/text-markup/issues/20 | ||||||||||||
| 46 | |||||||||||||
| 47 | # Routes to generate the HTML from Markdown (main logic) {{{1 | ||||||||||||
| 48 | 1 | ▶ | use parent 'Exporter'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 49 | 1 | ▶ | use vars::i '@EXPORT' => [qw(unbelievable)]; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 50 | |||||||||||||
| 51 | 1 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=2 SCAR=7.0
| |||||||||||||
| 52 | 1 | my $target = caller; | |||||||||||
| 53 | 1 | _diag('Loading ' . __PACKAGE__ . ' into ' . $target); | |||||||||||
| 54 | 1 | __PACKAGE__->export_to_level(1, @_); | |||||||||||
| 55 | |||||||||||||
| 56 | 1 | require Import::Into; | |||||||||||
| 57 | 1 | require feature; | |||||||||||
| 58 | 1 | require Dancer2; | |||||||||||
| 59 | 1 | $_->import::into($target) foreach qw(Dancer2 strict warnings); | |||||||||||
| 60 | 0 | feature->import::into($target, ':5.10'); | |||||||||||
| 61 | } #import() | ||||||||||||
| 62 | |||||||||||||
| 63 | # The list of syntaxes we know about, keyed lower-case | ||||||||||||
| 64 | # (Kamelon is case-sensitive). | ||||||||||||
| 65 | 1 | my %SYNTAXES; | |||||||||||
| 66 | |||||||||||||
| 67 | # Produce a file, e.g., by processing a markdown file into HTML. | ||||||||||||
| 68 | # Returns: | ||||||||||||
| 69 | # - falsy: pass | ||||||||||||
| 70 | # - hashref: | ||||||||||||
| 71 | # - {send_file => $filename}: a filename to send_file | ||||||||||||
| 72 | # - {template => [...]}: args for template(). | ||||||||||||
| 73 | 0 | ▶ | sub _produce_output { | ||||||||||
|
Subroutine
_produce_output: not called CC=8 SCAR=42.8
| |||||||||||||
| 74 | 0 | my ($public_dir, $content_dir, $request, $templater, $lrTags) = @_; | |||||||||||
| 75 | |||||||||||||
| 76 | 0 | ▶ | die "Assertion failure - empty megasplat??!!" unless @$lrTags; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 77 | |||||||||||||
| 78 | # Munge the last path component to provide the CLI with flexibility | ||||||||||||
| 79 | 0 | $lrTags->[$#$lrTags] =~ s{/$}{}; | |||||||||||
| 80 | # We handle /foo and /foo/ the same --- the CLI can request either. | ||||||||||||
| 81 | 0 | $lrTags->[$#$lrTags] =~ s{\.(?:html?|md|markdown)$}{}; | |||||||||||
| 82 | # We ignore the requested extension. Static files are served elsewhere | ||||||||||||
| 83 | # so never get here. | ||||||||||||
| 84 | |||||||||||||
| 85 | 0 | my $path = join('/', @$lrTags); | |||||||||||
| 86 | 0 | _diag("Processing file ", $path); | |||||||||||
| 87 | |||||||||||||
| 88 | 0 | my $fn; # File we are going to read from | |||||||||||
| 89 | my @candidates; # Where we might find it | ||||||||||||
| 90 | |||||||||||||
| 91 | 0 | push @candidates, File::Spec->catfile($content_dir, _suffix('.md', @$lrTags)); | |||||||||||
| 92 | 0 | push @candidates, File::Spec->catfile($content_dir, _suffix('.markdown', @$lrTags)); | |||||||||||
| 93 | 0 | push @candidates, File::Spec->catfile($public_dir, _suffix('.html', @$lrTags)); | |||||||||||
| 94 | 0 | push @candidates, File::Spec->catfile($public_dir, _suffix('.htm', @$lrTags)); | |||||||||||
| 95 | |||||||||||||
| 96 | 0 | foreach my $candidate (@candidates) { | |||||||||||
| 97 | 0 | ▶ | next unless -f -r $candidate; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 98 | 0 | $fn = $candidate; | |||||||||||
| 99 | 0 | last; | |||||||||||
| 100 | } | ||||||||||||
| 101 | |||||||||||||
| 102 | 0 | ▶ | die "Could not find file for $path (tried @{[join(' ', @candidates)]})" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | unless $fn; | ||||||||||||
| 104 | |||||||||||||
| 105 | 0 | ▶ | return {send_file => $fn} if $fn =~ /\.html?$/; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | |||||||||||||
| 107 | # Load Markdown file | ||||||||||||
| 108 | 0 | my @retval = ('index'); # Name of the template | |||||||||||
| 109 | 0 | my ($frontmatter, $markdown) = _load($fn); | |||||||||||
| 110 | 0 | _diag("Got frontmatter\n", Dumper $frontmatter); | |||||||||||
| 111 | 0 | _diag(\2, "Got contents:\n$markdown"); | |||||||||||
| 112 | |||||||||||||
| 113 | # shortcodes. TODO other than single-arg. | ||||||||||||
| 114 | 0 | $markdown =~ s[\{\{<\s*(?<code>\w+)\s+(?<arg0>\S+)\s*>\}\}] | |||||||||||
| 115 | 0 | [ _shortcode($+{code}, [$+{arg0}], $templater) ]exg; | |||||||||||
| 116 | |||||||||||||
| 117 | # Fenced, tagged code blocks. | ||||||||||||
| 118 | # TODO in S::K, don't repeat IDs for the line divs (currently each | ||||||||||||
| 119 | # block has lines id="1", id="2", ...). | ||||||||||||
| 120 | 0 | my (%styles, %scripts); # Map from text to 1. This way we only | |||||||||||
| 121 | # include each style or script block once. | ||||||||||||
| 122 | |||||||||||||
| 123 | 0 | pos($markdown) = 0; | |||||||||||
| 124 | 0 | ▶ | while($markdown =~ m{\G.*?(^```(\S+)$(.*?)^```)}gcms) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | |||||||||||||
| 126 | 0 | my ($lang, $text) = (_normalize_syntax($2), $3); | |||||||||||
| 127 | 0 | my $fence_start = $-[1]; # Where the fenced block ($1) is | |||||||||||
| 128 | 0 | my $fence_len = $+[1] - $-[1]; | |||||||||||
| 129 | |||||||||||||
| 130 | 0 | ▶ | if(!$SYNTAXES{$lang}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 131 | 0 | warn "Unknown language $2 in $path"; | |||||||||||
| 132 | 0 | substr($markdown, $fence_start, $fence_len) = | |||||||||||
| 133 | "```\n$text```"; # Just remove the language tag | ||||||||||||
| 134 | 0 | next; | |||||||||||
| 135 | } | ||||||||||||
| 136 | |||||||||||||
| 137 | # Trim newline at the start (because the $ above matched _before_ | ||||||||||||
| 138 | # the newline after the '```') | ||||||||||||
| 139 | 0 | $text =~ s{\A\R}{}; | |||||||||||
| 140 | |||||||||||||
| 141 | 0 | substr($markdown, $fence_start, $fence_len) = _highlight($lang, $text); | |||||||||||
| 142 | } | ||||||||||||
| 143 | |||||||||||||
| 144 | # Process what's left | ||||||||||||
| 145 | 0 | my $html = markdown($markdown, {base_url => '/'}); | |||||||||||
| 146 | |||||||||||||
| 147 | # TODO permit the user to specify a template | ||||||||||||
| 148 | 0 | _diag(\2, "Generated HTML:\n$html"); | |||||||||||
| 149 | 0 | return { template => [ | |||||||||||
| 150 | 'raw', | ||||||||||||
| 151 | { | ||||||||||||
| 152 | %$frontmatter, | ||||||||||||
| 153 | htmlsource => $html, | ||||||||||||
| 154 | styles => join("\n", keys %styles), | ||||||||||||
| 155 | scripts => join("\n", keys %scripts), | ||||||||||||
| 156 | } | ||||||||||||
| 157 | ] }; | ||||||||||||
| 158 | } #_produce_output | ||||||||||||
| 159 | |||||||||||||
| 160 | 0 | ▶ | sub unbelievable { | ||||||||||
|
Subroutine
unbelievable: not called CC=6 SCAR=35.1
Pod
documented
| |||||||||||||
| 161 | 0 | ▶ | my $appname = caller or die "No caller!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 162 | 0 | _diag('unbelievable ' . __PACKAGE__ . ' in ' . $appname); | |||||||||||
| 163 | |||||||||||||
| 164 | 0 | require Dancer2; | |||||||||||
| 165 | |||||||||||||
| 166 | 0 | ▶ | my $dsl = do { no strict 'refs'; &{ $appname . '::dsl' } }; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||||
| 167 | 0 | ▶ | die "Could not find Dancer2 DSL" unless $dsl; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 168 | |||||||||||||
| 169 | # Find the public dir and the content dir | ||||||||||||
| 170 | 0 | ▶ | my $public_dir = $dsl->setting('public_dir') or die "No public_dir set"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 171 | 0 | ▶ | $public_dir = File::Spec->rel2abs($public_dir) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 172 | unless File::Spec->file_name_is_absolute($public_dir); | ||||||||||||
| 173 | 0 | my ($vol, $dirs, $file) = File::Spec->splitpath($public_dir); | |||||||||||
| 174 | 0 | my $content_dir = File::Spec->catpath($vol, $dirs, 'content'); | |||||||||||
| 175 | |||||||||||||
| 176 | 0 | _diag("public_dir $public_dir"); | |||||||||||
| 177 | 0 | _diag("content_dir $content_dir"); | |||||||||||
| 178 | |||||||||||||
| 179 | # Create default routes. The caller must invoke this function | ||||||||||||
| 180 | # after defining any other routes, so these will only be used | ||||||||||||
| 181 | # as fallbacks. | ||||||||||||
| 182 | 0 | my $text = _line_mark_string(<<EOT); | |||||||||||
| 183 | package App::unbelievable::Routes; | ||||||||||||
| 184 | use 5.010001; | ||||||||||||
| 185 | use strict; | ||||||||||||
| 186 | use warnings; | ||||||||||||
| 187 | use vars::i { | ||||||||||||
| 188 | 0 | '\$APPNAME' => @{[_sqescape($appname)]}, | |||||||||||
| 189 | 0 | '\$PUBLIC' =>@{[_sqescape($public_dir)]}, | |||||||||||
| 190 | 0 | '\$CONTENT' => @{[_sqescape($content_dir)]}, | |||||||||||
| 191 | }; | ||||||||||||
| 192 | EOT | ||||||||||||
| 193 | |||||||||||||
| 194 | 0 | $text .= _line_mark_string(<<'EOTSQ'); | |||||||||||
| 195 | use Data::Dumper; | ||||||||||||
| 196 | use Dancer2 appname => $APPNAME; | ||||||||||||
| 197 | use App::unbelievable::Util; | ||||||||||||
| 198 | |||||||||||||
| 199 | _diag("Loading routes into $APPNAME"); | ||||||||||||
| 200 | App::unbelievable::_initialize(); | ||||||||||||
| 201 | |||||||||||||
| 202 | # Call our _produce_output, above, and take the appropriate action. | ||||||||||||
| 203 | # This way we don't have to try to directly invoke Dancer2 DSL in | ||||||||||||
| 204 | # _produce_output. | ||||||||||||
| 205 | sub _do_file { | ||||||||||||
| 206 | my $content = | ||||||||||||
| 207 | App::unbelievable::_produce_output($PUBLIC, $CONTENT, request, | ||||||||||||
| 208 | sub { template @_ }, shift); | ||||||||||||
| 209 | pass unless $content; | ||||||||||||
| 210 | send_file $content->{send_file} if $content->{send_file}; | ||||||||||||
| 211 | return template @{$content->{template}} if $content->{template}; | ||||||||||||
| 212 | die "Assertion failure: I don't know how to handle that file.\n" . | ||||||||||||
| 213 | Dumper $content; | ||||||||||||
| 214 | } | ||||||||||||
| 215 | |||||||||||||
| 216 | get '/' => sub { _do_file(['index']) }; | ||||||||||||
| 217 | # Note: we never get here on requests for static files --- those are | ||||||||||||
| 218 | # handled by middleware before Dancer2 checks routes. See | ||||||||||||
| 219 | # Dancer2::Core::App::to_app(). | ||||||||||||
| 220 | get '/**' => sub { _do_file(splat) }; | ||||||||||||
| 221 | |||||||||||||
| 222 | EOTSQ | ||||||||||||
| 223 | |||||||||||||
| 224 | 0 | eval $text; | |||||||||||
| 225 | 0 | ▶ | die $@ if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 226 | |||||||||||||
| 227 | 0 | return 1; # So the unbelievable() call can be the last thing in the file | |||||||||||
| 228 | } #unbelievable() | ||||||||||||
| 229 | |||||||||||||
| 230 | # }}}1 | ||||||||||||
| 231 | # Helper routines {{{1 | ||||||||||||
| 232 | |||||||||||||
| 233 | # Escape in single quotes | ||||||||||||
| 234 | 0 | ▶ | sub _sqescape { | ||||||||||
|
Subroutine
_sqescape: not called CC=1 SCAR=6.9
| |||||||||||||
| 235 | 0 | my $str = shift; | |||||||||||
| 236 | 0 | $str =~ s/'/\\'/g; | |||||||||||
| 237 | 0 | return "'$str'"; | |||||||||||
| 238 | } | ||||||||||||
| 239 | |||||||||||||
| 240 | # Render a shortcode. Usage: _shortcode($code, \@args, $templater) | ||||||||||||
| 241 | 0 | ▶ | sub _shortcode { | ||||||||||
|
Subroutine
_shortcode: not called CC=2 SCAR=17.9
| |||||||||||||
| 242 | 0 | my ($code, $lrArgs, $templater) = @_; | |||||||||||
| 243 | 0 | _diag(\2, "Shortcode $code @$lrArgs"); | |||||||||||
| 244 | |||||||||||||
| 245 | 0 | $_ =~ s{\A(['"])(.+)\1\z}{$2} foreach @$lrArgs; # de-quote | |||||||||||
| 246 | |||||||||||||
| 247 | my $result = $templater->( # render | ||||||||||||
| 248 | File::Spec->catfile('shortcodes', $code), | ||||||||||||
| 249 | 0 | { map {; "_$_" => $lrArgs->[$_]} 0..$#$lrArgs }, | |||||||||||
| 250 | { layout => undef } ); | ||||||||||||
| 251 | |||||||||||||
| 252 | 0 | $result =~ s{^\s+|\s+$}{}g; # trim | |||||||||||
| 253 | 0 | return $result; | |||||||||||
| 254 | } #_shortcode() | ||||||||||||
| 255 | |||||||||||||
| 256 | # Add a suffix to the last component of a list. No-op if the | ||||||||||||
| 257 | # list is empty. | ||||||||||||
| 258 | 0 | ▶ | sub _suffix { | ||||||||||
|
Subroutine
_suffix: not called CC=2 SCAR=17.9
| |||||||||||||
| 259 | 0 | my $suffix = shift; | |||||||||||
| 260 | 0 | ▶ | return () unless @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 261 | 0 | my $last = $_[$#_] . $suffix; | |||||||||||
| 262 | 0 | return @_[0..$#_-1], $last; | |||||||||||
| 263 | } | ||||||||||||
| 264 | |||||||||||||
| 265 | # Load a Markdown file, which may have front matter. | ||||||||||||
| 266 | # Input must be in UTF-8. | ||||||||||||
| 267 | # Returns an arrayref of YAML documents. | ||||||||||||
| 268 | 0 | ▶ | sub _load { | ||||||||||
|
Subroutine
_load: not called CC=8 SCAR=42.8
| |||||||||||||
| 269 | 0 | my $fn = shift; | |||||||||||
| 270 | 0 | my $text = read_file($fn, {binmode => ':utf8'}); | |||||||||||
| 271 | 0 | my ($frontmatter, $markdown); | |||||||||||
| 272 | 0 | my @errors; | |||||||||||
| 273 | 0 | my $reader; | |||||||||||
| 274 | |||||||||||||
| 275 | # Look for JSON frontmatter without separators (Hugo-style). | ||||||||||||
| 276 | 0 | my $charcount; | |||||||||||
| 277 | 0 | eval { | |||||||||||
| 278 | 0 | $reader = JSON->new; | |||||||||||
| 279 | 0 | ($frontmatter, $charcount) = $reader->decode_prefix($text); | |||||||||||
| 280 | 0 | ▶ | $markdown = substr $text, $charcount//0; | ||||||||||
|
Condition0%0/2
$charcount // 0
MC/DC0%0/1
$charcount // 0
$charcount
Truth table0%0/2
A
A= $charcount
| |||||||||||||
| 281 | }; | ||||||||||||
| 282 | 0 | ▶ | push @errors, "Could not read JSON: $@" if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 283 | 0 | ▶ | push @errors, "No JSON found" unless $charcount; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 284 | |||||||||||||
| 285 | 0 | ▶ | return ($frontmatter, $markdown) unless(@errors); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 286 | |||||||||||||
| 287 | # Mainline case: YAML frontmatter with `---` separators | ||||||||||||
| 288 | 0 | eval { | |||||||||||
| 289 | 0 | $reader = Text::FrontMatter::YAML->new( | |||||||||||
| 290 | document_string => $text | ||||||||||||
| 291 | ); | ||||||||||||
| 292 | 0 | $frontmatter = $reader->frontmatter_hashref; | |||||||||||
| 293 | 0 | ▶ | $markdown = $reader->data_text // ''; | ||||||||||
|
Condition0%0/2
$reader->data_text // ""
MC/DC0%0/1
$reader->data_text // ""
$reader->data_text
Truth table0%0/2
A
A= $reader->data_text
| |||||||||||||
| 294 | }; | ||||||||||||
| 295 | 0 | ▶ | push @errors, "Could not read YAML-frontmatter document: $@" if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 296 | |||||||||||||
| 297 | 0 | ▶ | return ($frontmatter, $markdown) unless @errors; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 298 | |||||||||||||
| 299 | # Default: assume the full contents are markdown, and there is | ||||||||||||
| 300 | # no frontmatter. | ||||||||||||
| 301 | 0 | _diag(join "\n ", "Assuming plain Markdown $fn --- errors were:", @errors); | |||||||||||
| 302 | |||||||||||||
| 303 | 0 | return ({}, $text); | |||||||||||
| 304 | } #_load() | ||||||||||||
| 305 | |||||||||||||
| 306 | # Normalize a syntax name into a key for %SYNTAXES | ||||||||||||
| 307 | 0 | ▶ | sub _normalize_syntax { | ||||||||||
|
Subroutine
_normalize_syntax: not called CC=1 SCAR=6.9
| |||||||||||||
| 308 | 0 | my $retval = lc(shift); | |||||||||||
| 309 | 0 | $retval =~ s/[^A-Za-z0-9]/_/g; | |||||||||||
| 310 | 0 | $retval =~ tr/_/_/s; | |||||||||||
| 311 | 0 | return $retval; | |||||||||||
| 312 | } | ||||||||||||
| 313 | |||||||||||||
| 314 | # Populates %SYNTAXES. Must be called before | ||||||||||||
| 315 | # _produce_output(). Called by unbelievable(). | ||||||||||||
| 316 | 0 | ▶ | sub _initialize { | ||||||||||
|
Subroutine
_initialize: not called CC=4 SCAR=30.0
| |||||||||||||
| 317 | 0 | foreach(keys %INC) { | |||||||||||
| 318 | 0 | ▶ | next unless m{Syntax/Highlight/Engine/Kate/([^\.]+).pm$}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 319 | 0 | $SYNTAXES{_normalize_syntax($1)} = $1; | |||||||||||
| 320 | } | ||||||||||||
| 321 | 0 | ▶ | say "Syntaxes:\n", Dumper(\%SYNTAXES) if $VERBOSE >= 2; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 322 | } #_initialize() | ||||||||||||
| 323 | |||||||||||||
| 324 | # Syntax-highlight text | ||||||||||||
| 325 | 0 | ▶ | sub _highlight { | ||||||||||
|
Subroutine
_highlight: not called CC=1 SCAR=6.9
| |||||||||||||
| 326 | 0 | my ($lang, $text) = @_; | |||||||||||
| 327 | |||||||||||||
| 328 | # Make the highlighter | ||||||||||||
| 329 | my $hl = Syntax::Highlight::Engine::Kate->new( | ||||||||||||
| 330 | 0 | language => $SYNTAXES{$lang}, | |||||||||||
| 331 | |||||||||||||
| 332 | substitutions => { | ||||||||||||
| 333 | "<" => "<", | ||||||||||||
| 334 | ">" => ">", | ||||||||||||
| 335 | "&" => "&", | ||||||||||||
| 336 | " " => " ", | ||||||||||||
| 337 | "\t" => " ", | ||||||||||||
| 338 | "\n" => "<BR>\n", | ||||||||||||
| 339 | }, | ||||||||||||
| 340 | |||||||||||||
| 341 | format_table => { | ||||||||||||
| 342 | Alert => [ "<font color=\"#0000ff\">", "</font>" ], | ||||||||||||
| 343 | BaseN => [ "<font color=\"#007f00\">", "</font>" ], | ||||||||||||
| 344 | BString => [ "<font color=\"#c9a7ff\">", "</font>" ], | ||||||||||||
| 345 | Char => [ "<font color=\"#ff00ff\">", "</font>" ], | ||||||||||||
| 346 | Comment => [ "<font color=\"#7f7f7f\"><i>", "</i></font>" ], | ||||||||||||
| 347 | DataType => [ "<font color=\"#0000ff\">", "</font>" ], | ||||||||||||
| 348 | DecVal => [ "<font color=\"#00007f\">", "</font>" ], | ||||||||||||
| 349 | Error => [ "<font color=\"#ff0000\"><b><i>", "</i></b></font>" ], | ||||||||||||
| 350 | Float => [ "<font color=\"#00007f\">", "</font>" ], | ||||||||||||
| 351 | Function => [ "<font color=\"#007f00\">", "</font>" ], | ||||||||||||
| 352 | IString => [ "<font color=\"#ff0000\">", "" ], | ||||||||||||
| 353 | Keyword => [ "<b>", "</b>" ], | ||||||||||||
| 354 | Normal => [ "", "" ], | ||||||||||||
| 355 | Operator => [ "<font color=\"#ffa500\">", "</font>" ], | ||||||||||||
| 356 | Others => [ "<font color=\"#b03060\">", "</font>" ], | ||||||||||||
| 357 | RegionMarker => [ "<font color=\"#96b9ff\"><i>", "</i></font>" ], | ||||||||||||
| 358 | Reserved => [ "<font color=\"#9b30ff\"><b>", "</b></font>" ], | ||||||||||||
| 359 | String => [ "<font color=\"#ff0000\">", "</font>" ], | ||||||||||||
| 360 | Variable => [ "<font color=\"#0000ff\"><b>", "</b></font>" ], | ||||||||||||
| 361 | Warning => [ "<font color=\"#0000ff\"><b><i>", "</b></i></font>" ], | ||||||||||||
| 362 | }, | ||||||||||||
| 363 | ); | ||||||||||||
| 364 | |||||||||||||
| 365 | 0 | return '<div>' . $hl->highlightText($text) . '</div>'; | |||||||||||
| 366 | } #_make_highlighter() | ||||||||||||
| 367 | |||||||||||||
| 368 | # }}}1 | ||||||||||||
| 369 | 1 | 1; | |||||||||||
| 370 | __END__ | ||||||||||||