Module: Jekyll::WordPress
- Defined in:
- lib/jekyll/migrators/wordpress.rb
Class Method Summary collapse
- .clean_entities(text) ⇒ Object
-
.process(dbname, user, pass, host = 'localhost', options = {}) ⇒ Object
Main migrator function.
- .process_post(post, db, options) ⇒ Object
- .sluggify(title) ⇒ Object
Class Method Details
.clean_entities(text) ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/jekyll/migrators/wordpress.rb', line 267 def self.clean_entities( text ) if text.respond_to?(:force_encoding) text.force_encoding("UTF-8") end text = HTMLEntities.new.encode(text, :named) # We don't want to convert these, it would break all # HTML tags in the post and comments. text.gsub!("&", "&") text.gsub!("<", "<") text.gsub!(">", ">") text.gsub!(""", '"') text.gsub!("'", "'") text end |
.process(dbname, user, pass, host = 'localhost', options = {}) ⇒ Object
Main migrator function. Call this to perform the migration.
- dbname
-
The name of the database
- user
-
The database user name
- pass
-
The database user’s password
- host
-
The address of the MySQL database host. Default: ‘localhost’
- options
-
A hash table of configuration options.
Supported options are:
- :table_prefix
-
Prefix of database tables used by WordPress. Default: ‘wp_’
- :clean_entities
-
If true, convert non-ASCII characters to HTML entities in the posts, comments, titles, and names. Requires the ‘htmlentities’ gem to work. Default: true.
- :comments
-
If true, migrate post comments too. Comments are saved in the post’s YAML front matter. Default: true.
- :categories
-
If true, save the post’s categories in its YAML front matter.
- :tags
-
If true, save the post’s tags in its YAML front matter.
- :more_excerpt
-
If true, when a post has no excerpt but does have a <!– more –> tag, use the preceding post content as the excerpt. Default: true.
- :more_anchor
-
If true, convert a <!– more –> tag into two HTML anchors with ids “more” and “more-NNN” (where NNN is the post number). Default: true.
- :status
-
Array of allowed post statuses. Only posts with matching status will be migrated. Known statuses are :publish, :draft, :private, and :revision. If this is nil or an empty array, all posts are migrated regardless of status. Default: [:publish].
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 |
# File 'lib/jekyll/migrators/wordpress.rb', line 53 def self.process(dbname, user, pass, host='localhost', ={}) = { :table_prefix => 'wp_', :clean_entities => true, :comments => true, :categories => true, :tags => true, :more_excerpt => true, :more_anchor => true, :status => [:publish] # :draft, :private, :revision }.merge() if [:clean_entities] begin require 'htmlentities' rescue LoadError STDERR.puts "Could not require 'htmlentities', so the " + ":clean_entities option is now disabled." [:clean_entities] = false end end FileUtils.mkdir_p("_posts") db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host, :encoding => 'utf8') px = [:table_prefix] posts_query = " SELECT posts.ID AS `id`, posts.guid AS `guid`, posts.post_type AS `type`, posts.post_status AS `status`, posts.post_title AS `title`, posts.post_name AS `slug`, posts.post_date AS `date`, posts.post_content AS `content`, posts.post_excerpt AS `excerpt`, posts.comment_count AS `comment_count`, users.display_name AS `author`, users.user_login AS `author_login`, users.user_email AS `author_email`, users.user_url AS `author_url` FROM #{px}posts AS `posts` LEFT JOIN #{px}users AS `users` ON posts.post_author = users.ID" if [:status] and not [:status].empty? status = [:status][0] posts_query << " WHERE posts.post_status = '#{status.to_s}'" [:status][1..-1].each do |status| posts_query << " OR posts.post_status = '#{status.to_s}'" end end db[posts_query].each do |post| process_post(post, db, ) end end |
.process_post(post, db, options) ⇒ Object
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 |
# File 'lib/jekyll/migrators/wordpress.rb', line 118 def self.process_post(post, db, ) px = [:table_prefix] title = post[:title] if [:clean_entities] title = clean_entities(title) end slug = post[:slug] if !slug or slug.empty? slug = sluggify(title) end date = post[:date] || Time.now name = "%02d-%02d-%02d-%s.markdown" % [date.year, date.month, date.day, slug] content = post[:content].to_s if [:clean_entities] content = clean_entities(content) end excerpt = post[:excerpt].to_s more_index = content.index(/<!-- *more *-->/) more_anchor = nil if more_index if [:more_excerpt] and (post[:excerpt].nil? or post[:excerpt].empty?) excerpt = content[0...more_index] end if [:more_anchor] more_link = "more" content.sub!(/<!-- *more *-->/, "<a id=\"more\"></a>" + "<a id=\"more-#{post[:id]}\"></a>") end end categories = [] = [] if [:categories] or [:tags] cquery = "SELECT terms.name AS `name`, ttax.taxonomy AS `type` FROM #{px}terms AS `terms`, #{px}term_relationships AS `trels`, #{px}term_taxonomy AS `ttax` WHERE trels.object_id = '#{post[:id]}' AND trels.term_taxonomy_id = ttax.term_taxonomy_id AND terms.term_id = ttax.term_id" db[cquery].each do |term| if [:categories] and term[:type] == "category" if [:clean_entities] categories << clean_entities(term[:name]) else categories << term[:name] end elsif [:tags] and term[:type] == "post_tag" if [:clean_entities] << clean_entities(term[:name]) else << term[:name] end end end end comments = [] if [:comments] and post[:comment_count].to_i > 0 cquery = "SELECT comment_ID AS `id`, comment_author AS `author`, comment_author_email AS `author_email`, comment_author_url AS `author_url`, comment_date AS `date`, comment_date_gmt AS `date_gmt`, comment_content AS `content` FROM #{px}comments WHERE comment_post_ID = '#{post[:id]}' AND comment_approved != 'spam'" db[cquery].each do |comment| comcontent = comment[:content].to_s if comcontent.respond_to?(:force_encoding) comcontent.force_encoding("UTF-8") end if [:clean_entities] comcontent = clean_entities(comcontent) end = comment[:author].to_s if [:clean_entities] = clean_entities() end comments << { 'id' => comment[:id].to_i, 'author' => , 'author_email' => comment[:author_email].to_s, 'author_url' => comment[:author_url].to_s, 'date' => comment[:date].to_s, 'date_gmt' => comment[:date_gmt].to_s, 'content' => comcontent, } end comments.sort!{ |a,b| a['id'] <=> b['id'] } end # Get the relevant fields as a hash, delete empty fields and # convert to YAML for the header. data = { 'layout' => post[:type].to_s, 'status' => post[:status].to_s, 'published' => (post[:status].to_s == "publish"), 'title' => title.to_s, 'author' => post[:author].to_s, 'author_login' => post[:author_login].to_s, 'author_email' => post[:author_email].to_s, 'author_url' => post[:author_url].to_s, 'excerpt' => excerpt, 'more_anchor' => more_anchor, 'wordpress_id' => post[:id], 'wordpress_url' => post[:guid].to_s, 'date' => date, 'categories' => [:categories] ? categories : nil, 'tags' => [:tags] ? : nil, 'comments' => [:comments] ? comments : nil, }.delete_if { |k,v| v.nil? || v == '' }.to_yaml # Write out the data and content to file File.open("_posts/#{name}", "w") do |f| f.puts data f.puts "---" f.puts content end end |
.sluggify(title) ⇒ Object
283 284 285 286 287 288 289 290 291 |
# File 'lib/jekyll/migrators/wordpress.rb', line 283 def self.sluggify( title ) begin require 'unidecode' title = title.to_ascii rescue LoadError STDERR.puts "Could not require 'unidecode'. If your post titles have non-ASCII characters, you could get nicer permalinks by installing unidecode." end title.downcase.gsub(/[^0-9A-Za-z]+/, " ").strip.gsub(" ", "-") end |