<commit_before>require 'set'
require 'nokogiri'
require 'uri'
require 'css_parser'

module Roadie
  class Inliner
    # @param [String] css
    def initialize(css)
      # TODO: Pass a list of Stylesheet rather than raw CSS
      @stylesheet = Stylesheet.new("unnamed", css)
    end

    # Start the inlining, mutating the DOM tree.
    #
    # @param [Nokogiri::HTML::Document] dom
    # @return [nil]
    def inline(dom)
      style_map = StyleMap.new

      stylesheet.each_inlinable_block do |selector, properties|
        style_map.add_elements elements_in_selector(selector, dom), properties
      end

      style_map.each_element do |element, rules|
        apply_element_style element, rules
      end

      nil
    end

    private
    attr_reader :stylesheet

    def apply_element_style(element, rules)
      element["style"] = [rules.to_s, element["style"]].compact.join(";")
    end

    def elements_in_selector(selector, dom)
      dom.css(selector.to_s)
    # There's no way to get a list of supported pseudo rules, so we're left
    # with having to rescue errors.
    # Pseudo selectors that are known to be bad are skipped automatically but
    # this will catch the rest.
    rescue Nokogiri::XML::XPath::SyntaxError, Nokogiri::CSS::SyntaxError => error
      warn "Roadie cannot use #{selector.inspect} when inlining stylesheets"
      []
    rescue => error
      warn "Roadie got error when looking for #{selector.inspect}: #{error}"
      raise unless error.message.include?('XPath')
      []
    end

    class StyleMap
      def initialize
        @map = {}
      end

      def add_elements(elements, new_properties)
        elements.each { |element| add(element, new_properties) }
      end

      def add(element, new_properties)
        properties = (@map[element] ||= StyleProperties.new([]))
        properties.merge!(new_properties)
      end

      def each_element
        @map.each_pair { |element, rules| yield element, rules }
      end
    end
  end
end<commit_msg>Refactor and improve readability of Inliner code<commit_after>require 'set'
require 'nokogiri'
require 'uri'
require 'css_parser'

module Roadie
  class Inliner
    # @param [String] css
    def initialize(css)
      # TODO: Pass a list of Stylesheet rather than raw CSS
      @stylesheet = Stylesheet.new("unnamed", css)
    end

    # Start the inlining, mutating the DOM tree.
    #
    # @param [Nokogiri::HTML::Document] dom
    # @return [nil]
    def inline(dom)
      apply style_map(dom)
      nil
    end

    private
    attr_reader :stylesheet

    def apply(style_map)
      style_map.each_element do |element, properties|
        apply_element_style element, properties
      end
    end

    def style_map(dom)
      style_map = StyleMap.new

      stylesheet.each_inlinable_block do |selector, properties|
        elements = elements_matching_selector(selector, dom)
        style_map.add elements, properties
      end

      style_map
    end

    def apply_element_style(element, properties)
      element["style"] = [properties.to_s, element["style"]].compact.join(";")
    end

    def elements_matching_selector(selector, dom)
      dom.css(selector.to_s)
    # There's no way to get a list of supported pseudo selectors, so we're left
    # with having to rescue errors.
    # Pseudo selectors that are known to be bad are skipped automatically but
    # this will catch the rest.
    rescue Nokogiri::XML::XPath::SyntaxError, Nokogiri::CSS::SyntaxError => error
      warn "Roadie cannot use #{selector.inspect} when inlining stylesheets"
      []
    rescue => error
      warn "Roadie got error when looking for #{selector.inspect}: #{error}"
      raise unless error.message.include?('XPath')
      []
    end

    class StyleMap
      def initialize
        @map = Hash.new { |hash, key|
          hash[key] = StyleProperties.new([])
        }
      end

      def add(elements, new_properties)
        Array(elements).each do |element|
          @map[element].merge!(new_properties)
        end
      end

      def each_element
        @map.each_pair { |element, properties| yield element, properties }
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09025444071051368, 'fraction_numerical': 0.0, 'mean_word_length': 2.7608303249097474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2810118', 'n_tokens_mistral': 1252, 'n_tokens_neox': 1206, 'n_words': 370}