Class: Mill::Resource::Text
Constant Summary
Constants included
from HTMLHelpers
HTMLHelpers::IgnoreErrors
Instance Attribute Summary collapse
#content, #date, #input_file, #mill, #output_file, #public
Class Method Summary
collapse
Instance Method Summary
collapse
#amazon_button, #google_analytics, #html_document, #html_fragment, #parse_html, #parse_tidy_errors, #paypal_button, #replace_element, #tidy_html
#absolute_uri, #build, #change_frequency, #tag_uri, #uri, #validate, #validate_xml
Constructor Details
#initialize(params = {}) ⇒ Text
Returns a new instance of Text.
15
16
17
18
19
20
21
|
# File 'lib/mill/resources/text.rb', line 15
def initialize(params={})
super(
{
public: true,
}.merge(params)
)
end
|
Instance Attribute Details
#title ⇒ Object
Returns the value of attribute title.
9
10
11
|
# File 'lib/mill/resources/text.rb', line 9
def title
@title
end
|
Class Method Details
.type ⇒ Object
11
12
13
|
# File 'lib/mill/resources/text.rb', line 11
def self.type
:text
end
|
Instance Method Details
#add_external_link_targets ⇒ Object
101
102
103
104
105
106
107
|
# File 'lib/mill/resources/text.rb', line 101
def add_external_link_targets
@content.xpath('//a').each do |a|
if a['href'] && a['href'] =~ /^\w+:/
a['target'] = '_blank'
end
end
end
|
#add_image_sizes ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/mill/resources/text.rb', line 109
def add_image_sizes
@content.xpath('//img').each do |img|
next if img[:width] || img[:height]
img_link = Addressable::URI.parse(img['src'])
raise "no link in <img> element: #{img.to_s}" if img_link.nil? || img_link.empty?
next if img_link.host
img_uri = uri + img_link
img_resource = @mill.find_resource(img_uri) or raise "Can't find image for #{img_uri}"
img[:width], img[:height] = img_resource.width, img_resource.height
end
end
|
#body ⇒ Object
91
92
93
|
# File 'lib/mill/resources/text.rb', line 91
def body
@content.at_xpath('/html/body').children
end
|
#convert_relative_link(elem_attr) ⇒ Object
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/mill/resources/text.rb', line 128
def convert_relative_link(elem_attr)
@content.xpath("//#{elem_attr}").each do |attribute|
elem = attribute.parent
link_uri = Addressable::URI.parse(attribute.value) or raise "Can't parse #{attribute.value.inspect} from #{xpath.inspect}"
if !link_uri.path.empty? && link_uri.path[0] != '/'
attribute.value = uri + link_uri
end
end
end
|
#convert_relative_links ⇒ Object
122
123
124
125
126
|
# File 'lib/mill/resources/text.rb', line 122
def convert_relative_links
@mill.link_elem_attrs.each do |xpath|
convert_relative_link(xpath)
end
end
|
#feed_content ⇒ Object
148
149
150
151
|
# File 'lib/mill/resources/text.rb', line 148
def feed_content
body = @content.at_xpath('/html/body') or raise "#{uri} has no content"
['html', body.children.to_html]
end
|
#feed_summary ⇒ Object
139
140
141
142
143
144
145
146
|
# File 'lib/mill/resources/text.rb', line 139
def feed_summary
;;raise "#{uri} has no content" unless @content
if (p = @content.at_xpath('/html/body/p[1]'))
['html', p.to_html]
else
nil
end
end
|
#final_content ⇒ Object
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/mill/resources/text.rb', line 74
def final_content
html_document do |doc|
doc.html(lang: 'en') do |html|
html.head do
html << head.to_html
end
html.body do
html << body.to_html
end
end
end
end
|
#head ⇒ Object
87
88
89
|
# File 'lib/mill/resources/text.rb', line 87
def head
@content.at_xpath('/html/head').children
end
|
#load ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/mill/resources/text.rb', line 23
def load
if @input_file
@content = @input_file.read
markup_class = case @input_file.extname
when '.md', '.mdown', '.markdown'
Kramdown::Document
when '.textile'
RedCloth
when '.txt'
PreText
else
nil
end
if markup_class
raise "#{uri}: Content is empty" unless @content
@content = markup_class.new(@content).to_html
@output_file = @output_file.replace_extension('.html')
end
begin
@content = parse_html(@content)
rescue HTMLError => e
raise "failed to parse #{@input_file}: #{e}"
end
end
add_image_sizes
convert_relative_links
super
end
|
54
55
56
57
58
59
60
61
|
# File 'lib/mill/resources/text.rb', line 54
def
if (title_elem = @content.at_xpath('/html/head/title'))
@title = title_elem.text
end
@content.xpath('/html/head/meta[@name]').each do |meta|
send("#{meta['name']}=", meta['content'])
end
end
|
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/mill/resources/text.rb', line 63
def
if @content =~ /^\w+:\s+/
, @content = @content.split(/\n\n/, 2)
.split(/\n/).map do |line|
key, value = line.strip.split(/:\s+/, 2)
key = key.gsub('-', '_').downcase.to_sym
send("#{key}=", value)
end
end
end
|
#verify ⇒ Object
95
96
97
98
99
|
# File 'lib/mill/resources/text.rb', line 95
def verify
tidy_html(@output_file.read) do |error_str|
warn "#{uri}: #{error_str}"
end
end
|