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.



348
349
350
# File 'lib/qipowl/bowlers/html.rb', line 348

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)



375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/qipowl/bowlers/html.rb', line 375

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.



339
340
341
342
# File 'lib/qipowl/bowlers/html.rb', line 339

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



318
319
320
321
322
323
324
325
326
327
328
# File 'lib/qipowl/bowlers/html.rb', line 318

def special_handler method, *args, &block
  # Sublevel markers, e.g. “ •” is level 2 line-item
  if level(method) > 0 && self.class::REGULAR_TAGS.keys.include?(m = (canonize method))
    self.class.class_eval "alias_method :#{method}, :#{m}"
    send method, args, block
  elsif self.class.instance_methods.include?(m = (normalize method))
    send(m, args) { yield if block_given? ; method.to_s }
  else
    [method, args].flatten
  end
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.



355
356
357
# File 'lib/qipowl/bowlers/html.rb', line 355

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

#tagger_format(tag) ⇒ Array

Handler for tags.

Parameters:

  • arg (String)

    the word to be processed

Returns:

  • (Array)

    the array of words with procesed tag



98
99
100
# File 'lib/qipowl/bowlers/html.rb', line 98

def tagger_format tag
  "<a href='/tags/#{tag}'>#{tag}</a>"
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.



365
366
367
368
# File 'lib/qipowl/bowlers/html.rb', line 365

def tagify tag, params, *args
  text = [*args].join(SEPARATOR)
  text.vacant? ? '' : "#{opening tag, params}#{text.strip}#{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



139
140
141
142
# File 'lib/qipowl/bowlers/html.rb', line 139

def —— *args
  harvest nil, orphan(args.join(SEPARATOR)) unless args.vacant?
  harvest __callee__, standalone(∃_alone(__callee__)[:tag])
end

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



107
108
109
110
111
112
# File 'lib/qipowl/bowlers/html.rb', line 107

def  *args
  data = ∃_grip(__callee__)
  term, *title = args.flatten
  mine, rest = [*title].join(SEPARATOR).split("#{__callee__}", 2)
  [tagify(data[:tag], {:title => mine, :class => data[:class]}, term), rest]
end

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



35
36
37
38
# File 'lib/qipowl/bowlers/html.rb', line 35

def ∀_alone *args
  data = ∃_alone(__callee__)
  [standalone(data[:tag], {:class => data[:class]}), args]
end

#∀_block(arg) ⇒ Nil

:block default handler FIXME TODO make an ability to supply class 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



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/qipowl/bowlers/html.rb', line 46

def ∀_block arg
  data = ∃_block(__callee__)
  param, body = Base64.decode64(
    "#{arg.to_s.unbowl.uncarriage(false)}"
  ).force_encoding('UTF-8').split(/\n/, 2)
  param, body = '', param if body.nil?
  harvest __callee__,
          tagify(
            data[:tag],
            {:class => param.strip.empty? ? data[:class] : param.strip },
            body.hsub(String::HTML_ENTITIES)
          )
end

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



26
27
28
29
30
# File 'lib/qipowl/bowlers/html.rb', line 26

def ∀_grip *args
  data = ∃_grip(__callee__)
  mine, rest = [*args].join(SEPARATOR).split("#{__callee__}", 2)
  [tagify(data[:tag], {:class => data[:class]}, mine), rest]
end

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



63
64
65
66
67
68
# File 'lib/qipowl/bowlers/html.rb', line 63

def ∀_magnet *args
  data = ∃_magnet(__callee__)
  param, *rest = args.flatten
  param = param.unbowl.to_s.prepend("#{__callee__}#{String::NBSP}")
  [tagify(data[:tag], {:class => data[:class]}, param), rest]
end

#∀_regular(*args) ⇒ Object

:regular default handler

Parameters:

  • args (Array)

    the words, gained since last call to #harvest



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

def ∀_regular *args
  data = ∃_regular(canonize __callee__)
  harvest __callee__, tagify(data[:tag], {:class => data[:class]}, args)
end

#∀_self(*args, &cb) ⇒ Object

:self default handler

Parameters:

  • args (Array)

    the words, gained since last call to #harvest



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/qipowl/bowlers/html.rb', line 79

def ∀_self *args, &cb
  data = ∃_self(__callee__)
  cally = block_given? ? yield : __callee__
  text = case data[:format]
         when NilClass then cally
         when String then cally.to_s.gsub(/(#{cally})/, data[:format])
#               when Regexp then cally.to_s.gsub(cally.to_s, data[:format])
         when Symbol then send(data[:format], cally) rescue cally # FIXME Do I need rescue here?!
         else raise "Bad format specified for #{data[:tag]}"
         end
  [data[:tag] ? tagify(data[:tag], {:class => data[:class]}, text) : text, args]
end

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



212
213
214
215
216
217
218
# File 'lib/qipowl/bowlers/html.rb', line 212

def  *args
  dt, dd = args.join(SEPARATOR).split(/\s+(?:—)\s+/)
  harvest __callee__, %Q(
#{tagify :dt, {}, dt}
#{tagify :dd, {}, dd}
)
end

#(*args) ⇒ Object



170
171
172
173
# File 'lib/qipowl/bowlers/html.rb', line 170

def  *args
  param, *rest = args.flatten
  [tagify(∃_magnet(__callee__)[:tag], {:name => param.unbowl}, String::ZERO_WIDTH_SPACE), rest]
end

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



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/qipowl/bowlers/html.rb', line 117

def  *args
  data = ∃_grip(__callee__)
  href, *title = args.flatten
  mine, rest = [*title].join(SEPARATOR).split("#{__callee__}", 2)
  href = href.unbowl
  [
    case get_href_content(href)
    when :img
      standalone :img, { :src => href, :alt => [*mine].join(SEPARATOR), :class => 'inplace' }
    else
      tagify data[:tag], {:href => href}, mine
    end, rest
  ]
end

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



195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/qipowl/bowlers/html.rb', line 195

def  *args
  href, *title = args.flatten
  title = title.join(SEPARATOR).strip
  harvest __callee__, %Q(
<figure>
  <img src='#{href.unbowl}'/>
  <figcaption>
    <p>#{title}</p>
  </figcaption>
</figure>
)
end

#(*args) ⇒ Nil

Handler for Youtube video

Parameters:

  • args (Array)

    the words, gained since last call to #harvest

Returns:

  • (Nil)

    nil



181
182
183
184
185
186
187
188
# File 'lib/qipowl/bowlers/html.rb', line 181

def  *args
  id, *rest = args.flatten
  harvest nil, orphan(rest.join(SEPARATOR)) unless rest.vacant?
  harvest __callee__, %Q(
<iframe class='youtube' width='560' height='315' src='http://www.youtube.com/embed/#{id.unbowl}'
  frameborder='0' allowfullscreen></iframe>
)
end

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



152
153
154
# File 'lib/qipowl/bowlers/html.rb', line 152

def  *args
  []
end

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



163
164
165
166
167
168
# File 'lib/qipowl/bowlers/html.rb', line 163

def  *args
  param, *rest = args.flatten
  param = param.unbowl
  ljref = "<span style='white-space: nowrap;'><a href='http://#{param}.livejournal.com/profile?mode=full'><img src='http://l-stat.livejournal.com/img/userinfo.gif' alt='[info]' style='border: 0pt none ; vertical-align: bottom; padding-right: 1px;' height='17' width='17'></a><a href='http://#{param}.livejournal.com/?style=mine'><b>#{param}</b></a></span>"
  [ljref, rest]
end