home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / WikiFormat.pm < prev    next >
Encoding:
Perl POD Document  |  2003-08-04  |  22.2 KB  |  789 lines

  1. package Text::WikiFormat;
  2.  
  3. use strict;
  4.  
  5. use Carp;
  6. use URI;
  7. use URI::Escape;
  8.  
  9. use vars qw( $VERSION %tags $indent );
  10. $VERSION = 0.71;
  11.  
  12. $indent = qr/^(?:\t+|\s{4,})/;
  13. %tags = (
  14.     indent        => qr/^(?:\t+|\s{4,})/,
  15.     newline        => '<br />',
  16.     link        => \&make_html_link,
  17.     strong        => sub { "<strong>$_[0]</strong>" },
  18.     emphasized    => sub { "<em>$_[0]</em>" },
  19.     strong_tag  => qr/'''(.+?)'''/,
  20.     emphasized_tag => qr/''(.+?)''/,
  21.  
  22.     code        => [ '<pre><code>', "</code></pre>\n", '', "\n" ],
  23.     line        => [ '', "\n", '<hr />',  "\n" ],
  24.     paragraph    => [ '<p>', "</p>\n", '', "<br />\n", 1 ],
  25.     unordered    => [ "<ul>\n", "</ul>\n", '<li>', "</li>\n" ],
  26.     ordered        => [ "<ol>\n", "</ol>\n", 
  27.         sub { qq|<li value="$_[2]">|, $_[0], "</li>\n" } ],
  28.     header      => [ '', "\n", sub {
  29.         my $level = length $_[2];
  30.         return "<h$level>", format_line($_[3], @_[-2, -1]), "</h$level>\n" }
  31.     ],
  32.  
  33.     blocks        => {
  34.         ordered        => qr/^([\dA-Za-z]+)\.\s*/,
  35.         unordered    => qr/^\*\s*/,
  36.         code        => qr/  /,
  37.         header      => qr/^(=+) (.+) \1/,
  38.         paragraph   => qr/^/,
  39.         line        => qr/^-{4,}/,
  40.     },
  41.  
  42.     indented    => { map { $_ => 1 } qw( ordered unordered code )},
  43.     nests       => { map { $_ => 1 } qw( ordered unordered ) },
  44.  
  45.     blockorder               =>
  46.         [qw( header line ordered unordered code paragraph )],
  47.     extended_link_delimiters => [qw( [ ] )],
  48. );
  49.  
  50. sub process_args
  51. {
  52.     my $self = shift;
  53.     my $name = @_ == 1 ? shift : 'wikiformat';
  54.     return ( as => $name, @_ );
  55. }
  56.  
  57. sub default_opts
  58. {
  59.     my ($class, $args) = @_;
  60.  
  61.     my %defopts = ( implicit_links => 1, map { $_ => delete $args->{ $_ } }
  62.         qw( prefix extended implicit_links) );
  63.  
  64.     return %defopts;
  65. }
  66.  
  67. sub merge_hash
  68. {
  69.     my ($from, $to) = @_;
  70.     while (my ($key, $value) = each %$from)
  71.     {
  72.         if (UNIVERSAL::isa( $value, 'HASH' ))
  73.         {
  74.             $to->{$key} = {} unless defined $to->{$key};
  75.             merge_hash( $value, $to->{$key} );
  76.         }
  77.         else
  78.         {
  79.             $to->{$key} = $value;
  80.         }
  81.     }
  82. }
  83.  
  84. sub import
  85. {
  86.     my $class = shift;
  87.     return unless @_;
  88.  
  89.     my %args    = $class->process_args( @_ );
  90.     my %defopts = $class->default_opts( \%args );
  91.  
  92.     my $caller = caller();
  93.     my $name   = delete $args{as};
  94.  
  95.     no strict 'refs';
  96.     *{ $caller . "::$name" } = sub {
  97.         my ($text, $tags, $opts) = @_;
  98.  
  99.         $tags ||= {};
  100.         $opts ||= {};
  101.  
  102.         my %tags = %args;
  103.         merge_hash( $tags, \%tags );
  104.         my %opts = %defopts;
  105.         merge_hash( $opts, \%opts );
  106.  
  107.         Text::WikiFormat::format( $text, \%tags, \%opts);
  108.     }
  109. }
  110.  
  111. sub format
  112. {
  113.     my ($text, $newtags, $opts) = @_;
  114.     $opts    ||= { prefix => '', extended => 0, implicit_links => 1 };
  115.     my %tags   = %tags;
  116.  
  117.     merge_hash( $newtags, \%tags )
  118.         if defined $newtags and UNIVERSAL::isa( $newtags, 'HASH' );
  119.     check_blocks( \%tags )
  120.         if exists $newtags->{blockorder} or exists $newtags->{blocks};
  121.  
  122.     my @blocks =  find_blocks( $text,     \%tags, $opts );
  123.     @blocks    = merge_blocks( \@blocks,  \%tags, $opts );
  124.     @blocks    =  nest_blocks( \@blocks,  \%tags, $opts );
  125.     return     process_blocks( \@blocks,  \%tags, $opts );
  126. }
  127.  
  128. sub check_blocks
  129. {
  130.     my $tags   = shift;
  131.     my %blocks = %{ $tags->{blocks} };
  132.     delete @blocks{ @{ $tags->{blockorder} } };
  133.     Carp::carp(
  134.         "No order specified for blocks '" . join(', ', keys %blocks ) . "'\n")
  135.         if keys %blocks;
  136. }
  137.  
  138. sub find_blocks
  139. {
  140.     my ($text, $tags, $opts) = @_;
  141.  
  142.     my @blocks;
  143.     for my $line ( split(/\n/, $text) )
  144.     {
  145.         my $block = start_block( $line, $tags, $opts );
  146.         push @blocks, $block if $block;
  147.     }
  148.  
  149.     return @blocks;
  150. }
  151.  
  152. sub start_block
  153. {
  154.     my ($text, $tags, $opts) = @_;
  155.     return { type => 'end', level => 0 } unless $text;
  156.  
  157.     for my $block (@{ $tags->{blockorder} })
  158.     {
  159.         my ($line, $level) = ( $text, 0 );
  160.         if ($tags->{indented}{$block})
  161.         {
  162.             ($level, $line) = get_indentation( $tags, $line );
  163.             next unless $level;
  164.         }
  165.  
  166.         next unless $line =~ /$tags->{blocks}{$block}/;
  167.         $level = 0 if $block eq 'code';
  168.         
  169.         $line =~ s/$tags->{blocks}{$block}//;
  170.         return {
  171.             args => [ grep { defined } $1, $2, $3, $4, $5, $6, $7, $8, $9 ],
  172.             type => $block,
  173.             text => $block eq 'code' ? $line : format_line($line, $tags, $opts),
  174.             level=> $level || 0,
  175.         };
  176.     }
  177. }
  178.  
  179. sub merge_blocks
  180. {
  181.     my ($blocks, $tags, $opts) = @_;
  182.  
  183.     my @merged;
  184.     for my $block (@$blocks)
  185.     {
  186.         if (@merged and $block->{type} eq $merged[-1]{type}
  187.             and $block->{level} == $merged[-1]{level})
  188.         {
  189.             push @{ $merged[-1]{text} }, $block->{text};
  190.             push @{ $merged[-1]{args} }, $block->{args};
  191.             next;
  192.         }
  193.  
  194.         push @merged, {
  195.             text  => [ $block->{text} ],
  196.             type  => $block->{type},
  197.             level => $block->{level},
  198.             args  => [ $block->{args} ],
  199.         };
  200.     }
  201.  
  202.     return @merged;
  203. }
  204.  
  205. sub nest_blocks
  206. {
  207.     my ($blocks, $tags, $opts) = @_;
  208.     my @merged;
  209.  
  210.     for my $block (@$blocks)
  211.     {
  212.         if (@merged and $tags->{nests}{ $block->{type} }
  213.             and $tags->{nests}{ $merged[-1]{type} }
  214.             and $block->{level} > $merged[-1]{level})
  215.         {
  216.             push @{ $merged[-1]{text} }, $block;
  217.             next;
  218.         }
  219.         push @merged, $block;
  220.     }
  221.  
  222.     return @merged;
  223. }
  224.  
  225. sub process_blocks
  226. {
  227.     my ($blocks, $tags, $opts) = @_;
  228.  
  229.     my @open;
  230.     for my $block (@$blocks)
  231.     {
  232.         push @open, process_block( $block, $tags, $opts )
  233.             unless $block->{type} eq 'end';
  234.     }
  235.  
  236.     return join('', @open);
  237. }
  238.  
  239. sub process_block
  240. {
  241.     my ($block, $tags, $opts) = @_;
  242.  
  243.     my ($start, $end, $start_line, $end_line, $between)
  244.         = @{ $tags->{ $block->{type} } };
  245.  
  246.     my @text;
  247.  
  248.     for my $line (@{ $block->{text} })
  249.     {
  250.         if (UNIVERSAL::isa( $line, 'HASH' ))
  251.         {
  252.             my $prev_end = pop @text || ();
  253.             push @text, process_block( $line, $tags, $opts ), $prev_end;
  254.             next;
  255.         }
  256.  
  257.         if (UNIVERSAL::isa( $start_line, 'CODE' ))
  258.         {
  259.             (my $start_line, $line, $end_line) = 
  260.                 $start_line->( $line, $block->{level}, 
  261.                 @{ shift @{ $block->{args} } }, $tags, $opts);
  262.             push @text, $start_line, $line, $end_line;
  263.         }
  264.         else
  265.         {
  266.             push @text, $start_line, $line, $end_line;
  267.         }
  268.     }
  269.  
  270.     pop @text if $between;
  271.     return join('', $start, @text, $end);
  272. }
  273.  
  274. sub get_block
  275. {
  276.     my ($line, $tags, $opts) = @_;
  277.  
  278.     return 'header', $line if $line =~ /$tags->{blocks}{header}/;
  279.  
  280.     if ((my $level, $line) = get_indentation( $tags, $line ))
  281.     {
  282.         return 'list', $line, $level;
  283.     }
  284.  
  285.     return 'paragraph', $line;
  286. }
  287.  
  288. sub get_indentation
  289. {
  290.     my ($tags, $text) = @_;
  291.  
  292.     return 0, $text unless $text =~ s/($tags->{indent})//;
  293.     return length( $1 ) + 1, $text;
  294. }
  295.  
  296. sub end_all_lists
  297. {
  298.     my ($lists, $tags) = @_;
  299.     my $parsed = '';
  300.  
  301.     while (@$lists and $lists->[0]{level} == 0)
  302.     {
  303.         my $list = shift @$lists;
  304.         $parsed .= end_list( $list, $tags->{ $list->{list} } );
  305.     }
  306.  
  307.     while ( my $list = pop @$lists )
  308.     {
  309.         $parsed .= end_list( $list, $tags->{ $list->{list} } );
  310.     }
  311.  
  312.     return $parsed;
  313. }
  314.  
  315. sub find_list
  316. {
  317.     my ( $line, $list_types, $tags, $opts ) = @_;
  318.  
  319.     for my $list (@$list_types) {
  320.         my $regex = $tags->{lists}{$list};
  321.  
  322.         next unless $line =~ s/^$regex//;
  323.         
  324.         my @captures = map { defined $_ ? $_ : () }
  325.             $1, $2, $3, $4, $5, $6, $7, $8, $9;
  326.  
  327.         $line = format_line($line, $tags, $opts) unless $list eq 'code';
  328.  
  329.         return unless my $action = $tags->{$list};
  330.         my @formatted;
  331.  
  332.         if (@$action == 3)
  333.         {
  334.             my $subref = $action->[2];
  335.             if (defined $subref and defined &$subref)
  336.             {
  337.                 @formatted = $subref->($line, @captures);
  338.             }
  339.             else
  340.             {
  341.                 warn "Bad actions for list type '$list'\n";
  342.             }
  343.         }
  344.         else
  345.         {
  346.             @formatted = ( $action->[2], $line, $action->[3] );
  347.         }
  348.         return $list, @formatted;
  349.     }
  350. }
  351.  
  352. sub end_list
  353. {
  354.     my ($list, $tags) = @_;
  355.     return join('', grep { defined } @{ $list->{lines} }, $tags->[1]);
  356. }
  357.  
  358. sub format_line
  359. {
  360.     my ($text, $tags, $opts) = @_;
  361.     $opts ||= {};
  362.  
  363.     $text =~ s!$tags->{strong_tag}!$tags->{strong}->($1, $opts)!eg;
  364.     $text =~ s!$tags->{emphasized_tag}!$tags->{emphasized}->($1, $opts)!eg;
  365.  
  366.     $text = find_extended_links( $text, $tags, $opts ) if $opts->{extended};
  367.  
  368.     $text =~ s|(?<!["/>=])\b([A-Za-z]+(?:[A-Z]\w+)+)|
  369.               $tags->{link}->($1, $opts)|egx
  370.             if !defined $opts->{implicit_links} or $opts->{implicit_links};
  371.  
  372.     return $text;
  373. }
  374.  
  375. sub find_extended_links
  376. {
  377.     my ($text, $tags, $opts) = @_;
  378.  
  379.     my ($start, $end) = @{ $tags->{extended_link_delimiters} };
  380.  
  381.     my $position = 0;
  382.     while (1)
  383.     {
  384.         my $open       = index $text, $start, $position;
  385.         last if $open  == -1;
  386.         my $close      = index $text, $end, $open;
  387.         last if $close == -1;
  388.  
  389.         my $text_start = $open + length $start;
  390.         my $extended   = substr $text, $text_start, $close - $text_start;
  391.  
  392.         $extended  = $tags->{link}->( $extended, $opts );
  393.         substr $text, $open, $close - $open + length $end, $extended;
  394.         $position += length $extended;
  395.     };
  396.  
  397.     return $text;
  398. }
  399.  
  400. sub make_html_link
  401. {
  402.     my ($link, $opts) = @_;
  403.     $opts ||= {};
  404.     ($link, my $title) = find_link_title( $link, $opts );
  405.  
  406.     $link = escape_link( $link, $opts );
  407.  
  408.     my $prefix = defined $opts->{prefix} ? $opts->{prefix} : '';
  409.     return qq|<a href="$prefix$link">$title</a>|;
  410. }
  411.  
  412. sub escape_link
  413. {
  414.     my ($link, $opts) = @_;
  415.     return uri_escape( $link ) unless $opts->{absolute_links};
  416.         
  417.     my $u = URI->new( $link );
  418.     return $link if $u->scheme();
  419.  
  420.     return uri_escape( $link );
  421. }
  422.  
  423. sub find_link_title
  424. {
  425.     my ($link, $opts)    = @_;
  426.     my $title;
  427.     ($link, $title)      = split(/\|/, $link, 2) if $opts->{extended};
  428.     $title             ||= $link;
  429.  
  430.     return $link, $title;
  431. }
  432.  
  433. 'shamelessly adapted from the Jellybean project';
  434.  
  435. __END__
  436.  
  437. =head1 NAME
  438.  
  439. Text::WikiFormat - module for translating Wiki formatted text into other formats
  440.  
  441. =head1 SYNOPSIS
  442.  
  443.     use Text::WikiFormat;
  444.     my $html = Text::WikiFormat::format($raw);
  445.  
  446. =head1 DESCRIPTION
  447.  
  448. The original Wiki web site was intended to have a very simple interface to
  449. edit and to add pages.  Its formatting rules are simple and easy to use.  They
  450. are also easily translated into other, more complicated markup languages with
  451. this module.  It creates HTML by default, but can be extended to produce valid
  452. POD, DocBook, XML, or any other format imaginable.
  453.  
  454. The most important function is C<format()>.  It is not exported by default.
  455.  
  456. =head2 format()
  457.  
  458. C<format()> takes one required argument, the text to convert, and returns the
  459. converted text.  It allows two optional arguments.  The first is a reference to
  460. a hash of tags.  Anything passed in here will override the default tag
  461. behavior.  These tags are described later.  The second argument is a hash
  462. reference of options.  There are currently limited to:
  463.  
  464. =over 4
  465.  
  466. =item * prefix
  467.  
  468. The prefix of any links.  In HTML mode, this is the path to the Wiki.  The
  469. actual linked item itself will be appended to the prefix.  This is used to
  470. create full URIs:
  471.  
  472.     { prefix => 'http://example.com/wiki.pl?page=' }
  473.  
  474. =item * extended
  475.  
  476. A boolean flag, false by default, to use extended linking semantics.  This is
  477. stolen from the Everything Engine (L<http://everydevel.com/>), where links are
  478. marked by square brackets.  An optional title may occur after the link target,
  479. preceded by an open pipe.  That is to say, these are valid extended
  480. links:
  481.  
  482.     [a valid link]
  483.     [link|title]
  484.  
  485. Where the linking semantics of the destination format allow it, the title will
  486. be displayed instead of the URI.  In HTML terms, the title is the content of an
  487. A element (not the content of its HREF attribute).
  488.  
  489. You can use delimiters other than single square brackets for marking extended
  490. links, by passing a value for C<extended_link_delimiters> in the C<%tags> hash
  491. when calling C<format> (see below for details).
  492.  
  493. =item * implicit_links
  494.  
  495. A boolean flag, true by default, to cause links to be created wherever a
  496. StudlyCapsString is seen.  Note that if you disable this flag, you'll most
  497. likely be wanting to enable the C<extended> one also, or there will be no way
  498. of creating links in your documents.  To disable it, use the pair:
  499.  
  500.     { implicit_links => 0 }
  501.  
  502. =item * absolute_links
  503.  
  504. A boolean flag, false by default, which treats any links that are absolute URIs
  505. (such as http://www.cpan.org/) to be treated specially. Any prefix will not
  506. apply and the URIs aren't quoted. Must be used in conjunction with the
  507. C<extended> option for the link to be detected.
  508.  
  509. =head2 Wiki Format
  510.  
  511. Wiki formatting is very simple.  An item wrapped in three single quotes is
  512. marked as B<strong>.  An item wrapped in two single quotes is marked as
  513. I<emphasized>.  Any word with multiple CapitalLetters (e. g., StudlyCaps) will
  514. be turned into a link.  Four or more hyphen characters at the start of a line
  515. create a horizontal line.  Newlines are translated into the appropriate tag.
  516. Headers are marked with matching equals signs around the header text -- the
  517. more signs, the lesser the header.
  518.  
  519. Lists are indented by one tab or four spaces, by default.  Indentation may be
  520. disabled.  Lists can be unordered, where each item has its own bullet point.
  521. These are marked by a leading asterisk and space.  They can also be ordered,
  522. where any combination of one or more alphanumeric characters can be followed by
  523. a period and an optional space.  Any indented text without either marking is
  524. considered to be code, and is handled literally.  Lists can be nested.
  525.  
  526. The following is valid Wiki formatting, with an extended link as marked.
  527.  
  528.     = my interesting text =
  529.  
  530.     ANormalLink
  531.     [let the Sun shine|AnExtendedLink]
  532.  
  533.     == my interesting lists ==
  534.  
  535.         * unordered one
  536.         * unordered two
  537.  
  538.         1. ordered one
  539.         2. ordered two
  540.             a. nested one
  541.             b. nested two
  542.  
  543.         code one
  544.         code two
  545.  
  546.     The first line of a normal paragraph.
  547.     The second line of a normal paragraph.  Whee.
  548.  
  549. =head1 EXPORT
  550.  
  551. If you'd like to make your life more convenient, you can optionally import a
  552. subroutine that already has default tags and options set up.  This is
  553. especially handy if you will be using a prefix:
  554.  
  555.     use Text::WikiFormat prefix => 'http://www.example.com/';
  556.     wikiformat( 'some text' );
  557.  
  558. tags are interpreted as, well, tags, except for four special keys:
  559.  
  560. =over 4
  561.  
  562. =item * C<prefix>, interpreted as a link prefix
  563.  
  564. =item * C<extended>, interpreted as the extended link flag
  565.  
  566. =item * C<implicit_links>, interpreted as the flag to control implicit links
  567.  
  568. =item * C<as>, interpreted as an alias for the imported function
  569.  
  570. =back
  571.  
  572. Use the C<as> flag to control the name by which the imported function is
  573. called.  For example,
  574.  
  575.     use Text::WikiFormat as => 'formatTextInWikiStyle';
  576.     formatTextInWikiStyle( 'some text' );
  577.  
  578. You might choose a better name, though.
  579.  
  580. The calling semantics are effectively the same as those of the format()
  581. function.  Any additional tags or options to the imported function will
  582. override the defaults.  In this example:
  583.  
  584.     use Text::WikiFormat as => 'wf', extended => 0;
  585.     wf( 'some text', {}, { extended => 1 });
  586.  
  587. extended links will be enabled, though the default is to disable them.
  588.  
  589. This feature was suggested by Tony Bowden E<lt>tony@kasei.comE<gt>, but all
  590. implementation blame rests solely with me.  Kate L Pugh
  591. (E<lt>kake@earth.liE<gt>) pointed out that it didn't work, with tests, so it's
  592. fixed now.
  593.  
  594. =head1 GORY DETAILS
  595.  
  596. =head2 Tags
  597.  
  598. There are two types of Wiki markup: line items and blocks.  Blocks include
  599. lists, which are made up of lines and can also contain other lists.
  600.  
  601. =head3 Line items
  602.  
  603. There are two classes of line items: simple tags, and tags that contain data.
  604. The simple tags are C<newline> and C<line>.  A newline is inserted whenever a
  605. newline character (C<\n>) is encountered.  A line is inserted whenever four or
  606. more dash characters (C<---->) occur at the start of a line.  No whitespace is
  607. allowed.  These default to the E<lt>brE<gt> and E<lt>hrE<gt> HTML tags,
  608. respectively.  To override either, simply pass tags such as:
  609.  
  610.     my $html = format($text, { newline => "\n" });
  611.  
  612. The three line items are more complex, and require subroutine references. This
  613. category includes the C<strong> and C<emphasized> tags as well as C<link>s.
  614. The first argument passed to the subref will be the data found in between the
  615. marks.  The second argument is the $opts hash reference.  The default action
  616. for a strong tag can be reimplemented with this syntax:
  617.  
  618.     my $html = format($text, { strong => sub { "<b>$_[0]</b>" } });
  619.  
  620. As of version 0.70, you can change the regular expressions used to find strong
  621. and emphasized tags:
  622.  
  623.     %tags = (
  624.         strong_tag     => qr/\*(.+?)\*/,
  625.         emphasized_tag => qr|(?<!<)/(.+?)/|,
  626.     );
  627.  
  628.     $wikitext = 'this is *strong*, /emphasized/, and */emphasized strong/*';
  629.     $htmltext = Text::WikiFormat::format( $wikitext, \%tags, {} );
  630.  
  631. Be aware that using forward slashes to mark anything leads to the hairy regular
  632. expression -- use something else.  B<This interface is experimental> and may
  633. change if I find something better.  It's nice to be able to override those
  634. tags, though.
  635.  
  636. Finally, there are C<extended_link_delimiters>, which allow you to use
  637. delimiters other than single square brackets for marking extended links.  Pass
  638. the tags as:
  639.  
  640.     my $html = format( $text, { extended_link_delimiters => [ '[[', ']]' ] });
  641.  
  642. This will allow you to use double square brackets as UseMod supports:
  643.  
  644.     [[an extended link]]
  645.     [[a titled extended link|title]]
  646.  
  647. =head3 Blocks
  648.  
  649. Five block types are recognized by default:  C<paragraph>, C<header>, C<code>,
  650. C<unordered>, and C<ordered>.  Each of these is I<usually> marked by
  651. indentation, either one or more tabs or four or more whitespace characters.
  652. (This does not include newlines, however.)  Any line that does not fall in any
  653. of these three categories is automatically put in a C<paragraph> list.
  654.  
  655. Lists (code, unordered, and ordered blocks) are usually marked by indentation.
  656. This is not required, however, it's used to mark nesting.  Be careful.  To mark
  657. a block as requiring indentation, use the C<indented> tag, which contains a
  658. reference to a hash:
  659.  
  660.     my $html = format($text, { 
  661.         indented    => { map { $_ => 1 } qw( ordered unordered code )}
  662.     });
  663.  
  664. Block entries in the tag hashes must contain array references.  The first two
  665. items are the tags used at the start and end of the block.  As you'd expect,
  666. the last items contain the tags used at the start and end of each line.  Where
  667. there needs to be more processing of individual lines, use a subref as the
  668. third item.  This is how ordered lines are numbered in HTML lists:
  669.  
  670.     my $html = format($text, { ordered => [ '<ol>', "</ol>\n",
  671.         sub { qq|<li value="$_[2]">$_[0]</li>\n| } ] });
  672.  
  673. The first argument to these subrefs is the text of the line itself, after it
  674. has been processed.  (The indentation and tokens used to mark this as a list
  675. item are removed, and the rest of the line is checked for other line
  676. formattings.)  The second argument is the indentation level.  The subsequent
  677. arguments are captured variables in the regular expression used to find this
  678. list type.  The regexp for ordered lists is:
  679.  
  680.     qr/^([\dA-Za-z]+)\.\s*/;
  681.  
  682. Indentation is processed first, if applicable, and the indentation level (the
  683. length of the indentation removed) is stored.  The line must contain one or
  684. more alphanumeric character followed by a single period and optional whitespace
  685. to be identified as an ordered list item.  The contents of this last group, the
  686. value of the list item, is saved, and will be passed to the subref as the third
  687. argument.
  688.  
  689. Lists are automatically started and ended as necessary.
  690.  
  691. Because of the indentation issue, blocks must be processed in a specific order.
  692. The C<blockorder> tag governs this order.  It contains a reference to an array
  693. of the names of the appropriate blocks to process.  If you add a block type, be
  694. sure to add an entry for it in C<blockorder>:
  695.  
  696.     my $html = format($text, {
  697.         escaped       => [ '', '', '', '' ],
  698.         blocks        => {
  699.             invisible => qr!^--(.*?)--$!,
  700.         },
  701.         blockorder    =>
  702.             [qw( header line ordered unordered code paragraph invisible )],
  703.     });
  704.  
  705. =head3 Finding blocks
  706.  
  707. Text::WikiFormat uses regular expressions to find blocks.  These are kept in
  708. the %tags hash, under the C<blocks> key.  To change the regular expression to
  709. find code block items, use:
  710.  
  711.     my $html     =  format($wikitext, {
  712.         blocks   => { 
  713.             code => qr/^:\s+/,
  714.         },
  715.         indented => {
  716.             code => 1,
  717.         },
  718.     );
  719.  
  720. This will require indentation and a colon to mark code lines.  A potential
  721. shortcut is to use the C<indent> tag to match or to change the indentation
  722. marker.  
  723.  
  724. B<Note>: if you want to mark a block type as non-indented, you B<cannot> use an
  725. empty regex such as C<qr//>.  Use a mostly-empty, always-true regex such as
  726. C<qr/^/> instead.
  727.  
  728. =head3 Finding Blocks in the Correct Order
  729.  
  730. As intrepid bug reporter Tom Hukins pointed out in CPAN RT bug #671, the order
  731. in which Text::WikiFormat searches for blocks varies by platform and version of
  732. Perl.  Because some block-finding regular expressions are more specific than
  733. others, what's intended to be one type of block may be caught by a different
  734. list type.
  735.  
  736. If you're adding new block types, be aware of this.  The C<blockorder> entry in
  737. C<%tags> exists to force Text::WikiFormat to apply its regexes from most
  738. specific to least specific.  It contains an array reference.  By default, it
  739. looks for ordered lists first, unordered lists second, and code references at
  740. the end.
  741.  
  742. =back
  743.  
  744. =head1 AUTHOR
  745.  
  746. chromatic, C<chromatic@wgz.org>, with much input from the Jellybean team
  747. (including Jonathan Paulett).  Kate L Pugh has also provided several patches,
  748. many failing tests, and is usually the driving force behind new features and
  749. releases.  If you think this module is worth buying me a beer, she deserves at
  750. least half of it.  
  751.  
  752. Tony Bowden, Tom Hukins, and Andy H. all suggested useful features that are now
  753. implemented.  
  754.  
  755. Sam Vilain found a silly bug.
  756.  
  757. Blame me for the implementation.
  758.  
  759. =head1 BUGS
  760.  
  761. The link checker in C<format_line()> may fail to detect existing links that do
  762. not follow HTML, XML, or SGML style.  They may die with some SGML styles too.
  763. I<Sic transit gloria mundi>.
  764.  
  765. =head1 TODO
  766.  
  767. =over 4
  768.  
  769. =item * Find a nicer way to mark list as having unformatted lines
  770.  
  771. =item * Optimize C<format_line()> to work on a list of lines
  772.  
  773. =item * Handle nested C<strong> and C<emphasized> markings better
  774.  
  775. =back
  776.  
  777. =head1 OTHER MODULES
  778.  
  779. Brian "Ingy" Ingerson's CGI::Kwiki has a fairly nice parser.
  780.  
  781. John McNamara's Pod::Simple::Wiki looks like a good project.
  782.  
  783. Matt Sergeant keeps threatening to write a nice SAX-throwing Wiki formatter.
  784.  
  785. =head1 COPYRIGHT
  786.  
  787. Copyright (c) 2002 - 2003, chromatic.  All rights reserved.  This module is
  788. distributed under the same terms as Perl itself.
  789.