Class: Mill::Resource::Text

Inherits:
Mill::Resource show all
Includes:
HTMLHelpers
Defined in:
lib/mill/resources/text.rb

Constant Summary

Constants included from HTMLHelpers

HTMLHelpers::IgnoreErrors

Instance Attribute Summary collapse

Attributes inherited from Mill::Resource

#content, #date, #input_file, #mill, #output_file, #public

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HTMLHelpers

#amazon_button, #google_analytics, #html_document, #html_fragment, #parse_html, #parse_tidy_errors, #paypal_button, #replace_element, #tidy_html

Methods inherited from Mill::Resource

#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

#titleObject

Returns the value of attribute title.



9
10
11
# File 'lib/mill/resources/text.rb', line 9

def title
  @title
end

Class Method Details

.typeObject



11
12
13
# File 'lib/mill/resources/text.rb', line 11

def self.type
  :text
end

Instance Method Details



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_sizesObject



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|
    # skip elements that already have width/height defined
    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

#bodyObject



91
92
93
# File 'lib/mill/resources/text.rb', line 91

def body
  @content.at_xpath('/html/body').children
end


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
      # ;;warn "[#{uri}] absolutized #{elem.name}/@#{attribute.name}: #{link_uri} => #{attribute.value}"
    end
  end
end


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_contentObject



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_summaryObject



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_contentObject



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

#headObject



87
88
89
# File 'lib/mill/resources/text.rb', line 87

def head
  @content.at_xpath('/html/head').children
end

#loadObject



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
      parse_text_header
      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
    parse_html_header
  end
  add_image_sizes
  convert_relative_links
  super
end

#parse_html_headerObject



54
55
56
57
58
59
60
61
# File 'lib/mill/resources/text.rb', line 54

def parse_html_header
  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

#parse_text_headerObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/mill/resources/text.rb', line 63

def parse_text_header
  if @content =~ /^\w+:\s+/
    header, @content = @content.split(/\n\n/, 2)
    header.split(/\n/).map do |line|
      key, value = line.strip.split(/:\s+/, 2)
      key = key.gsub('-', '_').downcase.to_sym
      send("#{key}=", value)
    end
  end
end

#verifyObject



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