Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 151 lines (123 sloc) 4.46 KB
#!/usr/bin/env ruby
#
# Copyright (C) 2011 Michael Paul Thomas Conigliaro
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
require 'logger'
require 'optparse'
require 'fileutils'
require 'uri'
require 'rubygems'
require 'nanoc3'
require 'wpxml_parser'
include WpxmlParser
VERSION = '2011.09.07'
AUTHOR = 'Michael Paul Thomas Conigliaro'
AUTHOR_EMAIL = 'mike [at] conigliaro [dot] org'
URL = 'http://conigliaro.org'
options = {
dry_run: false,
nanoc_path: File.expand_path(File.join(File.dirname(__FILE__), '..')),
xpxml_path: nil,
item_extension: '.md'
}
LOG = Logger.new(STDOUT)
LOG.level = Logger::INFO
OptionParser.new do |opts|
opts.version = VERSION
opts.banner = "Usage: #{$PROGRAM_NAME} [options] <WPXML Path>\n\n"
opts.on('-n', '--nanoc-path', "Path to nanoc installation (Default: #{options[:nanoc_path]})") do |opt|
options[:nanoc_path] = opt
end
opts.on('-e', '--item-extension', "Extension to use for new items (Default: #{options[:item_extension]})") do |opt|
options[:item_extension] = opt
end
opts.on('-v', '--verbose', 'Enable verbose logging') do
LOG.level = Logger::DEBUG
end
opts.on('--dry-run', 'Perform a trial run without making changes') do |opt|
options[:dry_run] = opt
end
opts.on_tail('-h', '--help', 'Show this message') do
puts opts.help
exit
end
opts.on_tail('--version', 'Show version') do
puts format("%s v%s\n%s <%s>\n%s", opts.program_name, opts.version, AUTHOR, AUTHOR_EMAIL, URL)
exit
end
if ARGV.length == 0
puts opts.help
exit(1)
end
end.parse!
# Rubocop wants a comment here
class String
# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def html2md
self.strip!
self.gsub!(/\t/, ' ')
(1...6).each do |n|
self.gsub!(%r{\n*<h#{n}>(.*?)</h#{n}>\n*}, "\n\n#{'#' * n} \\1\n\n")
end
self.gsub!(%r{<a href="(.*?)">(.*?)</a>}, '[\\2](\\1)')
self.gsub!(%r{<em>(.*?)</em>}, '*\\1*')
self.gsub!(%r{<strong>(.*?)</strong>}, '**\\1**')
self.gsub!(%r{(<ol>|</ol>|<ul>|</ul>)}, '')
self.gsub!(%r{[ ]*?<li>(.*?)</li>}, '* \\1')
self.gsub!(%r{\n*<pre (.*?)>\n*(.*?)\n*</pre>\n*}m) do |_match|
"\n\n<pre #{Regexp.last_match(1)}>\n#{Regexp.last_match(2).gsub('<', '&lt;')}\n</pre>\n\n"
end
self.gsub!(/<pre lang="(.*?)".*?>/, "<pre class=\"brush: \\1\">")
self.gsub!(/<pre>/, "<pre class=\"brush: plain\">")
self.gsub!(/brush: (apache|cisco|text|winbatch)/, 'brush: plain')
self.gsub!(%r{\[caption .*?\](.*?)\[\/caption\]}, '\\1')
self + "\n"
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
end
options[:wpxml_path] = ARGV[0]
LOG.info("Using WP XML path: #{options[:wpxml_path]}")
posts = Blog.new(options[:wpxml_path]).posts
LOG.info("Using nanoc path: #{options[:nanoc_path]}")
FileUtils.cd(options[:nanoc_path]) do
posts.each do |post|
item = {
body: options[:item_extension] == '.md' ? post.body.html2md : post.body,
attributes: {
title: post.title,
created_at: post.date,
kind: 'article',
tags: post.categories
},
identifier: URI.parse(post.link).path,
params: {
extension: options[:item_extension]
}
}
LOG.debug("#{options[:dry_run] ? '(NOT) ' : nil}Creating item: #{item[:identifier]}")
Nanoc3::Site.new('.').data_sources.first.create_item(
item[:body],
item[:attributes],
item[:identifier],
item[:params]
) unless options[:dry_run]
end
end
LOG.info("Created #{posts.count} item(s)")