| 1 | package XML::OPML::SimpleGen; | ||||||||||
| 2 | |||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 4 | ▶ | use base 'Class::Accessor'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use DateTime; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 4 | ▶ | use POSIX qw(setlocale LC_TIME LC_CTYPE); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 4 | __PACKAGE__->mk_accessors(qw|groups xml_options outline group xml_head xml_outlines xml|); | |||||||||
| 13 | |||||||||||
| 14 | # Version set by dist.ini; do not change here. | ||||||||||
| 15 | 4 | our $VERSION = '0.07'; # VERSION | |||||||||
| 16 | |||||||||||
| 17 | 3 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 18 | 3 | my $class = shift; | |||||||||
| 19 | 3 | my @args = @_; | |||||||||
| 20 | |||||||||||
| 21 | 3 | my $args = { | |||||||||
| 22 | groups => {}, | ||||||||||
| 23 | |||||||||||
| 24 | xml => { | ||||||||||
| 25 | version => '1.1', | ||||||||||
| 26 | @args, | ||||||||||
| 27 | }, | ||||||||||
| 28 | |||||||||||
| 29 | # XML::Simple options | ||||||||||
| 30 | xml_options => { | ||||||||||
| 31 | RootName => 'opml', | ||||||||||
| 32 | XMLDecl => '<?xml version="1.0" encoding="utf-8" ?>', | ||||||||||
| 33 | AttrIndent => 1, | ||||||||||
| 34 | }, | ||||||||||
| 35 | |||||||||||
| 36 | # default values for nodes | ||||||||||
| 37 | outline => { | ||||||||||
| 38 | type => 'rss', | ||||||||||
| 39 | version => 'RSS', | ||||||||||
| 40 | text => '', | ||||||||||
| 41 | title => '', | ||||||||||
| 42 | description => '', | ||||||||||
| 43 | }, | ||||||||||
| 44 | |||||||||||
| 45 | group => { | ||||||||||
| 46 | isOpen => 'true', | ||||||||||
| 47 | }, | ||||||||||
| 48 | |||||||||||
| 49 | xml_head => {}, | ||||||||||
| 50 | xml_outlines => [], | ||||||||||
| 51 | |||||||||||
| 52 | id => 1, | ||||||||||
| 53 | }; | ||||||||||
| 54 | |||||||||||
| 55 | 3 | my $self = bless $args, $class; | |||||||||
| 56 | |||||||||||
| 57 | # Force locale to 'C' rather than local, then reset after setting times. | ||||||||||
| 58 | # Fixes RT51000. Thanks to KAPPA for the patch. | ||||||||||
| 59 | 3 | my $old_loc = POSIX::setlocale(LC_TIME, "C"); | |||||||||
| 60 | 3 | my $ts_ar = [ localtime() ]; | |||||||||
| 61 | 3 | $self->head( | |||||||||
| 62 | title => '', | ||||||||||
| 63 | $self->_date( dateCreated => $ts_ar ), | ||||||||||
| 64 | $self->_date( dateModified => $ts_ar ), | ||||||||||
| 65 | ); | ||||||||||
| 66 | 3 | POSIX::setlocale(LC_TIME,$old_loc); | |||||||||
| 67 | |||||||||||
| 68 | 3 | return $self; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | 8 | ▶ | sub _date { | ||||||||
|
Subroutine
_date: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 8 | my $self = shift; | |||||||||
| 73 | 8 | my $type = shift; # dateCreated or dateModified. | |||||||||
| 74 | 8 | my $ts_ar = shift; # e.g [ localtime() ] | |||||||||
| 75 | |||||||||||
| 76 | 8 | my %arg; | |||||||||
| 77 | @arg{qw(second minute hour day month year)} = ( | ||||||||||
| 78 | 8 | @{$ts_ar}[0..3], | |||||||||
| 79 | $ts_ar->[4]+1, | ||||||||||
| 80 | $ts_ar->[5]+1900 ); | ||||||||||
| 81 | 8 | my $dt = DateTime->new( %arg ); | |||||||||
| 82 | 8 | return ( $type => $dt->strftime('%a, %e %b %Y %H:%M:%S %z') ); | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 0 | ▶ | sub id { | ||||||||
|
Subroutine
id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 86 | 0 | my $self = shift; | |||||||||
| 87 | |||||||||||
| 88 | 0 | return $self->{id}++; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | 4 | ▶ | sub head { | ||||||||
|
Subroutine
head: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 92 | 4 | my $self = shift; | |||||||||
| 93 | 4 | my $data = {@_}; | |||||||||
| 94 | |||||||||||
| 95 | #this is necessary, otherwise XML::Simple will just generate attributes | ||||||||||
| 96 | 4 | ▶ | while (my ($key,$value) = each %{ $data }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | 11 | $self->xml_head->{$key} = [ $value ]; | |||||||||
| 98 | } | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | 0 | ▶ | sub add_group { | ||||||||
|
Subroutine
add_group: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 102 | 0 | my $self = shift; | |||||||||
| 103 | 0 | my %defaults = %{$self->group}; | |||||||||
| 104 | 0 | my $data = { | |||||||||
| 105 | id => $self->id, | ||||||||||
| 106 | %defaults, | ||||||||||
| 107 | @_ }; | ||||||||||
| 108 | |||||||||||
| 109 | 0 | ▶ | die "Need to define 'text' attribute" unless defined $data->{text}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | |||||||||||
| 111 | 0 | $data->{outline} = []; | |||||||||
| 112 | |||||||||||
| 113 | 0 | push @{$self->xml_outlines}, $data; | |||||||||
| 114 | 0 | $self->groups->{$data->{text}} = $data->{outline}; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 0 | ▶ | sub insert_outline { | ||||||||
|
Subroutine
insert_outline: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 118 | 0 | my $self = shift; | |||||||||
| 119 | 0 | my %defaults = %{$self->outline}; | |||||||||
| 120 | 0 | my $data = { | |||||||||
| 121 | id => $self->id, | ||||||||||
| 122 | %defaults, | ||||||||||
| 123 | @_}; | ||||||||||
| 124 | |||||||||||
| 125 | 0 | my $parent = $self->xml_outlines; | |||||||||
| 126 | |||||||||||
| 127 | 0 | ▶ | if (exists $data->{group}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 0 | ▶ | if (exists $self->groups->{$data->{group}}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 0 | $parent = $self->groups->{$data->{group}}; | |||||||||
| 130 | 0 | delete($data->{group}); | |||||||||
| 131 | } | ||||||||||
| 132 | else { | ||||||||||
| 133 | 0 | $self->add_group('text' => $data->{group}); | |||||||||
| 134 | 0 | $self->insert_outline(%$data); | |||||||||
| 135 | 0 | return; | |||||||||
| 136 | } | ||||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | 0 | push @{$parent}, $data; | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | 0 | ▶ | sub add_outline { | ||||||||
|
Subroutine
add_outline: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 143 | 0 | my $self = shift; | |||||||||
| 144 | 0 | $self->insert_outline(@_); | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | 2 | ▶ | sub as_string { | ||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 148 | 2 | my $self = shift; | |||||||||
| 149 | |||||||||||
| 150 | 2 | require XML::Simple; | |||||||||
| 151 | 2 | my $xs = XML::Simple->new(); | |||||||||
| 152 | |||||||||||
| 153 | 2 | return $xs->XMLout( $self->_mk_hashref, %{$self->xml_options} ); | |||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | 2 | ▶ | sub _mk_hashref { | ||||||||
|
Subroutine
_mk_hashref: called CC=1 SCAR=0.0
| |||||||||||
| 157 | 2 | my $self = shift; | |||||||||
| 158 | |||||||||||
| 159 | my $hashref = { | ||||||||||
| 160 | 2 | %{$self->xml}, | |||||||||
| 161 | head => $self->xml_head, | ||||||||||
| 162 | body => { outline => $self->xml_outlines }, | ||||||||||
| 163 | }; | ||||||||||
| 164 | |||||||||||
| 165 | 2 | return $hashref; | |||||||||
| 166 | } | ||||||||||
| 167 | |||||||||||
| 168 | 0 | ▶ | sub save { | ||||||||
|
Subroutine
save: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 169 | 0 | my $self = shift; | |||||||||
| 170 | 0 | my $filename = shift; | |||||||||
| 171 | |||||||||||
| 172 | 0 | require XML::Simple; | |||||||||
| 173 | 0 | my $xs = XML::Simple->new(); | |||||||||
| 174 | |||||||||||
| 175 | 0 | $xs->XMLout( $self->_mk_hashref, %{$self->xml_options}, OutputFile => $filename ); | |||||||||
| 176 | } | ||||||||||
| 177 | |||||||||||
| 178 | 4 | 1; | |||||||||
| 179 | |||||||||||
| 180 | # ABSTRACT: create OPML using XML::Simple | ||||||||||
| 181 | |||||||||||
| 182 | __END__ | ||||||||||