| 1 | #! /bin/false | ||
| 2 | |||
| 3 | # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>, | ||
| 4 | # all rights reserved. | ||
| 5 | |||
| 6 | # This program is free software: you can redistribute it and/or modify | ||
| 7 | # it under the terms of the GNU General Public License as published by | ||
| 8 | # the Free Software Foundation; either version 3 of the License, or | ||
| 9 | # (at your option) any later version. | ||
| 10 | |||
| 11 | # This program is distributed in the hope that it will be useful, | ||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | # GNU General Public License for more details. | ||
| 15 | |||
| 16 | # You should have received a copy of the GNU General Public License | ||
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | |||
| 19 | package Qgoda::Migrator::Jekyll; | ||
| 20 | $Qgoda::Migrator::Jekyll::VERSION = 'v0.9.2'; | ||
| 21 | use strict; | ||
| 22 | |||
| 23 | use Locale::TextDomain qw(qgoda); | ||
| 24 | use File::Find; | ||
| 25 | use YAML::XS; | ||
| 26 | |||
| 27 | use Qgoda; | ||
| 28 | use Qgoda::Util qw(empty read_file write_file yaml_error safe_yaml_load); | ||
| 29 | use Qgoda::Migrator::Jekyll::LiquidConverter; | ||
| 30 | |||
| 31 | use base qw(Qgoda::Migrator); | ||
| 32 | |||
| 33 | sub migrate { | ||
| 34 | my ($self) = @_; | ||
| 35 | |||
| 36 | my $qgoda = Qgoda->new; | ||
| 37 | |||
| 38 | my $config = $self->readConfig; | ||
| 39 | |||
| 40 | my $src_dir = $config->{source}; | ||
| 41 | $src_dir = '.' if empty $config->{source}; | ||
| 42 | $self->{_src_dir} = $src_dir; | ||
| 43 | |||
| 44 | my $out_dir = $qgoda->getOption('output_directory'); | ||
| 45 | $out_dir = '_migrated' if empty $out_dir; | ||
| 46 | $self->{_out_dir} = $out_dir; | ||
| 47 | |||
| 48 | my $layouts_dir = delete $config->{layouts_dir}; | ||
| 49 | $layouts_dir = '_layouts' if empty $layouts_dir; | ||
| 50 | $self->{__layouts_dir} = $layouts_dir; | ||
| 51 | |||
| 52 | my $plugins_dir = delete $config->{plugins_dir}; | ||
| 53 | $plugins_dir = '_plugins' if empty $plugins_dir; | ||
| 54 | $self->markFileDone($plugins_dir); | ||
| 55 | |||
| 56 | my $destination = delete $config->{destination}; | ||
| 57 | $destination = '_site' if empty $destination; | ||
| 58 | $self->markFileDone($destination); | ||
| 59 | |||
| 60 | my $includes_dir = delete $config->{includes_dir}; | ||
| 61 | $includes_dir = '_includes' if empty $includes_dir; | ||
| 62 | $self->{__includes_dir} = $includes_dir; | ||
| 63 | |||
| 64 | my $new_config = $self->{_config} = $self->migrateConfig($config); | ||
| 65 | |||
| 66 | my $views_dir = '_views'; | ||
| 67 | my $cc = -1; | ||
| 68 | while (-e File::Spec->catdir($layouts_dir, $views_dir)) { | ||
| 69 | $views_dir .= --$cc; | ||
| 70 | $self->{_config}->{directory}->{views} = $views_dir; | ||
| 71 | } | ||
| 72 | $self->{__views_dir} = $views_dir; | ||
| 73 | |||
| 74 | my $partials_dir = 'partials'; | ||
| 75 | while (-e File::Spec->catdir($layouts_dir, $partials_dir)) { | ||
| 76 | $partials_dir .= 'X'; | ||
| 77 | } | ||
| 78 | $self->{__partials_dir} = $partials_dir; | ||
| 79 | |||
| 80 | $self->createOutputDirectory; | ||
| 81 | |||
| 82 | $self->migrateLayouts; | ||
| 83 | $self->migrateIncludes; | ||
| 84 | $self->migratePosts; | ||
| 85 | |||
| 86 | $self->writeConfig($new_config); | ||
| 87 | |||
| 88 | # Convert assets while they are copied. | ||
| 89 | my $fcopy = sub { | ||
| 90 | my ($from, $to) = @_; | ||
| 91 | |||
| 92 | return if !-e $to; | ||
| 93 | |||
| 94 | my $liquid = read_file $to; | ||
| 95 | # Front matter? | ||
| 96 | if ($liquid !~ /^---[ \t\r]*\n.*\n---[ \t\r]*\n/s) { | ||
| 97 | return $_[-1]; | ||
| 98 | } | ||
| 99 | |||
| 100 | my $tt2 = $self->convertLiquidTemplate($to); | ||
| 101 | write_file $to, $tt2 | ||
| 102 | or return $self->logError(__x("cannot write '{filename}':" | ||
| 103 | . " {error}", | ||
| 104 | filename => $to, | ||
| 105 | error => $!)); | ||
| 106 | |||
| 107 | return $_[-1]; | ||
| 108 | }; | ||
| 109 | $self->copyUndone($fcopy); | ||
| 110 | |||
| 111 | if ($self->{_err_count}) { | ||
| 112 | $self->logger->error(__(<<EOF)); | ||
| 113 | The migration had errors! See above for details! | ||
| 114 | EOF | ||
| 115 | } | ||
| 116 | |||
| 117 | return $self; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub migratePosts { | ||
| 121 | my ($self) = @_; | ||
| 122 | |||
| 123 | # This does not seem to be configurable in Jekyll. | ||
| 124 | my $in_dir = '_posts'; | ||
| 125 | $self->markFileDone($in_dir); | ||
| 126 | my $out_dir = $self->outputDirectory; | ||
| 127 | |||
| 128 | my $posts_dir = 'posts'; | ||
| 129 | while (-e File::Spec->catfile($out_dir, $posts_dir)) { | ||
| 130 | $posts_dir .= 'X'; | ||
| 131 | } | ||
| 132 | $posts_dir = File::Spec->catfile($out_dir, $posts_dir); | ||
| 133 | |||
| 134 | my $logger = $self->logger; | ||
| 135 | $logger->info(__x("Migrating Jekyll posts from '{from_dir}' to " | ||
| 136 | . "Qgoda views in '{to_dir}'.\n", | ||
| 137 | from_dir => $in_dir, to_dir => $out_dir)); | ||
| 138 | |||
| 139 | my @jobs; | ||
| 140 | my $wanted = sub { | ||
| 141 | return if -d $_; | ||
| 142 | push @jobs, $File::Find::name; | ||
| 143 | }; | ||
| 144 | File::Find::find($wanted, $in_dir); | ||
| 145 | |||
| 146 | foreach my $name (@jobs) { | ||
| 147 | my $relpath = File::Spec->abs2rel($name, $in_dir); | ||
| 148 | my ($volume, $directory, $filename) = File::Spec->splitpath($relpath); | ||
| 149 | $filename =~ s/^[0-9]{4}-[0-9]{2}-[0-9]{2}-//; | ||
| 150 | $relpath = File::Spec->catpath($volume, $directory, $filename); | ||
| 151 | my $outpath = File::Spec->catfile($posts_dir, $relpath); | ||
| 152 | |||
| 153 | my $count = -1; | ||
| 154 | while (-e $outpath) { | ||
| 155 | # Conflict. | ||
| 156 | my ($stem, $extender) = ($relpath, ''); | ||
| 157 | if ($relpath =~ /(.*)(\..+)/) { | ||
| 158 | ($stem, $extender) = ($1, $2); | ||
| 159 | } | ||
| 160 | $stem .= --$count; | ||
| 161 | $relpath = $stem . $extender; | ||
| 162 | $outpath = File::Spec->catfile($posts_dir, $relpath); | ||
| 163 | } | ||
| 164 | |||
| 165 | $logger->debug(" '$name' => '$outpath'"); | ||
| 166 | |||
| 167 | my $tt2 = $self->convertLiquidTemplate($name); | ||
| 168 | |||
| 169 | $self->createFile($outpath, $tt2); | ||
| 170 | } | ||
| 171 | |||
| 172 | return $self; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub migrateLayouts { | ||
| 176 | my ($self) = @_; | ||
| 177 | |||
| 178 | return $self->migrateLiquidDirectory($self->{__layouts_dir}, | ||
| 179 | $self->{__views_dir}); | ||
| 180 | } | ||
| 181 | |||
| 182 | sub migrateIncludes { | ||
| 183 | my ($self) = @_; | ||
| 184 | |||
| 185 | my $partials_dir = File::Spec->catdir($self->{__views_dir}, | ||
| 186 | $self->{__partials_dir}); | ||
| 187 | return $self->migrateLiquidDirectory($self->{__includes_dir}, | ||
| 188 | $partials_dir); | ||
| 189 | } | ||
| 190 | |||
| 191 | sub migrateLiquidDirectory { | ||
| 192 | my ($self, $in_dir, $out_dir) = @_; | ||
| 193 | |||
| 194 | $self->markFileDone($in_dir); | ||
| 195 | $out_dir = File::Spec->catfile($self->outputDirectory, $out_dir); | ||
| 196 | |||
| 197 | my $logger = $self->logger; | ||
| 198 | $logger->info(__x("Migrating Jekyll liquid template from '{from_dir}' to " | ||
| 199 | . "Qgoda views in '{to_dir}'.\n", | ||
| 200 | from_dir => $in_dir, to_dir => $out_dir)); | ||
| 201 | |||
| 202 | $self->createDirectory($out_dir); | ||
| 203 | |||
| 204 | my @jobs; | ||
| 205 | my $wanted = sub { | ||
| 206 | return if -d $_; | ||
| 207 | push @jobs, $File::Find::name; | ||
| 208 | }; | ||
| 209 | File::Find::find($wanted, $in_dir); | ||
| 210 | |||
| 211 | foreach my $name (@jobs) { | ||
| 212 | my $relpath = File::Spec->abs2rel($name, $in_dir); | ||
| 213 | my $outpath = File::Spec->catfile($out_dir, $relpath); | ||
| 214 | |||
| 215 | $logger->debug(" '$name' => '$outpath'"); | ||
| 216 | |||
| 217 | my $tt2 = $self->convertLiquidTemplate($name); | ||
| 218 | |||
| 219 | $self->createFile($outpath, $tt2); | ||
| 220 | } | ||
| 221 | |||
| 222 | return $out_dir; | ||
| 223 | } | ||
| 224 | |||
| 225 | sub convertLiquidTemplate { | ||
| 226 | my ($self, $name) = @_; | ||
| 227 | |||
| 228 | my $code = read_file $name | ||
| 229 | or return $self->logError(__x("cannot read '{filename}': {error}", | ||
| 230 | file => $name, error => $!)); | ||
| 231 | my $logger = $self->logger; | ||
| 232 | my %options = ( | ||
| 233 | partials_dir => $self->{__partials_dir}, | ||
| 234 | includes_dir => $self->{__includes_dir}, | ||
| 235 | ); | ||
| 236 | my $converter = Qgoda::Migrator::Jekyll::LiquidConverter->new($name, | ||
| 237 | $code, | ||
| 238 | $logger, | ||
| 239 | %options); | ||
| 240 | my $tt2 = $converter->convert($code); | ||
| 241 | $self->{_err_count} += $converter->errorCount; | ||
| 242 | |||
| 243 | return $tt2; | ||
| 244 | } | ||
| 245 | |||
| 246 | sub migrateConfig { | ||
| 247 | my ($self, $config) = @_; | ||
| 248 | |||
| 249 | $self->markFileDone('_config.yml'); | ||
| 250 | $self->migrateDefaults($config); | ||
| 251 | |||
| 252 | # Variables which currently do not exist in qgoda. This is actually a | ||
| 253 | # todo list for qgoda. | ||
| 254 | # FIXME! "source" is supported but is called "srcdir". But it is not | ||
| 255 | # enough to just change the key. We also have to make sure that it | ||
| 256 | # not only exists. We need a method like translateConfigVariable() for | ||
| 257 | # that. | ||
| 258 | foreach my $variable (qw(source destination safe keep_files timezone | ||
| 259 | encoding show_drafts future lsi | ||
| 260 | limit_posts incremental profile | ||
| 261 | port host baseurl detach webrick | ||
| 262 | no_fenced_code_blocks smart markdown | ||
| 263 | plugins_dir data_dir includes_dir | ||
| 264 | collections markdown_ext unpublished whitelist | ||
| 265 | gems highlighter excerpt_separator | ||
| 266 | show_dir_listing permalink paginate_path | ||
| 267 | quiet verbose liquid rdiscount redcarpet | ||
| 268 | kramdown error_mode)) { | ||
| 269 | if (exists $config->{$variable}) { | ||
| 270 | $self->logError(__x("The configuration variable '{varname}'" | ||
| 271 | . " from '_config.yaml' is not supported or" | ||
| 272 | . " by Qgoda or it does not make sense.", | ||
| 273 | varname => $variable)); | ||
| 274 | } | ||
| 275 | } | ||
| 276 | |||
| 277 | # JEKYLL_ENV = production | ||
| 278 | |||
| 279 | return $config; | ||
| 280 | } | ||
| 281 | |||
| 282 | sub migrateDefaults { | ||
| 283 | my ($self, $config) = @_; | ||
| 284 | |||
| 285 | my $logger = $self->logger; | ||
| 286 | $logger->debug(__"Migrating defaults."); | ||
| 287 | |||
| 288 | my $old_defaults = $config->{defaults} or return $self; | ||
| 289 | |||
| 290 | eval { | ||
| 291 | my %defaults; | ||
| 292 | foreach my $default (@$old_defaults) { | ||
| 293 | my $scope = $default->{scope} or next; | ||
| 294 | my $values = $default->{values} or next; | ||
| 295 | if (!exists $scope->{path}) { | ||
| 296 | my $dump = YAML::XS::Dump($scope); | ||
| 297 | $dump =~ s/^/ /gm; | ||
| 298 | $dump .= " ---\n"; | ||
| 299 | $self->logError(__x("Cannot migrate default without path:\n" | ||
| 300 | . "{dump}", dump => $dump)); | ||
| 301 | next; | ||
| 302 | } | ||
| 303 | my $path = $scope->{path}; | ||
| 304 | my $new = { | ||
| 305 | values => {}, | ||
| 306 | }; | ||
| 307 | |||
| 308 | foreach my $key (keys %$values) { | ||
| 309 | my ($name, $value) = $self->translateVariable($key, | ||
| 310 | $values->{$key}); | ||
| 311 | $new->{values}->{$name} = $value; | ||
| 312 | } | ||
| 313 | |||
| 314 | $defaults{$path} = $new; | ||
| 315 | } | ||
| 316 | |||
| 317 | $config->{defaults} = \%defaults; | ||
| 318 | }; | ||
| 319 | if ($@) { | ||
| 320 | $self->logError($@); | ||
| 321 | } | ||
| 322 | |||
| 323 | return $self; | ||
| 324 | } | ||
| 325 | |||
| 326 | sub readConfig { | ||
| 327 | my ($self) = @_; | ||
| 328 | |||
| 329 | my $logger = $self->logger; | ||
| 330 | my $filename = '_config.yml'; | ||
| 331 | $logger->info(__x("reading configuration from '{filename}'", | ||
| 332 | filename => $filename)); | ||
| 333 | |||
| 334 | my $yaml = read_file $filename; | ||
| 335 | if (!defined $yaml) { | ||
| 336 | $logger->fatal(__x("cannot read '{filename}': {error}", | ||
| 337 | filename => $filename, error => $!)); | ||
| 338 | } | ||
| 339 | my $config = eval { safe_yaml_load $yaml }; | ||
| 340 | $logger->fatal(yaml_error $filename, $@) if $@; | ||
| 341 | |||
| 342 | return $config; | ||
| 343 | } | ||
| 344 | |||
| 345 | sub translateVariable { | ||
| 346 | my ($self, $variable, $value) = @_; | ||
| 347 | |||
| 348 | my %value_mapping = ( | ||
| 349 | type => { | ||
| 350 | posts => 'post', | ||
| 351 | }, | ||
| 352 | ); | ||
| 353 | my (%name_mapping) = ( | ||
| 354 | lang => 'lingua', | ||
| 355 | ); | ||
| 356 | |||
| 357 | if (exists $value_mapping{$variable} | ||
| 358 | && exists $value_mapping{$variable}->{$value}) { | ||
| 359 | $value = $value_mapping{$variable}->{$value}; | ||
| 360 | } | ||
| 361 | |||
| 362 | if (exists $name_mapping{$variable}) { | ||
| 363 | $variable = $name_mapping{$variable}; | ||
| 364 | } | ||
| 365 | |||
| 366 | return $variable, $value; | ||
| 367 | } | ||
| 368 | |||
| 369 | 1; |