OSDN Git Service

linkプラグインは1リクエストの処理内ではパース結果をキャッシュして負荷を減らすようにした。
[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         my $self->{cache} = {};
24         return bless $self,$class;
25 }
26
27 #==============================================================================
28 # ¥¤¥ó¥é¥¤¥ó¥á¥½¥Ã¥É
29 #==============================================================================
30 sub inline {
31         my $self   = shift;
32         my $wiki   = shift;
33         my $anchor = shift;
34         my $page   = shift;
35         
36         $page = $wiki->get_CGI->param('page') unless $page;
37         my $source = $wiki->get_page($page);
38         
39         unless(defined($self->{cache}->{$page})){
40                 $self->{cache}->{$page} = {};
41                 my $source = $wiki->get_page($page);
42
43                 my $text = '';
44         
45                 my @lines = split(/\n/,$wiki->get_page($page));
46                 my $chapter = '';
47                 my $count = {};
48                 my $title1Count = 0;
49                 my $title2Count = 0;
50                 my $title3Count = 0;
51                 
52                 # TODO {{pre}}¥×¥é¥°¥¤¥óÆâ¤Îµ­½Ò¤ÏÈô¤Ð¤µ¤Ê¤¤¤È¥À¥á¡ª¡ª¥Ñ¡¼¥µ¤òºî¤é¤Ê¤¤¤È¥À¥á¤Ã¤Ý¤¤¡©
53                 foreach my $line (@lines){
54                         if($line =~ /^{{(chapter.+}})$/){
55                                 my $plugin = $wiki->parse_inline_plugin($1);
56                                 $chapter = $plugin->{'args'}->[0];
57                                 $title1Count = 0;
58                                 $title2Count = 0;
59                                 $title3Count = 0;
60                                 foreach my $key (keys(%$count)){
61                                         $count->{$key} = 0;
62                                 }
63                         } elsif($line =~ /^!!!{{(title1.+}})(.+)$/){
64                                 my $plugin = $wiki->parse_inline_plugin($1);
65                                 $title1Count++;
66                                 $title2Count = 0;
67                                 $title3Count = 0;
68                                 $text= $chapter.'-'.$title1Count.'. '.Util::trim($2);
69                                 $self->{cache}->{$page}->{$plugin->{'args'}->[0]} = $text;
70                                 
71                         } elsif($line =~ /^!!{{(title2.+}})$/){
72                                 my $plugin = $wiki->parse_inline_plugin($1);
73                                 $title2Count++;
74                                 $title3Count = 0;
75                                 $text= $chapter.'-'.$title1Count.'-'.$title2Count.'. '.Util::trim($2);
76                                 $self->{cache}->{$page}->{$plugin->{'args'}->[0]} = $text;
77                                 
78                         } elsif($line =~ /^!{{(title3.+}})$/){
79                                 my $plugin = $wiki->parse_inline_plugin($1);
80                                 $title3Count++;
81                                 $text= $chapter.'-'.$title1Count.'-'.$title2Count.'-'.$title3Count.'. '.Util::trim($2);
82                                 $self->{cache}->{$page}->{$plugin->{'args'}->[0]} = $text;
83                                 
84                         } elsif($line =~ /^{{(caption.+}})$/){
85                                 my $plugin = $wiki->parse_inline_plugin($1);
86                                 my $type = $plugin->{'args'}->[0];
87                                 $count->{$type}++;
88                                 $text = $type.$chapter.'-'.$count->{$type}.': '.Util::trim($plugin->{'args'}->[1]);
89                                 $self->{cache}->{$page}->{$plugin->{'args'}->[2]} = $text;
90                         }
91                 }
92         }
93         if(defined($self->{cache}->{$page}->{$anchor})){
94                 my $text = $self->{cache}->{$page}->{$anchor};
95                 return '<a href="?page='.Util::url_encode($page).'#'.Util::escapeHTML($anchor).'" class="xref">'.Util::escapeHTML($text).'</a>';
96         } else {
97                 return '<span class="xref-error">»²¾ÈÀ褬¸«¤Ä¤«¤ê¤Þ¤»¤ó¡ª</span>';
98         }
99 }
100
101 1;