OSDN Git Service

リソースのパスのプレフィックス処理を修正。
[fswiki/sandbox.git] / takezoe / plugin / book / TocParser.pm
1 ###############################################################################
2 #
3 # book¥×¥é¥°¥¤¥ó¤ËÂбþ¤·¤¿¥¢¥¦¥È¥é¥¤¥ó¥Ñ¡¼¥µ
4 #
5 ###############################################################################
6 package plugin::book::TocParser;
7 use strict;
8 use vars qw(@ISA);
9 use Wiki::HTMLParser;
10
11 @ISA = qw(Wiki::HTMLParser);
12
13 #==============================================================================
14 # ¥³¥ó¥¹¥È¥é¥¯¥¿
15 #==============================================================================
16 sub new {
17         my $class = shift;
18         my $self  = Wiki::HTMLParser->new(shift);
19         my $page  = shift;
20         my $level = shift;
21         
22         if($level ne ""){
23                 $self->{'display_level'} = $level;
24         } else {
25                 $self->{'display_level'} = 3;
26         }
27         
28         $self->{'outline_html'}  = "";
29         $self->{'outline_level'} =  0;
30         $self->{'outline_cnt'}   =  0;
31         $self->{'outline_fix'}   =  0;
32         $self->{'pagename'}      = $page;
33         $self->{'section_cnt'}   =  0;
34         return bless $self,$class;
35 }
36
37 #==============================================================================
38 # ¥Ø¥Ã¥É¥é¥¤¥ó¤Î¤ßÃê½Ð
39 #==============================================================================
40 sub l_headline {
41         my $self  = shift;
42         my $level = shift;
43         my $obj   = shift;
44         
45         $level = $level + $self->{'outline_fix'};
46         
47         if($level > $self->{'display_level'}){
48                 $self->{outline_cnt}++;
49                 return;
50         }
51         
52         my $text = &Util::delete_tag(join("",@$obj));
53         
54         if($level > $self->{outline_level}){
55                 while($level!=$self->{outline_level}){
56                         if($self->{'outline_close_'.($self->{outline_level})} == 1){\r
57                                 $self->{outline_html} .= "</li>\n";\r
58                                 $self->{'outline_close_'.($self->{outline_level})} = 0;\r
59                         }
60                         $self->{outline_html} .= "<ul class=\"outline\">\n";
61                         $self->{outline_level}++;
62                 }
63         } elsif($level <= $self->{outline_level}){
64                 while($level-1  != $self->{outline_level}){
65                         if($self->{'outline_close_'.($self->{outline_level})} == 1){
66                                 $self->{outline_html} .= "</li>\n";
67                                 $self->{'outline_close_'.($self->{outline_level})} = 0;
68                         }
69                         if($level == $self->{outline_level}){
70                                 last;
71                         }
72                         $self->{outline_html} .= "</ul>\n";
73                         $self->{outline_level}--;
74                 }
75         } else {
76                 $self->{outline_html} .= "</li>\n";
77         }
78         
79         $self->{'outline_close_'.$level} = 1;
80         if($self->{'outline_fix'} == 1 && $level == 1){
81                 $self->{outline_html} .= "<li>$text";
82         } else {
83                 $self->{outline_html} .= "<li><a href=\"?page=".Util::url_encode($self->{pagename})."#p".$self->{outline_cnt}."\">$text</a>";
84                 $self->{outline_cnt}++;
85         }
86 }
87
88 #==============================================================================
89 # ¥¢¥¦¥È¥é¥¤¥óɽ¼¨ÍÑHTML¤Î¼èÆÀ
90 #==============================================================================
91 sub outline {
92         my $self   = shift;
93         my $source = shift;
94         $self->parse($source);
95         
96         while($self->{outline_level} != 0){
97                 if($self->{'outline_close_'.($self->{outline_level})} == 1){
98                         $self->{outline_html} .= "</li>\n";
99                 }
100                 $self->{outline_html} .= "</ul>\n";
101                 $self->{outline_level}--;
102         }
103         
104         return $self->{outline_html};
105 }
106
107 #==============================================================================
108 # ¥×¥é¥°¥¤¥ó¤Î²òÀϤò¹Ô¤¦¤È̵¸Â¥ë¡¼¥×¤·¤Æ¤·¤Þ¤¦¤¿¤á
109 #==============================================================================
110 sub plugin{
111         my $self   = shift;
112         my $plugin = shift;
113         
114         if($plugin->{'command'} eq 'title1'){
115                 $self->{'title1'}++;
116                 $self->{'title2'} = 0;
117                 $self->{'title3'} = 0;
118                 return $self->{'chapter'}.'-'.$self->{'title1'}.'. ';
119                 
120         } elsif($plugin->{'command'} eq 'title2'){
121                 $self->{'title2'}++;
122                 $self->{'title3'} = 0;
123                 return $self->{'chapter'}.'-'.$self->{'title1'}.'-'.$self->{'title2'}.'. ';
124                 
125         } elsif($plugin->{'command'} eq 'title3'){
126                 $self->{'title3'}++;
127                 return $self->{'chapter'}.'-'.$self->{'title1'}.'-'.$self->{'title2'}.'-'.$self->{'title3'}.'. ';
128                 
129         }
130         return undef;
131 }
132
133 #==============================================================================
134 # ¥×¥é¥°¥¤¥ó¤Î²òÀϤò¹Ô¤¦¤È̵¸Â¥ë¡¼¥×¤·¤Æ¤·¤Þ¤¦¤¿¤á
135 #==============================================================================
136 sub l_plugin{
137         my $self   = shift;
138         my $plugin = shift;
139         
140         if($plugin->{'command'} eq 'chapter'){
141                 $self->{'chapter'} = $plugin->{'args'}->[0];
142                 return undef;
143         }
144         if($plugin->{'command'} eq 'section'){
145                 if($self->{'outline_fix'} == 0){
146                         $self->{'outline_fix'} = 1;
147                         $self->{'display_level'}++;
148                 }
149                 $self->{'section_cnt'}++;
150                 $self->l_headline(0, [$plugin->{'args'}->[0]]);
151                 return undef;
152         }
153         
154         # outline°Ê³°¤Î¾ì¹ç¤Î¤ß½èÍý¤ò¹Ô¤¦
155         if($plugin->{command} ne "outline"){
156                 my $info = $self->{wiki}->get_plugin_info($plugin->{command});
157                 if($info->{FORMAT} eq "WIKI"){
158                         return $self->SUPER::l_plugin($plugin);
159                 }
160         } else {
161                 return undef;
162         }
163 }
164
165 1;