Sha256: c2132dc136521c76c6401473769346f46e667041bebbad8f7941f71ca745b657

Contents?: true

Size: 1.91 KB

Versions: 21

Compression:

Stored size: 1.91 KB

Contents

require 'jsduck/logger'
require 'jsduck/io'
require 'fileutils'

module JsDuck

  # Deals with creation of main HTML or PHP files.
  class IndexHtml
    def initialize(assets, opts)
      @assets = assets
      @opts = opts
    end

    # In normal mode creates index.html.
    #
    # When --seo enabled, creates index.php, template.html and print-template.html.
    def write
      if @opts.seo
        FileUtils.cp(@opts.template_dir+"/index.php", @opts.output_dir+"/index.php")
        create_template_html(@opts.template_dir+"/template.html", @opts.output_dir+"/template.html")
        create_print_template_html(@opts.template_dir+"/print-template.html", @opts.output_dir+"/print-template.html")
      else
        create_template_html(@opts.template_dir+"/template.html", @opts.output_dir+"/index.html")
      end
    end

    private

    def create_template_html(in_file, out_file)
      write_template(in_file, out_file, {
        "{title}" => @opts.title,
        "{header}" => @opts.header,
        "{footer}" => "<div id='footer-content' style='display: none'>#{@opts.footer}</div>",
        "{extjs_path}" => @opts.extjs_path,
        "{welcome}" => @assets.welcome.to_html,
        "{categories}" => @assets.categories.to_html,
        "{guides}" => @assets.guides.to_html,
        "{head_html}" => @opts.head_html,
        "{body_html}" => @opts.body_html,
      })
    end

    def create_print_template_html(in_file, out_file)
      write_template(in_file, out_file, {
        "{title}" => @opts.title,
        "{header}" => @opts.header,
      })
    end

    # Opens in_file, replaces {keys} inside it, writes to out_file
    def write_template(in_file, out_file, replacements)
      Logger.instance.log("Writing", out_file)
      html = JsDuck::IO.read(in_file)
      html.gsub!(/\{\w+\}/) do |key|
        replacements[key] ? replacements[key] : key
      end
      File.open(out_file, 'w') {|f| f.write(html) }
    end

  end

end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
jsduck-4.0.1 lib/jsduck/index_html.rb
jsduck-4.0.0 lib/jsduck/index_html.rb
jsduck-3.11.2 lib/jsduck/index_html.rb
jsduck-4.0.beta2 lib/jsduck/index_html.rb
jsduck-3.11.1 lib/jsduck/index_html.rb
jsduck-4.0.beta lib/jsduck/index_html.rb
jsduck-3.11.0 lib/jsduck/index_html.rb
jsduck-3.10.5 lib/jsduck/index_html.rb
jsduck-3.10.4 lib/jsduck/index_html.rb
jsduck-3.10.3 lib/jsduck/index_html.rb
jsduck-3.10.2 lib/jsduck/index_html.rb
jsduck-3.10.1 lib/jsduck/index_html.rb
jsduck-3.10.0 lib/jsduck/index_html.rb
jsduck-3.9.1 lib/jsduck/index_html.rb
jsduck-3.9.0 lib/jsduck/index_html.rb
jsduck-3.9.0.pre lib/jsduck/index_html.rb
jsduck-3.8.4 lib/jsduck/index_html.rb
jsduck-3.8.3 lib/jsduck/index_html.rb
jsduck-3.8.2 lib/jsduck/index_html.rb
jsduck-3.8.1 lib/jsduck/index_html.rb