Noss/OPML.pm

filter: Statementstmt 83.2% 114 / 137 Branchbran 50.0% 17 / 34 Conditioncond 42.8% 9 / 21 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 85.7% 12 / 14 Podpod 100% 10 / 10 totaltotal 71.1% 163 / 229 CC 36 SCAR 23.5
CC
36
Cov
73%
rename_group
30.0
to_xml
13.4
set_feeds
11.6
CRAP 10.4
« Noss/Lynx.pm ↑ Summary Noss/Timestamp.pm »
1 package WWW::Noss::OPML;
2 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = '2.05';
6  
7 18 ▶sub _read_outline {
Subroutine _read_outline: called CC=8 SCAR=21.9
8  
9 18 my ($self, $node, $groups) = @_;
10  
11 18 my $type = $node->getAttribute('type');
12  
13 18 ▶ return unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 18 18
14  
15 18 my $title = $node->getAttribute('title');
16  
17 18 ▶ if ($type eq 'folder') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'folder') { } 6 12 18
elsif ($type eq 'rss') { } 12 0 12
18  
19 6 ▶ my @new = ($title // (), @$groups);
Condition33%1/3
$title // ()
l!l&&r!l&&!r
600
MC/DC0%0/2
$title // ()
$title ()
Truth table33%1/3
A // B
A= $titleB= ()
ABresult
1X1
011
000
20  
21 6 for my $n ($node->findnodes('./outline')) {
22 12 $self->_read_outline($n, \@new);
23 }
24  
25 } elsif ($type eq 'rss') {
26  
27 12 ▶ my $name = $title // return;
Condition50%1/2
$title // (return)
l!l
120
MC/DC0%0/1
$title // (return)
$title
Truth table50%1/2
A
A= $title
Aresult
11
00
28 12 ▶ my $xmlurl = $node->getAttribute('xmlUrl') // return;
Condition50%1/2
$node->getAttribute("xmlUrl") // (return)
l!l
120
MC/DC0%0/1
$node->getAttribute("xmlUrl") // (return)
$node->getAttribute("xmlUrl")
Truth table50%1/2
A
A= $node->getAttribute("xmlUrl")
Aresult
11
00
29 12 my $text = $node->getAttribute('text');
30 12 my $htmlurl = $node->getAttribute('htmlUrl');
31  
32 12 push @{ $self->feeds }, {
33 title => $name,
34 xml_url => $xmlurl,
35 text => $text,
36 html_url => $htmlurl,
37 groups => [ @$groups ],
38 };
39  
40 } else {
41  
42 0 return;
43  
44 }
45  
46 18 return 1;
47  
48 }
49  
50 1 ▶sub from_perl {
Subroutine from_perl: called CC=2 SCAR=7.0
Pod documented
51  
52 1 my ($class, %param) = @_;
53  
54 1 my $self = bless {}, $class;
55  
56 1 $self->set_title($param{ title });
57 1 ▶ $self->set_feeds($param{ feeds } // []);
Condition50%1/2
$param{"feeds"} // []
l!l
10
MC/DC0%0/1
$param{"feeds"} // []
$param{"feeds"}
Truth table50%1/2
A
A= $param{"feeds"}
Aresult
11
00
58  
59 1 return $self;
60  
61 }
62  
63 3 ▶sub from_xml {
Subroutine from_xml: called CC=4 SCAR=14.0
Pod documented
64  
65 3 my ($class, $file) = @_;
66  
67 3 require XML::LibXML;
68  
69 3 my $self = bless {}, $class;
70  
71 3 my $dom = eval { XML::LibXML->load_xml(location => $file) };
72  
73 3 ▶ unless (defined $dom) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dom) 0 3 3
74 0 die "Failed to read $file as an XML document\n";
75 }
76  
77 3 my ($title) = $dom->findnodes('/opml/head/title');
78  
79 3 ▶ $self->set_title(
Branch: 1 branch
BranchTrueFalseTotal
defined $title ? : 3 0 3
80 defined $title ? $title->textContent : ''
81 );
82  
83 3 $self->set_feeds([]);
84  
85 3 my @outs = $dom->findnodes('/opml/body/outline');
86  
87 3 for my $o (@outs) {
88 6 $self->_read_outline($o, []);
89 }
90  
91 3 return $self;
92  
93 }
94  
95 3 ▶sub to_xml {
Subroutine to_xml: called CC=13 SCAR=25.9
Pod documented
96  
97 3 my ($self, %param) = @_;
98  
99 3 require XML::LibXML;
100  
101 3 my $dom = XML::LibXML::Document->new('1.0', 'UTF-8');
102  
103 3 my $root = XML::LibXML::Element->new('opml');
104 3 $root->setAttribute('version', '1.0');
105  
106 3 $dom->setDocumentElement($root);
107  
108 3 my $head = XML::LibXML::Element->new('head');
109 3 my $title = XML::LibXML::Element->new('title');
110 3 $title->addChild(
111 XML::LibXML::Text->new($self->title)
112 );
113 3 $head->addChild($title);
114 3 $root->addChild($head);
115  
116 3 my $body = XML::LibXML::Element->new('body');
117 3 $root->addChild($body);
118  
119 3 my %folders;
120 my @ungrouped;
121  
122 3 ▶ if (defined $param{ folders } and !$param{ folders }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $param{'folders'} and not $param{'folders'}) { } 1 2 3
Condition66%2/3
defined $param{"folders"} and not $param{"folders"}
!ll&&!rl&&r
201
MC/DC50%1/2
defined $param{"folders"} and not $param{"folders"}
defined $param{"folders"} not $param{"folders"}
Truth table66%2/3
A and not(B)
A= defined $param{"folders"}B= not $param{"folders"}
ABresult
0X0
100
111
123 1 @ungrouped = @{ $self->{ Feeds } };
124 } else {
125 2 for my $f (@{ $self->{ Feeds } }) {
126 8 ▶ if (@{ $f->{ groups } }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$f->{'groups'};}) { } 8 0 8
127 8 for my $g (@{ $f->{ groups } }) {
128 8 push @{ $folders{ $g } }, $f;
129 }
130 } else {
131 0 push @ungrouped, $f;
132 }
133 }
134 }
135  
136 3 for my $g (sort keys %folders) {
137  
138 4 my $folder = XML::LibXML::Element->new('outline');
139 4 $folder->setAttribute('type', 'folder');
140 4 $folder->setAttribute('title', $g);
141 4 $folder->setAttribute('text', $g);
142 4 $folder->setAttribute('description', $g);
143  
144 4 for my $f (@{ $folders{ $g } }) {
145 8 my $feed = XML::LibXML::Element->new('outline');
146 8 $feed->setAttribute('type', 'rss');
147 8 $feed->setAttribute('title', $f->{ title });
148 8 ▶ $feed->setAttribute('text', $f->{ text } // $f->{ title });
Condition33%1/3
$f->{"text"} // $f->{"title"}
l!l&&r!l&&!r
800
MC/DC0%0/2
$f->{"text"} // $f->{"title"}
$f->{"text"} $f->{"title"}
Truth table33%1/3
A // B
A= $f->{"text"}B= $f->{"title"}
ABresult
1X1
011
000
149 8 $feed->setAttribute('xmlUrl', $f->{ xml_url });
150 8 ▶ $feed->setAttribute('htmlUrl', $f->{ html_url }) if defined $f->{ html_url };
Branch: 1 branch
BranchTrueFalseTotal
if defined $f->{"html_url"} 0 8 8
151 8 $folder->addChild($feed);
152 }
153  
154 4 $body->addChild($folder);
155  
156 }
157  
158 3 for my $f (sort { $a->{ title } cmp $b->{ title } } @ungrouped) {
159 4 my $feed = XML::LibXML::Element->new('outline');
160 4 $feed->setAttribute('type', 'rss');
161 4 $feed->setAttribute('title', $f->{ title });
162 4 ▶ $feed->setAttribute('text', $f->{ text } // $f->{ title });
Condition33%1/3
$f->{"text"} // $f->{"title"}
l!l&&r!l&&!r
400
MC/DC0%0/2
$f->{"text"} // $f->{"title"}
$f->{"text"} $f->{"title"}
Truth table33%1/3
A // B
A= $f->{"text"}B= $f->{"title"}
ABresult
1X1
011
000
163 4 $feed->setAttribute('xmlUrl', $f->{ xml_url });
164 4 ▶ $feed->setAttribute('htmlUrl', $f->{ html_url }) if defined $f->{ html_url };
Branch: 1 branch
BranchTrueFalseTotal
if defined $f->{"html_url"} 0 4 4
165 4 $body->addChild($feed);
166 }
167  
168 3 return $dom;
169  
170 }
171  
172 1 ▶sub to_file {
Subroutine to_file: called CC=2 SCAR=7.0
Pod documented
173  
174 1 my ($self, $file, %param) = @_;
175  
176 1 ▶ open my $fh, '>', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Failed to open $file for writing: $!\n" 1 0 1
177 or die "Failed to open $file for writing: $!\n";
178 1 binmode $fh;
179 1 $self->to_xml(%param)->toFH($fh, 1);
180 1 close $fh;
181  
182 1 return $file;
183  
184 }
185  
186 0 ▶sub to_fh {
Subroutine to_fh: not called CC=1 SCAR=6.9
Pod documented
187  
188 0 my ($self, $fh, %param) = @_;
189  
190 0 binmode $fh;
191 0 $self->to_xml(%param)->toFH($fh, 1);
192  
193 0 return $fh;
194  
195 }
196  
197 5 ▶sub title {
Subroutine title: called CC=1 SCAR=0.0
Pod documented
198  
199 5 my ($self) = @_;
200  
201 5 return $self->{ Title };
202  
203 }
204  
205 4 ▶sub set_title {
Subroutine set_title: called CC=2 SCAR=7.6
Pod documented
206  
207 4 my ($self, $new) = @_;
208  
209 4 ▶ unless (defined $new) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new) 0 4 4
210 0 die "title cannot be undefined";
211 }
212  
213 4 $self->{ Title } = $new;
214  
215 }
216  
217 14 ▶sub feeds {
Subroutine feeds: called CC=1 SCAR=0.0
Pod documented
218  
219 14 my ($self) = @_;
220  
221 14 return $self->{ Feeds };
222  
223 }
224  
225 4 ▶sub set_feeds {
Subroutine set_feeds: called CC=7 SCAR=24.5
Pod documented
226  
227 4 my ($self, $new) = @_;
228  
229 4 ▶ unless (ref $new eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $new eq "ARRAY") 0 4 4
230 0 die "feeds must be an array ref";
231 }
232  
233 4 for my $i (0 .. $#$new) {
234 3 ▶ unless (defined $new->[$i]{ xml_url }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new->[$i]{"xml_url"}) 0 3 3
235 0 die "feeds[$i] is missing xml_url";
236 }
237 3 ▶ unless (defined $new->[$i]{ title }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new->[$i]{"title"}) 0 3 3
238 0 die "feeds[$i] is missing title";
239 }
240 3 ▶ if (defined $new->[$i]{ groups } and ref $new->[$i]{ groups } ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new->[$i]{"groups"} and ref $new->[$i]{"groups"} ne "ARRAY") 0 3 3
Condition33%1/3
defined $new->[$i]{"groups"} and ref $new->[$i]{"groups"} ne "ARRAY"
!ll&&!rl&&r
030
MC/DC0%0/2
defined $new->[$i]{"groups"} and ref $new->[$i]{"groups"} ne "ARRAY"
defined $new->[$i]{"groups"} ref $new->[$i]{"groups"} ne "ARRAY"
Truth table33%1/3
A and B
A= defined $new->[$i]{"groups"}B= ref $new->[$i]{"groups"} ne "ARRAY"
ABresult
0X0
100
111
241 0 die "feeds[$i]{ groups } is not an array ref";
242 }
243 }
244  
245 4 $self->{ Feeds } = $new;
246  
247 }
248  
249 0 ▶sub rename_group {
Subroutine rename_group: not called CC=5 SCAR=34.0
Pod documented
250  
251 0 my ($self, $old, $new) = @_;
252  
253 0 my $rn = 0;
254  
255 0 for my $f (@{ $self->{ Feeds } }) {
256  
257 0 ▶ next unless defined $f->{ groups };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f->{"groups"} 0 0 0
258  
259 0 for my $i (0 .. $#{ $f->{ groups } }) {
260 0 ▶ if ($f->{ groups }[$i] eq $old) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->{"groups"}[$i] eq $old) 0 0 0
261 0 $f->{ groups }[$i] = $new;
262 0 $rn++;
263 }
264 }
265  
266 }
267  
268 0 return $rn;
269  
270 }
271  
272 2 1;
273  
274 =head1 NAME
275  
276 WWW::Noss::OPML - OPML file reader/writer
277  
278 =head1 USAGE
279  
280 use WWW::Noss::OPML;
281  
282 my $opml = WWW::Noss::OPML->from_perl(
283 title => 'Name',
284 feeds => [ { ... }, ... ],
285 );
286  
287 $opml->to_file('path/to/xml');
288  
289 # Read from file
290 $opml = WWW::Noss::OPML->from_xml('path/to/xml');
291  
292 =head1 DESCRIPTION
293  
294 B<WWW::Noss::OPML> is a module that provides an interface for reading and
295 writing OPML files. This is a private module, please consult the L<noss>
296 manual for user documentation.
297  
298 =head1 METHODS
299  
300 =over 4
301  
302 =item $opml = WWW::Noss::OPML->from_perl(%param)
303  
304 Creates a new B<WWW::Noss::OPML> object from the parameters supplied via the
305 C<%param> hash.
306  
307 The following are a list of valid fields for the C<%param> hash. The only
308 required field is C<title>.
309  
310 =over 4
311  
312 =item title
313  
314 Title string for the OPML file.
315  
316 =item feeds
317  
318 Array ref of outline feed hashes. The hashes should look something like this:
319  
320 {
321 title => ..., # required
322 xml_url => ..., # required
323 text => ...,
324 html_url => ...,
325 groups => [ ... ],
326 }
327  
328 =back
329  
330 =item $opml = WWW::Noss::OPML->from_xml($file)
331  
332 Create B<WWW::Noss::OPML> object from the given OPML file.
333  
334 =item $dom = $opml->to_xml([ %param ])
335  
336 Returns a L<XML::LibXML::Document> DOM object representing the object.
337  
338 C<%param> is an optional hash of additional parameters to configure the
339 converted XML structure. The following are valid values:
340  
341 =over 4
342  
343 =item folders
344  
345 Boolean determining whether to include outline folders in the XML strucutre.
346 Defaults to true.
347  
348 =back
349  
350 =item $file = $opml->to_file($file, [ %param ])
351  
352 Writes the OPML object's XML structure to C<$file>. Returns C<$file> on
353 success, dies on failure.
354  
355 C<%param> is an optional hash of additional parameters. Accepts the same
356 options as C<to_xml()>.
357  
358 =item $fh = $opml->to_fh($fh, [ %param ])
359  
360 Writes the OPML object's XML structure to the C<$fh> file handle. Returns C<$fh>
361 on success, dies on failure.
362  
363 C<%param> is an optional hash of additional parameters. Accepts the same
364 options as C<to_xml()>.
365  
366 =item $title = $opml->title()
367  
368 =item $opml->set_title($title)
369  
370 Getter/setter for the OPML's title attribute.
371  
372 =item \@feeds = $opml->feeds()
373  
374 =item $opml->set_feeds(\@feeds)
375  
376 Getter/setter for the OPML's feeds attribute.
377  
378 =item $rn = $opml->rename_group($old, $new)
379  
380 Goes through each feed's group list and renames the group C<$old> to C<$new>.
381 Returns the number of groups renamed.
382  
383 =back
384  
385 =head1 AUTHOR
386  
387 Written by Samuel Young, E<lt>samyoung@posteo.comE<gt>.
388  
389 This project's source can be found on its
390 L<Codeberg page|https://codeberg.org/1-1sam/noss.git>. Comments and pull
391 requests are welcome!
392  
393 =head1 COPYRIGHT
394  
395 Copyright (C) 2025-2026 Samuel Young
396  
397 This program is free software: you can redistribute it and/or modify
398 it under the terms of the GNU General Public License as published by
399 the Free Software Foundation, either version 3 of the License, or
400 (at your option) any later version.
401  
402 =head1 SEE ALSO
403  
404 L<XML::LibXML::Document>, L<noss>
405  
406 =cut
407  
408 # vim: expandtab shiftwidth=4
« Noss/Lynx.pm ↑ Summary Noss/Timestamp.pm »