Commit 46dc39ff authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Support for detecting inline example options.

Touch examples with "raw" option now work as intended.
parent 88da3e52
Loading
Loading
Loading
Loading
+21 −3
Original line number Diff line number Diff line
@@ -16,7 +16,8 @@ module JsDuck
            :id => cls.full_name + "-" + i.to_s,
            :name => cls.full_name + " example #" + (i+1).to_s,
            :href => '#!/api/' + cls.full_name,
            :code => ex,
            :code => ex[:code],
            :options => ex[:options],
          }
        end
      end
@@ -34,9 +35,16 @@ module JsDuck
      while !s.eos? do
        if s.check(/</)
          if s.check(@begin_example_re)
            s.scan(@begin_example_re)

            s.scan(@begin_example_re) =~ @begin_example_re
            options = build_options_hash($1)

            ex = s.scan_until(@end_example_re).sub(@end_example_re, '')
            examples << CGI.unescapeHTML(strip_tags(ex))

            examples << {
              :code => CGI.unescapeHTML(strip_tags(ex)),
              :options => options,
            }
          else
            s.skip(/</)
          end
@@ -48,6 +56,16 @@ module JsDuck
      examples
    end

    private

    def build_options_hash(css_classes)
      hash = {}
      css_classes.split(/ +/).each do |k|
        hash[k] = true
      end
      hash
    end

    def strip_tags(str)
      str.gsub(/<.*?>/, "")
    end
+23 −4
Original line number Diff line number Diff line
@@ -3,9 +3,10 @@ require "jsduck/doc_formatter"

describe JsDuck::InlineExamples do

  def extract(doc)
    html = JsDuck::DocFormatter.new.format(doc)
    JsDuck::InlineExamples.new([]).extract(html)
  def extract(doc, opts=nil)
    html = (opts == :html) ? doc : JsDuck::DocFormatter.new.format(doc)
    result = JsDuck::InlineExamples.new([]).extract(html)
    (opts == :raw) ? result : result.map {|ex| ex[:code] }
  end

  it "finds no examples from empty string" do
@@ -55,6 +56,8 @@ And another...
    EOS
  end

  # Escaping

  it "preserves HTML inside example" do
    extract(<<-EOS).should == ["document.write('<b>Hello</b>');\n"]
    @example
@@ -63,10 +66,26 @@ And another...
  end

  it "ignores links inside examples" do
    JsDuck::InlineExamples.new([]).extract(<<-EOS).should == ["Ext.define();\n"]
    extract(<<-EOS, :html).should == ["Ext.define();\n"]
<pre class='inline-example '><code><a href="#!/api/Ext">Ext</a>.define();
</code></pre>
EOS
  end

  # Options

  it "detects options after @example tag" do
    extract(<<-EOS, :raw).should == [{:code => "foo();\n", :options => {"raw" => true, "blah" => true}}]
    @example raw blah
    foo();
    EOS
  end

  it "detects no options when none of them after @example tag" do
    extract(<<-EOS, :raw).should == [{:code => "foo();\n", :options => {}}]
    @example
    foo();
    EOS
  end

end
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ Ext.define('Docs.model.DocTest', {
        'name',
        'href',
        'code',
        'options',
        {name: 'status', defaultValue: 'ready'},
        'message'
    ]
+4 −1
Original line number Diff line number Diff line
@@ -140,12 +140,15 @@ Ext.define('Docs.view.doctests.Index', {
        this.clearTestRunner();
        var testRunner = this.getComponent('testrunner');
        var record = config.examples.shift();
        var options = record.get('options');
        options.preview = false; // always disable the preview option

        var example = testRunner.add(
            Ext.create('Docs.view.examples.Inline', {
                cls: 'doc-test-preview',
                height: 0,
                value: record.get('code')
                value: record.get('code'),
                options: options
            })
        );

+5 −0
Original line number Diff line number Diff line
@@ -20,6 +20,11 @@ Ext.define('Docs.view.examples.Inline', {
    // Make too long examples scrollable
    maxCodeHeight: 400,

    /**
     * @cfg {String} value
     * The JavaScript code of the example.
     */

    /**
     * @cfg {Object} options
     * A set of options for configuring the preview: