Text::Markdown::Parser -- parse Markdown
use strict;
use warnings;
use Text::Markdown::Parser;
my $parse=Text::Markdown::Parser->new(grep_exessive=>0);
my $end=0;
my $deep=0;
$parse->add_handler('start',sub{
print "\n"," "x$deep,'>>>',shift(),"\n";
$deep+=2;
$end=1;
return 1;
},
'type');
$parse->add_handler('text',sub{
my $txt=shift;
$txt=~s/\n/"\n".(" "x$deep).'|'/gse;
if($end)
{
$txt=(" "x$deep).'|'.$txt;
$end=0;
}
print $txt;
return 1;
},
'text');
$parse->add_handler('end',sub{
my $type=shift;
my $text=shift;
if($text && ref($text))
{
$text="(".join('|',@$text).")";
$text=~s/\n/\\n/gs;
}
else
{ $text='' }
$deep-=2;
$end=1;
print "\n"," "x$deep,"<<<$type $text\n";
return 1;
},'type,text');
$parse->parse(<<'EOT');
AT&T has an ampersand in their name.
=====================================
AT&T is another way to write it.
------------------------------------
EOT
Parser for markdown formated Documents
ToPeG
create a new Parser Object
returns a Text::Markdown::Parser Object
tabwidth => <int> (default 4)set the amount of spaces for one tab
grep_exessive => 1/0 (default 0)if this option is true, linlinehtml will gab as mutch as possible text
parse data from a File or Filehandle
parse text
add a link definition.
Add an handler.
returnes the name of the handler in the Stack
whenever an formating-tag will be started, or text added, or finished, the specified handler will be executed
Allowed types: start, text, end
$param is an string of comma separated valued, who specifed witch values will be pased to the codereference.
Allowed:
self - the parser object
text - the text inside the tag (empty in "start", a String in "text" and an array in "end")
url - an URL set by links or images
alt - alternatibe text for images
title - title of an link or image
id - id of a link definition
type - type of the Tag
The following strings of types will be pasted to the coderefence:
list_plain - a dotted list
list_count - a counted list
list_entry - list entry
h[1..6] - heads 1 .. 6 (param: self text)
hr - line (possible params: self)
link - link (possible params: self text url title)
linkref - link (possible params: self text url title id)
link_def - link (possible params: self url title id)
image - image (possible params: self url title alt)
imageref - image (possible params: self url title alt id)
page - page (possible params: self text)
code - codeblock (possible params: self text)
inlinecode - inlinecode (possible params: self text)
html - htmlblock (possible params: self text)
inlinehtml - inlinehtml (possible params: self text)
quote - quoted text (possible params: self text)
strong - strog (possible params: self text)
em - em (possible params: self text)
break - linebreak (possible params: self)
comment - html-comment (possible params: self text)
the code refence has to return true '1'. If false (0) returned the handler will be ermoved from the handlerstack
the last added handler will be executed first.
delete handler by id.
To prevent doubleparsing the text the linkdefinitions are captured by an regexp when the parsing beginns. but there is no easy way to find _all_ linkdefinitions without doubleparsing, so it's possible not all link-refences will be found.
- optional doubeparsing to find all linkdefinitions
- optional warnings
- implement an easy way to extend the parser
Tobias Grönhagen <markdown@topeg.de>
Version 0.1 1 January 2011
Copyright (c) Tobias Grönhagen. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms and conditions as Perl itself.
This means that you can, at your option, redistribute it and/or modify it under either the terms the GNU Public License (GPL) version 2 or later, or under the Perl Artistic License.
See http://dev.perl.org/licenses/
THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Use of this software in any way or in any form, source or binary, is not allowed in any country which prohibits disclaimers of any implied warranties of merchantability or fitness for a particular purpose or any disclaimers of a similar nature.
IN NO EVENT SHALL I BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION (INCLUDING, BUT NOT LIMITED TO, LOST PROFITS) EVEN IF I HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE