HTML-Formatting

* added simple_format for content content
* added support for SyntaxHighlighter JS
* finished rake tasks for blog
This commit is contained in:
Marc Remolt
2011-06-02 16:22:50 +02:00
parent a45f502f44
commit afb35f58ad
4 changed files with 43 additions and 42 deletions

View File

@@ -1,6 +1,9 @@
module Refinery
module WordPress
class Page
include ::ActionView::Helpers::TagHelper
include ::ActionView::Helpers::TextHelper
attr_reader :node
def initialize(node)
@@ -19,6 +22,31 @@ module Refinery
node.xpath("content:encoded").text
end
def content_formatted
# WordPress doesn't export <p>-Tags, so let's run a simple_format over
# the content
formatted = simple_format(content)
# Support for 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>
formatted.gsub!(/\[(\w+)\]/, '<pre class="brush: \1">')
formatted.gsub!(/\[\/\w+\]/, '</pre>')
# remove all tags inside <pre> that simple_format created
# TODO: replace simple_format with a method, that ignores pre-tags
formatted.gsub!(/(<pre.*?>)(.+?)(<\/pre>)/m) do |match|
"#{$1}#{strip_tags($2)}#{$3}"
end
formatted
end
def creator
node.xpath("dc:creator").text
end
@@ -51,7 +79,7 @@ module Refinery
page = ::Page.create!(:title => title, :created_at => post_date,
:draft => draft?, :parent_id => parent_id)
page.parts.create(:title => 'Body', :body => content)
page.parts.create(:title => 'Body', :body => content_formatted)
page
end
end

View File

@@ -31,7 +31,7 @@ module Refinery
post = BlogPost.new
post = ::BlogPost.create! :title => title, :body => content, :draft => draft?,
post = ::BlogPost.create! :title => title, :body => content_formatted, :draft => draft?,
:published_at => post_date, :created_at => post_date, :author => user,
:tag_list => tag_list