OSDN Git Service

書籍執筆用のプラグインをsandboxにコミットしておきます。
[fswiki/sandbox.git] / takezoe / plugin / book / Link.pm
1 ################################################################################
2 #
3 # <p>¸«½Ð¤·¤ä¿Þɽ¤Ø¤Î¥ê¥ó¥¯¤ò½ÐÎϤ¹¤ë¤¿¤á¤Î¥¤¥ó¥é¥¤¥ó¥×¥é¥°¥¤¥ó¤Ç¤¹¡£</p>
4 # <p>°ú¿ô¤Ë¤Ïtitle1¡Átitle3¥×¥é¥°¥¤¥ó¤äcaption¥×¥é¥°¥¤¥ó¤Çµ­½Ò¤·¤¿»²¾ÈÍѤΥé¥Ù¥ë¤ò»ØÄꤷ¤Þ¤¹¡£</p>
5 # <pre>
6 # !!!{{title1 installation}}¥¤¥ó¥¹¥È¡¼¥ëÊýË¡
7 # ...
8 # ¥¤¥ó¥¹¥È¡¼¥ëÊýË¡¤Ë¤Ä¤¤¤Æ¤Ï{{link installation}}¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
9 # </pre>
10 # <p>Âè2°ú¿ô¤Ç¥Ú¡¼¥¸¤ò»ØÄꤹ¤ë¤³¤È¤Ç¡¢ÊÌ¥Ú¡¼¥¸¤Î¸«½Ð¤·¤ä¿Þɽ¤ò»²¾È¤¹¤ë¤³¤È¤â¤Ç¤­¤Þ¤¹¡£</p>
11 # <pre>
12 # ¥¤¥ó¥¹¥È¡¼¥ëÊýË¡¤Ë¤Ä¤¤¤Æ¤Ï{{link installation,Chapter1}}¤ò»²¾È¤·¤Æ¤¯¤À¤µ¤¤¡£
13 # </pre>
14 #
15 ################################################################################
16 package plugin::book::Link;
17 #==============================================================================
18 # ¥³¥ó¥¹¥È¥é¥¯¥¿
19 #==============================================================================
20 sub new {
21         my $class = shift;
22         my $self = {};
23         return bless $self,$class;
24 }
25
26 #==============================================================================
27 # ¥¤¥ó¥é¥¤¥ó¥á¥½¥Ã¥É
28 #==============================================================================
29 sub inline {
30         my $self   = shift;
31         my $wiki   = shift;
32         my $anchor = shift;
33         my $page   = shift;
34         
35         $page = $wiki->get_CGI->param('page') unless $page;
36         my $source = $wiki->get_page($page);
37         
38         my $text = '';
39         
40         my @lines = split(/\n/,$wiki->get_page($page));
41         my $chapter = '';
42         my $count = {};
43         my $title1Count = 0;
44         my $title2Count = 0;
45         my $title3Count = 0;
46         
47         # TODO {{pre}}¥×¥é¥°¥¤¥óÆâ¤Îµ­½Ò¤ÏÈô¤Ð¤µ¤Ê¤¤¤È¥À¥á¡ª¡ª¥Ñ¡¼¥µ¤òºî¤é¤Ê¤¤¤È¥À¥á¤Ã¤Ý¤¤¡©
48         foreach my $line (@lines){
49                 if($line =~ /^{{(chapter.+}})$/){
50                         my $plugin = $wiki->parse_inline_plugin($1);
51                         $chapter = $plugin->{'args'}->[0];
52                         $title1Count = 0;
53                         $title2Count = 0;
54                         $title3Count = 0;
55                         foreach my $key (keys(%$count)){
56                                 $count->{$key} = 0;
57                         }
58                 } elsif($line =~ /^!!!{{(title1.+}})(.+)$/){
59                         my $plugin = $wiki->parse_inline_plugin($1);
60                         $title1Count++;
61                         $title2Count = 0;
62                         $title3Count = 0;
63                         if($plugin->{'args'}->[0] eq $anchor){
64                                 $text= $chapter.'-'.$title1Count.'. '.Util::trim($2);
65                                 last;
66                         }
67                 } elsif($line =~ /^!!{{(title2.+}})$/){
68                         my $plugin = $wiki->parse_inline_plugin($1);
69                         $title2Count++;
70                         $title3Count = 0;
71                         if($plugin->{'args'}->[0] eq $anchor){
72                                 $text= $chapter.'-'.$title1Count.'-'.$title2Count.'. '.Util::trim($2);
73                                 last;
74                         }
75                 } elsif($line =~ /^!{{(title3.+}})$/){
76                         my $plugin = $wiki->parse_inline_plugin($1);
77                         $title3Count++;
78                         if($plugin->{'args'}->[0] eq $anchor){
79                                 $text= $chapter.'-'.$title1Count.'-'.$title2Count.'-'.$title3Count.'. '.Util::trim($2);
80                                 last;
81                         }
82                 } elsif($line =~ /^{{(caption.+}})$/){
83                         my $plugin = $wiki->parse_inline_plugin($1);
84                         my $type = $plugin->{'args'}->[0];
85                         $count->{$type}++;
86                         if($plugin->{'args'}->[2] eq $anchor){
87                                 $text = $type.$chapter.'-'.$count->{$type}.': '.Util::trim($plugin->{'args'}->[1]);
88                                 last;
89                         }
90                 }
91         }
92         if($text ne ''){
93                 return '<a href="?page='.Util::url_encode($page).'#'.Util::escapeHTML($anchor).'" class="xref">'.Util::escapeHTML($text).'</a>';
94         } else {
95                 return '<span class="xref-error">»²¾ÈÀ褬¸«¤Ä¤«¤ê¤Þ¤»¤ó¡ª</span>';
96         }
97 }
98
99 1;