Commit 7314307e authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Implement export of inline examples.

parent 69888191
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ require 'jsduck/index_html'
require 'jsduck/api_exporter'
require 'jsduck/full_exporter'
require 'jsduck/app_exporter'
require 'jsduck/examples_exporter'
require 'jsduck/inline_examples'
require 'fileutils'

@@ -54,7 +55,11 @@ module JsDuck
      if @opts.export
        format_classes
        FileUtils.rm_rf(@opts.output_dir) unless @opts.output_dir == :stdout
        exporters = {:full => FullExporter, :api => ApiExporter}
        exporters = {
          :full => FullExporter,
          :api => ApiExporter,
          :examples => ExamplesExporter,
        }
        cw = ClassWriter.new(exporters[@opts.export], @relations, @opts)
        cw.write(@opts.output_dir, ".json")
      else
@@ -75,7 +80,7 @@ module JsDuck
        format_classes

        if @opts.doctests
          InlineExamples.new(@relations).write(@opts.output_dir+"/inline-examples.js")
          InlineExamples.new.add_classes(@relations).write(@opts.output_dir+"/inline-examples.js")
        end

        cw = ClassWriter.new(AppExporter, @relations, @opts)
+35 −0
Original line number Diff line number Diff line
require 'jsduck/inline_examples'

module JsDuck

  # Exporter for inline examples.
  #
  # It produces the following structure:
  #
  # {
  #   :name => "Panel",
  #   :examples => [
  #     {:code => "bla bla", :options => {}},
  #     {:code => "bla bla", :options => {"raw" => true}},
  #     ...
  #   ]
  # }
  #
  class ExamplesExporter
    def initialize(relations, opts)
      # All params ignored, they're present to be compatible with
      # other exporters.
      @inline_examples = InlineExamples.new
    end

    # Returns hash of class name and inline examples
    def export(cls)
      {
        :name => cls[:name],
        :examples => @inline_examples.extract(cls[:doc]),
      }
    end

  end

end
+8 −2
Original line number Diff line number Diff line
@@ -5,11 +5,14 @@ module JsDuck

  # Extracts inline examples from formatted docs and writes to file
  class InlineExamples
    def initialize(relations)
    def initialize
      @begin_example_re = /<pre class='inline-example ([^']*)'><code>/
      @end_example_re = /<\/code><\/pre>/

      @examples = []
    end

    # Extracts inline examples from classes
    def add_classes(relations)
      relations.each do |cls|
        extract(cls[:doc]).each_with_index do |ex, i|
          @examples << {
@@ -21,8 +24,11 @@ module JsDuck
          }
        end
      end

      self
    end

    # Writes all found examples to .js file
    def write(filename)
      JsonDuck.write_jsonp(filename, "__inline_examples__", @examples)
    end
+4 −3
Original line number Diff line number Diff line
@@ -274,7 +274,8 @@ module JsDuck
        opts.on('--export=TYPE',
          "Exports docs in JSON.  TYPE is one of:",
          "* full     - full class docs.",
          "* api  - only class- and member names.", " ") do |format|
          "* api      - only class- and member names.",
          "* examples - extracts inline examples from classes.", " ") do |format|
          @export = format.to_sym
        end

@@ -493,7 +494,7 @@ module JsDuck
      elsif @output_dir == :stdout && !@export
        puts "Output to STDOUT only works when using --export option."
        exit(1)
      elsif ![nil, :full, :api].include?(@export)
      elsif ![nil, :full, :api, :examples].include?(@export)
        puts "Unknown export format: #{@export}"
        exit(1)
      elsif @output_dir != :stdout
+1 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ describe JsDuck::InlineExamples do

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