-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp2statocles.pl
343 lines (287 loc) · 13.8 KB
/
wp2statocles.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/perl
# WordPress migration for Statocles
#
# Copyright (c) 2016 by William Lindley <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
use v5.20;
use warnings;
use strict;
use Mojo::DOM;
sub rectify_html {
# WordPress uses a variant of HTML in which blank lines indicate
# paragraph breaks. Here we translate those into a semblance of standard
# HTML, ensuring that blank lines inside <pre> blocks do not get changed.
# We also ensure that comments in the HTML are retained.
my $munged_text = shift;
my @pre_segments; # Stash for the <pre> blocks.
my $seg_id=0; # Number each block.
$munged_text =~ s{<pre\b(.*?)>(.*?)</pre\s*>}
{$pre_segments[++$seg_id]=$2; "<pre data-seg=\"$seg_id\"$1></pre>";}gsex;
$munged_text =~ s/\n\s*\n/\n<p>/g;
my $atree = Mojo::DOM->new("<html><body>$munged_text</body></html>");
for my $e ($atree->find('pre')->each) {
my $segment_text = $pre_segments[$e->attr('data-seg')];
# double-quote % signs in <pre> to prevent Mojo template from seeing
$segment_text =~ s/<%/<%%/g;
# Quote leading % to %%
$segment_text =~ s/^(\s*)%/$1%%/gm;
# Restore content
$e->append_content($segment_text);
delete $e->attr->{'data-seg'};
}
return $atree->to_string;
}
###############
sub blog_style_date {
# Convert an arbitrary text date into YYYY/MM/DD format
my $text_date = shift;
# year,month,day from array returned by Date::Parse …
my ($year, $month, $day) = (strptime($text_date))[5,4,3];
return undef unless defined $year;
return sprintf("%04d/%02d/%02d", $year+1900, $month+1, $day);
}
###############
# Read input, presumably a WordPress export file, and parse XML tree
use XML::Simple qw(:strict);
binmode(STDOUT, ":utf8"); # enable Unicode output
my $xs = XML::Simple->new();
my $ref = $xs->XMLin($ARGV[0], ForceArray => 1, KeyAttr => []);
###############
use HTML::FormatMarkdown;
#use YAML::XS; # seems to emit our 'tag' arrays incorrectly?
use YAML;
use File::Path qw(make_path);
use Path::Tiny;
use Date::Parse;
# ~~~ TODO: Included as hook for future support.
# As of January 2016, Statocles does not yet support post/page workflow status.
#
my %status_map = ( draft => 'draft', private => 'private', publish => 'published' );
# Default values taken from site.yml created by command: $ statocles create ---:
my $site = {
'site' => {
'class' => 'Statocles::Site',
'args' => {
'apps' => { # User may want to add/remove some
'static' => {
'$ref' => 'static_app'
},
'page' => {
'$ref' => 'page_app'
},
'blog' => {
'$ref' => 'blog_app'
}
},
'base_url' => 'http://www.~~~.com/', # take from WP
'title' => '~~~', # " "
'theme' => { '$ref' => 'theme' },
# 'site/theme', # ~~~ TODO: or: What is a reasonable default?
'index' => '/page',
'nav' => {
'main' => [ # TODO: Take from WP, possibly add others?
{
'href' => '/blog',
'text' => 'Blog'
},
]
},
'plugins' => {
'link_check' => {
'$class' => 'Statocles::Plugin::LinkCheck'
}
},
'deploy' => {
'$ref' => 'deploy'
},
},
},
'theme' => {
'class' => 'Statocles::Theme',
'args' => {
'store' => '::default'
}
},
'page_app' => {
'class' => 'Statocles::App::Basic',
'args' => {
'store' => 'page', # should match $page_base_dir, below
'url_root' => '/', # ~~~ TODO: match to existing WP site?
}
},
'blog_app' => {
'class' => 'Statocles::App::Blog',
'args' => {
'store' => 'blog', # should match $post_base_dir, below
'url_root' => '/', # ~~~ TODO: match to existing WP site?
},
},
'static_app' => { # default location
'class' => 'Statocles::App::Basic',
'args' => {
'store' => 'static',
'url_root' => '/static', # ~~~ TODO: match to existing WP site?
},
},
'deploy' => { # User will probably want to change later
'class' => 'Statocles::Deploy::File',
'args' => {
'path' => '.'
},
},
};
my $posts = $ref->{channel}[0]->{item};
# 'wp:post_type': 'post' or 'page' − handle appropriately
# 'wp:status': 'draft' 'private' 'publish' − Later: Probably save this
# as a draft flag. For now discard any unpublished.
# 'dc:creator': "bill"
# -- Use as Statocles: "author"
# 'title': use as Statocles: "title"
# 'wp:post_date': use as Statocles: "date" [also consider 'wp:post_date_gmt']
# 'category': an array of hashes including post tags; process if follows example:
# [{domain => 'post_tag', content => 'command line'},
# {domain => 'post_tag', content => 'Uncategorized'}]
# and turn into 'tags' array
# 'wp:menu_order': numeric. Need to regard this, at each subdirectory level,
# as the ordering in the site.yaml file
# ~~~ TODO: Consider making these user-definable
my $post_base_dir = path("./blog"); # Where the markdown files and resources are on our local filesystem
my $page_base_dir = path("./page");
# ~~~ TODO: Use more various cpan modules
# Set site's global data from WP config
use Mojo::URL;
my $url_base = Mojo::URL->new($ref->{channel}[0]->{'wp:base_site_url'}[0]);
# e.g., http://www.wlindley.com (usually without trailing '/')
# If just http://www.example.com -- add explicit root path:
$url_base->path('/') unless length($url_base->path);
# ~~~ NOTE: Probably want to do something clever with wp:base_blog_url - how do we define the mapping to be consistent?
$site->{site}->{args}->{base_url} = $url_base->to_string;
$site->{site}->{args}->{title} = $ref->{channel}[0]->{title}[0];
my %pages; # Accumulate the entire site here
foreach my $post (@{$posts}) {
if ($post->{'wp:post_type'}[0] =~ /^post|page$/) {
my %post_info = (
status => $status_map{$post->{'wp:status'}[0]},
author => $post->{'dc:creator'}[0],
title => $post->{'title'}[0],
date => $post->{'wp:post_date'}[0],
data => { post_type => $post->{'wp:post_type'}[0],
wp_post_name => $post->{'wp:post_name'}[0],
wp_post_path => $post->{'link'}[0],
wp_post_id => $post->{'wp:post_id'}[0],
wp_post_parent => $post->{'wp:post_parent'}[0],
wp_menu_order => $post->{'wp:menu_order'}[0],
# And possibly also custom-post-type info?
},
);
# ~~~ TODO: Add provision for draft pages/posts
# ~~~ For now, just ignore all unpublished items
next unless ($post_info{status} eq 'published');
my @tags;
my @other_categories;
foreach my $taxonomy (@{$post->{category}}) {
next unless ref $taxonomy; # Very old export files contain bare strings in addition the hash we want
if ($taxonomy->{domain} eq 'post_tag') {
push @tags, $taxonomy->{content};
} else {
push @other_categories, $taxonomy;
}
# could also keep @categories for taxonomy 'category'
}
$post_info{tags} = \@tags if (scalar @tags);
$post_info{data}->{categories} = \@other_categories if scalar @other_categories;
my $p = $post_info{data};
my $post_basename = $p->{wp_post_name};
my $post_path = $p->{wp_post_path};
my $is_home_page;
next if ($post_path =~ m/[?]/); # Does not have a well-formed URL: probably a draft. Skip this.
$post_path =~ s/^$url_base//; # Strip off http://www.example.com
if (!length($post_path)) { # this is the site's index
$post_path = '/index';
$is_home_page = 1;
}
$post_path = path($post_path); # Convert to Path::Tiny object
print " ($post_path)\n";
my $blog_style_path = blog_style_date($post_info{date});
next unless defined $blog_style_path;
my $new_path;
if ($post_info{data}->{'post_type'} eq 'post') {
$new_path = $post_base_dir->child($blog_style_path,$post_basename); # becomes directory name
$post_basename = 'index';
} else { # Page …
$new_path = $page_base_dir->child($post_path->parent); # strip the basename for just the directory
# $post_basename is post name within that.
}
my $create_path;
if ($is_home_page) {
$post_basename = 'index';
}
$create_path= $new_path->child($post_basename); # Tentative path as seen by browser
# print " [$create_path]\n";
if (defined $pages{$create_path}) {
warn " Conflict with existing page at $create_path:\n" .
" $post_info{title}\n";
next;
}
# To avoid a bug in HTML::FormatMarkdown, prepend cipher
my $cipher = '~~1~~';
my $content = $cipher . $post->{'content:encoded'}[0];
$content = HTML::FormatMarkdown->format_string(rectify_html($content));
$content =~ s/$cipher//; # And remove it after
$pages{$create_path} = {
path => $new_path,
filename => $post_basename, # WAS: $create_path . '.markdown',
header => Dump(\%post_info), # needs to be followed by line: "---"
content => $content,
};
if ($is_home_page) {
$pages{$create_path}->{home} = 1;
}
# NOTE: We might want to create a mapping file from old URLs to new URLs
# á la .htaccess redirects
}
}
my %page_children;
foreach my $apage (keys %pages) {
my $parent = $pages{$apage}->{path};
my $leaf = $pages{$apage}->{filename};
while (defined $parent && ($parent ne '.')) {
push @{$page_children{$parent}}, $leaf;
# Traverse up one level
($parent, $leaf) = ($parent->parent, path($parent->basename)->child($leaf));
last if exists $page_children{$parent}; # unless we already know about that
}
}
foreach my $apage (keys %pages) {
my $file_path = $pages{$apage}->{path};
my $file_name = $pages{$apage}->{filename};
if (defined $page_children{$apage}) {
# This page has children: make it the index of a new subdirectory
$file_path = $file_path->child($file_name);
$file_name = 'index';
}
# print " [$file_path] [$file_name]\n";
# Actually write each file in appopriate directory
make_path($file_path);
open NEWFILE, '>:encoding(UTF-8)', $file_path->child($file_name . '.markdown');
print NEWFILE $pages{$apage}->{header} . "---\n";
print NEWFILE $pages{$apage}->{content};
close NEWFILE;
}
# Write default Site Configuration
open SITE, '>:encoding(UTF-8)', 'site.yml';
print SITE Dump($site);
close SITE;