Support JSDuck v5.x
authorJames D. Forrester <jforrester@wikimedia.org>
Fri, 11 Apr 2014 22:41:50 +0000 (15:41 -0700)
committerKrinkle <krinklemail@gmail.com>
Sat, 12 Apr 2014 00:46:45 +0000 (00:46 +0000)
Add support to JSDuck v5.x with a new "CustomTags.rb" following the new
format (and with a new name for one of the existing flags). Also works
for JSDuck v4.x as before. The call in maintenance/mwjsduck-gen is now
sync'ed with the call made by Jenkins in the CI environment.

Change-Id: Ie6649a3e39457bde7a0d8bd00da8ea53e76e3085

maintenance/jsduck/CustomTags.rb [new file with mode: 0644]
maintenance/jsduck/config.json
maintenance/mwjsduck-gen

diff --git a/maintenance/jsduck/CustomTags.rb b/maintenance/jsduck/CustomTags.rb
new file mode 100644 (file)
index 0000000..2aff988
--- /dev/null
@@ -0,0 +1,116 @@
+# 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)
+    if tag =~ /\A([^\s]+)( .*)?\Z/m
+      name = $1
+      doc = $2 ? ': ' + $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)
+    if tag =~ /\A([^\s]+)/m
+      name = $1
+      return formatter.format("`context` : {@link #{name}}")
+    else
+      JsDuck::Logger.warn(nil, 'Unexpected @context argument: "'+tag+'"', position)
+      return tag
+    end
+  end
+end
index 0635769..493815e 100644 (file)
@@ -2,9 +2,7 @@
        "--title": "MediaWiki core - Documentation",
        "--footer": "Documentation for MediaWiki core. Generated on {DATE} by {JSDUCK} {VERSION}.",
        "--categories": "./categories.json",
-       "--meta-tags": "./MetaTags.rb",
        "--eg-iframe": "./eg-iframe.html",
-       "--warnings": ["-no_doc"],
        "--builtin-classes": true,
        "--output": "../../docs/js",
        "--external": "HTMLElement,HTMLDocument,Window",
index 4254887..62d1bba 100755 (executable)
@@ -11,10 +11,20 @@ then
        exit 1
 fi
 
+# Support jsduck 4.x and 5.x
+JSDUCK_VERSION="$(jsduck --version | sed -e 's/[.].*//')"
+if [  "$JSDUCK_VERSION" = "JSDuck 4" ]; then
+       JSDUCK_VERSION_OPT="--meta-tags ./maintenance/jsduck/MetaTags.rb --warnings=-no_doc"
+else
+       JSDUCK_VERSION_OPT="--tags ./maintenance/jsduck/CustomTags.rb --warnings=-nodoc(class,public)"
+fi
+
 MWCORE_DIR=$(cd $(dirname $0)/..; pwd)
 
 jsduck \
 --config=$MWCORE_DIR/maintenance/jsduck/config.json \
---footer="Documentation for MediaWiki core ($JSDUCK_MWVERSION). Generated on {DATE} by {JSDUCK} {VERSION}." \
+$JSDUCK_VERSION_OPT \
+--footer="Documentation for branch ($JSDUCK_MWVERSION) on {DATE} by {JSDUCK} {VERSION}." \
+--processes 0 --warnings-exit-nonzero \
 && echo 'JSDuck execution finished.' \
 && ln -s ../../resources $MWCORE_DIR/docs/js/modules