OSDN Git Service

書籍執筆用のプラグインをsandboxにコミットしておきます。
[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         $self->{'outline_html'}  = "";
21         $self->{'outline_level'} =  0;
22         $self->{'outline_cnt'}   =  0;
23         $self->{'pagename'}      = $page;
24         return bless $self,$class;
25 }
26
27 #==============================================================================
28 # ¥Ø¥Ã¥É¥é¥¤¥ó¤Î¤ßÃê½Ð
29 #==============================================================================
30 sub l_headline {
31         my $self  = shift;
32         my $level = shift;
33         my $obj   = shift;
34         my $text  = &Util::delete_tag(join("",@$obj));
35         
36         if($level > $self->{outline_level}){
37                 while($level!=$self->{outline_level}){
38                         if($self->{'outline_close_'.($self->{outline_level})} == 1){\r
39                                 $self->{outline_html} .= "</li>\n";\r
40                                 $self->{'outline_close_'.($self->{outline_level})} = 0;\r
41                         }
42                         $self->{outline_html} .= "<ul class=\"outline\">\n";
43                         $self->{outline_level}++;
44                 }
45         } elsif($level <= $self->{outline_level}){
46                 while($level-1  != $self->{outline_level}){
47                         if($self->{'outline_close_'.($self->{outline_level})} == 1){
48                                 $self->{outline_html} .= "</li>\n";
49                                 $self->{'outline_close_'.($self->{outline_level})} = 0;
50                         }
51                         if($level == $self->{outline_level}){
52                                 last;
53                         }
54                         $self->{outline_html} .= "</ul>\n";
55                         $self->{outline_level}--;
56                 }
57         } else {
58                 $self->{outline_html} .= "</li>\n";
59         }
60         
61         $self->{'outline_close_'.$level} = 1;
62         $self->{outline_html} .= "<li><a href=\"?page=".Util::url_encode($self->{pagename})."#p".$self->{outline_cnt}."\">$text</a>";
63         $self->{outline_cnt}++;
64 }
65
66 #==============================================================================
67 # ¥¢¥¦¥È¥é¥¤¥óɽ¼¨ÍÑHTML¤Î¼èÆÀ
68 #==============================================================================
69 sub outline {
70         my $self   = shift;
71         my $source = shift;
72         $self->parse($source);
73         
74         while($self->{outline_level} != 0){
75                 if($self->{'outline_close_'.($self->{outline_level})} == 1){
76                         $self->{outline_html} .= "</li>\n";
77                 }
78                 $self->{outline_html} .= "</ul>\n";
79                 $self->{outline_level}--;
80         }
81         
82         return $self->{outline_html};
83 }
84
85 #==============================================================================
86 # ¥×¥é¥°¥¤¥ó¤Î²òÀϤò¹Ô¤¦¤È̵¸Â¥ë¡¼¥×¤·¤Æ¤·¤Þ¤¦¤¿¤á
87 #==============================================================================
88 sub plugin{
89         my $self   = shift;
90         my $plugin = shift;
91         
92         if($plugin->{'command'} eq 'title1'){
93                 $self->{'title1'}++;
94                 $self->{'title2'} = 0;
95                 $self->{'title3'} = 0;
96                 return $self->{'chapter'}.'-'.$self->{'title1'}.'. ';
97                 
98         } elsif($plugin->{'command'} eq 'title2'){
99                 $self->{'title2'}++;
100                 $self->{'title3'} = 0;
101                 return $self->{'chapter'}.'-'.$self->{'title1'}.'-'.$self->{'title2'}.'. ';
102                 
103         } elsif($plugin->{'command'} eq 'title3'){
104                 $self->{'title3'}++;
105                 return $self->{'chapter'}.'-'.$self->{'title1'}.'-'.$self->{'title2'}.'-'.$self->{'title3'}.'. ';
106                 
107         }
108         return undef;
109 }
110
111 #==============================================================================
112 # ¥×¥é¥°¥¤¥ó¤Î²òÀϤò¹Ô¤¦¤È̵¸Â¥ë¡¼¥×¤·¤Æ¤·¤Þ¤¦¤¿¤á
113 #==============================================================================
114 sub l_plugin{
115         my $self   = shift;
116         my $plugin = shift;
117         
118         if($plugin->{'command'} eq 'chapter'){
119                 $self->{'chapter'} = $plugin->{'args'}->[0];
120                 return undef;
121         }
122         
123         # outline°Ê³°¤Î¾ì¹ç¤Î¤ß½èÍý¤ò¹Ô¤¦
124         if($plugin->{command} ne "outline"){
125                 my $info = $self->{wiki}->get_plugin_info($plugin->{command});
126                 if($info->{FORMAT} eq "WIKI"){
127                         return $self->SUPER::l_plugin($plugin);
128                 }
129         } else {
130                 return undef;
131         }
132 }
133
134 1;