require 'rdoc/rdoc'
require 'rdoc/generator'
require 'yaml'
require File.dirname(__FILE__) + '/to_texinfo'

module RDoc
  RDoc::GENERATORS['texinfo'] = RDoc::Generator.new("rdoc_texinfo",
                                                    :Texinfo,
                                                    'texinfo')
  module Generator
    class Context
      attr_reader :values
      def initialize(context, options)
        @context = context
        @options = options
        # @template = options.template_class
        # HACK: can't find a way to override this elsewhere
        @formatter = ::RDoc::Markup::ToTexInfo.new
        # Oh wait, this doesn't actually *do* anything.
      end
    end

    class Method
      def markup_code(tokens)
        # Duh; will need to escape this
        tokens.map{ |t| t.text }.join("\n")
      end
    end

    # This generates Texinfo files for viewing with GNU Info or Emacs
    # from RDoc extracted from Ruby source files.
    class Texinfo
      VERSION = '0.0.1'

      # Where does the texinfo output go?
      DEFAULT_FILENAME = 'rdoc.texinfo'

      # What should the .info file be named by default?
      DEFAULT_INFO_FILENAME = 'rdoc.info'
      
      include Generator::MarkUp

      # Accept some options
      def initialize(options)
        # TODO: do something about these options at some point.
        @options = options
        @options.inline_source = true
        # TODO: @options.title = 
      end

      # Generate the +texinfo+ files
      def generate(toplevels)
        @toplevels = toplevels
        @files, @classes = ::RDoc::Generator::Context.build_indicies(@toplevels,
                                                                     @options)

        (@files + @classes).each { |x| x.value_hash }

        open(@options.op_name || DEFAULT_FILENAME, 'w') do |f|
          f.puts TexinfoTemplate.new('files' => @files,
                                     'classes' => @classes,
                                     'filename' => DEFAULT_INFO_FILENAME, # TODO: set
                                     'title' => @options.title).render
        end
        # create info files and install?
      end

      class << self
        # Factory? We don't need no stinkin' factory!
        alias_method :for, :new
      end
    end

    # Basically just a wrapper around ERB. Should inline this into
    # Generator::Texinfo probably.
    # TODO: maybe steal RDoc::TemplatePage
    class TexinfoTemplate
      def initialize(values, file = 'texinfo.erb')
        @values, @file = [values, file]
        @v = @values
      end
      
      def template
        ::File.read(::File.dirname(__FILE__) + '/' + @file)
      end

      # Go!
      def render
        ERB.new(template).result binding
      end

      def href(location, text)
        text # TODO: how does texinfo do hyperlinks?
      end

      def target(name, text)
        text # TODO: how do hyperlink targets work?
      end

      def hash_or_dot(section)
        { 'Class' => '.',
          'Module' => '::',
          'Instance' => '#',
        }[section['category']]
      end
    end
  end
end

Generated using scpaste by Phil Hagelberg at Wed Apr 2 15:49:07 2008.