Class: Qipowl::Bowlers::Html

Inherits:
Bowler show all
Defined in:
lib/qipowl/bowlers/html.rb

Constant Summary

Constants inherited from Bowler

Bowler::SEPARATOR

Instance Attribute Summary

Attributes inherited from Bowler

#in, #out

Instance Method Summary collapse

Methods inherited from Bowler

#add_entity, #block, const_missing, #custom, #execute, #grip, #method_missing, #remove_entity, #respond_to?, #split

Methods included from TypoLogging

#logger, logger

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Qipowl::Bowlers::Bowler

Instance Method Details

#closing(tag) ⇒ String (private)

Constructs closing html tag for the input given.

Parameters:

  • tag (String)

    to produce closing tag string from.

Returns:

  • (String)

    opening tag for the input given.



276
277
278
# File 'lib/qipowl/bowlers/html.rb', line 276

def closing tag
  "</#{tag}>"
end

#get_href_content(href) ⇒ Symbol (private)

Determines content of remote link by href. TODO Make image patterns configurable.

Parameters:

  • href (String)

    link to remote resource

Returns:

  • (Symbol)

    content type (:img or :text currently)



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/qipowl/bowlers/html.rb', line 303

def get_href_content href
  href = href.to_s.unbowl.strip
  if href.end_with?(* %w{png jpg jpeg gif PNG JPG JPEG GIF})
    :img
  elsif /\/\/i\.chzbgr/ =~ href
    :img
  else
    :text
  end
    
#  uri = URI(href.to_s.unbowl)
#  Net::HTTP.start(uri.host, uri.port) do |http|
#    http.open_timeout = 1
#    http.read_timeout = 1
#
#    request = Net::HTTP::Head.new uri
#    response = http.request request
#    case response.to_hash["content-type"].first
#    when /image/ then return :img
#    when /text/ then return :text
#    end
#  end
#  :unknown
#rescue
#  logger.warn "Unable to determine link [#{href.to_s.unbowl}] type: no internet connection. Reverting to default."
#  :unknown
end

#opening(tag, params = {}) ⇒ String (private)

Constructs opening html tag for the input given.

To construct abbr tag with title Title and class default:

opening :abbr, { :title=>'Title', :class=>'default' }

Parameters:

  • tag (String)

    to produce opening tag string from.

  • params (Hash) (defaults to: {})

    to be put into opening tag as attributes.

Returns:

  • (String)

    opening tag for the input given.



267
268
269
270
# File 'lib/qipowl/bowlers/html.rb', line 267

def opening tag, params={}
  attrs = params.inject("") { |m, el| m.prepend " #{el.first}='#{el.last}'" unless el.last.nil? ; m }
  "<#{tag}#{attrs}>"
end

#orphan(str) ⇒ String (private)

Produces html paragraph tag (<p>) with class owl.

Parameters:

  • str

    the words, to be put in paragraph tag.

Returns:

See Also:

  • Qipowl::Bowler#orphan


255
256
257
# File 'lib/qipowl/bowlers/html.rb', line 255

def orphan str
  "#{tagify(:p, {}, str.to_s.strip)}"
end

#special_handler(method, *args, &block) ⇒ Array (private)

Hence we cannot simply declare the DSL for it, we need to handle calls to all the methods, starting with those symbols.

Parameters:

  • method (Symbol)

    as specified by caller (method_missing.)

  • args (Array)

    as specified by caller (method_missing.)

  • block (Proc)

    as specified by caller (method_missing.)

Returns:

  • (Array)

    the array of words



242
243
244
245
246
247
248
249
# File 'lib/qipowl/bowlers/html.rb', line 242

def special_handler method, *args, &block
  # Sublevel markers, e.g. “ •” is level 2 line-item
  return [method, args].flatten \
    unless level(method) > 0 && self.class::REGULAR_TAGS.keys.include?(canonize(method))
  
  self.class.class_eval "alias_method :#{method}, :#{canonize(method)}"
  send method, args, block
end

#standalone(tag, params = {}) ⇒ String (private)

Constructs opening html tag for the input given.

To construct abbr tag with title Title and class default:

opening :abbr, { :title=>'Title', :class=>'default' }

Acts most like an #opening method, but closes an element inplace (used for hr, br, img).

Parameters:

  • tag (String)

    to produce opening tag string from.

  • params (Hash) (defaults to: {})

    to be put into opening tag as attributes.

Returns:

  • (String)

    opening tag for the input given.



283
284
285
# File 'lib/qipowl/bowlers/html.rb', line 283

def standalone tag, params={}
  opening(tag, params).sub('>', '/>')
end

#tagify(tag, params, *args) ⇒ String (private)

Constructs valid tag for the input given, concatenating opening and closing tags around the text passed in args.

Parameters:

  • tag (String)

    to produce html tag string from.

  • params (Hash)

    to be put into opening tag as attributes.

  • args (Array)

    the words, to be tagged around.

Returns:

  • (String)

    opening tag for the input given.



293
294
295
296
# File 'lib/qipowl/bowlers/html.rb', line 293

def tagify tag, params, *args
  text = [*args].join(SEPARATOR)
  text.vacant? ? '' : "#{opening tag, params}#{text}#{closing tag}"
end

#——(*args) ⇒ Nil

:alone handler for horizontal rule; it differs from default handler since orphans around must be handled as well.

Parameters:

  • args (Array)

    the words, gained since last call to #harvest

Returns:

  • (Nil)

    nil



103
104
105
106
# File 'lib/qipowl/bowlers/html.rb', line 103

def 

#(*args) ⇒ Array

Handler for abbrs.

Parameters:

  • args (Array)

    the words, gained since last call to #harvest

Returns:

  • (Array)

    the array of words with trimmed abbr tag



73
74
75
76
77
# File 'lib/qipowl/bowlers/html.rb', line 73

def 

#∀_alone(*args) ⇒ Array

:alone default handler

Parameters:

  • args (Array)

    the words, gained since last call to #harvest

Returns:

  • (Array)

    the array of words with prepended alone tag



29
30
31
# File 'lib/qipowl/bowlers/html.rb', line 29

def 

#∀_block(param, args) ⇒ Nil

:block default handler opening tag

Parameters:

  • args (Array)

    the words, gained since last call to #harvest

  • param (String)

    the text to be places on the same string as

Returns:

  • (Nil)

    nil



38
39
40
41
42
43
44
45
# File 'lib/qipowl/bowlers/html.rb', line 38

def 

#∀_grip(*args) ⇒ Array

:grip default handler

Parameters:

  • args (Array)

    the words, gained since last call to #harvest

Returns:

  • (Array)

    the array of words with trimmed grip tag



20
21
22
23
24
# File 'lib/qipowl/bowlers/html.rb', line 20

def 

#∀_magnet(*args) ⇒ Array

:magnet default handler

Parameters:

  • args (Array)

    the words, gained since last call to #harvest

Returns:

  • (Array)

    the array of words with trimmed magnet tag



50
51
52
53
54
# File 'lib/qipowl/bowlers/html.rb', line 50

def 

#∀_regular(*args) ⇒ Object

:regular default handler

Parameters:

  • args (Array)

    the words, gained since last call to #harvest



58
59
60
61
62
63
64
65
# File 'lib/qipowl/bowlers/html.rb', line 58

def 

#(*args) ⇒ Nil Also known as:

:regular handler for data lists (required since data list items consist of two tags: dt and dd.)

Parameters:

  • args (Array)

    the words, gained since last call to #harvest

Returns:

  • (Nil)

    nil



177
178
179
180
181
182
183
# File 'lib/qipowl/bowlers/html.rb', line 177

def 

#(*args) ⇒ Object



134
135
136
137
# File 'lib/qipowl/bowlers/html.rb', line 134

def 

#(*args) ⇒ Array

Handler for anchors.

Parameters:

  • args (Array)

    the words, gained since last call to #harvest

Returns:

  • (Array)

    the array of words with trimmed a tag



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/qipowl/bowlers/html.rb', line 82

def 

#(*args) ⇒ Nil

TODO:

Make it to understand quotes when there is a plain HTML on the other side

Handler for standalone pictures and

Parameters:

Returns:

  • (Nil)

    nil



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/qipowl/bowlers/html.rb', line 159

def 

#(*args) ⇒ Nil

Handler for Youtube video

Parameters:

  • args (Array)

    the words, gained since last call to #harvest

Returns:

  • (Nil)

    nil



145
146
147
148
149
150
151
152
# File 'lib/qipowl/bowlers/html.rb', line 145

def 

#(*args) ⇒ Nil

:block handler for comment (required because comments are formatted in HTML in some specific way.)

Parameters:

  • param (String)

    the text to be places on the same string as opening tag

  • args (Array)

    the words, gained since last call to #harvest

Returns:

  • (Nil)

    nil



116
117
118
# File 'lib/qipowl/bowlers/html.rb', line 116

def 

#(*args) ⇒ Nil

:magnet handler for reference to Livejournal user.

Parameters:

  • param (String)

    the text to be places on the same string as opening tag

  • args (Array)

    the words, gained since last call to #harvest

Returns:

  • (Nil)

    nil



127
128
129
130
131
132
# File 'lib/qipowl/bowlers/html.rb', line 127

def