App-Mowyw
view release on metacpan or search on metacpan
<pre>[% syntax perl %]
#!/usr/bin/perl
sub blubb {
print "This sub only prints this stupid message\n";
}
[% endsyntax %]pre>
If you don't have Text::VimColor installed, the characters '&', '<' and '>'
will still be automatically escaped.
The only argument to 'syntax' is the language that the code is in, if you use
a value that vim doesn't know it will try to guess the language.
The standard CSS classes are:
.synComment { color: #0000FF }
.synConstant { color: #FF00FF }
.synIdentifier { color: #008B8B }
.synStatement { color: #A52A2A ; font-weight: bold }
.synPreProc { color: #A020F0 }
lib/App/Mowyw.pm view on Meta::CPAN
}
# strip leading and trailing whitespaces from a string
sub strip_ws {
my $s = shift;
$s =~ s/^\s+//g;
$s =~ s/\s+$//g;
return $s;
}
sub escape {
my $str = shift;
my %esc = (
"\\" => '\\\\',
"\t" => '\t',
"\n" => '\n',
);
my $re = join '|', map quotemeta, keys %esc;
$str =~ s/($re)/$esc{$1}/g;
return $str;
}
sub parse_error {
my $message = shift;
my @filenames = @{shift()};
my $token = shift;
my $str = "Parse error in file '$filenames[0]': $message\n";
if ($token) {
$str .= "in line $token->[3] near'" . escape($token->[0]) ."'\n";
}
for (@filenames[0..$#filenames]) {
$str .= " ...included from file '$_'\n";
}
confess $str;
exit 1;
}
# parse sub: anything is treated as normal text that does not start or end a
# command
lib/App/Mowyw.pm view on Meta::CPAN
return "";
}
sub p_readvar {
my ($tokens, $meta) = @_;
my $str = strip_ws(slurp_upto_token($tokens, 'TAG_END', $meta));
my ($name, $rest) = split m/\s+/, $str, 2;
my %options = parse_hash($rest, 'readvar', $meta);
my $c = resolve_var($name, $meta);
if (defined $options{escape} && lc $options{escape} eq 'html'){
return encode_entities($c);
}
return $c if defined $c;
return '';
}
sub p_syntaxfile {
my $tokens = shift;
my $meta = shift;
my $tag_content = shift @$tokens;
lib/App/Mowyw.pm view on Meta::CPAN
}
p_expect($tokens, "TAG_START", $meta);
p_expect($tokens, "KEYWORD", $meta);
p_expect($tokens, "TAG_END", $meta);
return do_hilight($str, $lang, $meta);
}
sub do_hilight {
my ($str, $lang, $meta) = @_;
if ($lang eq 'escape'){
return encode_entities($str);
}
eval {
no warnings "all";
require Text::VimColor;
};
if ($@){
# require was not successfull
print STDERR " Not syntax hilighting, Text::VimColor not found\n" unless $config{quiet};
# encode at least some special chars "by hand"
script/mowyw view on Meta::CPAN
C<comment> ignores everything up to the tag end. Example: C<[% comment this
comments appears nowhere in the output %]>.
=item
C<setvar> sets a variable. Example: C<[% setvar title Title of this page%]>.
=item
C<readvar> reads and outputs a variable. You can optionally specify an
escape mechanism. Example: C<< <h1>[% readvar title %]</h1> >> or
C<< [% readvar db.column escape:html %] >>.
=item
C<bind> binds a variable to a datasource. See section L</DATA SOURCES> below.
=back
The following block elements are supported:
script/mowyw view on Meta::CPAN
marker, which must be present at the end tag as well. Everything inbetween
is taken verbatimly, i.e. it is not processed at all. Keywords are not
allowed as markers. Example:
C<[% verbatim foo %] Some [% non-processed %] markup [% endverbatim foo %]>
=item
C<syntax> uses vim(1) to do syntax hilighting. Expects the name of the
language or configuration as its only argument. Example: `<pre>[%syntax
perl%]print "Hello, world\n";[%endsyntax%]</pre>`. The pseudo-syntax
C<escape> HTML-escapes the string and doesn't to any other syntax
hilighting.
=item
C<for> iterates of a datasource. See section <<datasource,DATA SOURCES>>
below.
=item
C<ifvar> executes the block only if the variable is defined.
script/mowyw view on Meta::CPAN
C<[% menu sec subitem1 %]>.
It's best to take a look at the examples in the distribution, which should
nicely illustrate the menu mechanism.
=head1 SYNTAX HILIGHTING
Syntax hilighting requires vim (see L<http://www.vim.org/>) and
L<Text::VimColor> to be installed
(otherwise the code is just HTML escaped, not hilighted).
Since you can't tell vim which encoding a source file is in,
non-ASCII-characters might not survive the round trip to vim if the locales
don't fit. On my systems UTF-8 locales worked, everything else didn't. So use
with caution.
=head1 OPTIONS
Currently only two options are supported, C<no-header> and C<no-footer>.
If they are set in a file via C<[% option no-header %]>, the inclusion of
script/mowyw view on Meta::CPAN
This has some limitations, for example you can't reuse database connections,
so every C<bind> statement actually opens a database connection on its own.
For the brave, here is an example of how to use it:
[% bind my_db type:dbi dsn:DBI:mysql:database=yourdatabse;host=dbhost
username:your_db_user password:you_db_password encoding:latin1
sql:'SELECT headline, status FROM news LIMIT 10'
%]
[% for i in my_db %]
<h2>Breaking news: [% readvar i.headline escape:html %]</h2>
<p>Status: [% readvar i.status escape:html %]</p>
[% endfor %]
The options are as follows:
=over
=item
The C<dsn> option is the "data source name" that C<DBI>'s C<connect> method
accepts. It always starts with C<DBI:>, then followed by the driver name
no warnings 'once';
$App::Mowyw::Quiet = 1;
{
# Test without Vim::TextColor first
local @INC = ();
local $App::Mowyw::config{quiet} = 1;
my $ps = parse_str('[% syntax foobar %]<argl>[% endsyntax %]', {});
ok $ps !~ m/<argl>/, 'Syntax hilighting at least escapes';
}
{
my $ps = parse_str('[% syntax foobar %]<argl>[% endsyntax %]', {});
ok $ps !~ m/<argl>/, 'Syntax hilighting at least escapes';
}
t/variables.t view on Meta::CPAN
my %meta = ( VARS => {} );
is parse_str('[%setvar foo bar baz %]', \%meta),
'',
'setvar returns empty string';
is parse_str('[%readvar foo%]', \%meta),
'bar baz',
'readvar returns previous value';
is parse_str('[%setvar a <>&"a%][%readvar a escape:html%]', \%meta),
'<>&"a',
'HTML escape';
is parse_str('[%ifvar foo%]bar[%endifvar%]', \%meta),
'bar',
'ifvar works on defined variables';
is parse_str('[%ifvar undef%]bar[%readvar argl %][%endifvar%]', \%meta),
'',
'ifvar works on undefined variables';
%meta = ( VARS => { foo => { bar => 'baz' } } );
( run in 0.444 second using v1.01-cache-2.11-cpan-c21f80fb71c )