Sha256: cfcc867e241d18181fb04714196317e0741d773aa19c6a02aeb690727098caa7

Contents?: true

Size: 1.81 KB

Versions: 3

Compression:

Stored size: 1.81 KB

Contents

require 'erb'
require 'fileutils'
require 'doc_util'
require 'resource_doc'
require 'rapi_config'
require 'rapi_doc/railtie' if defined?(Rails)

module RapiDoc
  class RAPIDoc

    include RapiConfig

    # Initalize the ApiDocGenerator
    def initialize(resources)
      puts "Apidoc started..."
      @resources = resources
      generate_templates!
      move_structure!
      puts "Finished."
    end

    # Iterates over the resources creates views for them.
    # Creates an index file
    def generate_templates!

      @resources.each do |r|
        r.parse_apidoc!
        r.generate_view!(@resources, temp_dir)
      end
      generate_index!
      copy_styles!
    end

    # generate the index file for the api views
    def generate_index!
      template = ""
      @page_type = 'index'
      File.open(layout_file(:target)).each { |line| template << line }
      parsed = ERB.new(template).result(binding)
      File.open(File.join(temp_dir, "index.html"), 'w') { |file| file.write parsed }
    end

    def move_structure!
      target_folder = "#{::Rails.root.to_s}/public/apidoc/"
      Dir.mkdir(target_folder) if (!File.directory?(target_folder))

      Dir.new(temp_dir).each do |d|
        if d =~ /^[a-zA-Z]+\.(html|css)$/ # Only want to copy over the .html files, not the .erb templates
          FileUtils.cp  File.join(temp_dir + d), target_folder + d
        end

        #Clean up the no longer needed files
        filepath = "#{temp_dir}/#{d}"
        File.delete(filepath) unless File.directory?(filepath)
      end
    end

    def copy_styles!
      Dir[File.join(File.dirname(__FILE__), '..', 'templates/*')].each do |f|
        if f =~ /[\/a-zA-Z\.]+\.css$/i
          FileUtils.cp f, temp_dir
        end
      end
    end

    private

    def temp_dir
      @temp_dir ||= "#{Dir.mktmpdir("apidoc")}/"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rapi_doc-0.2.2 lib/rapi_doc.rb
rapi_doc-0.2.1 lib/rapi_doc.rb
rapi_doc-0.1.2 lib/rapi_doc.rb