1 package Newslash::Plugin::WikiContentsReader;
2 use Mojo::Base 'Mojolicious::Plugin';
19 Newslash::Plugin::WikiContentsReader - Read Wiki Content
24 $app->plugin('Newslash::Plugin::WikiContentsReaders');
28 L<Newslash::Plugin::WikiContentsReaders> is a helper plugin to
29 read wiki content from osdn.jp.
36 $plugin->register(Mojolicious->new);
38 Register helpers in L<Mojolicious> application.
43 my ($self, $app, $conf) = @_;
46 $app->helper(wiki_reader => sub { state $wiki_reader = $self });
51 L<Mojolicious::Plugin::WikiContentsReaders> implements the following helpers.
56 my ($self, $page_name) = @_;
58 my $cf = $self->app->config;
59 my $config = $cf->{WikiContents} ||= {};
60 my $con_timeout = $config->{connect_timeout} //= 10;
61 my $req_timeout = $config->{request_timeout} //= 10;
62 my $max_redirects = $config->{max_redirects} //= 10;
64 my $http_proxy = $config->{http_proxy};
65 my $https_proxy = $config->{https_proxy};
67 my $project = $config->{project_name} //= "";
68 my $wiki_url = "http://osdn.jp/projects/$project/wiki";
69 my $wiki_url_regex = "https?://[0-9a-zA-Z.]*osdn.(jp|net)/projects/$project/wiki/";
70 my $page_url = "$wiki_url/$page_name";
73 $self->app->log->error("WikiContentsReader::_read: no project given.");
78 my $ua = Mojo::UserAgent->new;
79 $ua->connect_timeout($con_timeout);
80 $ua->request_timeout($req_timeout);
81 $ua->max_redirects($max_redirects);
82 $ua->proxy->http($http_proxy) if $http_proxy;
83 $ua->proxy->https($https_proxy) if $https_proxy;
86 my $tx = $ua->get($page_url);
88 $self->app->log->error("WikiContentsReader::_read: get '$page_name' failed.");
91 if ($tx->res->code != 200) {
92 my $code = $tx->res->code;
93 $self->app->log->error("WikiContentsReader::_read: get '$page_name' failed. code: $code");
97 my $html = Encode::decode_utf8($tx->res->body);
100 my $p = Newslash::Plugin::WikiContentsReader::Filter->new;
101 $p->set_replace_url($wiki_url_regex, '/');
104 my $content = { body => $p->filtered_html,
105 title => $p->{title},
106 page_name => $page_name,
109 if (!$content->{title} && $html =~ m!<title>([^<]+)</title>!si) {
110 $content->{title} = $1;
113 $content->{body} =~ s/\A.*(<div class=\"wiki-content\">.*)<!-- google_ad_section_end -->.*\z/$1/s;
114 if (!$content->{body}) {
118 $content->{body} =~ s/<script type='text\/javascript'>.*?<\/script>//sg;
119 if (!$content->{body}) {
127 =head2 read($page_name)
129 get content from srad wiki (https://osdn.net/projects/slashdotjp/wiki/)
134 my ($self, $page_name) = @_;
135 my $cache_key = "wiki_page";
136 my $content = $self->app->ccache->get($cache_key, $page_name);
138 if (defined $content) {
142 $content = $self->_read($page_name);
145 $self->app->ccache->set($cache_key, $page_name, $content);
151 # filter for anchor URL replace
152 package Newslash::Plugin::WikiContentsReader::Filter;
153 use Mojo::Base 'HTML::Filter';
155 sub set_replace_url {
156 my ($self, $from, $to) = @_;
157 $self->{url_from} = $from;
158 $self->{url_to} = $to;
162 return shift->{title};
166 my ($self, $tagname, $attrs, $attrnames, $text) = @_;
168 if ($tagname eq "a" && $self->{url_from} && $self->{url_to} && $attrs->{href}) {
169 if ($attrs->{href} =~ s/^$self->{url_from}//) {
170 $attrs->{href} =~ s!-!/!;
171 $attrs->{href} = "$self->{url_to}$attrs->{href}";
172 $attrs->{href} =~ s!^/+!/!;
173 $attrs->{href} =~ s!^/faq$!/faq/!;
175 if ($attrs->{href} !~ m!/attach/!) {
176 $text = "<$tagname " . join(" ", map { "$_=\"$attrs->{$_}\"" } @$attrnames) . ">";
178 } elsif ($tagname eq "form") {
179 $self->{form_seen}++;
180 } elsif ($tagname eq "h1") {
184 $self->output($text);
189 my ($tagname, $text) = @_;
191 if ($tagname eq "form") {
192 $self->{form_seen}--;
193 } elsif ($tagname eq "h1") {
197 $self->output($text);
204 if ($self->{h1_seen} && int($self->{h1_seen}) == 3) {
205 $self->{title} = $text;
209 $self->output($text);
212 sub output { push(@{$_[0]->{fhtml}}, $_[1]) unless ($_[0]->{form_seen}); }
213 sub filtered_html { join("", @{$_[0]->{fhtml}}) }
219 L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicious.org>.