<commit_before>module HTMLHelpers

  LinkElementsXPath = '//@href | //@src'

  def html_document(type=:html4_transitional, &block)
    doc = Nokogiri::HTML::Document.new
    doc.encoding = 'UTF-8'
    doc.internal_subset.remove
    case type
    when :html4_transitional
      doc.create_internal_subset('html', '-//W3C//DTD HTML 4.01 Transitional//EN', 'http://www.w3.org/TR/html4/loose.dtd')
    when :html5
      doc.create_internal_subset('html', nil, nil)
    else
      raise "Unknown HTML type: #{type.inspect}"
    end
    Nokogiri::HTML::Builder.with(doc) do |doc|
      yield(doc)
    end
    doc
  end

  def html_fragment(&block)
    html = Nokogiri::HTML::DocumentFragment.parse('')
    Nokogiri::HTML::Builder.with(html) do |html|
      yield(html)
    end
    html
  end

  def parse_html(str)
    html = Nokogiri::HTML::Document.parse(str) { |config| config.strict }
    html.errors.each do |error|
      next if error.message =~ /^Tag (.*?) invalid$/
      raise Error, "HTML error at line #{error.line}, column #{error.column}: #{error.message}"
    end
    html
  end

  def find_link_elements(html)
    html.xpath(LinkElementsXPath)
  end

  def replace_element(html, xpath, &block)
    html.xpath(xpath).each do |elem|
      elem.replace(yield(elem))
    end
  end

  def amazon_button(asin)
    html_fragment do |html|
      html.a(href: "http://www.amazon.com/dp/#{asin}") do
        html.img(src: '/images/buy1._V46787834_.gif', alt: 'Buy from Amazon.com')
      end
    end
  end

  def paypal_button(id)
    html_fragment do |html|
      html.form(action: 'https://www.paypal.com/cgi-bin/webscr', method: 'post') do
        html.input(
          type: 'hidden',
          name: 'cmd',<commit_msg>Fix error reporting for HTML parsing.<commit_after>module HTMLHelpers

  LinkElementsXPath = '//@href | //@src'

  def html_document(type=:html4_transitional, &block)
    doc = Nokogiri::HTML::Document.new
    doc.encoding = 'UTF-8'
    doc.internal_subset.remove
    case type
    when :html4_transitional
      doc.create_internal_subset('html', '-//W3C//DTD HTML 4.01 Transitional//EN', 'http://www.w3.org/TR/html4/loose.dtd')
    when :html5
      doc.create_internal_subset('html', nil, nil)
    else
      raise "Unknown HTML type: #{type.inspect}"
    end
    Nokogiri::HTML::Builder.with(doc) do |doc|
      yield(doc)
    end
    doc
  end

  def html_fragment(&block)
    html = Nokogiri::HTML::DocumentFragment.parse('')
    Nokogiri::HTML::Builder.with(html) do |html|
      yield(html)
    end
    html
  end

  def parse_html(str)
    html = Nokogiri::HTML::Document.parse(str) { |config| config.strict }
    html.errors.each do |error|
      raise "HTML error #{error}" unless error.message =~ /Tag .+? invalid$/
    end
    html
  end

  def find_link_elements(html)
    html.xpath(LinkElementsXPath)
  end

  def replace_element(html, xpath, &block)
    html.xpath(xpath).each do |elem|
      elem.replace(yield(elem))
    end
  end

  def amazon_button(asin)
    html_fragment do |html|
      html.a(href: "http://www.amazon.com/dp/#{asin}") do
        html.img(src: '/images/buy1._V46787834_.gif', alt: 'Buy from Amazon.com')
      end
    end
  end

  def paypal_button(id)
    html_fragment do |html|
      html.form(action: 'https://www.paypal.com/cgi-bin/webscr', method: 'post') do
        html.input(
          type: 'hidden',
          name: 'cmd',
{'fraction_non_alphanumeric': 0.1413427561837456, 'fraction_numerical': 0.011189634864546525, 'mean_word_length': 2.891179839633448, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 6, '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': '738350', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1186, 'n_words': 249}