| 1 | package Text::Xatena; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 20 | ▶ | use UNIVERSAL::require; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 20 | ▶ | use Text::Xatena::LineScanner; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 20 | ▶ | use Text::Xatena::Node; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 20 | ▶ | use Text::Xatena::Node::Root; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 20 | ▶ | use Text::Xatena::Inline; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 20 | ▶ | use Text::Xatena::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 20 | our $VERSION = '0.18'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 20 | our $SYNTAXES = [ | |||||||||||||||||||||||||||
| 16 | 'Text::Xatena::Node::SeeMore', | ||||||||||||||||||||||||||||
| 17 | 'Text::Xatena::Node::SuperPre', | ||||||||||||||||||||||||||||
| 18 | 'Text::Xatena::Node::StopP', | ||||||||||||||||||||||||||||
| 19 | 'Text::Xatena::Node::Blockquote', | ||||||||||||||||||||||||||||
| 20 | 'Text::Xatena::Node::Pre', | ||||||||||||||||||||||||||||
| 21 | 'Text::Xatena::Node::List', | ||||||||||||||||||||||||||||
| 22 | 'Text::Xatena::Node::DefinitionList', | ||||||||||||||||||||||||||||
| 23 | 'Text::Xatena::Node::Table', | ||||||||||||||||||||||||||||
| 24 | 'Text::Xatena::Node::Section', | ||||||||||||||||||||||||||||
| 25 | 'Text::Xatena::Node::Comment', | ||||||||||||||||||||||||||||
| 26 | ]; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 124 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 29 | 124 | my ($class, %opts) = @_; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 124 | my $self = bless { %opts }, $class; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 124 | ▶ | $self->{templates} ||= {}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"templates"} ||= {}
MC/DC100%1/1
$self->{"templates"} ||= {}
$self->{"templates"}
Truth table100%2/2
A
A= $self->{"templates"}
| |||||||||||||||||||||||||||||
| 34 | $self->{templates} = { | ||||||||||||||||||||||||||||
| 35 | map { | ||||||||||||||||||||||||||||
| 36 | 2 | ▶ | my $pkg = $_ =~ /::/ ? $_ : "Text::Xatena::Node::$_"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 2 | $pkg => $self->{templates}->{$_}; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 124 | keys %{ $self->{templates} } | |||||||||||||||||||||||||||
| 40 | }; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | $self->{syntaxes} = [ | ||||||||||||||||||||||||||||
| 43 | map { | ||||||||||||||||||||||||||||
| 44 | 1221 | ▶ | my $pkg = $_ =~ /::/ ? $_ : "Text::Xatena::Node::$_"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 1221 | ▶ | $pkg->use or die $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 1221 | $pkg; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 124 | ▶ | @{ $opts{syntaxes} || $SYNTAXES } | ||||||||||||||||||||||||||
|
Condition66%2/3
$opts{'syntaxes'} || $Text::Xatena::SYNTAXES
MC/DC0%0/2
$opts{'syntaxes'} || $Text::Xatena::SYNTAXES
$opts{'syntaxes'}
$Text::Xatena::SYNTAXES
Truth table66%2/3
A || B
A= $opts{'syntaxes'}B= $Text::Xatena::SYNTAXES
| |||||||||||||||||||||||||||||
| 49 | ]; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 124 | ▶ | $self->{inline} ||= Text::Xatena::Inline->new; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"inline"} ||= "Text::Xatena::Inline"->new
MC/DC0%0/2
$self->{"inline"} ||= "Text::Xatena::Inline"->new
$self->{"inline"}
"Text::Xatena::Inline"->new
Truth table66%2/3
A ||= B
A= $self->{"inline"}B= "Text::Xatena::Inline"->new
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 124 | $self; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 128 | ▶ | sub format { | ||||||||||||||||||||||||||
|
Subroutine
format: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 128 | my ($self, $string, %opts) = @_; | |||||||||||||||||||||||||||
| 58 | 128 | $string =~ s{\r\n?|\n}{\n}g; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 128 | ▶ | $self->inline($opts{inline}) if $opts{inline}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 128 | ▶ | if ($self->{hatena_compatible}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 9 | $self->{templates}->{'Text::Xatena::Node::Section'} = q[ | |||||||||||||||||||||||||||
| 64 | <h{{= $level + 2 }}>{{= $title }}</h{{= $level + 2 }}> | ||||||||||||||||||||||||||||
| 65 | {{= $content }} | ||||||||||||||||||||||||||||
| 66 | ]; | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 20 | ▶ | no warnings "once", "redefine"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 14 | ▶ | local *Text::Xatena::Node::as_html_paragraph = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 70 | 14 | my ($self, $context, $text, %opts) = @_; | |||||||||||||||||||||||||||
| 71 | 14 | $text = $context->inline->format($text, %opts); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 14 | $text =~ s{\n$}{}g; | |||||||||||||||||||||||||||
| 74 | 14 | ▶ | if ($opts{stopp}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | $text; | |||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | "<p>" . join("", | ||||||||||||||||||||||||||||
| 78 | map { | ||||||||||||||||||||||||||||
| 79 | 14 | ▶ | if (/^(\n+)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 6 | "</p>" . ("<br />\n" x (length($1) - 2)) . "<p>"; | |||||||||||||||||||||||||||
| 81 | } else { | ||||||||||||||||||||||||||||
| 82 | 20 | $_; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | split(/(\n+)/, $text) | ||||||||||||||||||||||||||||
| 86 | ) . "</p>\n"; | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 9 | }; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 9 | $self->_parse($string)->as_html($self); | |||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||
| 92 | 119 | $self->_parse($string)->as_html($self); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 283 | ▶ | sub inline { | ||||||||||||||||||||||||||
|
Subroutine
inline: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | 283 | my ($self, $new) = @_; | |||||||||||||||||||||||||||
| 98 | 283 | ▶ | if (@_ > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 1 | $self->{inline} = $new; | |||||||||||||||||||||||||||
| 100 | } else { | ||||||||||||||||||||||||||||
| 101 | 282 | $self->{inline}; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 141 | ▶ | sub _parse { | ||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 106 | 141 | my ($self, $string) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 141 | my @syntaxes = @{ $self->{syntaxes} }; | |||||||||||||||||||||||||||
| 109 | 141 | my $s = Text::Xatena::LineScanner->new($string); | |||||||||||||||||||||||||||
| 110 | 141 | my $root = Text::Xatena::Node::Root->new ; | |||||||||||||||||||||||||||
| 111 | 141 | my $stack = [ $root ]; | |||||||||||||||||||||||||||
| 112 | 141 | ▶ | loop: until ($s->eos) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 429 | my $parent = $stack->[-1]; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 429 | for my $pkg (@syntaxes) { | |||||||||||||||||||||||||||
| 116 | 3599 | ▶ | $pkg->parse($s, $parent, $stack) and next loop; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # plain lines | ||||||||||||||||||||||||||||
| 120 | 274 | push @$parent, $s->next; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 141 | $root; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 109 | ▶ | sub _tmpl { | ||||||||||||||||||||||||||
|
Subroutine
_tmpl: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 127 | 109 | my ($self, $pkg, $default, $stash) = @_; | |||||||||||||||||||||||||||
| 128 | 109 | my $tmpl = $self->{templates}->{$pkg}; | |||||||||||||||||||||||||||
| 129 | 109 | ▶ | my $sub = ref($tmpl) eq 'CODE' ? $tmpl : template($tmpl || $default, [ keys %$stash ]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$tmpl || $default
MC/DC0%0/2
$tmpl || $default
$tmpl
$default
Truth table66%2/3
A || B
A= $tmplB= $default
| |||||||||||||||||||||||||||||
| 130 | 109 | $self->{templates}->{$pkg} = $sub; | |||||||||||||||||||||||||||
| 131 | 109 | $sub->($stash); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 20 | 1; | |||||||||||||||||||||||||||
| 135 | __END__ | ||||||||||||||||||||||||||||