-
Notifications
You must be signed in to change notification settings - Fork 4
/
wordpress-to-jekyll.php
156 lines (112 loc) · 2.94 KB
/
wordpress-to-jekyll.php
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
<?php
/**
* Wordpress to Jekyll
*
* A small conversion script that will convert a Wordpress export
* into individual post files that you can use for a Jekyll
* powered website.
*
* @author David Winter <i@djw.me>
*/
class WordpressToJekyll {
protected $_export_file;
protected $_build_directory;
protected $_layout_post = 'post';
protected $_items;
protected $_yaml;
public function __construct($wordpress_xml_file, YamlDumperInterface $yaml, $build_directory)
{
$this->_export_file = $wordpress_xml_file;
$this->_load_items();
$this->_yaml = $yaml;
$this->_build_directory = rtrim($build_directory, '/');
}
protected function _setup_build_directory()
{
if ( ! file_exists($this->_build_directory))
{
return mkdir($this->_build_directory);
}
return TRUE;
}
public function convert()
{
$this->_setup_build_directory();
foreach ($this->_items as $item)
{
$post = $this->_post_array($item);
$formatted_post = $this->_format_post($post);
$this->_write_post($post, $formatted_post);
}
}
protected function _load_items()
{
$xml = simplexml_load_file($this->_export_file, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->_items = $xml->channel->item;
}
protected function _post_array($item)
{
$post = array();
$namespaces = $item->getNameSpaces(TRUE);
$wp = $item->children($namespaces['wp']);
$post['filename'] = sprintf('%s-%s.md',
date('Y-m-d', strtotime($wp->post_date)),
$wp->post_name
);
$post['meta']['layout'] = $this->_layout_post;
$post['meta']['title'] = (string) $item->title;
$tags = array();
if ($item->category)
{
foreach ($item->category as $tag)
{
if ( (string) $tag['domain'] === 'post_tag')
{
$tags[] = (string) $tag['nicename'];
}
}
}
if ( ! empty($tags))
{
$post['meta']['tags'] = $tags;
}
$content = $item->children($namespaces['content']);
$post['content'] = str_replace("<!--more-->\n\n", '', $content->encoded);
return $post;
}
protected function _format_post($post)
{
$meta = $this->_yaml->dump($post['meta']);
$post_content = <<<EOT
---
{$meta}
---
{$post['content']}
EOT;
return $post_content;
}
protected function _write_post($post, $formatted)
{
return file_put_contents($this->_build_directory.'/'.$post['filename'], $formatted);
}
}
interface YamlDumperInterface {
public function dump($data);
}
$sfyaml = __DIR__.'/vendor/yaml/lib/sfYaml.php';
if ( ! file_exists($sfyaml))
{
throw new Exception('Symfony yaml was not found. You may need to initialise the git submodules: git submodule update --init --recursive');
}
require($sfyaml);
class YamlDumper implements YamlDumperInterface {
public function dump($data)
{
$yaml = new sfYaml();
return $yaml->dump($data);
}
}
$file = (isset($argv[1])) ? $argv[1] : getcwd().'/export.xml';
$build = (isset($argv[2])) ? $argv[2] : getcwd().'/posts';
$convert = new WordpressToJekyll($file, new YamlDumper, $build);
$convert->convert();