Compare commits

...

1 Commits

Author SHA1 Message Date
  Masayoshi Sekimura c40d3eb7db support MKD_NOPANTS and MKD_NOHTML options 12 years ago
5 changed files with 147 additions and 4 deletions
Split View
  1. +7
    -2
      Discount.xs
  2. +47
    -2
      lib/Text/Markdown/Discount.pm
  3. +23
    -0
      t/01.simple.t
  4. +23
    -0
      t/02.oo.t
  5. +47
    -0
      t/03.oo-flags.t

+ 7
- 2
Discount.xs View File

@ -42,15 +42,20 @@ MODULE = Text::Markdown::Discount PACKAGE = Text::Markdown::Discount PREFIX = T
PROTOTYPES: DISABLE
SV *
TextMarkdown__markdown(text)
TextMarkdown__markdown(text, uflags)
char *text;
int uflags;
PREINIT:
SV* r = &PL_sv_undef;
int flags = MKD_NOHEADER|MKD_NOPANTS;
int flags = MKD_TABSTOP | MKD_NOHEADER;
char *html = NULL;
int szhtml;
Document *doc;
CODE:
if ( uflags ) {
flags |= uflags;
}
if ( (doc = mkd_string(text, strlen(text), flags)) == 0 ) {
croak("failed at mkd_string");
}


+ 47
- 2
lib/Text/Markdown/Discount.pm View File

@ -27,11 +27,56 @@ our @EXPORT = qw(
our $VERSION = '0.02';
use constant {
MKD_NOPANTS => 0x00000004,
MKD_NOHTML => 0x00000008,
};
require XSLoader;
XSLoader::load('Text::Markdown::Discount', $VERSION);
sub new {
return bless {}, 'Text::Markdown::Discount';
my $class = shift;
my %arg = @_;
# MKD_NOHEADER | MKD_NOPANTS
my $flags = 0x00010000;
if (exists $arg{smart}) {
if ($arg{smart}) {
$flags &= ~MKD_NOPANTS;
} else {
$flags |= MKD_NOPANTS;
}
}
if (exists $arg{filter_html}) {
if ($arg{filter_html}) {
$flags |= MKD_NOHTML;
} else {
$flags &= ~MKD_NOHTML;
}
}
return bless {_flags => $flags}, $class;
}
sub smart {
my ($self, $val) = @_;
my $flags = $self->{_flags};
if (defined $val and $val == 0) {
$self->{_flags} = $flags |= MKD_NOPANTS;
} else {
$self->{_flags} = $flags &= ~MKD_NOPANTS;
}
return $self;
}
sub filter_html {
my ($self, $val) = @_;
my $flags = $self->{_flags};
if (defined $val and $val == 0) {
$self->{_flags} = $flags &= ~MKD_NOHTML;
} else {
$self->{_flags} = $flags |= MKD_NOHTML;
}
return $self;
}
sub markdown {
@ -48,7 +93,7 @@ sub markdown {
croak('Calling ' . $self . '->markdown (as a class method) is not supported.');
}
}
return _markdown($text);
return _markdown($text, $self->{_flags});
}


+ 23
- 0
t/01.simple.t View File

@ -0,0 +1,23 @@
use Test::More tests => 3;
use Text::Markdown::Discount;
{
is(
Text::Markdown::Discount::markdown('Hello, world.'),
qq{<p>Hello, world.</p>\n},
);
}
{
is(
Text::Markdown::Discount::markdown('_Hello, world_!'),
qq{<p><em>Hello, world</em>!</p>\n},
);
}
{
is(
Text::Markdown::Discount::markdown('4 < 5'),
qq{<p>4 &lt; 5</p>\n},
);
}

+ 23
- 0
t/02.oo.t View File

@ -0,0 +1,23 @@
use Test::More tests => 3;
use Text::Markdown::Discount;
{
is(
Text::Markdown::Discount->new->markdown('Hello, world.'),
qq{<p>Hello, world.</p>\n},
);
}
{
is(
Text::Markdown::Discount->new->markdown('_Hello, world_!'),
qq{<p><em>Hello, world</em>!</p>\n},
);
}
{
is(
Text::Markdown::Discount->new->markdown('4 < 5'),
qq{<p>4 &lt; 5</p>\n},
);
}

+ 47
- 0
t/03.oo-flags.t View File

@ -0,0 +1,47 @@
use Test::More tests => 10;
use Text::Markdown::Discount;
{
my $mkd = Text::Markdown::Discount->new(smart => 1);
is(
$mkd->markdown(qq{"Quoted text"}),
qq{<p>&ldquo;Quoted text&rdquo;</p>\n},
);
$mkd = Text::Markdown::Discount->new(smart => 0);
is(
$mkd->markdown(qq{"Quoted text"}),
qq{<p>"Quoted text"</p>\n},
);
my $output = Text::Markdown::Discount->new->smart->markdown(qq{"Quoted text"});
is($output, qq{<p>&ldquo;Quoted text&rdquo;</p>\n});
$output = Text::Markdown::Discount->new->smart(0)->markdown(qq{"Quoted text"});
is($output, qq{<p>"Quoted text"</p>\n});
$output = Text::Markdown::Discount->new->smart(1)->markdown(qq{"Quoted text"});
is($output, qq{<p>&ldquo;Quoted text&rdquo;</p>\n});
}
{
my $mkd = Text::Markdown::Discount->new(filter_html => 1);
is(
$mkd->markdown(qq{<p>para</p>}),
qq{<p>&lt;p>para&lt;/p></p>\n},
);
$mkd = Text::Markdown::Discount->new(filter_html => 0);
is(
$mkd->markdown(qq{<p>para</p>}),
qq{<p>para</p>\n\n},
);
my $output = Text::Markdown::Discount->new->filter_html->markdown(qq{<p>para</p>});
is($output, qq{<p>&lt;p>para&lt;/p></p>\n});
$output = Text::Markdown::Discount->new->filter_html(0)->markdown(qq{<p>para</p>});
is($output, qq{<p>para</p>\n\n});
$output = Text::Markdown::Discount->new->filter_html(1)->markdown(qq{<p>para</p>});
is($output, qq{<p>&lt;p>para&lt;/p></p>\n});
}

Loading…
Cancel
Save