Commit 9b991b1f authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Extract IndexHtml class from TemplateDir class.

The TemplateDir is now only responsible for copying or linking
the tempate directory.
parent bf1833a3
Loading
Loading
Loading
Loading
+7 −5
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ require 'jsduck/template_dir'
require 'jsduck/class_writer'
require 'jsduck/source_writer'
require 'jsduck/app_data'
require 'jsduck/index_html'
require 'fileutils'

module JsDuck
@@ -61,12 +62,13 @@ module JsDuck
        cw.write(@opts.output_dir, ".json")
      else
        FileUtils.rm_rf(@opts.output_dir)
        TemplateDir.new(@opts).write

        template = TemplateDir.new(@relations, @opts)
        template.welcome = @welcome
        template.categories = @categories
        template.guides = @guides
        template.write
        index = IndexHtml.new(@opts)
        index.welcome = @welcome
        index.categories = @categories
        index.guides = @guides
        index.write

        app_data = AppData.new(@relations, @opts)
        app_data.guides = @guides
+65 −0
Original line number Diff line number Diff line
require 'jsduck/logger'
require 'fileutils'

module JsDuck

  # Deals with creation of main HTML or PHP files.
  class IndexHtml
    attr_accessor :welcome
    attr_accessor :categories
    attr_accessor :guides

    def initialize(opts)
      @opts = opts
    end

    def write
      create_template_html
      create_print_template_html

      if !@opts.seo
        FileUtils.rm(@opts.output_dir+"/index.php")
        FileUtils.cp(@opts.output_dir+"/template.html", @opts.output_dir+"/index.html")
      end
    end

    def create_template_html
      write_template("template.html", {
        "{title}" => @opts.title,
        "{header}" => @opts.header,
        "{footer}" => "<div id='footer-content' style='display: none'>#{@opts.footer}</div>",
        "{extjs_path}" => @opts.extjs_path,
        "{local_storage_db}" => @opts.local_storage_db,
        "{show_print_button}" => @opts.seo ? "true" : "false",
        "{touch_examples_ui}" => @opts.touch_examples_ui ? "true" : "false",
        "{welcome}" => @welcome.to_html,
        "{categories}" => @categories.to_html,
        "{guides}" => @guides.to_html,
        "{head_html}" => @opts.head_html,
        "{body_html}" => @opts.body_html,
      })
    end

    def create_print_template_html
      write_template("print-template.html", {
        "{title}" => @opts.title,
        "{header}" => @opts.header,
      })
    end

    # Opens file in template dir, replaces {keys} inside it, writes to output dir
    def write_template(filename, replacements)
      in_file = @opts.template_dir + '/' + filename
      out_file = @opts.output_dir + '/' + filename
      Logger.instance.log("Writing", out_file)
      html = IO.read(in_file)
      html.gsub!(/\{\w+\}/) do |key|
        replacements[key] ? replacements[key] : key
      end
      FileUtils.rm(out_file)
      File.open(out_file, 'w') {|f| f.write(html) }
    end

  end

end
+8 −57
Original line number Diff line number Diff line
require 'jsduck/logger'
require 'jsduck/json_duck'
require 'fileutils'

module JsDuck

  # Copies over the template directory and creates few files inside it.
  # Copies over the template directory.
  #
  # Or links when --template-links option specified.
  class TemplateDir
    attr_accessor :welcome
    attr_accessor :categories
    attr_accessor :guides

    def initialize(relations, opts)
      @relations = relations
    def initialize(opts)
      @opts = opts
    end

    def write
      if @opts.template_links
        link_template
        link_files
      else
        copy_template
      end

      create_template_html
      create_print_template_html

      if !@opts.seo
        FileUtils.rm(@opts.output_dir+"/index.php")
        FileUtils.cp(@opts.output_dir+"/template.html", @opts.output_dir+"/index.html")
        copy_files
      end

      if @opts.eg_iframe
@@ -36,12 +24,12 @@ module JsDuck
      end
    end

    def copy_template
    def copy_files
      Logger.instance.log("Copying template files to", @opts.output_dir)
      FileUtils.cp_r(@opts.template_dir, @opts.output_dir)
    end

    def link_template
    def link_files
      Logger.instance.log("Linking template files to", @opts.output_dir)
      FileUtils.mkdir(@opts.output_dir)
      Dir.glob(@opts.template_dir + "/*").each do |file|
@@ -49,43 +37,6 @@ module JsDuck
      end
    end

    def create_template_html
      write_template("template.html", {
        "{title}" => @opts.title,
        "{header}" => @opts.header,
        "{footer}" => "<div id='footer-content' style='display: none'>#{@opts.footer}</div>",
        "{extjs_path}" => @opts.extjs_path,
        "{local_storage_db}" => @opts.local_storage_db,
        "{show_print_button}" => @opts.seo ? "true" : "false",
        "{touch_examples_ui}" => @opts.touch_examples_ui ? "true" : "false",
        "{welcome}" => @welcome.to_html,
        "{categories}" => @categories.to_html,
        "{guides}" => @guides.to_html,
        "{head_html}" => @opts.head_html,
        "{body_html}" => @opts.body_html,
      })
    end

    def create_print_template_html
      write_template("print-template.html", {
        "{title}" => @opts.title,
        "{header}" => @opts.header,
      })
    end

    # Opens file in template dir, replaces {keys} inside it, writes to output dir
    def write_template(filename, replacements)
      in_file = @opts.template_dir + '/' + filename
      out_file = @opts.output_dir + '/' + filename
      Logger.instance.log("Writing", out_file)
      html = IO.read(in_file)
      html.gsub!(/\{\w+\}/) do |key|
        replacements[key] ? replacements[key] : key
      end
      FileUtils.rm(out_file)
      File.open(out_file, 'w') {|f| f.write(html) }
    end

  end

end