Class: Mill::Resource::Text
Direct Known Subclasses
Index
Constant Summary
collapse
- FileTypes =
%w{
text/plain
text/html
}
Constants included
from HTMLHelpers
HTMLHelpers::LinkElementsXPath
Instance Attribute Summary collapse
#content, #date, #input_file, #output_file, #public, #site, #type
Instance Method Summary
collapse
#check_errors, #find_link_elements, #google_analytics, #html_document, #html_fragment, #link_if, #parse_html, #parse_html_fragment, #replace_element
#absolute_uri, #build, #change_frequency, #find_sibling_resources, #parent_uri, #public?, #save, #tag_uri, #uri
Constructor Details
#initialize(title: nil, summary: nil, author: nil, public: true, **args) ⇒ Text
Returns a new instance of Text.
18
19
20
21
22
23
|
# File 'lib/mill/resources/text.rb', line 18
def initialize(title: nil, summary: nil, author: nil, public: true, **args)
@title = title
@summary = summary
@author = author
super(public: public, **args)
end
|
Instance Attribute Details
#author ⇒ Object
Returns the value of attribute author.
16
17
18
|
# File 'lib/mill/resources/text.rb', line 16
def author
@author
end
|
#summary ⇒ Object
Returns the value of attribute summary.
15
16
17
|
# File 'lib/mill/resources/text.rb', line 15
def summary
@summary
end
|
#title ⇒ Object
Returns the value of attribute title.
14
15
16
|
# File 'lib/mill/resources/text.rb', line 14
def title
@title
end
|
Instance Method Details
#add_external_link_targets ⇒ Object
132
133
134
135
136
137
138
139
|
# File 'lib/mill/resources/text.rb', line 132
def add_external_link_targets
@content.xpath('//a').each do |a|
if a['href'] && a['href'] =~ /^\w+:/
a['target'] = '_blank'
a['rel'] = 'noopener noreferrer'
end
end
end
|
#add_image_sizes ⇒ Object
147
148
149
150
151
152
153
154
155
156
157
158
|
# File 'lib/mill/resources/text.rb', line 147
def add_image_sizes
@content.xpath('//img').each do |img|
next if img[:width] || img[:height]
img_link = Addressable::URI.parse(img['src'])
raise Error, "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 = @site.find_resource(img_uri) or raise Error, "Can't find image for #{img_uri}"
img[:width], img[:height] = img_resource.width, img_resource.height
end
end
|
#body(&block) ⇒ Object
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/mill/resources/text.rb', line 113
def body(&block)
html_fragment do |html|
html.body do
if (elem = content_body)
html << elem.children.to_html
end
yield(html) if block_given?
end
end
end
|
#content_body ⇒ Object
128
129
130
|
# File 'lib/mill/resources/text.rb', line 128
def content_body
@content && @content.at_xpath('/html/body')
end
|
#content_head ⇒ Object
124
125
126
|
# File 'lib/mill/resources/text.rb', line 124
def content_head
@content && @content.at_xpath('/html/head')
end
|
#feed_content ⇒ Object
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/mill/resources/text.rb', line 179
def feed_content
if (body = content_body)
if (main = body.at_xpath('//div[@id="main"]')) || (main = body.at_xpath('//main'))
main.children
elsif (article = body.at_xpath('//article'))
article.children
else
body_elem = body.clone
%w{header nav masthead footer}.each do |name|
if (elem = body_elem.at_xpath("//div[@id=\"#{name}\"]")) || (elem = body_elem.at_xpath("//#{name}"))
elem.remove
end
end
body_elem.children
end
else
warn "Warning: Resource #{uri} (#{self.class}) has no content"
nil
end
end
|
#final_content ⇒ Object
87
88
89
90
91
92
93
94
|
# File 'lib/mill/resources/text.rb', line 87
def final_content
html_document(@site.html_version) do |doc|
doc.html(lang: 'en') do |html|
html.parent << head
html.parent << body
end
end.to_html
end
|
#head(&block) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/mill/resources/text.rb', line 96
def head(&block)
html_fragment do |html|
html.head do
head = content_head
if (title = @title || (head && head.at_xpath('title')))
html.title { html << title.to_html }
end
yield(html) if block_given?
if head
head.children.reject { |e| e.text? || e.name == 'title' }.each do |e|
html << e.to_html
end
end
end
end
end
|
#inspect ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/mill/resources/text.rb', line 25
def inspect
super + ", title: %p, summary: %p, author: %p" % [
@title,
@summary,
@author,
]
end
|
#load ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/mill/resources/text.rb', line 33
def load
super
if @input_file
@content = @input_file.read
mode = case @input_file.extname.downcase
when '.md', '.mdown', '.markdown'
:markdown
when '.textile'
:textile
when '.txt'
:pre
when '.htm', '.html'
:html
else
raise "Unknown text type: #{@input_file}"
end
if mode != :html
@content = (@content || '').to_html(mode: mode, multiline: true)
@output_file = @output_file.replace_extension('.html')
end
begin
@content = parse_html(@content)
rescue Error => e
raise e, "#{@input_file}: #{e}"
end
end
end
|
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/mill/resources/text.rb', line 63
def
unless @title
if (title_elem = @content.at_xpath('/html/head/title'))
@title = title_elem.text
else
@title = uri.to_s
end
end
@content.xpath('/html/head/meta[@name]').each do |meta|
send("#{meta['name']}=", meta['content'])
end
end
|
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/mill/resources/text.rb', line 76
def
if @content.split(/\n/, 2).first =~ /^\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
|
141
142
143
144
145
|
# File 'lib/mill/resources/text.rb', line 141
def
@content.xpath('//comment()').each do ||
.remove
end
end
|
#shorten_links ⇒ Object
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
# File 'lib/mill/resources/text.rb', line 160
def shorten_links
find_link_elements(@content).each do |attribute|
elem = attribute.parent
link_uri = Addressable::URI.parse(attribute.value) or raise Error, "Can't parse #{attribute.value.inspect} from #{xpath.inspect}"
link_uri = uri + link_uri
if link_uri.relative?
self_uri = uri.normalize
self_uri.scheme = 'http'
link_uri.scheme = 'http'
attribute.value = self_uri.route_to(link_uri)
end
end
end
|