Commit e573ea09 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Add width and height attributes to expanded @img tags.

Include the Dimensions gem to retrieve image width/height meta-data.

Additional %w an %h placeholders to --img-tpl template.
parent ed82556b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
  s.add_dependency 'json'
  s.add_dependency 'parallel'
  s.add_dependency 'therubyracer', '>= 0.10.0', '< 0.11.0'
  s.add_dependency 'dimensions'

  s.add_development_dependency 'rspec'
  s.add_development_dependency 'rake'
+11 −2
Original line number Diff line number Diff line
require 'dimensions'

module JsDuck

  # Looks up images from a directory.
@@ -14,6 +16,8 @@ module JsDuck
    # - :filename - the same as the parameter of this method
    # - :full_path - actual path in the filesystem.
    # - :relative_path - relative path to be used inside <img> tag.
    # - :width - Image width
    # - :height - Image height
    #
    # When the image is not found, returns nil.
    def get(filename)
@@ -57,10 +61,15 @@ module JsDuck
    end

    def img_record(filename)
      {
      full_path = File.join(@full_path, filename)
      width, height = Dimensions.dimensions(full_path)

      return {
        :filename => filename,
        :relative_path => File.join(@relative_path, filename),
        :full_path => File.join(@full_path, filename),
        :full_path => full_path,
        :width => width,
        :height => height,
      }
    end

+6 −2
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ module JsDuck
      attr_accessor :doc_context

      def initialize(opts={})
        @tpl = opts[:img_tpl] || '<img src="%u" alt="%a"/>'
        @tpl = opts[:img_tpl] || '<img src="%u" alt="%a" width="%w" height="%h"/>'

        @re = /\{@img\s+(\S*?)(?:\s+(.+?))?\}/m
      end
@@ -39,7 +39,7 @@ module JsDuck
        img = @images.get(url)
        if !img
          Logger.warn(:image, "Image #{url} not found.", @doc_context[:filename], @doc_context[:linenr])
          img = {:relative_path => ""}
          img = {}
        end

        @tpl.gsub(/(%\w)/) do
@@ -48,6 +48,10 @@ module JsDuck
            img[:relative_path]
          when '%a'
            Util::HTML.escape(alt_text||"")
          when '%w'
            img[:width]
          when '%h'
            img[:height]
          else
            $1
          end
+4 −2
Original line number Diff line number Diff line
@@ -109,7 +109,7 @@ module JsDuck
      @link_tpl = '<a href="#!/api/%c%-%m" rel="%c%-%m" class="docClass">%a</a>'
      # Note that we wrap image template inside <p> because {@img} often
      # appears inline within text, but that just looks ugly in HTML
      @img_tpl = '<p><img src="%u" alt="%a"></p>'
      @img_tpl = '<p><img src="%u" alt="%a" width="%w" height="%h"></p>'
      @export = nil
      @seo = false
      @eg_iframe = nil
@@ -504,8 +504,10 @@ module JsDuck
          "",
          "%u - URL from @img tag (e.g. 'some/path.png')",
          "%a - alt text for image",
          "%w - width of image",
          "%h - height of image",
          "",
          "Defaults to: '<p><img src=\"%u\" alt=\"%a\"></p>'") do |tpl|
          "Defaults to: '<p><img src=\"%u\" alt=\"%a\" width=\"%w\" height=\"%h\"></p>'") do |tpl|
          @img_tpl = tpl
        end

+1 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ describe JsDuck::DocFormatter do
  end

  before do
    @formatter = JsDuck::DocFormatter.new
    @formatter = JsDuck::DocFormatter.new(:img_tpl => '<img src="%u" alt="%a"/>')
    @formatter.class_context = "Context"
    @formatter.images = ImageDirMock.new
    @formatter.relations = JsDuck::Relations.new([