# encoding: utf-8 require 'net/http' require 'base64' require 'typogrowth' require_relative '../core/bowler' module Qipowl module Mappers class HtmlBowlerMapper < BowlerMapper end end # Module placeholder for dynamically created bowlers module Bowlers class Html < Bowler ############################################################################## ### Default handlers for all the types of markup ### ############################################################################## # `:grip` default handler # @param [Array] args the words, gained since last call to {#harvest} # @return [Array] the array of words with trimmed `grip` tag def ∀_grip *args data = ∃_grip(__callee__) mine, rest = [*args].join(SEPARATOR).split("#{__callee__}∎", 2) [tagify(data[:tag], {:class => data[:class]}, mine), rest] end # `:alone` default handler # @param [Array] args the words, gained since last call to {#harvest} # @return [Array] the array of words with prepended `alone` tag def ∀_alone *args data = ∃_alone(__callee__) [standalone(data[:tag], {:class => data[:class]}), args] end # `:block` default handler # FIXME TODO make an ability to supply class # @param [Array] args the words, gained since last call to {#harvest} # @param [String] param the text to be places on the same string as # opening tag # @return [Nil] nil 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 # `:magnet` default handler # @param [Array] args the words, gained since last call to {#harvest} # @return [Array] the array of words with trimmed `magnet` tag 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` default handler # @param [Array] args the words, gained since last call to {#harvest} def ∀_regular *args data = ∃_regular(canonize __callee__) harvest __callee__, tagify(data[:tag], {:class => data[:class]}, args) end # `:self` default handler # @param [Array] args the words, gained since last call to {#harvest} 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 ############################################################################## ### Self :: Specific handlers ### ############################################################################## # Handler for tags. # @param [String] arg the word to be processed # @return [Array] the array of words with procesed tag def tagger_format tag "#{tag}" end ############################################################################## ### Grip :: Specific handlers ### ############################################################################## # Handler for abbrs. # @param [Array] args the words, gained since last call to {#harvest} # @return [Array] the array of words with trimmed `abbr` tag 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 # Handler for anchors. # @param [Array] args the words, gained since last call to {#harvest} # @return [Array] the array of words with trimmed `a` tag 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 ############################################################################## ### Alone :: Specific handlers ### ############################################################################## # `:alone` handler for horizontal rule; it differs from default # handler since orphans around must be handled as well. # @param [Array] args the words, gained since last call to {#harvest} # @return [Nil] nil def —— *args harvest nil, orphan(args.join(SEPARATOR)) unless args.vacant? harvest __callee__, standalone(∃_alone(__callee__)[:tag]) end ############################################################################## ### Block :: Specific handlers ### ############################################################################## # `:block` handler for comment (required because comments are # formatted in HTML in some specific way.) # @param [String] param the text to be places on the same string as opening tag # @param [Array] args the words, gained since last call to {#harvest} # @return [Nil] nil def ✍ *args [] end ############################################################################## ### Magnet :: Specific handlers ### ############################################################################## # `:magnet` handler for reference to Livejournal user. # @param [String] param the text to be places on the same string as opening tag # @param [Array] args the words, gained since last call to {#harvest} # @return [Nil] nil def ✎ *args param, *rest = args.flatten param = param.unbowl ljref = "#{param}" [ljref, rest] end def ☇ *args param, *rest = args.flatten [tagify(∃_magnet(__callee__)[:tag], {:name => param.unbowl}, String::ZERO_WIDTH_SPACE), rest] end ############################################################################## ### Regular :: Specific handlers ### ############################################################################## # Handler for Youtube video # @param [Array] args the words, gained since last call to {#harvest} # @return [Nil] nil def ✇ *args id, *rest = args.flatten harvest nil, orphan(rest.join(SEPARATOR)) unless rest.vacant? harvest __callee__, %Q( ) end # Handler for standalone pictures and # @todo Make it to understand quotes when there is a plain HTML on the other side # # @param # @return [Nil] nil def ⚘ *args href, *title = args.flatten title = title.join(SEPARATOR).strip harvest __callee__, %Q( ) end # `:regular` handler for data lists (required since data list items # consist of two tags: `dt` and `dd`.) # @param [Array] args the words, gained since last call to {#harvest} # @return [Nil] nil def ▶ *args dt, dd = args.join(SEPARATOR).split(/\s+(?:—)\s+/) harvest __callee__, %Q( #{tagify :dt, {}, dt} #{tagify :dd, {}, dd} ) end # Alias for {#▶}, according to YAML rules specifies additional # class for the data list `
`) with no class. # @see Qipowl::Bowler#orphan # @param str the words, to be put in paragraph tag. # @return [String] tagged words. def orphan str "#{tagify(:p, {}, str.to_s.strip)}" end # @see Qipowl::Bowler#harvest # # Additionally it checks if there was a `:linewide` item, requiring # surrounding html element (like `