Class: Refinery::WordPress::Page

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper
Defined in:
lib/wordpress/page.rb

Direct Known Subclasses

Post

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ Page

Returns a new instance of Page.



9
10
11
# File 'lib/wordpress/page.rb', line 9

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



7
8
9
# File 'lib/wordpress/page.rb', line 7

def node
  @node
end

Instance Method Details

#==(other) ⇒ Object



78
79
80
# File 'lib/wordpress/page.rb', line 78

def ==(other)
  post_id == other.post_id
end

#contentObject



21
22
23
# File 'lib/wordpress/page.rb', line 21

def content
  node.xpath("content:encoded").text
end

#content_formattedObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wordpress/page.rb', line 25

def content_formatted
  # WordPress doesn't export <p>-Tags, so let's run a simple_format over
  # the content. As we trust ourselves, no sanatize.
  formatted = simple_format(content, {}, { :sanitize => false })

  # 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> 
  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

#creatorObject



50
51
52
# File 'lib/wordpress/page.rb', line 50

def creator
  node.xpath("dc:creator").text
end

#draft?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/wordpress/page.rb', line 70

def draft?
  status != 'publish'
end

#inspectObject



13
14
15
# File 'lib/wordpress/page.rb', line 13

def inspect
  "WordPress::Page(#{post_id}): #{title}"     
end

#parent_idObject



62
63
64
# File 'lib/wordpress/page.rb', line 62

def parent_id
  node.xpath("wp:post_parent").text.to_i
end

#post_dateObject



54
55
56
# File 'lib/wordpress/page.rb', line 54

def 
  DateTime.parse node.xpath("wp:post_date").text
end

#post_idObject



58
59
60
# File 'lib/wordpress/page.rb', line 58

def post_id
  node.xpath("wp:post_id").text.to_i
end

#published?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/wordpress/page.rb', line 74

def published?
  ! draft?
end

#statusObject



66
67
68
# File 'lib/wordpress/page.rb', line 66

def status
  node.xpath("wp:status").text
end

#titleObject



17
18
19
# File 'lib/wordpress/page.rb', line 17

def title
  node.xpath("title").text
end

#to_refineryObject



82
83
84
85
86
87
88
# File 'lib/wordpress/page.rb', line 82

def to_refinery
  page = ::Page.create!(:title => title, :created_at => , 
                        :draft => draft?, :parent_id => parent_id)

  page.parts.create(:title => 'Body', :body => content_formatted)
  page
end