App-Mowyw

 view release on metacpan or  search on metacpan

Changelog  view on Meta::CPAN

	* lib/Mowyw/Datasource/Result.pm: First shot at a more general result set
	  framework. Not yet used by the rest of the program.
mowyw (0.5.1)
	* mowyw: make mowyw encoding aware, added --encoding option. Default is
	  utf-8
mowyw (0.5.0)
	* mowyw: Moved nearly all functionally to a module to allow better testing
	* mowyw.1.txt: structured documentation in the asciidoc format
	* t/: many more new test files
	* lib/Mowyw.pm: added [% ifvar %] tag
	* lib/Mowyw.pm: added [% bind %] and [% for %] tags
	* lib/Mowyw/Datasource.pm: Added generic datasource interface
	* lib/Mowyw/Datasource/DBI.pm: DBI based SQL backend
	* lib/Mowyw/Datasource/XML.pm: XML::Simple based XML backend
	* Build.PL: install helper script
mowyw (0.4.1)
	* mowyw: Added support for a global include file (includes/global)
	* mowyw: Reads config file, and modifies include filenames according to
	that
mowyw (0.4.0)
	* mowyw: Added support for new delimiters: [% ... %] instead of [[[

example/source/index.shtml  view on Meta::CPAN

$marker%] should be treated as plain text, e.g. tags inbetween should be
ignored: [%include foo%][%endverbatim foobar%].</p>

<h3>Global include file</h3>

<p>If the quotes are empty, the global include file has not been parsed
correctly: "[%readvar site_title%]"</p>

<h3>Datasource test</h3>

[% bind news type:xml file:news.xml root:news foo:'bar\' baz'%]

[% for i in news %]
    <h4>[% readvar i.date %]: <strong>[% readvar i.headline %]</strong></h4>
    <p>[% readvar i.msg %]</p>
[% endfor %]


</div>

[%menu nav home%]

lib/App/Mowyw.pm  view on Meta::CPAN

            |option
            |item
            |endverbatim
            |verbatim
            |comment
            |setvar
            |readvar
            |synatxfile
            |syntax
            |endsyntax
            |bind
            |for
            |endfor
            |ifvar
            |endifvar
            )/x                         ],
        [ 'TAG_END',        qr/\s*\]\]\]/],
        [ 'TAG_END',        qr/\s*\%\]/],
        [ 'BRACES_START',   qr/\{\{/],
        [ 'BRACES_END',     qr/\}\}/],
    );

lib/App/Mowyw.pm  view on Meta::CPAN

        $m->{ITEMS} = [];
        $m->{CURRENT_ITEM} = undef;
    }
    $m->{INSIDE_ITEM} = 1;
    my $str = $content . parse_tokens($tokens, $m);
    p_expect($tokens, "TAG_END", $meta);
    return $str;

}

sub p_bind {
    my ($tokens, $meta) = @_;
    my $contents = strip_ws(slurp_upto_token($tokens, 'TAG_END', $meta));
    my ($var, $rest) = split m/\s+/, $contents, 2;
    my $string = qr{(
         '[^'\\]*(?>\\.[^'\\]*)*'
        |"[^"\\]*(?>\\.[^"\\]*)*'
        |[^"']\S*
    )}x;
    my %options = parse_hash($rest, 'bind', $meta);

    if ($options{file}){
        $options{file} = get_include_filename('include', $options{file}, $meta->{FILES}->[-1]);
    }
    $meta->{VARS}{$var} = App::Mowyw::Datasource->new(\%options);

    return '';
}

sub p_for {

lib/App/Mowyw.pm  view on Meta::CPAN

            }

            my %dispatch = (
                include     => \&p_include,
                system      => \&p_system,
                menu        => \&p_menu,
                item        => \&p_item,
                option      => \&p_option,
                verbatim    => \&p_verbatim,
                endverbatim => $error_sub->(qw(endverbatim verbatim)),
                bind        => \&p_bind,
                comment     => \&p_comment,
                setvar      => \&p_setvar,
                readvar     => \&p_readvar,
                syntax      => \&p_syntax,
                syntaxfile  => \&p_syntaxfile,
                endsyntax   => $error_sub->(qw(endsyntax syntax)),
                for         => \&p_for,
                endfor      => $error_sub->(qw(endfor for)),
                ifvar       => \&p_ifvar,
                endifvar    => $error_sub->(qw(endifvar ifvar)),

script/mowyw  view on Meta::CPAN

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:

=over

=item

script/mowyw  view on Meta::CPAN


=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
header or footer files will be omitted.


=head1 DATA SOURCES

You can access external data sources by first C<bind>'ing a variable to a
data source, and then iterating with a C<for> loop over that source.

This is best illustrated with a short example.

File C<includes/news.xml>:
        
    <rootTag>
        <item>
            <headline>China buys Google</headline>
            <status>April's fool joke</stoke>

script/mowyw  view on Meta::CPAN

        <item>
            <headline>Perl and Python join forces: Larry Wall and Guido von
            Rossum announce 'parrot'</headline>
            <status>April's fool joke</stoke>
            <date>Very old</date>
        </item>
    </rootTag>

Now you can access the contents of this XML file in your source files:

    [% bind news_variable type:xml file:news.xml root:item %]
    [% comment and iterate over news_variable %]
    [% for i in news_variable %]
        <h2>Breaking news: [% readvar i.headline %]</h2>
        <p>Status: [% readvar i.status %]</p>
    [% endfor %]

Data sources are handled via plugins. Currently XML and DBI are supported.

The XML source is explained by the example above. The only additional option
is 'limit', which can be set to a positive number and which limits the number
of iterations. This plugin is quite limited in that the file structure always
has be the
same: one root tag that contains a list of secondary tags, each of which many
only contain distinct tags. Nested tags might work, but aren't officially
supported.

DBI is perls generic database interface. You can use it to access a database.
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:

t/datasource.t  view on Meta::CPAN

    }
}

BEGIN { use_ok('App::Mowyw', 'parse_str'); };

my %meta = ( VARS => {}, FILES => [qw(t/datasource.t)]);

$App::Mowyw::config{default}{include} = 't/';
$App::Mowyw::config{default}{postfix} = '';

is parse_str('[% bind a type:xml file:sample.xml root:item %]', \%meta),
    '',
    'bind returns empty string';

ok blessed $meta{VARS}{a}, 'Bound variable is a blessed ref';

my $reader = q{[% for i in a %]![%readvar i.foo%][%endfor%]};

is parse_str($reader, \%meta), '!bar1!bar2!bar3', 'retrieved data';
is parse_str($reader, \%meta), '!bar1!bar2!bar3', 'The same works again';



( run in 0.829 second using v1.01-cache-2.11-cpan-2398b32b56e )