Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 65 lines (57 sloc) 1.737 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
include YARD
include Templates

module TagTemplateHelper
  def all_tags
    Registry.all(:method).map {|m| m.tag('yard.tag') }.compact
  end

  def all_directives
    Registry.all(:method).map {|m| m.tag('yard.directive') }.compact
  end

  def collect_tags
    (all_tags + all_directives).sort_by {|t| t.name }
  end

  def tag_link(tag)
    link_file("docs/Tags.md", tag_link_name(tag), tag.name)
  end

  def tag_link_name(tag)
    prefix = tag.tag_name == 'yard.directive' ? '@!' : '@'
    h(prefix + tag.name)
  end

  # Wrap url_for and url_for_file to rewrite object when generating docs for
  # yard.tag/directive objects.
  %w(url_for, url_for_file).each do |meth|
    self.class.send(:define_method, meth) do
      if object.is_a?(CodeObjects::Base) &&
            (object.tag('yard.tag') || object.tag('yard.directive') ||
            (object.type == :class && object.superclass.name == :Directive))
          obj, self.object = object, Registry.root
          url = super
          self.object = obj
          url
      else
        super
      end
    end
  end

  def linkify(*args)
    if args.first.is_a?(String)
      case args.first
      when "yard:include_tags"
        return T('yard_tags').run(options)
      when /^tag:(\S+)/
        tag_name, suffix = $1, "tag"
        if tag_name =~ /^!/
          tag_name, suffix = tag_name[1..-1], "directive"
        end
        if obj = Registry.at("YARD::Tags::Library##{tag_name}_#{suffix}")
          return tag_link(obj.tag("yard.#{suffix}"))
        end
        log.warn "Cannot find tag: #{args.first}"
        return args.first
      end
    end
    super
  end
end

Template.extra_includes << TagTemplateHelper
Engine.register_template_path(File.dirname(__FILE__))
Something went wrong with that request. Please try again.