# encoding: utf-8 require 'net/http' require 'htmlbeautifier' require_relative '../core/bowler' require_relative '../bowlers/htmldoc' module Qipowl # 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 text = [*args].join(SEPARATOR) mine, rest = text.split("#{__callee__}∎", 2) [tagify(∃_grip_tag(__callee__), {:class => ∃_grip(__callee__)[: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 [standalone(∃_alone_tag(__callee__), {:class => ∃_alone(__callee__)[:class]}), args] end # `:block` default handler # @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 param, args harvest __callee__, tagify( ∃_block_tag(__callee__), {:class => (param.strip.empty? ? ∃_block(__callee__)[:class] : param.strip)}, args.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 param, *rest = args.flatten param = param.unbowl.to_s.prepend("#{__callee__}#{String::NBSP}") [tagify(∃_magnet_tag(__callee__), {:class => ∃_magnet(__callee__)[:class]}, param), rest] end # `:regular` default handler # @param [Array] args the words, gained since last call to {#harvest} def ∀_regular *args harvest __callee__, tagify( ∃_regular_tag(canonize(__callee__)), {:class => ∃_regular(canonize(__callee__))[:class]}, args ) 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 term, *title = args.flatten mine, rest = [*title].join(SEPARATOR).split("#{__callee__}∎", 2) [tagify(∃_grip_tag(__callee__), {:title => mine, :class => ∃_grip(__callee__)[: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 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 ∃_grip_tag(__callee__), {: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_tag(__callee__)) 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 = "[info]#{param}" [ljref, rest] end def ☇ *args param, *rest = args.flatten [tagify(∃_magnet_tag(__callee__), {: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 harvest __callee__, %Q(

#{title.join(SEPARATOR)}

) 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 `
` tag behind (`dl-horizontal`.) alias_method :▷, :▶ protected # Computes the level of the `:linewide` element by counting # preceeding non-breakable spaces. For instance, nested lists # are produced by appending `"\u{00A0}"` to the line item # DSL tag: # # li = "• li1 \u{00A0}• nested 1 \u{00A0}• nested 2 • li2" # # @param [Symbol|String] callee the DSL symbol to get the level information for. # @return [Integer] the level requested. # def level callee (callee = callee.to_s).gsub(/#{String::NBSP}/, '').empty? ? -1 : (0..callee.length-1).each { |i| break i if callee[i] != String::NBSP } end def canonize callee callee.to_s.gsub(/^#{String::NBSP}*/, '').to_sym if callee end # @see Qipowl::Bowler#harvest # # Additionally it checks if there was a `:linewide` item, requiring # surrounding html element (like `