| 1 | package Miril::Publisher; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use autodie; | ||
| 6 | |||
| 7 | use Try::Tiny; | ||
| 8 | use Exception::Class; | ||
| 9 | use Carp; | ||
| 10 | use Module::Load; | ||
| 11 | use Miril::Warning; | ||
| 12 | use Miril::Exception; | ||
| 13 | use Miril::Config; | ||
| 14 | use Miril::List; | ||
| 15 | use Ref::List qw(list); | ||
| 16 | use File::Spec::Functions qw(catfile splitpath); | ||
| 17 | use Text::Sprintf::Named; | ||
| 18 | use Miril::URL; | ||
| 19 | use File::Path qw(make_path); | ||
| 20 | |||
| 21 | |||
| 22 | our $VERSION = '0.007'; | ||
| 23 | |||
| 24 | sub publish { | ||
| 25 | my ($class, $miril, $rebuild) = @_; | ||
| 26 | |||
| 27 | my $cfg = $miril->cfg; | ||
| 28 | |||
| 29 | my (@to_create, @to_update); | ||
| 30 | |||
| 31 | my @posts = $miril->store->get_posts; | ||
| 32 | |||
| 33 | foreach my $post (@posts) | ||
| 34 | { | ||
| 35 | if (-x $post->out_path) { | ||
| 36 | # TODO - check if this is correct! | ||
| 37 | if ( $rebuild or ($post->modified->epoch > -M $post->out_path) ) { | ||
| 38 | push @to_update, $post->id; | ||
| 39 | } | ||
| 40 | } else { | ||
| 41 | push @to_create, $post->id; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | for (@to_create, @to_update) { | ||
| 46 | my $post = $miril->store->get_post($_); | ||
| 47 | |||
| 48 | my $output = $miril->tmpl->load( | ||
| 49 | name => $post->type->template, | ||
| 50 | params => { | ||
| 51 | post => $post, | ||
| 52 | cfg => $cfg, | ||
| 53 | title => $post->title, | ||
| 54 | id => $post->id, | ||
| 55 | }); | ||
| 56 | |||
| 57 | _file_write($post->out_path, $output); | ||
| 58 | } | ||
| 59 | |||
| 60 | foreach my $list ($cfg->lists->list) | ||
| 61 | { | ||
| 62 | my @posts; | ||
| 63 | |||
| 64 | # accept ids | ||
| 65 | if ( $list->match->id ) | ||
| 66 | { | ||
| 67 | push @posts, $miril->store->get_post($_) for $list->match->id->list; | ||
| 68 | } | ||
| 69 | else | ||
| 70 | { | ||
| 71 | my @params = qw( | ||
| 72 | author | ||
| 73 | type | ||
| 74 | status | ||
| 75 | topic | ||
| 76 | created_before | ||
| 77 | created_on | ||
| 78 | created_after | ||
| 79 | updated_before | ||
| 80 | updated_on | ||
| 81 | updated_after | ||
| 82 | published_before | ||
| 83 | published_on | ||
| 84 | published_after | ||
| 85 | last | ||
| 86 | ); | ||
| 87 | |||
| 88 | my %params; | ||
| 89 | |||
| 90 | foreach my $param (@params) { | ||
| 91 | if ( exists $list->match->{$param} ) { | ||
| 92 | $params{$param} = $list->match->{$param}; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | @posts = $miril->store->get_posts(%params); | ||
| 97 | } | ||
| 98 | |||
| 99 | if ($list->group) { | ||
| 100 | |||
| 101 | |||
| 102 | my $list_main = Miril::List->new( | ||
| 103 | posts => \@posts, | ||
| 104 | title => $list->name, | ||
| 105 | sort => $cfg->sort, | ||
| 106 | id => $list->id, | ||
| 107 | ); | ||
| 108 | |||
| 109 | my $group_key = $list->group; | ||
| 110 | foreach my $list_group ($list_main->group($group_key)) | ||
| 111 | { | ||
| 112 | |||
| 113 | my ($f_args, $tag_url_id); | ||
| 114 | |||
| 115 | if ($group_key eq 'topic') | ||
| 116 | { | ||
| 117 | $f_args = { topic => $list_group->key->id }; | ||
| 118 | $tag_url_id = $list->id . '/' . $list_group->key->id; | ||
| 119 | } | ||
| 120 | elsif ($group_key eq 'type') | ||
| 121 | { | ||
| 122 | $f_args = { type => $list_group->key->id }; | ||
| 123 | $tag_url_id = $list->id . '/' . $list_group->key->id; | ||
| 124 | } | ||
| 125 | elsif ($group_key eq 'author') | ||
| 126 | { | ||
| 127 | $f_args = { author => $list_group->key}; | ||
| 128 | $tag_url_id = $list->id . '/' . $list_group->key; | ||
| 129 | } | ||
| 130 | else | ||
| 131 | { | ||
| 132 | $f_args = { | ||
| 133 | year => $list_group->key->strftime('%Y'), | ||
| 134 | month => $list_group->key->strftime('%m'), | ||
| 135 | date => $list_group->key->strftime('%d'), | ||
| 136 | }; | ||
| 137 | } | ||
| 138 | |||
| 139 | my $formatter = Text::Sprintf::Named->new({fmt => $list->location}); | ||
| 140 | my $location = $formatter->format({args => $f_args}); | ||
| 141 | $list_group->{url} = $miril->util->inflate_list_url($tag_url_id, $list->location); | ||
| 142 | |||
| 143 | my $output = $miril->tmpl->load( | ||
| 144 | name => $list->template, | ||
| 145 | params => { | ||
| 146 | list => $list_group, | ||
| 147 | cfg => $cfg, | ||
| 148 | title => $list_group->title, | ||
| 149 | id => $list->id, | ||
| 150 | }); | ||
| 151 | |||
| 152 | my $new_filename = catfile($cfg->output_path, $location); | ||
| 153 | _file_write($new_filename, $output); | ||
| 154 | } | ||
| 155 | } | ||
| 156 | elsif ($list->page) | ||
| 157 | { | ||
| 158 | my $pager = Data::Page->new; | ||
| 159 | my $posts_no = scalar @posts; | ||
| 160 | $pager->total_entries($posts_no); | ||
| 161 | $pager->entries_per_page($list->page); | ||
| 162 | foreach my $page_no ($pager->first_page .. $pager->last_page) | ||
| 163 | { | ||
| 164 | my $current_pager = Data::Page->new; | ||
| 165 | $current_pager->total_entries($posts_no); | ||
| 166 | $current_pager->entries_per_page($list->page); | ||
| 167 | $current_pager->current_page($page_no); | ||
| 168 | my @current_posts = $pager->splice(\@posts); | ||
| 169 | |||
| 170 | my $formatter = Text::Sprintf::Named->new({fmt => $list->location}); | ||
| 171 | my $location = $formatter->format({args => { page => $page_no }}); | ||
| 172 | |||
| 173 | my $list_page = Miril::List->new( | ||
| 174 | posts => \@current_posts, | ||
| 175 | pager => $current_pager, | ||
| 176 | title => $list->name, | ||
| 177 | url => $miril->util->inflate_list_url(undef, $location), | ||
| 178 | sort => $cfg->sort, | ||
| 179 | id => $list->id, | ||
| 180 | ); | ||
| 181 | |||
| 182 | my $output = $miril->tmpl->load( | ||
| 183 | name => $list->template, | ||
| 184 | params => { | ||
| 185 | list => $list_page, | ||
| 186 | cfg => $cfg, | ||
| 187 | title => $list_page->title, | ||
| 188 | id => $list->id, | ||
| 189 | }); | ||
| 190 | |||
| 191 | my $new_filename = catfile($cfg->output_path, $location); | ||
| 192 | _file_write($new_filename, $output); | ||
| 193 | } | ||
| 194 | } | ||
| 195 | else | ||
| 196 | { | ||
| 197 | my $output = $miril->tmpl->load( | ||
| 198 | name => $list->template, | ||
| 199 | params => { | ||
| 200 | list => Miril::List->new( | ||
| 201 | posts => \@posts, | ||
| 202 | title => $list->name, | ||
| 203 | url => $miril->util->inflate_list_url($list->id, $list->location), | ||
| 204 | id => $list->id, | ||
| 205 | sort => $cfg->sort, | ||
| 206 | ), | ||
| 207 | cfg => $cfg, | ||
| 208 | title => $list->name, | ||
| 209 | id => $list->id, | ||
| 210 | } | ||
| 211 | ); | ||
| 212 | |||
| 213 | my $new_filename = catfile($cfg->output_path, $list->location); | ||
| 214 | _file_write($new_filename, $output); | ||
| 215 | } | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | sub _file_write { | ||
| 220 | my ($filename, $data) = @_; | ||
| 221 | my ($volume, $directories, $file) = splitpath($filename); | ||
| 222 | my $path = $volume . $directories; | ||
| 223 | try { | ||
| 224 | make_path($path); | ||
| 225 | my $fh = IO::File->new($filename, ">") or die $!; | ||
| 226 | $fh->print($data); | ||
| 227 | $fh->close; | ||
| 228 | } catch { | ||
| 229 | Miril::Exception->throw( | ||
| 230 | errorvar => $_, | ||
| 231 | message => 'Could not save information', | ||
| 232 | ); | ||
| 233 | } | ||
| 234 | #my $post_fh = IO::File->new($filename, "<") or die $!; | ||
| 235 | #while (<$post_fh>) | ||
| 236 | #{ | ||
| 237 | # warn "BOM before write!" if /\x{FEFF}/; | ||
| 238 | #} | ||
| 239 | } | ||
| 240 | |||
| 241 | 1; | ||
| 242 | |||
| 243 | |||
| 244 |