Commit 30d111c4 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Report unused images in guides.

Both the ImagesDir and ImagesDirSet now have #report_unused method.
No more is the unused images reporting part of #copy method.

Also take care to ignore the icon.png and icon-lg.png files inside
guides - these don't have to be reference by @img tag, but are
used automatically.
parent e573ea09
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -38,6 +38,9 @@ module JsDuck
        # amounts of images, the performance penalty should be minimal.
        cls[:images].each {|img| assets.images.get(img[:filename]) }
      end

      # Print warnings for unused images
      assets.images.report_unused
    end

    # Factory method to create new ClassFormatter instances.
+7 −1
Original line number Diff line number Diff line
@@ -61,8 +61,14 @@ module JsDuck
      begin
        @formatter.doc_context = {:filename => guide_file, :linenr => 0}
        @formatter.images = ImageDir.new(guide["url"], "guides/#{guide["name"]}")
        html = add_toc(guide, @formatter.format(Util::IO.read(guide_file)))

        return add_toc(guide, @formatter.format(Util::IO.read(guide_file)))
        # Report unused images (but ignore the icon files)
        @formatter.images.get("icon.png")
        @formatter.images.get("icon-lg.png")
        @formatter.images.report_unused

        return html
      rescue
        Logger.fatal_backtrace("Error while reading/formatting guide #{guide['url']}", $!)
        exit(1)
+8 −3
Original line number Diff line number Diff line
require 'dimensions'
require 'jsduck/logger'

module JsDuck

@@ -33,9 +34,9 @@ module JsDuck
      @images.values
    end

    # Returns all unused images.
    def all_unused
      scan_for_unused_images
    # Print warnings about all unused images.
    def report_unused
      scan_for_unused_images.each {|img| warn_unused(img) }
    end

    private
@@ -60,6 +61,10 @@ module JsDuck
      unused
    end

    def warn_unused(img)
      Logger.warn(:image_unused, "Image not used.", img[:full_path])
    end

    def img_record(filename)
      full_path = File.join(@full_path, filename)
      width, height = Dimensions.dimensions(full_path)
+2 −7
Original line number Diff line number Diff line
@@ -23,14 +23,13 @@ module JsDuck
      @dirs.map {|dir| dir.all_used }.flatten
    end

    def all_unused
      @dirs.map {|dir| dir.all_unused }.flatten
    def report_unused
      @dirs.each {|dir| dir.report_unused }
    end

    # Copys over images to given output dir
    def copy(output_dir)
      all_used.each {|img| copy_img(img, output_dir) }
      all_unused.each {|img| report_unused(img) }
    end

    private
@@ -43,10 +42,6 @@ module JsDuck
      FileUtils.cp(img[:full_path], dest)
    end

    def report_unused(img)
      Logger.warn(:image_unused, "Image not used.", img[:full_path])
    end

  end

end