summaryrefslogtreecommitdiff
path: root/maintenance/jsduck/custom_tags.rb
blob: 39589a06415024da775a1a57854366f3805d988d (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Custom tags for JSDuck 5.x
# See also:
# - https://github.com/senchalabs/jsduck/wiki/Tags
# - https://github.com/senchalabs/jsduck/wiki/Custom-tags
# - https://github.com/senchalabs/jsduck/wiki/Custom-tags/7f5c32e568eab9edc8e3365e935bcb836cb11f1d
require 'jsduck/tag/tag'

class CommonTag < JsDuck::Tag::Tag
  def initialize
    @html_position = POS_DOC + 0.1
    @repeatable = true
  end

  def parse_doc(scanner, _position)
    if @multiline
      return { tagname: @tagname, doc: :multiline }
    else
      text = scanner.match(/.*$/)
      return { tagname: @tagname, doc: text }
    end
  end

  def process_doc(context, tags, _position)
    context[@tagname] = tags
  end

  def format(context, formatter)
    context[@tagname].each do |tag|
      tag[:doc] = formatter.format(tag[:doc])
    end
  end
end

class SourceTag < CommonTag
  def initialize
    @tagname = :source
    @pattern = 'source'
    super
  end

  def to_html(context)
    context[@tagname].map do |source|
      <<-EOHTML
        <h3 class='pa'>Source</h3>
        #{source[:doc]}
      EOHTML
    end.join
  end
end

class SeeTag < CommonTag
  def initialize
    @tagname = :see
    @pattern = 'see'
    super
  end

  def format(context, formatter)
    position = context[:files][0]
    context[@tagname].each do |tag|
      tag[:doc] = '<li>' + render_long_see(tag[:doc], formatter, position) + '</li>'
    end
  end

  def to_html(context)
    <<-EOHTML
      <h3 class="pa">Related</h3>
      <ul>
      #{ context[@tagname].map { |tag| tag[:doc] }.join("\n") }
      </ul>
    EOHTML
  end

  def render_long_see(tag, formatter, position)
    match = /\A([^\s]+)( .*)?\Z/m.match(tag)

    if match
      name = match[1]
      doc = match[2] ? ': ' + match[2] : ''
      return formatter.format("{@link #{name}} #{doc}")
    else
      JsDuck::Logger.warn(nil, 'Unexpected @see argument: "' + tag + '"', position)
      return tag
    end
  end
end

class ContextTag < CommonTag
  def initialize
    @tagname = :context
    @pattern = 'context'
    super
  end

  def format(context, formatter)
    position = context[:files][0]
    context[@tagname].each do |tag|
      tag[:doc] = render_long_context(tag[:doc], formatter, position)
    end
  end

  def to_html(context)
    <<-EOHTML
      <h3 class="pa">Context</h3>
      #{ context[@tagname].last[:doc] }
    EOHTML
  end

  def render_long_context(tag, formatter, position)
    match = /\A([^\s]+)/m.match(tag)

    if match
      name = match[1]
      return formatter.format("`context` : {@link #{name}}")
    else
      JsDuck::Logger.warn(nil, 'Unexpected @context argument: "' + tag + '"', position)
      return tag
    end
  end
end