Commit eb8631d3 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Added --json and --img command line options.

These allow to define templates to use in export for replacing
{@link} and {@img} tags.
parent aed18021
Loading
Loading
Loading
Loading
+19 −1
Original line number Diff line number Diff line
@@ -34,10 +34,28 @@ opts = OptionParser.new do | opts |
    app.template_dir = path
  end

  opts.on('--json', "Produces JSON export instead of HTML documentation.") do |path|
  opts.on('--json', "Produces JSON export instead of HTML documentation.") do
    app.export = :json
  end

  opts.on('--link=TPL', "HTML template for replacing {@link} in export.",
    "Possible placeholders:",
    "%c - full class name (e.g. 'Ext.Panel')",
    "%m - class member name (e.g. 'urlEncode')",
    "%M - class member name, prefixed with hash (e.g. '#urlEncode')",
    "%a - anchor text for link",
    "Default value: '<a href=\"%c%M\">%a</a>'") do |tpl|
    app.link_tpl = tpl
  end

  opts.on('--img=TPL', "HTML template for replacing {@img} in export.",
    "Possible placeholders:",
    "%u - URL from @img tag (e.g. 'some/path.png')",
    "%a - alt text for image",
    "Default value: '<img src=\"%u\" alt=\"%a\"/>'") do |tpl|
    app.img_tpl = tpl
  end

  # For debugging it's often useful to set --processes=0 to get deterministic results.
  opts.on('-p', '--processes=COUNT', "The number of parallel processes to use.",
    "Defaults to the number of processors/cores.",
+9 −1
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ module JsDuck
    attr_accessor :input_files
    attr_accessor :verbose
    attr_accessor :export
    attr_accessor :link_tpl
    attr_accessor :img_tpl

    def initialize
      @output_dir = nil
@@ -32,6 +34,8 @@ module JsDuck
      @input_files = []
      @verbose = false
      @export = nil
      @link_tpl = nil
      @img_tpl = nil
      @timer = Timer.new
      @parallel = ParallelWrap.new
    end
@@ -174,7 +178,11 @@ module JsDuck

    # Writes JSON export file for each class
    def write_json(path, relations)
      exporter = Exporter.new(relations)
      formatter = DocFormatter.new
      formatter.link_tpl = @link_tpl if @link_tpl
      formatter.img_tpl = @img_tpl if @img_tpl
      formatter.relations = relations
      exporter = Exporter.new(relations, formatter)
      @parallel.each(relations.classes) do |cls|
        filename = path + "/" + cls[:name] + ".json"
        puts "Writing to #{filename} ..." if @verbose
+2 −6
Original line number Diff line number Diff line
@@ -9,13 +9,9 @@ module JsDuck
  # Also all the :doc elements will be formatted - converted from
  # markdown to HTML and @links resolved.
  class Exporter
    attr_accessor :relations

    def initialize(relations)
    def initialize(relations, formatter)
      @relations = relations
      @formatter = DocFormatter.new
      @formatter.css_class = 'docClass'
      @formatter.relations = relations
      @formatter = formatter
    end

    # Returns all data in Class object as hash.