File Coverage

blib/lib/Text/Livedoor/Wiki.pm
Criterion Covered Total %
statement 79 79 100.0
branch 10 10 100.0
condition 10 10 100.0
subroutine 14 14 100.0
pod 3 3 100.0
total 116 116 100.0


line stmt bran cond sub pod time code
1             package Text::Livedoor::Wiki;
2              
3 10     10   589203 use warnings;
  10         23  
  10         282  
4 10     10   52 use strict;
  10         18  
  10         314  
5 10     10   54 use Scalar::Util;
  10         21  
  10         651  
6 10     10   11466 use Text::Livedoor::Wiki::Block;
  10         29  
  10         140  
7 10     10   7065 use Text::Livedoor::Wiki::CatalogKeeper;
  10         28  
  10         121  
8 10     10   6253 use Text::Livedoor::Wiki::Function;
  10         28  
  10         112  
9 10     10   5970 use Text::Livedoor::Wiki::IDKeeper;
  10         30  
  10         110  
10 10     10   6412 use Text::Livedoor::Wiki::Inline;
  10         30  
  10         119  
11 10     10   6133 use Text::Livedoor::Wiki::Plugin;
  10         34  
  10         151  
12              
13             our $VERSION ='0.02';
14              
15             # Everybody can not be happy but I am happy.
16             sub new {
17 9     9 1 2444 my $class = shift;
18 9   100     52 my $self = shift || {};
19             # * god bless you
20 9         30 $self = bless $self, $class;
21              
22             # * initialize args
23 9 100       107 my $block_plugins = $self->{block_plugins} ? $self->{block_plugins} : Text::Livedoor::Wiki::Plugin->block_plugins;
24 9 100       92 my $inline_plugins = $self->{inline_plugins} ? $self->{inline_plugins} : Text::Livedoor::Wiki::Plugin->inline_plugins;
25 9 100       93 my $function_plugins= $self->{function_plugins} ? $self->{function_plugins} : Text::Livedoor::Wiki::Plugin->function_plugins;
26 9   100     71 my $on_mobile = $self->{on_mobile} || 0;
27              
28             # * creating parser objects
29 9         124 my $function = Text::Livedoor::Wiki::Function->new( { plugins => $function_plugins , on_mobile => $on_mobile } );
30              
31 9         111 $self->{inline}
32             = Text::Livedoor::Wiki::Inline->new({
33             plugins => $inline_plugins,
34             function => $function,
35             on_mobile => $on_mobile
36             });
37              
38             # nobody likes memory leak.
39 9         65 Scalar::Util::weaken($self->{inline});
40              
41 9         131 $self->{block}
42             = Text::Livedoor::Wiki::Block->new({
43             block_plugins => $block_plugins ,
44             inline => $self->{inline} ,
45             on_mobile => $on_mobile
46             });
47              
48              
49             # default options
50 9   100     71 $self->{opts} ||= {};
51 9         73 return $self;
52             }
53              
54 87     87 1 199 sub opts { $Text::Livedoor::Wiki::opts }
55              
56             sub parse {
57 92     92 1 297921 my $self = shift;
58 92         271 my $text = shift;
59 92   100     646 my $opts = shift || {};
60 92         232 %$opts = ( %{$self->{opts}}, %$opts );
  92         574  
61              
62 92         443 my $want_pos = delete $opts->{want_pos} ;
63             # * creating scrathpad and options which scorpe is only for this parse() , so that this module can be as a Singleton.
64 92   100     626 $opts->{name} = $opts->{name} || 'content';
65 92         821 my $id_keeper = Text::Livedoor::Wiki::IDKeeper->new({ name => $opts->{name} } );
66 92         801 my $catalog_keeper = Text::Livedoor::Wiki::CatalogKeeper->new();
67 92         269 local $Text::Livedoor::Wiki::scratchpad = {};
68 92         624 local $Text::Livedoor::Wiki::opts = { id_keeper => $id_keeper , catalog_keeper => $catalog_keeper , %$opts };
69 92         692 $Text::Livedoor::Wiki::scratchpad->{core}{block_uid} = 0;
70 92         288 $Text::Livedoor::Wiki::scratchpad->{core}{inline_uid} = 0;
71 92         566 $Text::Livedoor::Wiki::scratchpad->{core}{block_trigger} = $self->{block}->trigger ;
72 92         270 $Text::Livedoor::Wiki::scratchpad->{core}{current_pos} = 0;
73              
74 92         323 $self->_init_text( \$text );
75 92         1397 my $html = $self->{block}->parse( $text );
76 90         588 $html .= $self->{block}->footer_section();
77              
78 90 100       336 if( $want_pos ) {
79 3         64 return $Text::Livedoor::Wiki::scratchpad->{core}{h3pos};
80             }
81             else {
82 87         5117 return $self->_build( $html );
83             }
84             }
85              
86             sub _init_text() {
87 92     92   188 my $self = shift;
88 92         165 my $text = shift;
89 92         585 $$text =~ s/\r//g;
90 92         458 $$text =~ s/\n$//;
91             }
92             sub _build {
93 87     87   157 my $self = shift;
94 87         240 my $sections = shift;
95 87         260 my $opts = $self->opts;
96              
97 87         320 $sections =~ s/
\n$/\n/;
98             # TODO customize class name
99             # * build sections
100 87         185 my $html = qq|
\n|;
101 87         316 $html .= $sections;
102 87         178 $html .= qq|\n|;
103              
104             # * build contents
105 87 100       294 if( !$self->{on_mobile} ) {
106 76         283 my $catalog_keeper = $opts->{catalog_keeper};
107 76         389 my $contents = $catalog_keeper->contents();
108 76         312 $html =~ s/\n#contents/\n$contents\n/g;
109             }
110             else {
111 11         56 $html =~ s/\n#contents
\n/\n/g;
112             }
113              
114 87         6499 return $html;
115              
116             }
117              
118              
119             1;
120              
121             =head1 NAME
122              
123             Text::Livedoor::Wiki - Perl extension for formatting text with livedoor Wiki Style.
124              
125             =head1 SYNOPSIS
126              
127             use Text::Livedoor::Wiki;
128             my $parser = Text::Livedoor::Wiki->new( { opts => { storage => 'http://static.wiki.livedoor.jp/formatter-storage' } } );
129              
130             my $data = "* ''polocky''";
131             $html = $parser->parse( $data );
132              
133             =head1 DESCRIPTION
134              
135             This is livedoor Wiki Style Parser. you can download CSS and images from http://static.wiki.livedoor.jp/download/livedoor-wiki.0.02.tar.gz
136              
137             =head1 METHOD
138              
139             =head2 new
140              
141             constructor. you can set arguments as hash ref.
142              
143             =over 4
144              
145             =item block_plugins
146              
147             set list of block plugins you want to load. default is Text::Livedoor::Wiki::Plugin->block_plugins
148              
149             =item inline_plugins
150              
151             set list of inline plugins you want to load. default is Text::Livedoor::Wiki::Plugin->inline_plugins
152              
153             =item function_plugins
154              
155             set list of function plugins you want to load. default is Text::Livedoor::Wiki::Plugin->function_plugins
156              
157             =item on_mobile
158              
159             set 1 for mobile mode.
160              
161             =back
162              
163             =head2 parse
164              
165             parse Wiki to HTML.
166              
167             $parser->parse( $wiki, { your_options => 'hoge' } );
168              
169             =head2 opts
170              
171             get options
172              
173             =head1 AUTHOR
174              
175             Polocky
176              
177             =head1 SEE ALSO
178              
179             http://wiki.livedoor.com (Japanese)
180             http://wiki.livedoor.jp/wiki_text/ (Japanese)
181              
182             =cut