lfa-wordpress-import/lib/wordpress/page.rb

145 lines
3.8 KiB
Ruby
Raw Permalink Normal View History

2014-03-04 22:47:53 +00:00
module WordPressImport
class Page
include ::ActionView::Helpers::TagHelper
include ::ActionView::Helpers::TextHelper
2014-03-04 22:47:53 +00:00
attr_reader :node
2014-03-04 22:47:53 +00:00
def initialize(node)
@node = node
end
2014-03-04 22:47:53 +00:00
def inspect
"WordPress::Page(#{post_id}): #{title}"
end
def link
node.xpath("link").text
end
2014-03-04 22:47:53 +00:00
def title
node.xpath("title").text
end
2014-03-04 22:47:53 +00:00
def content
node.xpath("content:encoded").text
end
2014-03-04 22:47:53 +00:00
def content_formatted
2014-03-18 06:27:02 +00:00
formatted = format_shortcodes(format_syntax_highlighter(format_paragraphs(content)))
2014-03-04 22:47:53 +00:00
# remove all tags inside <pre> that simple_format created
# TODO: replace format_paragraphs with a method, that ignores pre-tags
formatted.gsub!(/(<pre.*?>)(.+?)(<\/pre>)/m) do |match|
"#{$1}#{strip_tags($2)}#{$3}"
end
2014-03-04 22:47:53 +00:00
formatted
end
2014-03-04 22:47:53 +00:00
def creator
node.xpath("dc:creator").text
end
2014-03-04 22:47:53 +00:00
def post_date
2014-03-06 04:27:57 +00:00
Time.parse node.xpath("wp:post_date").text
end
def publish_date
Time.parse node.xpath("pubDate").text
end
def post_name
node.xpath("wp:post_name").text
2014-03-04 22:47:53 +00:00
end
2014-03-04 22:47:53 +00:00
def post_id
node.xpath("wp:post_id").text.to_i
end
2014-03-04 22:47:53 +00:00
def parent_id
dump_id = node.xpath("wp:post_parent").text.to_i
dump_id == 0 ? nil : dump_id
end
2014-03-04 22:47:53 +00:00
def status
node.xpath("wp:status").text
end
2014-03-04 22:47:53 +00:00
def draft?
status != 'publish'
end
2014-03-04 22:47:53 +00:00
def published?
! draft?
end
2014-03-04 22:47:53 +00:00
def ==(other)
post_id == other.post_id
end
2014-03-04 22:47:53 +00:00
#NEED:
2014-03-06 04:27:57 +00:00
# creator -> "user_id"
2014-03-04 22:47:53 +00:00
# wp:post_name -> "slug"
# pubDate -> "published_at"
#OK:
# title -> "title"
# content:encoded -> "body"
# wp:post_date_gmt -> "created_at"
2014-03-06 04:27:57 +00:00
def to_rails
# :user_id => creator
2014-03-04 22:47:53 +00:00
page = ::Page.create!(:id => post_id, :title => title,
2014-03-06 04:27:57 +00:00
:created_at => post_date, :slug => post_name,
:published_at => publish_date, :body => content_formatted)
2014-03-04 22:47:53 +00:00
end
2014-03-04 22:47:53 +00:00
private
2014-03-04 22:47:53 +00:00
def format_paragraphs(text, html_options={})
# WordPress doesn't export <p>-Tags, so let's run a simple_format over
# the content. As we trust ourselves, no sanatize. This code is heavily
# inspired by the simple_format rails helper
text = ''.html_safe if text.nil?
start_tag = tag('p', html_options, true)
2014-03-18 06:27:02 +00:00
text.gsub!(/\n\n+/, "</p>#{start_tag}") # 2+ newline -> paragraph
text.gsub!(/\r?\n/, "<br/>\n") # \r\n and \n -> line break (must be after the paragraph detection to avoid <br/><br/>)
2014-03-04 22:47:53 +00:00
text.insert 0, start_tag
2014-03-04 22:47:53 +00:00
text.html_safe.safe_concat("</p>")
end
2011-06-05 11:38:39 +00:00
2014-03-04 22:47:53 +00:00
def format_syntax_highlighter(text)
# Support for SyntaxHighlighter (http://alexgorbatchev.com/SyntaxHighlighter/):
# In WordPress you can (via a plugin) enclose code in [lang][/lang]
# blocks, which are converted to a <pre>-tag with a class corresponding
# to the language.
#
# Example:
# [ruby]p "Hello World"[/ruby]
# -> <pre class="brush: ruby">p "Hello world"</pre>
text.gsub(/\[(\w+)\](.+?)\[\/\1\]/m, '<pre class="brush: \1">\2</pre>')
end
2014-03-18 06:27:02 +00:00
# Replace Wordpress shortcodes with formatted HTML (see shortcode gem and support/templates folder)
def format_shortcodes(text)
Shortcode.setup do |config|
# the template parser to use
config.template_parser = :haml # :erb or :haml supported, :haml is default
# location of the template files
config.template_path = ::File.join(::File.dirname(__FILE__), "..", "..","support/templates/haml")
# a list of block tags to support e.g. [quote]Hello World[/quote]
config.block_tags = [:caption, :column]
# a list of self closing tags to support e.g. [youtube id="12345"]
config.self_closing_tags = [:end_columns, "google-map-v3"]
end
Shortcode.process(text)
end
end
end