Commit 1d1455f0 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Add --encoding option.

We can now have input files in any encoding as long as --encoding
option specifies it correctly.  When encoding differs from UTF-8,
we convert the text to UTF-8 internally and always output in UTF-8
also.

This option only works in Ruby 1.9.  Under 1.8 it has no effect.
parent 4df5d2c3
Loading
Loading
Loading
Loading
+15 −3
Original line number Diff line number Diff line
@@ -4,13 +4,25 @@ module JsDuck
  # correct encoding.
  #
  # By default in Ruby 1.9 the encoding is auto-detected, which can
  # have surprising results.  So in here we force all files to be
  # treated as UTF-8.
  # have surprising results.  So in here we read in all files in UTF-8
  # (the default) or in some other encoding specified through --encoding
  # option and convert it to UTF-8 internally.
  class IO
    @@encoding = "UTF-8"

    # Sets the external encoding to be used for reading files.
    # When it's different from UTF-8, the input will be converted to UTF-8.
    def self.encoding=(e)
      if e =~ /^UTF-8$/i
        @@encoding = e
      else
        @@encoding = e+":UTF-8"
      end
    end

    # Reads given filename into string
    def self.read(filename)
      File.open(filename, "r:UTF-8") {|f| f.read }
      File.open(filename, "r:"+@@encoding) {|f| f.read }
    end

  end
+5 −0
Original line number Diff line number Diff line
@@ -171,6 +171,10 @@ module JsDuck
          Logger.instance.set_warning(:all, false)
        end

        opts.on('--encoding=NAME', "Input encoding (defaults to UTF-8).", " ") do |encoding|
          JsDuck::IO.encoding = encoding
        end

        opts.on('-v', '--verbose', "This will fill up your console.", " ") do
          Logger.instance.verbose = true
        end
@@ -382,6 +386,7 @@ module JsDuck
              /--output/,
              /--builtin-classes/,
              /--no-warnings/,
              /--encoding/,
              /--verbose/,
              /--help/,
              /--version/,