<commit_before>require 'yard/templates/helpers/html_helper'

module YARD::Templates::Helpers::HtmlHelper
  def topicize(str)
    ::YARD::APIPlugin::Serializer.topicize(str)
  end

  def url_for_file(file, anchor = nil)
    link = file.filename
    link += (anchor ? '#' + urlencode(anchor) : '')
    link
  end

  def visible_static_pages()
    @@visible_static_pages ||= begin
      static_pages.reject { |page| page[:exclude_from_sidebar] }
    end
  end

  def static_pages()
    @@static_pages ||= begin
      locate_static_pages(YARD::APIPlugin.options)
    end
  end

  def locate_static_pages(options)
    title_overrides = {}
    excludes = {}

    paths = Array(options.static).map do |entry|
      pages = if entry.is_a?(Hash)
        if !entry.has_key?('path')
          raise "Static page entry must contain a 'path' parameter: #{entry}"
        elsif !entry.has_key?('title')
          raise "Static page entry must contain a 'title' parameter: #{entry}"
        end

        title_overrides[entry['path']] = entry['title']
        excludes[entry['path']] = true if entry['exclude_from_sidebar']

        entry['path']
      elsif entry.is_a?(Array)
        entry.map(&:to_s)
      elsif entry.is_a?(String)
        entry
      end
    end

    pages = Dir.glob(paths.flatten.compact)

    if options.readme && !options.one_file
      pages.delete_if { |page| page.match(options.readme) }
    end

    pages.uniq.map do |page|
      filename = 'file.' + File.split(page).last.sub(/\..*$/, '.html')

      title = (
        title_overrides[page] ||
        File.basename(page)
          .sub(/\.\w+$/, '')
          .gsub('_', ' ')
          .gsub(/\W/, ' ')
          .gsub(/\s+/, ' ')
          .capitalize
      )

      logger.debug "Serializing static page #{page} (#{title})"

      {
        src: page,
        filename: filename,
        title: title,
        exclude_from_sidebar: !!excludes[page]
      }
    end.sort_by { |page| page[:title] }
  end
  protected :locate_static_pages

  # override yard-appendix link_appendix
  def link_appendix(ref)
    logger.debug "Linking appendix: #{ref}"

    unless appendix = lookup_appendix(ref.to_s)
      YARD::APIPlugin.on_error "Unable to locate referenced appendix '#{ref}'"
      return ref
    end

    html_file = if options[:all_resources] && api_options.one_file
      'index.html'
    elsif options[:all_resources]
      'all_resources.html'
    else
      url_for(appendix.namespace)
    end

    bookmark = "#{appendix.name.to_s.gsub(' ', '+')}-appendix"
    link_url("#{html_file}##{bookmark}", appendix.title).tap do |link|
      logger.debug "\tAppendix link: #{link}"
    end
  end

  def sidebar_link(href, title, is_active)
    link_url(href, title, { class: is_active ? 'active' : '' })
  end

  # Turns text into HTML using +markup+ style formatting.
  #
  # @override Syntax highlighting is not performed on the HTML block.
  #
  # @param [String] text the text to format
  # @param [Symbol] markup examples are +:markdown+, +:textile+, +:rdoc+.
  #   To add a custom markup type, see {MarkupHelper}<commit_msg>Assume sidebar links are not active by default<commit_after>require 'yard/templates/helpers/html_helper'

module YARD::Templates::Helpers::HtmlHelper
  def topicize(str)
    ::YARD::APIPlugin::Serializer.topicize(str)
  end

  def url_for_file(file, anchor = nil)
    link = file.filename
    link += (anchor ? '#' + urlencode(anchor) : '')
    link
  end

  def visible_static_pages()
    @@visible_static_pages ||= begin
      static_pages.reject { |page| page[:exclude_from_sidebar] }
    end
  end

  def static_pages()
    @@static_pages ||= begin
      locate_static_pages(YARD::APIPlugin.options)
    end
  end

  def locate_static_pages(options)
    title_overrides = {}
    excludes = {}

    paths = Array(options.static).map do |entry|
      pages = if entry.is_a?(Hash)
        if !entry.has_key?('path')
          raise "Static page entry must contain a 'path' parameter: #{entry}"
        elsif !entry.has_key?('title')
          raise "Static page entry must contain a 'title' parameter: #{entry}"
        end

        title_overrides[entry['path']] = entry['title']
        excludes[entry['path']] = true if entry['exclude_from_sidebar']

        entry['path']
      elsif entry.is_a?(Array)
        entry.map(&:to_s)
      elsif entry.is_a?(String)
        entry
      end
    end

    pages = Dir.glob(paths.flatten.compact)

    if options.readme && !options.one_file
      pages.delete_if { |page| page.match(options.readme) }
    end

    pages.uniq.map do |page|
      filename = 'file.' + File.split(page).last.sub(/\..*$/, '.html')

      title = (
        title_overrides[page] ||
        File.basename(page)
          .sub(/\.\w+$/, '')
          .gsub('_', ' ')
          .gsub(/\W/, ' ')
          .gsub(/\s+/, ' ')
          .capitalize
      )

      logger.debug "Serializing static page #{page} (#{title})"

      {
        src: page,
        filename: filename,
        title: title,
        exclude_from_sidebar: !!excludes[page]
      }
    end.sort_by { |page| page[:title] }
  end
  protected :locate_static_pages

  # override yard-appendix link_appendix
  def link_appendix(ref)
    logger.debug "Linking appendix: #{ref}"

    unless appendix = lookup_appendix(ref.to_s)
      YARD::APIPlugin.on_error "Unable to locate referenced appendix '#{ref}'"
      return ref
    end

    html_file = if options[:all_resources] && api_options.one_file
      'index.html'
    elsif options[:all_resources]
      'all_resources.html'
    else
      url_for(appendix.namespace)
    end

    bookmark = "#{appendix.name.to_s.gsub(' ', '+')}-appendix"
    link_url("#{html_file}##{bookmark}", appendix.title).tap do |link|
      logger.debug "\tAppendix link: #{link}"
    end
  end

  def sidebar_link(href, title, is_active=false)
    link_url(href, title, { class: is_active ? 'active' : '' })
  end

  # Turns text into HTML using +markup+ style formatting.
  #
  # @override Syntax highlighting is not performed on the HTML block.
  #
  # @param [String] text the text to format
  # @param [Symbol] markup examples are +:markdown+, +:textile+, +:rdoc+.
  #   To add a custom markup type, see {MarkupHelper}
{'fraction_non_alphanumeric': 0.13744987971130715, 'fraction_numerical': 0.0, 'mean_word_length': 2.720763723150358, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2881051', 'n_tokens_mistral': 2092, 'n_tokens_neox': 2018, 'n_words': 460}