| 1 | 6 | ▶ | use strictures 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | package Mojito::Page::Parse; | ||||||||||
| 3 | 6 | $Mojito::Page::Parse::VERSION = '0.25'; | |||||||||
| 4 | 6 | ▶ | use 5.010; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 6 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 6 | ▶ | use MooX::Types::MooseLike::Base qw(:all); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 6 | ▶ | use Data::Dumper::Concise; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | =head1 Name | ||||||||||
| 11 | |||||||||||
| 12 | Mojito::Page::Parse - turn page source into a page structure | ||||||||||
| 13 | |||||||||||
| 14 | =cut | ||||||||||
| 15 | |||||||||||
| 16 | # This is the page source | ||||||||||
| 17 | 6 | has 'page' => ( | |||||||||
| 18 | is => 'rw', | ||||||||||
| 19 | isa => Value, | ||||||||||
| 20 | ); | ||||||||||
| 21 | 6 | has 'sections' => ( | |||||||||
| 22 | is => 'ro', | ||||||||||
| 23 | isa => ArrayRef[HashRef], | ||||||||||
| 24 | lazy => 1, | ||||||||||
| 25 | builder => 'build_sections', | ||||||||||
| 26 | ); | ||||||||||
| 27 | 6 | has 'page_structure' => ( | |||||||||
| 28 | is => 'rw', | ||||||||||
| 29 | isa => HashRef, | ||||||||||
| 30 | lazy => 1, | ||||||||||
| 31 | builder => 'build_page_structure', | ||||||||||
| 32 | ); | ||||||||||
| 33 | has 'default_format' => ( | ||||||||||
| 34 | is => 'rw', | ||||||||||
| 35 | isa => Value, | ||||||||||
| 36 | lazy => 1, | ||||||||||
| 37 | 2 | ▶ | default => sub { 'HTML' }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 38 | 6 | ); | |||||||||
| 39 | 6 | has 'created' => ( | |||||||||
| 40 | is => 'ro', | ||||||||||
| 41 | isa => Int, | ||||||||||
| 42 | ); | ||||||||||
| 43 | has 'last_modified' => ( | ||||||||||
| 44 | is => 'ro', | ||||||||||
| 45 | isa => Int, | ||||||||||
| 46 | 4 | ▶ | default => sub { time() }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 6 | ); | |||||||||
| 48 | has 'section_open_regex' => ( | ||||||||||
| 49 | is => 'ro', | ||||||||||
| 50 | isa => RegexpRef, | ||||||||||
| 51 | 4 | ▶ | default => sub { qr/<sx\.[^>]+>/ }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 52 | 6 | ); | |||||||||
| 53 | has 'section_close_regex' => ( | ||||||||||
| 54 | is => 'ro', | ||||||||||
| 55 | isa => RegexpRef, | ||||||||||
| 56 | 4 | ▶ | default => sub { qr(</sx>) }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 57 | 6 | ); | |||||||||
| 58 | has 'debug' => ( | ||||||||||
| 59 | is => 'rw', | ||||||||||
| 60 | isa => Bool, | ||||||||||
| 61 | 4 | ▶ | default => sub { 0 }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 62 | 6 | ); | |||||||||
| 63 | has 'messages' => ( | ||||||||||
| 64 | is => 'rw', | ||||||||||
| 65 | isa => ArrayRef, | ||||||||||
| 66 | 4 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 67 | 6 | ); | |||||||||
| 68 | 6 | has 'message_string' => ( | |||||||||
| 69 | is => 'ro', | ||||||||||
| 70 | isa => Value, | ||||||||||
| 71 | lazy => 1, | ||||||||||
| 72 | builder => '_build_message_string', | ||||||||||
| 73 | ); | ||||||||||
| 74 | 3 | ▶ | sub _build_message_string { | ||||||||
|
Subroutine
_build_message_string: called CC=2 SCAR=7.6
| |||||||||||
| 75 | 3 | my ($self) = (shift); | |||||||||
| 76 | 3 | ▶ | return join ', ', @{$self->messages} if $self->messages; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 0 | return; | |||||||||
| 78 | } | ||||||||||
| 79 | |||||||||||
| 80 | =head2 has_nested_section | ||||||||||
| 81 | |||||||||||
| 82 | Test if we have nested sections. | ||||||||||
| 83 | |||||||||||
| 84 | =cut | ||||||||||
| 85 | |||||||||||
| 86 | 6 | ▶ | sub has_nested_section { | ||||||||
|
Subroutine
has_nested_section: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||
| 87 | 6 | my ($self) = @_; | |||||||||
| 88 | |||||||||||
| 89 | 6 | my $section_open_regex = $self->section_open_regex; | |||||||||
| 90 | |||||||||||
| 91 | #die "Got no page" if !$self->page; | ||||||||||
| 92 | 6 | my @stuff_between_section_opens = | |||||||||
| 93 | $self->page =~ m/${section_open_regex}(.*?)${section_open_regex}/si; | ||||||||||
| 94 | |||||||||||
| 95 | # If when find a section ending tag in the middle of the two consecutive | ||||||||||
| 96 | # opening section tags then we know first section has been closed and thus | ||||||||||
| 97 | # does NOT contain a nested section. | ||||||||||
| 98 | 6 | foreach my $tweener (@stuff_between_section_opens) { | |||||||||
| 99 | 4 | ▶ | if ( $tweener =~ m/<\/sx>/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | |||||||||||
| 101 | # The tweener section could cause us to think we're not nested | ||||||||||
| 102 | # due to an nested section of the general type (not the class=mc_ type) | ||||||||||
| 103 | # In this case we need to count the number of open and closed sections | ||||||||||
| 104 | # If they are the same then we dont' have </sec> left over to close the first | ||||||||||
| 105 | # and thus we have a nest. | ||||||||||
| 106 | 2 | my @opens = $tweener =~ m/(<sx[^>]*>)/sg; | |||||||||
| 107 | 2 | my @closes = $tweener =~ m/(<\/sx>)/sg; | |||||||||
| 108 | 2 | ▶ | if ( scalar @opens == scalar @closes ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 0 | return 1; | |||||||||
| 110 | } | ||||||||||
| 111 | } | ||||||||||
| 112 | else { | ||||||||||
| 113 | 2 | return 1; | |||||||||
| 114 | } | ||||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 4 | return 0; | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | =head2 add_implicit_sections | ||||||||||
| 121 | |||||||||||
| 122 | Add implicit sections to assist the building of the page_struct. | ||||||||||
| 123 | |||||||||||
| 124 | =cut | ||||||||||
| 125 | |||||||||||
| 126 | 9 | ▶ | sub add_implicit_sections { | ||||||||
|
Subroutine
add_implicit_sections: called CC=3 SCAR=11.5
Pod
documented
| |||||||||||
| 127 | 9 | my ($self) = @_; | |||||||||
| 128 | |||||||||||
| 129 | 9 | my $page = $self->page; | |||||||||
| 130 | |||||||||||
| 131 | # Add implicit sections in between explicit sections (if needed) | ||||||||||
| 132 | 9 | ▶ | if ( $page =~ m/<\/sx>(?!\s*<sx\.).*?<sx\./si ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 5 | $page =~ s/<\/sx>(?!\s*<sx\.)(.*?)<sx\./<\/sx>\n<sx.Implicit>$1<\/sx>\n<sx./sig; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | # Add implicit section at the beginning (if needed) | ||||||||||
| 137 | 9 | $page =~ s/(?<!<sx\.\w)(<sx\.\w)/<\/sx>\n$1/si; | |||||||||
| 138 | 9 | $page = "\n<sx.Implicit>\n${page}"; | |||||||||
| 139 | |||||||||||
| 140 | # Add implicit section at the end (if needed) | ||||||||||
| 141 | 9 | $page =~ s/(<\/sx>)(?!.*<\/sx>)/$1\n<sx.Implicit>/si; | |||||||||
| 142 | 9 | $page .= '</sx>'; | |||||||||
| 143 | |||||||||||
| 144 | # cut empty implicits | ||||||||||
| 145 | 9 | $page =~ s/<sx\.Implicit>\s*<\/sx>//sig; | |||||||||
| 146 | |||||||||||
| 147 | 9 | ▶ | if ( $self->debug ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | 0 | say "PREMATCH: ", ${^PREMATCH}; | |||||||||
| 149 | 0 | say "MATCH: ${^MATCH}"; | |||||||||
| 150 | 0 | say "POSTMATCH: ", ${^POSTMATCH}; | |||||||||
| 151 | 0 | say "page: $page"; | |||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | 9 | return $page; | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | =head2 parse_sections | ||||||||||
| 158 | |||||||||||
| 159 | Extract section class and content from the page. | ||||||||||
| 160 | |||||||||||
| 161 | =cut | ||||||||||
| 162 | |||||||||||
| 163 | 4 | ▶ | sub parse_sections { | ||||||||
|
Subroutine
parse_sections: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 164 | 4 | my ( $self, $page ) = @_; | |||||||||
| 165 | |||||||||||
| 166 | 4 | my $sections; | |||||||||
| 167 | 4 | my @sections = $page =~ m/(<sx\.[^>]+>.*?<\/sx>)/sig; | |||||||||
| 168 | 4 | foreach my $sx (@sections) { | |||||||||
| 169 | |||||||||||
| 170 | # Extract class and content | ||||||||||
| 171 | 12 | my ( $class, $content ) = $sx =~ m/<sx\.([^>]+)>(.*)?<\/sx>/si; | |||||||||
| 172 | 12 | push @{$sections}, { class => $class, content => $content }; | |||||||||
| 173 | } | ||||||||||
| 174 | |||||||||||
| 175 | 4 | return $sections; | |||||||||
| 176 | } | ||||||||||
| 177 | |||||||||||
| 178 | =head2 build_sections | ||||||||||
| 179 | |||||||||||
| 180 | Wrap up the getting of sections process. | ||||||||||
| 181 | |||||||||||
| 182 | =cut | ||||||||||
| 183 | |||||||||||
| 184 | 4 | ▶ | sub build_sections { | ||||||||
|
Subroutine
build_sections: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 185 | 4 | my $self = shift; | |||||||||
| 186 | |||||||||||
| 187 | # Deal with nested sections gracefully by adding a message | ||||||||||
| 188 | # to bubble up to the view and display in the #message_area. | ||||||||||
| 189 | 4 | ▶ | if ( $self->has_nested_section ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | 1 | $self->messages( [ @{$self->messages}, 'haz nested sexes'] ); | |||||||||
| 191 | } | ||||||||||
| 192 | 4 | my $page = $self->add_implicit_sections; | |||||||||
| 193 | |||||||||||
| 194 | 4 | return $self->parse_sections($page); | |||||||||
| 195 | } | ||||||||||
| 196 | |||||||||||
| 197 | =head2 build_page_structure | ||||||||||
| 198 | |||||||||||
| 199 | It's just an href that we'll persist as a Mongo document. | ||||||||||
| 200 | |||||||||||
| 201 | =cut | ||||||||||
| 202 | |||||||||||
| 203 | 2 | ▶ | sub build_page_structure { | ||||||||
|
Subroutine
build_page_structure: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 204 | 2 | my $self = shift; | |||||||||
| 205 | |||||||||||
| 206 | 2 | my $return = { | |||||||||
| 207 | sections => $self->sections, | ||||||||||
| 208 | default_format => $self->default_format, | ||||||||||
| 209 | |||||||||||
| 210 | # created => '1234567890', | ||||||||||
| 211 | # last_modified => time(), | ||||||||||
| 212 | page_source => $self->page, | ||||||||||
| 213 | |||||||||||
| 214 | # Set the message last to pick any builder message above | ||||||||||
| 215 | # e.g. ->sections can set a 'nested sections' message. | ||||||||||
| 216 | message => $self->message_string, | ||||||||||
| 217 | }; | ||||||||||
| 218 | 2 | return $return; | |||||||||
| 219 | } | ||||||||||
| 220 | |||||||||||
| 221 | 6 | 1 | |||||||||