Class: Qipowl::Bowlers::Html
- Defined in:
- lib/qipowl/bowlers/html.rb
Constant Summary
Constants inherited from Bowler
Instance Attribute Summary
Attributes inherited from Bowler
Instance Method Summary collapse
-
#closing(tag) ⇒ String
private
Constructs closing html tag for the input given.
-
#get_href_content(href) ⇒ Symbol
private
Determines content of remote link by href.
-
#opening(tag, params = {}) ⇒ String
private
Constructs opening html tag for the input given.
-
#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.
-
#standalone(tag, params = {}) ⇒ String
private
Constructs opening html tag for the input given.
-
#tagger_format(tag) ⇒ Array
Handler for tags.
-
#tagify(tag, params, *args) ⇒ String
private
Constructs valid tag for the input given, concatenating opening and closing tags around the text passed in
args. -
#——(*args) ⇒ Nil
:alonehandler for horizontal rule; it differs from default handler since orphans around must be handled as well. -
#†(*args) ⇒ Array
Handler for abbrs.
-
#∀_alone(*args) ⇒ Array
:alonedefault handler. -
#∀_block(arg) ⇒ Nil
:blockdefault handler FIXME TODO make an ability to supply class opening tag. -
#∀_grip(*args) ⇒ Array
:gripdefault handler. -
#∀_magnet(*args) ⇒ Array
:magnetdefault handler. -
#∀_regular(*args) ⇒ Object
:regulardefault handler. -
#∀_self(*args, &cb) ⇒ Object
:selfdefault handler. -
#▶(*args) ⇒ Nil
(also: #▷)
:regularhandler for data lists (required since data list items consist of two tags:dtanddd.). - #☇(*args) ⇒ Object
-
#⚓(*args) ⇒ Array
Handler for anchors.
-
#⚘(*args) ⇒ Nil
Handler for standalone pictures and.
-
#✇(*args) ⇒ Nil
Handler for Youtube video.
-
#✍(*args) ⇒ Nil
:blockhandler for comment (required because comments are formatted in HTML in some specific way.). -
#✎(*args) ⇒ Nil
:magnethandler for reference to Livejournal user.
Methods inherited from Bowler
#add_entity, #block, const_missing, #custom, #execute, #grip, #method_missing, #remove_entity, #respond_to?, #split
Methods included from TypoLogging
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.
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.
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' }
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.
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).
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.
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.
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.
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.
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
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
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
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
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
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
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.)
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.
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
Make it to understand quotes when there is a plain HTML on the other side
Handler for standalone pictures and
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
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.)
152 153 154 |
# File 'lib/qipowl/bowlers/html.rb', line 152 def ✍ *args [] end |
#✎(*args) ⇒ Nil
:magnet handler for reference to Livejournal user.
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 |