Commit 75577f10 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Reintroduce loading of custom tags.

The old --meta-tags options is now replaced with --tags, that
pretty much behaves the same, except the classes themselves
need to be implemented differently.
parent c4915ea9
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ require 'jsduck/util/json'
require 'jsduck/util/os'
require 'jsduck/util/io'
require 'jsduck/util/parallel'
require 'jsduck/tag_registry'

module JsDuck

@@ -103,6 +104,7 @@ module JsDuck
      @categories_path = nil
      @source = true
      @images = []
      @custom_tag_paths = []
      @link_tpl = '<a href="#!/api/%c%-%m" rel="%c%-%m" class="docClass">%a</a>'
      # Note that we wrap image template inside <p> because {@img} often
      # appears inline within text, but that just looks ugly in HTML
@@ -145,6 +147,8 @@ module JsDuck
        read_filenames(canonical(fname))
      end
      validate

      @custom_tag_paths.each {|path| TagRegistry.load_from(path) }
    end

    def create_option_parser
@@ -404,6 +408,18 @@ module JsDuck
        opts.separator "Tweaking:"
        opts.separator ""

        opts.on('--tags=PATH',
          "Path to custom tag implementations.",
          "",
          "Can be a path to single Ruby file or a directory.",
          "",
          "This option can be used repeatedly to include multiple",
          "tags from different places.",
          "",
          "See also: https://github.com/senchalabs/jsduck/wiki/Custom-tags") do |path|
          @custom_tag_paths << canonical(path)
        end

        opts.on('--ignore-global',
          "Turns off the creation of 'global' class.",
          "",
+23 −13
Original line number Diff line number Diff line
@@ -3,25 +3,35 @@ require "jsduck/tag/tag"
module JsDuck

  class TagLoader
    # Loads builtin tags from /tag dir.
    # Returns array of Tag classes.
    def load_builtins
      load_tag_classes(File.dirname(__FILE__) + "/tag")
      tag_classes
    def initialize
      @already_loaded = {}
    end

    private
    # Loads tag classes from given dir or single file.
    #
    # Returns the tag classes that got loaded, sorted alphabetically
    # by class name. This ensures attributes in member signatures are
    # always rendered in the same order.
    def load_from(path)
      if File.directory?(path)
        Dir[path+"/**/*.rb"].each {|file| require(file) }
      else
        require(path)
      end

    # Loads tags from given dir.
    def load_tag_classes(dirname)
      Dir[dirname+"/**/*.rb"].each {|file| require(file) }
      tag_classes
    end

    # Returns all available Tag classes sorted alphabetically.  This
    # ensures attributes in member signatures are always rendered in
    # the same order.
    private

    def tag_classes
      JsDuck::Tag::Tag.descendants.sort {|a, b| a.to_s <=> b.to_s }
      classes = JsDuck::Tag::Tag.descendants
      # exclude already loaded classes
      classes.reject! {|cls| @already_loaded[cls.name] }
      # remember these classes as loaded
      classes.each {|cls| @already_loaded[cls.name] = true }
      # sort by classname
      classes.sort {|a, b| a.name <=> b.name }
    end

  end
+7 −1
Original line number Diff line number Diff line
@@ -16,7 +16,13 @@ module JsDuck
      @signatures = []
      @html_renderers = {:top => [], :bottom => []}

      instantiate_tags(TagLoader.new.load_builtins)
      @loader = TagLoader.new
      load_from(File.dirname(__FILE__) + "/tag")
    end

    # Loads and instantiates tags from the given file or dir.
    def load_from(path)
      instantiate_tags(@loader.load_from(path))
    end

    # Instantiates all descendants of JsDuck::Tag::Tag