Sha256: feacac71db76753e1fd500843d005c3eb5de0395c12dd22844b356be8a277f53

Contents?: true

Size: 1.49 KB

Versions: 52

Compression:

Stored size: 1.49 KB

Contents

require 'jsduck/util/json'
require 'jsduck/util/null_object'
require 'jsduck/grouped_asset'

module JsDuck

  # Reads in examples JSON file
  class Examples < GroupedAsset
    # Creates Examples object from filename.
    def self.create(filename, opts)
      if filename
        Examples.new(filename, opts)
      else
        Util::NullObject.new(:to_array => [], :[] => nil)
      end
    end

    # Parses examples config file
    def initialize(filename, opts)
      @groups = Util::Json.read(filename)
      @opts = opts
      fix_examples_data
      build_map_by_name
    end

    # Prefix all relative URL-s in examples list with path given in --examples-base-url
    #
    # For backwards compatibility:
    #
    # - Create names for each example when not present
    # - Create title from text
    # - Create description from desc
    #
    def fix_examples_data
      each_item do |ex|
        ex["name"] = ex["url"] unless ex["name"]

        unless ex["url"] =~ /^https?:\/\//
          ex["url"] = @opts.examples_base_url + ex["url"]
        end
        unless ex["icon"] =~ /^https?:\/\//
          ex["icon"] = @opts.examples_base_url + ex["icon"]
        end

        unless ex["title"]
          ex["title"] = ex["text"]
          ex.delete("text")
        end
        unless ex["description"]
          ex["description"] = ex["desc"]
          ex.delete("desc")
        end
      end
    end

    # Extracts example icon URL from example hash
    def icon_url(example)
      example["icon"]
    end

  end

end

Version data entries

52 entries across 52 versions & 3 rubygems

Version Path
solvas-jsduck-6.0.0.30539 lib/jsduck/examples.rb
solvas-jsduck-6.0.0.9571 lib/jsduck/examples.rb
solvas-jsduck-6.0.0.6154 lib/jsduck/examples.rb
solvas-jsduck-6.0.0.4021 lib/jsduck/examples.rb
solvas-jsduck-6.0.0.2554 lib/jsduck/examples.rb
solvas-jsduck-6.0.0.1891 lib/jsduck/examples.rb
solvas-jsduck-6.0.0.beta.1888 lib/jsduck/examples.rb
jsduck-troopjs-0.0.10 lib/jsduck/examples.rb
jsduck-troopjs-0.0.9 lib/jsduck/examples.rb
jsduck-troopjs-0.0.8 lib/jsduck/examples.rb
jsduck-troopjs-0.0.7 lib/jsduck/examples.rb
jsduck-troopjs-0.0.5 lib/jsduck/examples.rb
jsduck-troopjs-0.0.4 lib/jsduck/examples.rb
jsduck-troopjs-0.0.3 lib/jsduck/examples.rb
jsduck-troopjs-0.0.1 lib/jsduck/examples.rb
jsduck-6.0.0beta lib/jsduck/examples.rb
jsduck-5.3.4 lib/jsduck/examples.rb
jsduck-5.3.3 lib/jsduck/examples.rb
jsduck-5.3.2 lib/jsduck/examples.rb
jsduck-5.3.1 lib/jsduck/examples.rb