line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package meon::Web::XML2Comment; |
2
|
|
|
|
|
|
|
|
3
|
4
|
|
|
4
|
|
5993610
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use MooseX::Types::Path::Class; |
5
|
|
|
|
|
|
|
use 5.010; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
use meon::Web::Util; |
8
|
|
|
|
|
|
|
use Path::Class 'dir'; |
9
|
|
|
|
|
|
|
use Carp 'croak'; |
10
|
|
|
|
|
|
|
use XML::LibXML 'XML_TEXT_NODE'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has 'path' => (is=>'rw',isa=>'Path::Class::File',required=>1,coerce=>1); |
13
|
|
|
|
|
|
|
has '_full_path' => (is=>'ro',isa=>'Path::Class::File',lazy=>1,builder=>'_build_full_path'); |
14
|
|
|
|
|
|
|
has 'xml' => (is=>'ro', isa=>'XML::LibXML::Document', lazy => 1, builder => '_build_xml'); |
15
|
|
|
|
|
|
|
has 'title' => (is=>'ro', isa=>'Str',lazy_build=>1,); |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub _build_xml { |
18
|
|
|
|
|
|
|
my ($self) = @_; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
return XML::LibXML->load_xml( |
21
|
|
|
|
|
|
|
location => $self->_full_path, |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _build_full_path { |
26
|
|
|
|
|
|
|
my ($self) = @_; |
27
|
|
|
|
|
|
|
my $path = $self->path; |
28
|
|
|
|
|
|
|
$path = $path.'index' if $path =~ m{/$}; |
29
|
|
|
|
|
|
|
return meon::Web::Util->full_path_fixup($path.'.xml'); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _build_title { |
33
|
|
|
|
|
|
|
my ($self) = @_; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $xml = $self->xml; |
36
|
|
|
|
|
|
|
my $xc = meon::Web::Util->xpc; |
37
|
|
|
|
|
|
|
my ($title) = $xc->findnodes('/w:page/w:content//w:timeline-entry/w:title',$xml); |
38
|
|
|
|
|
|
|
($title) = $xc->findnodes('/w:page/w:meta/w:title',$xml) |
39
|
|
|
|
|
|
|
unless $title; |
40
|
|
|
|
|
|
|
die 'missing title in '.$self->_full_path |
41
|
|
|
|
|
|
|
unless $title; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
return $title->textContent; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub web_uri { |
47
|
|
|
|
|
|
|
my ($self) = @_; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my $base_dir = meon::Web::env->content_dir; |
50
|
|
|
|
|
|
|
my $path = $self->_full_path; |
51
|
|
|
|
|
|
|
$path = '/'.$path->relative($base_dir); |
52
|
|
|
|
|
|
|
$path =~ s/\.xml$//; |
53
|
|
|
|
|
|
|
return $path; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub add_comment { |
57
|
|
|
|
|
|
|
my ($self, $comment_path) = @_; |
58
|
|
|
|
|
|
|
croak 'missing comment_path argument' |
59
|
|
|
|
|
|
|
unless $comment_path; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
my $xml = $self->xml; |
62
|
|
|
|
|
|
|
my $xpc = meon::Web::Util->xpc; |
63
|
|
|
|
|
|
|
my ($comments_el) = $xpc->findnodes('/w:page/w:content//w:timeline[@class="comments"]',$xml); |
64
|
|
|
|
|
|
|
croak 'comments not allowed' |
65
|
|
|
|
|
|
|
unless $comments_el; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
$comments_el->appendText(' 'x4); |
68
|
|
|
|
|
|
|
my $entry_node = $comments_el->addNewChild( undef, 'w:timeline-entry' ); |
69
|
|
|
|
|
|
|
$entry_node->setAttribute('href' => $comment_path); |
70
|
|
|
|
|
|
|
$comments_el->appendText("\n".' 'x4); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
IO::Any->spew($self->_full_path, $xml->toString, { atomic => 1 }); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub rm_comment { |
76
|
|
|
|
|
|
|
my ($self, $comment_path) = @_; |
77
|
|
|
|
|
|
|
croak 'missing comment_path argument' |
78
|
|
|
|
|
|
|
unless $comment_path; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
return |
81
|
|
|
|
|
|
|
unless -e $self->_full_path; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
my $xml = $self->xml; |
84
|
|
|
|
|
|
|
my $xpc = meon::Web::Util->xpc; |
85
|
|
|
|
|
|
|
my ($comment_el) = $xpc->findnodes('/w:page/w:content//w:timeline[@class="comments"]/w:timeline-entry[@href="'.$comment_path.'"]',$xml); |
86
|
|
|
|
|
|
|
return unless $comment_el; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
my $timeline = $comment_el->parentNode; |
89
|
|
|
|
|
|
|
my (@whitespaces) = |
90
|
|
|
|
|
|
|
grep { $_->nodeType == XML_TEXT_NODE && $_->textContent =~ m/^\s*$/ } |
91
|
|
|
|
|
|
|
($comment_el->previousSibling); |
92
|
|
|
|
|
|
|
foreach my $el ($comment_el, @whitespaces) { |
93
|
|
|
|
|
|
|
$timeline->removeChild($el); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
IO::Any->spew($self->_full_path, $xml->toString, { atomic => 1 }); |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
1; |