module Docs
  class GnuMake
    class EntriesFilter < Docs::EntriesFilter

      NO_ADDITIONAL_ENTRIES = [
        'Quick Reference', 'Instead of Executing Recipes',
        'Loaded Object Interface', 'Conversion of Guile Types',
        'Arguments to Specify the Goals', 'Standard Targets for Users',
        'Variables for Installation Directories', 'Errors Generated by Make',
        'The origin Function', 'The vpath Directive',
        'Interfaces from Guile to make', 'Output During Parallel Execution',
        'How to Run make', 'The flavor Function', 'Catalogue of Built-In Rules'
      ]

      DL_DT_TABLE = {
        'Automatic Variables' => 'Automatic Variables',
        'Other Special Variables' => 'Automatic Variables',
        'Variables Used by Implicit Rules' => 'Automatic Variables',
        'Special Built-in Target Names' => 'Built-in targets',
        'Functions for File Names' => 'File Names Functions',
        'Functions for String Substitution and Analysis' => 'String Substitution and Analysis Functions',
        'Functions for Conditionals' => 'Conditionals Functions',
        'Functions That Control Make' => 'Make Control Functions',
        'Syntax of Conditionals' => 'Conditionals Syntax'
      }

      def get_name
        name = at_css('.chapter', '.section', '.subsection', '.subsubsection', '.appendix')

        if name.nil?
          name = at_css('h1, h2, h3').content.slice(/[[:alpha:]]...*/)
        else
          name = name.content.slice(/[[:alpha:]]...*/)
        end

        name.gsub!(/Appendix.{2}/, '') if name.include?('Appendix')
        # remove withespace at the beginning left when "Appendix" is removed
        name.gsub!(/\G\s/, '')

        name
      end

      def get_type
        return 'Transforming text functions' if name =~ /The [a-z]+ Function/
        return 'Directives' if name =~ /The [a-z]+ Directive/
        'Manual'
      end

      def additional_entries
        entries = []

        return entries if NO_ADDITIONAL_ENTRIES.include?(name)

        css('dl dt').each do |node|

          break if name == 'Summary of Options'

          entry_type = ""

          if DL_DT_TABLE.key?(name)
            entry_type = DL_DT_TABLE[name]
          else
            entry_type = "Entry type missing"
          end

          entry_name = node.at_css('code')

          if entry_name.nil?
            next
          end

          entry_name = entry_name.content
          entry_path = slug.downcase + '#' + entry_name

          entries << [entry_name, entry_path, entry_type]
        end

        css('dt > samp').each do |node|

          break if name == 'Other Special Variables'

          entry_type = 'Automatic Variables' if name == 'Automatic Variables'
          entry_type = 'Functions for File Names' if name == 'Functions for File Names'
          entry_type = 'Make Cli Options' if name == 'Summary of Options'

          entry_name = node.content
          entry_path = slug.downcase + '#' + entry_name

          entries << [entry_name, entry_path, entry_type]
        end

        entries
      end

    end
  end
end
