Class: JekyllImport::Importers::MT
- Inherits:
-
JekyllImport::Importer
- Object
- JekyllImport::Importer
- JekyllImport::Importers::MT
- Defined in:
- lib/jekyll-import/importers/mt.rb
Constant Summary collapse
- STATUS_DRAFT =
1
- STATUS_PUBLISHED =
2
- MORE_CONTENT_SEPARATOR =
'<!--more-->'
Class Method Summary collapse
- .comment_content(comment, options = default_options) ⇒ Object
-
.comment_date(comment) ⇒ Object
Different versions of MT used different column names.
- .comment_file_dir_and_base_name(posts_name_by_id, comment, options = default_options) ⇒ Object
-
.comment_metadata(comment, options = default_options) ⇒ Object
Extracts metadata for YAML front matter from comment.
- .default_options ⇒ Object
- .encode(str, options = default_options) ⇒ Object
-
.extra_entry_text_empty?(post) ⇒ Boolean
Extracts text body from post.
- .post_content(post, options = default_options) ⇒ Object
-
.post_date(post) ⇒ Object
Different versions of MT used different column names.
- .post_file_name(post, options = default_options) ⇒ Object
-
.post_metadata(post, options = default_options) ⇒ Object
Extracts metadata for YAML front matter from post.
-
.process(options) ⇒ Object
Main migrator function.
- .require_deps ⇒ Object
- .specify_options(c) ⇒ Object
-
.suffix(entry_type) ⇒ Object
Ideally, this script would determine the post format (markdown, html, etc) and create files with proper extensions.
Methods inherited from JekyllImport::Importer
inherited, run, stringify_keys, subclasses
Class Method Details
.comment_content(comment, options = default_options) ⇒ Object
188 189 190 |
# File 'lib/jekyll-import/importers/mt.rb', line 188 def self.comment_content(comment, = ) comment[:comment_text] end |
.comment_date(comment) ⇒ Object
Different versions of MT used different column names
184 185 186 |
# File 'lib/jekyll-import/importers/mt.rb', line 184 def self.comment_date(comment) comment[:comment_modified_on] || comment[:comment_created_on] end |
.comment_file_dir_and_base_name(posts_name_by_id, comment, options = default_options) ⇒ Object
192 193 194 195 196 197 |
# File 'lib/jekyll-import/importers/mt.rb', line 192 def self.comment_file_dir_and_base_name(posts_name_by_id, comment, = ) post_basename = posts_name_by_id[comment[:comment_entry_id]].sub(/\.\w+$/, '') comment_id = comment[:comment_id] [post_basename, "#{comment_id}.markdown"] end |
.comment_metadata(comment, options = default_options) ⇒ Object
Extracts metadata for YAML front matter from comment
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/jekyll-import/importers/mt.rb', line 167 def self.(comment, = ) = { 'layout' => 'comment', 'comment_id' => comment[:comment_id], 'post_id' => comment[:comment_entry_id], 'author' => encode(comment[:comment_author], ), 'email' => comment[:comment_email], 'commenter_id' => comment[:comment_commenter_id], 'date' => comment_date(comment).strftime("%Y-%m-%d %H:%M:%S %z"), 'visible' => comment[:comment_visible] == 1, 'ip' => comment[:comment_ip], 'url' => comment[:comment_url] } end |
.default_options ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/jekyll-import/importers/mt.rb', line 9 def self. { 'blog_id' => nil, 'categories' => true, 'dest_encoding' => 'utf-8', 'src_encoding' => 'utf-8', 'comments' => false } end |
.encode(str, options = default_options) ⇒ Object
199 200 201 202 203 204 205 |
# File 'lib/jekyll-import/importers/mt.rb', line 199 def self.encode(str, = ) if str.respond_to?(:encoding) str.encode(['dest_encoding'], ['src_encoding']) else str end end |
.extra_entry_text_empty?(post) ⇒ Boolean
Extracts text body from post
146 147 148 |
# File 'lib/jekyll-import/importers/mt.rb', line 146 def self.extra_entry_text_empty?(post) post[:entry_text_more].nil? || post[:entry_text_more].strip.empty? end |
.post_content(post, options = default_options) ⇒ Object
150 151 152 153 154 155 156 |
# File 'lib/jekyll-import/importers/mt.rb', line 150 def self.post_content(post, = ) if extra_entry_text_empty?(post) post[:entry_text] else post[:entry_text] + "\n\n#{MORE_CONTENT_SEPARATOR}\n\n" + post[:entry_text_more] end end |
.post_date(post) ⇒ Object
Different versions of MT used different column names
141 142 143 |
# File 'lib/jekyll-import/importers/mt.rb', line 141 def self.post_date(post) post[:entry_authored_on] || post[:entry_created_on] end |
.post_file_name(post, options = default_options) ⇒ Object
158 159 160 161 162 163 164 |
# File 'lib/jekyll-import/importers/mt.rb', line 158 def self.post_file_name(post, = ) date = post_date(post) slug = post[:entry_basename] file_ext = suffix(post[:entry_convert_breaks]) "#{date.strftime('%Y-%m-%d')}-#{slug}.#{file_ext}" end |
.post_metadata(post, options = default_options) ⇒ Object
Extracts metadata for YAML front matter from post
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/jekyll-import/importers/mt.rb', line 125 def self.(post, = ) = { 'layout' => 'post', 'title' => encode(post[:entry_title], ), 'date' => post_date(post).strftime("%Y-%m-%d %H:%M:%S %z"), 'excerpt' => encode(post[:entry_excerpt].to_s, ), 'mt_id' => post[:entry_id], 'blog_id' => post[:entry_blog_id], 'post_id' => post[:entry_id], # for link with comments 'basename' => post[:entry_basename] } ['published'] = false if post[:entry_status] != STATUS_PUBLISHED end |
.process(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 of configuration options
Supported options are:
- blog_id
-
Specify a single MovableType blog to export by providing blog_id. Default: nil, importer will include posts for all blogs.
- categories
-
If true, save the post’s categories in its YAML front matter. Default: true
- src_encoding
-
Encoding of strings from the database. Default: UTF-8 If your output contains mangled characters, set src_encoding to something appropriate for your database charset.
- dest_encoding
-
Encoding of output strings. Default: UTF-8
- comments
-
If true, output comments in _comments directory, like the one mentioned at github.com/mpalmer/jekyll-static-comments/
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 |
# File 'lib/jekyll-import/importers/mt.rb', line 58 def self.process() = .merge() dbname = .fetch('dbname') user = .fetch('user') pass = .fetch('password', "") host = .fetch('host', "localhost") comments = .fetch('comments') posts_name_by_id = { } if comments db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host) post_categories = db[:mt_placement].join(:mt_category, :category_id => :placement_category_id) FileUtils.mkdir_p "_posts" posts = db[:mt_entry] posts = posts.filter(:entry_blog_id => ['blog_id']) if ['blog_id'] posts.each do |post| categories = post_categories.filter( :mt_placement__placement_entry_id => post[:entry_id] ).map {|ea| encode(ea[:category_basename], ) } file_name = post_file_name(post, ) data = (post, ) data['categories'] = categories if !categories.empty? && ['categories'] yaml_front_matter = data.delete_if { |k,v| v.nil? || v == '' }.to_yaml # save post path for comment processing posts_name_by_id[data['post_id']] = file_name if comments content = post_content(post, ) File.open("_posts/#{file_name}", "w") do |f| f.puts yaml_front_matter f.puts "---" f.puts encode(content, ) end end # process comment output, if enabled if comments FileUtils.mkdir_p "_comments" comments = db[:mt_comment] comments.each do |comment| if posts_name_by_id.has_key?(comment[:comment_entry_id]) # if the entry exists dir_name, base_name = comment_file_dir_and_base_name(posts_name_by_id, comment, ) FileUtils.mkdir_p "_comments/#{dir_name}" data = (comment, ) content = comment_content(comment, ) yaml_front_matter = data.delete_if { |k,v| v.nil? || v == '' }.to_yaml File.open("_comments/#{dir_name}/#{base_name}", "w") do |f| f.puts yaml_front_matter f.puts "---" f.puts encode(content, ) end end end end end |
.require_deps ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/jekyll-import/importers/mt.rb', line 19 def self.require_deps JekyllImport.require_with_fallback(%w[ rubygems sequel fileutils safe_yaml ]) end |
.specify_options(c) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/jekyll-import/importers/mt.rb', line 28 def self.(c) c.option 'dbname', '--dbname DB', 'Database name' c.option 'user', '--user USER', 'Database user name' c.option 'password', '--password PW', "Database user's password, (default: '')" c.option 'host', '--host HOST', 'Database host name (default: "localhost")' end |
.suffix(entry_type) ⇒ Object
Ideally, this script would determine the post format (markdown, html, etc) and create files with proper extensions. At this point it just assumes that markdown will be acceptable.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/jekyll-import/importers/mt.rb', line 210 def self.suffix(entry_type) if entry_type.nil? || entry_type.include?("markdown") || entry_type.include?("__default__") # The markdown plugin I have saves this as # "markdown_with_smarty_pants", so I just look for "markdown". "markdown" elsif entry_type.include?("textile") # This is saved as "textile_2" on my installation of MT 5.1. "textile" elsif entry_type == "0" || entry_type.include?("richtext") # Richtext looks to me like it's saved as HTML, so I include it here. "html" else # Other values might need custom work. entry_type end end |