diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2019-01-03 18:02:02 -0700 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2019-01-06 15:40:01 -0700 |
| commit | 67ec4dc95522e4e6ca4f70009fadc9cbe71b4446 (patch) | |
| tree | 7009c916dfae16efa1269c29beaa2bbbc84d21d2 | |
| parent | 860b9aa494c0a4f41b0771ad5d4093620ebed27b (diff) | |
remove workarounds for Ruby < 2.3
- remove constants for differentiating between Ruby versions
- remove fallback assignment for RUBY_ENGINE constant
- remove rubygems require
- use unicode character properties in regular expressions (e.g., \p{Word})
- remove COERCE_ENCODING constant
- always set FORCE_ENCODING constant to true unless default external encoding is not UTF-8
- remove workaround for computing line length
- use lines method instead of lines.entries to get lines for string
- always normalize lines
- check if JRuby class is defined instead of checking for JRuby engine
- remove global RUBY_ENGINE_JRUBY constant
- move RUBY_ENGINE_OPAL constant inside Asciidoctor module
- remove magic encoding comments (since default script encoding is UTF-8)
- remove workaround when printing encoding information
- remove fallback assignments for passthrough escape characters
- remove Ruby core backports (only leave Asciidoctor-specific extensions)
- use __dir__ instead of File.dirname __FILE__
- use Float#truncate to truncate column widths
- override Float#truncate on Ruby 2.3 to implement precision argument
- remove concurrent/hash backport
- simplify class_for_name helper
- don't call to_sym on constants returned from constants method
- remove unnecessary aliasing of regexp match variables; use magic variable instead (e.g., m = $~)
- remove workaround for sub_specialchars to accomodate gsub with map argument
- remove comment about %:z placeholder for strftime
- don't call .to_a on return value of Hash#keys
- get home directory using Dir.home
- remove exceptions and exclusions for Ruby < 2 in test suite
- update list of supported Ruby engines and versions in READMEs
- update ruby versions in run-tests.sh
75 files changed, 340 insertions, 630 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index bdc4b15b..37b2c2aa 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -18,6 +18,7 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[ Enhancements:: * add support for start attribute when using prettify to highlight source blocks with line numbering enabled + * drop support for Ruby < 2.3 and JRuby < 9.1 and remove workarounds (#2764) Bug Fixes:: diff --git a/README-de.adoc b/README-de.adoc index 5a34646b..6f5117ac 100644 --- a/README-de.adoc +++ b/README-de.adoc @@ -139,9 +139,8 @@ Es wird für die AsciiDoc Vorschau-Erweiterungen für Chrome, Atom, Brackets und Asciidoctor arbeitet unter Linux, MacOS und Windows und benötigt eine der folgenden Implementierungen von {uri-ruby}[Ruby]: -* Ruby (MRI/CRuby 1.8.7 - 2.6) -* JRuby (1.7 in Ruby 1.8 und 1.9 modes, 9000) -* Rubinius 2.2.x +* CRuby (aka MRI) 2.3 - 2.6 +* JRuby 9.1 - 9.2 * Opal (JavaScript) [CAUTION] diff --git a/README-fr.adoc b/README-fr.adoc index 6dc663ac..a4d2878e 100644 --- a/README-fr.adoc +++ b/README-fr.adoc @@ -136,9 +136,8 @@ Asciidoctor.js est utilisé pour faire fonctionner les extensions AsciiDoc Previ Asciidoctor fonctionne sur Linux, macOS et Windows et requiert une des implémentations suivantes de {uri-ruby}[Ruby] : -* Ruby (MRI/CRuby 1.8.7 - 2.6) -* JRuby (1.7 en mode Ruby 1.8 et 1.9, 9000) -* Rubinius 2.2.x +* CRuby (aka MRI) 2.3 - 2.6 +* JRuby 9.1 - 9.2 * Opal (JavaScript) [CAUTION] diff --git a/README-jp.adoc b/README-jp.adoc index 616cb942..ce1e612d 100644 --- a/README-jp.adoc +++ b/README-jp.adoc @@ -120,9 +120,8 @@ AsciidoctorはHTML5出力をスタイルするため, モダンで, {uri-founda AsciidoctorはLinux, OS X (Mac)とWindowsで動き, 下記の {uri-ruby}[Ruby]実装の一つを必要とします. -* MRI (Ruby 1.8.7, 1.9.3, 2.0, 2.1, 2.2 & 2.3) -* JRuby (1.7 in Ruby 1.8 and 1.9 modes, 9000) -* Rubinius 2.2.x +* CRuby (aka MRI) 2.3 - 2.6 +* JRuby 9.1 - 9.2 * Opal (JavaScript) [CAUTION] diff --git a/README-zh_CN.adoc b/README-zh_CN.adoc index e882910a..4bc0827d 100644 --- a/README-zh_CN.adoc +++ b/README-zh_CN.adoc @@ -122,9 +122,8 @@ Asciidoctor 还提供了一个基于 {uri-foundation}[Foundation] 的现代化 Asciidoctor 可以运行在 Linux,OSX (Mac) 和 Windows 系统,但需要安装下面任意一个 {uri-ruby}[Ruby] 环境去实现: -* MRI (Ruby 1.8.7, 1.9.3, 2.0, 2.1, 2.2 & 2.3) -* JRuby (1.7 in Ruby 1.8 and 1.9 modes, 9000) -* Rubinius 2.2.x +* CRuby (aka MRI) 2.3 - 2.6 +* JRuby 9.1 - 9.2 * Opal (JavaScript) 我们欢迎你来帮助在这些以及其他平台测试 Asciidoctor。 diff --git a/README.adoc b/README.adoc index 858edb09..12e9f400 100644 --- a/README.adoc +++ b/README.adoc @@ -142,9 +142,8 @@ It's used to power the AsciiDoc preview extensions for Chrome, Atom, Brackets an Asciidoctor works on Linux, macOS and Windows and requires one of the following implementations of {uri-ruby}[Ruby]: -* Ruby (MRI/CRuby 1.8.7 - 2.6) -* JRuby (1.7 in Ruby 1.8 and 1.9 modes, 9000) -* Rubinius 2.2.x +* CRuby (aka MRI) 2.3 - 2.6 +* JRuby 9.1 - 9.2 * Opal (JavaScript) [CAUTION] diff --git a/asciidoctor.gemspec b/asciidoctor.gemspec index d9195f85..37c77ac8 100644 --- a/asciidoctor.gemspec +++ b/asciidoctor.gemspec @@ -1,4 +1,3 @@ -# encoding: UTF-8 begin require File.expand_path '../lib/asciidoctor/version', __FILE__ rescue LoadError diff --git a/bin/asciidoctor b/bin/asciidoctor index ee394296..fa0b32e2 100755 --- a/bin/asciidoctor +++ b/bin/asciidoctor @@ -1,7 +1,5 @@ #!/usr/bin/env ruby -require 'rubygems' unless defined? Gem - if File.exist?(asciidoctor = (File.expand_path '../../lib/asciidoctor', __FILE__)) require asciidoctor else diff --git a/bin/asciidoctor-safe b/bin/asciidoctor-safe index 9a70f233..42053271 100755 --- a/bin/asciidoctor-safe +++ b/bin/asciidoctor-safe @@ -1,7 +1,5 @@ #!/usr/bin/env ruby -require 'rubygems' unless defined? Gem - if File.exist?(asciidoctor = (File.expand_path '../../lib/asciidoctor', __FILE__)) require asciidoctor else diff --git a/features/step_definitions.rb b/features/step_definitions.rb index a843aae1..f1ec3823 100644 --- a/features/step_definitions.rb +++ b/features/step_definitions.rb @@ -1,14 +1,10 @@ -# encoding: UTF-8 ASCIIDOCTOR_PROJECT_DIR = File.dirname File.dirname(__FILE__) +ASCIIDOCTOR_LIB_DIR = ENV['ASCIIDOCTOR_LIB_DIR'] || File.join(ASCIIDOCTOR_PROJECT_DIR, 'lib') Dir.chdir ASCIIDOCTOR_PROJECT_DIR -if RUBY_VERSION < '1.9' - require 'rubygems' -end - require 'simplecov' if ENV['COVERAGE'] == 'true' -require File.join(ASCIIDOCTOR_PROJECT_DIR, 'lib', 'asciidoctor') +require File.join(ASCIIDOCTOR_LIB_DIR, 'asciidoctor') require 'rspec/expectations' require 'tilt' diff --git a/lib/asciidoctor.rb b/lib/asciidoctor.rb index 743c44e6..dee90838 100644 --- a/lib/asciidoctor.rb +++ b/lib/asciidoctor.rb @@ -1,10 +1,3 @@ -# encoding: UTF-8 -RUBY_ENGINE = 'unknown' unless defined? RUBY_ENGINE -RUBY_ENGINE_OPAL = (RUBY_ENGINE == 'opal') -RUBY_ENGINE_JRUBY = (RUBY_ENGINE == 'jruby') -RUBY_MIN_VERSION_1_9 = (RUBY_VERSION >= '1.9') -RUBY_MIN_VERSION_2 = (RUBY_VERSION >= '2') - require 'set' # NOTE RUBY_ENGINE == 'opal' conditional blocks like this are filtered by the Opal preprocessor @@ -20,7 +13,7 @@ else end # ideally we should use require_relative instead of modifying the LOAD_PATH -$:.unshift File.dirname __FILE__ +$:.unshift __dir__ require 'asciidoctor/logging' @@ -54,12 +47,10 @@ require 'asciidoctor/logging' # Asciidoctor.convert_file 'sample.adoc', :template_dir => 'path/to/templates' # module Asciidoctor - - # alias the RUBY_ENGINE constant inside the Asciidoctor namespace - RUBY_ENGINE = ::RUBY_ENGINE + # alias the RUBY_ENGINE constant inside the Asciidoctor namespace and define a precomputed alias for runtime + RUBY_ENGINE_OPAL = (RUBY_ENGINE = ::RUBY_ENGINE) == 'opal' module SafeMode - # A safe mode level that disables any of the security features enforced # by Asciidoctor (Ruby is still subject to its own restrictions). UNSAFE = 0; @@ -185,7 +176,7 @@ module Asciidoctor end # The absolute root path of the Asciidoctor RubyGem - ROOT_PATH = ::File.dirname ::File.dirname ::File.expand_path __FILE__ + ROOT_PATH = ::File.dirname ::File.absolute_path __dir__ # The absolute lib path of the Asciidoctor RubyGem #LIB_PATH = ::File.join ROOT_PATH, 'lib' @@ -194,30 +185,19 @@ module Asciidoctor DATA_PATH = ::File.join ROOT_PATH, 'data' # The user's home directory, as best we can determine it - # NOTE not using infix rescue for performance reasons, see: https://github.com/jruby/jruby/issues/1816 - begin - USER_HOME = ::Dir.home - rescue - USER_HOME = ::ENV['HOME'] || ::Dir.pwd - end + USER_HOME = ::Dir.home - # Flag to indicate whether encoding can be coerced to UTF-8 + # Flag to indicate whether encoding should be forced to UTF-8 # _All_ input data must be force encoded to UTF-8 if Encoding.default_external is *not* UTF-8 # Addresses failures performing string operations that are reported as "invalid byte sequence in US-ASCII" - # Ruby 1.8 doesn't seem to experience this problem (perhaps because it isn't validating the encodings) - COERCE_ENCODING = !::RUBY_ENGINE_OPAL && ::RUBY_MIN_VERSION_1_9 - - # Flag to indicate whether encoding of external strings needs to be forced to UTF-8 - FORCE_ENCODING = COERCE_ENCODING && ::Encoding.default_external != ::Encoding::UTF_8 + # Opal doesn't require this since all strings are already encoding internally as UTF-16 + FORCE_ENCODING = ::Encoding.default_external != ::Encoding::UTF_8 # Byte arrays for UTF-* Byte Order Marks BOM_BYTES_UTF_8 = [0xef, 0xbb, 0xbf] BOM_BYTES_UTF_16LE = [0xff, 0xfe] BOM_BYTES_UTF_16BE = [0xfe, 0xff] - # Flag to indicate that line length should be calculated using a unicode mode hint - FORCE_UNICODE_LINE_LENGTH = !::RUBY_MIN_VERSION_1_9 - # The endline character used for output; stored in constant table as an optimization LF = EOL = "\n" @@ -393,29 +373,10 @@ module Asciidoctor # CC_ANY is any character except newlines CC_ANY = '.' CC_EOL = '$' - # character classes for the Regexp engine in Ruby >= 2 (Ruby 1.9 supports \p{} but has problems w/ encoding) - if ::RUBY_MIN_VERSION_2 - CC_ALPHA = CG_ALPHA = '\p{Alpha}' - CC_ALNUM = CG_ALNUM = '\p{Alnum}' - CG_BLANK = '\p{Blank}' - CC_WORD = CG_WORD = '\p{Word}' - # character classes for the Regexp engine in Ruby < 2 - else - CC_ALPHA = '[:alpha:]' - CG_ALPHA = '[[:alpha:]]' - CC_ALNUM = '[:alnum:]' - CG_ALNUM = '[[:alnum:]]' - if ::RUBY_MIN_VERSION_1_9 - CG_BLANK = '[[:blank:]]' - CC_WORD = '[:word:]' - CG_WORD = '[[:word:]]' - else - # NOTE Ruby 1.8 cannot match word characters beyond the ASCII range; if you need this feature, upgrade! - CG_BLANK = '[ \t]' - CC_WORD = '[:alnum:]_' - CG_WORD = '[[:alnum:]_]' - end - end + CC_ALPHA = CG_ALPHA = '\p{Alpha}' + CC_ALNUM = CG_ALNUM = '\p{Alnum}' + CG_BLANK = '\p{Blank}' + CC_WORD = CG_WORD = '\p{Word}' end ## Document header @@ -1093,7 +1054,7 @@ module Asciidoctor # one two three four # five six # - # TODO change to /(?<!\\)[ \t\n]+/ after dropping support for Ruby 1.8.7 + # TODO change to /(?<!\\)[ \t\n]+/ once lookbehind assertions are implemented in all modern browsers SpaceDelimiterRx = /([^\\])[ \t\n]+/ # Matches a + or - modifier in a subs list @@ -1109,12 +1070,6 @@ module Asciidoctor # TrailingDigitsRx = /\d+$/ - # Matches any character with multibyte support explicitly enabled (length of multibyte char = 1) - # - unless RUBY_ENGINE == 'opal' - UnicodeCharScanRx = /./u if FORCE_UNICODE_LINE_LENGTH - end - # Detects strings that resemble URIs. # # Examples @@ -1239,9 +1194,7 @@ module Asciidoctor quote_subs = nil compat_quote_subs = nil - # NOTE in Ruby 1.8.7, [^\\] does not match start of line, - # so we need to match it explicitly - # order is significant + # NOTE order of replacements is significant REPLACEMENTS = [ # (C) [/\\?\(C\)/, '©', :none], @@ -1249,8 +1202,8 @@ module Asciidoctor [/\\?\(R\)/, '®', :none], # (TM) [/\\?\(TM\)/, '™', :none], - # foo -- bar - # FIXME this drops the endline if it appears at end of line + # foo -- bar (where either space character can be a newline) + # NOTE this necessarily drops the newline if it appears at end of line [/(^|\n| |\\)--( |\n|$)/, ' — ', :none], # foo--bar [/(#{CG_WORD})\\?--(?=#{CG_WORD})/, '—​', :leading], @@ -1300,7 +1253,7 @@ module Asciidoctor if !(attrs = options[:attributes]) attrs = {} - elsif ::Hash === attrs || (::RUBY_ENGINE_JRUBY && ::Java::JavaUtil::Map === attrs) + elsif ::Hash === attrs || ((defined? ::Java::JavaUtil::Map) && ::Java::JavaUtil::Map === attrs) attrs = attrs.dup elsif ::Array === attrs attrs, attrs_arr = {}, attrs @@ -1341,7 +1294,6 @@ module Asciidoctor attrs['docyear'] ||= input_mtime.year.to_s end # %Z is OS dependent and may contain characters that aren't UTF-8 encoded (see asciidoctor#2770 and asciidoctor.js#23) - # Ruby 1.8 doesn't support %:z doctime = (attrs['doctime'] ||= input_mtime.strftime %(%T #{input_mtime.utc_offset == 0 ? 'UTC' : '%z'})) attrs['docdatetime'] = %(#{docdate} #{doctime}) elsif input.respond_to? :readlines @@ -1353,7 +1305,7 @@ module Asciidoctor end lines = input.readlines elsif ::String === input - lines = ::RUBY_MIN_VERSION_2 ? input.lines : input.each_line.to_a + lines = input.lines elsif ::Array === input lines = input.drop 0 else diff --git a/lib/asciidoctor/abstract_block.rb b/lib/asciidoctor/abstract_block.rb index 2c47814c..58d8178b 100644 --- a/lib/asciidoctor/abstract_block.rb +++ b/lib/asciidoctor/abstract_block.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor class AbstractBlock < AbstractNode # Public: Get the Array of Asciidoctor::AbstractBlock sub-blocks for this block diff --git a/lib/asciidoctor/abstract_node.rb b/lib/asciidoctor/abstract_node.rb index 66524ed3..b3d6f2cb 100644 --- a/lib/asciidoctor/abstract_node.rb +++ b/lib/asciidoctor/abstract_node.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # Public: An abstract base class that provides state and methods for managing a # node of AsciiDoc content. The state and methods on this class are common to @@ -404,7 +403,7 @@ class AbstractNode # caching requires the open-uri-cached gem to be installed # processing will be automatically aborted if these libraries can't be opened Helpers.require_library 'open-uri/cached', 'open-uri-cached' - elsif !::RUBY_ENGINE_OPAL + elsif !RUBY_ENGINE_OPAL # autoload open-uri ::OpenURI end @@ -510,7 +509,6 @@ class AbstractNode opts = { :warn_on_failure => (opts != false) } unless ::Hash === opts if ::File.readable? path if opts[:normalize] - # NOTE Opal does not yet support File#readlines (Helpers.normalize_lines_array ::File.open(path, 'rb') {|f| f.each_line.to_a }).join LF else # QUESTION should we chomp or rstrip content? @@ -546,7 +544,6 @@ class AbstractNode Helpers.require_library 'open-uri/cached', 'open-uri-cached' if doc.attr? 'cache-uri' begin if opts[:normalize] - # NOTE Opal does not yet support File#readlines (Helpers.normalize_lines_array ::OpenURI.open_uri(target) {|f| f.each_line.to_a }).join LF else ::OpenURI.open_uri(target) {|f| f.read } diff --git a/lib/asciidoctor/attribute_list.rb b/lib/asciidoctor/attribute_list.rb index 06c39010..66c85016 100644 --- a/lib/asciidoctor/attribute_list.rb +++ b/lib/asciidoctor/attribute_list.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # Public: Handles parsing AsciiDoc attribute lists into a Hash of key/value # pairs. By default, attributes must each be separated by a comma and quotes diff --git a/lib/asciidoctor/block.rb b/lib/asciidoctor/block.rb index b0cc5462..b3057cbb 100644 --- a/lib/asciidoctor/block.rb +++ b/lib/asciidoctor/block.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # Public: Methods for managing blocks of Asciidoc content in a section. # diff --git a/lib/asciidoctor/callouts.rb b/lib/asciidoctor/callouts.rb index b3eacec4..30f1044e 100644 --- a/lib/asciidoctor/callouts.rb +++ b/lib/asciidoctor/callouts.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # Public: Maintains a catalog of callouts and their associations. class Callouts diff --git a/lib/asciidoctor/cli/invoker.rb b/lib/asciidoctor/cli/invoker.rb index 5dd3c240..4bf998a3 100644 --- a/lib/asciidoctor/cli/invoker.rb +++ b/lib/asciidoctor/cli/invoker.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor module Cli # Public Invocation class for starting Asciidoctor via CLI diff --git a/lib/asciidoctor/cli/options.rb b/lib/asciidoctor/cli/options.rb index 4a3c09d9..a254ff35 100644 --- a/lib/asciidoctor/cli/options.rb +++ b/lib/asciidoctor/cli/options.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor module Cli FS = '/' @@ -286,14 +285,10 @@ Example: asciidoctor -b html5 source.asciidoc def print_version os = $stdout os.puts %(Asciidoctor #{::Asciidoctor::VERSION} [https://asciidoctor.org]) - if ::RUBY_MIN_VERSION_1_9 - encoding_info = { 'lc' => 'locale', 'fs' => 'filesystem', 'in' => 'internal', 'ex' => 'external' }.map do |k, v| - %(#{k}:#{v == 'internal' ? (::File.open(__FILE__) {|f| f.getc }).encoding : (::Encoding.find v)}) - end - os.puts %(Runtime Environment (#{::RUBY_DESCRIPTION}) (#{encoding_info.join ' '})) - else - os.puts %(Runtime Environment (#{::RUBY_DESCRIPTION})) + encoding_info = { 'lc' => 'locale', 'fs' => 'filesystem', 'in' => 'internal', 'ex' => 'external' }.map do |k, v| + %(#{k}:#{v == 'internal' ? (::File.open(__FILE__) {|f| f.getc }).encoding : (::Encoding.find v)}) end + os.puts %(Runtime Environment (#{::RUBY_DESCRIPTION}) (#{encoding_info.join ' '})) 0 end end diff --git a/lib/asciidoctor/converter.rb b/lib/asciidoctor/converter.rb index d5528ab0..ff58dc8d 100644 --- a/lib/asciidoctor/converter.rb +++ b/lib/asciidoctor/converter.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # A base module for defining converters that can be used to convert {AbstractNode} # objects in a parsed AsciiDoc document to a backend format such as HTML or diff --git a/lib/asciidoctor/converter/base.rb b/lib/asciidoctor/converter/base.rb index ffd9dfa0..83036e30 100644 --- a/lib/asciidoctor/converter/base.rb +++ b/lib/asciidoctor/converter/base.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor module Converter; end # required for Opal diff --git a/lib/asciidoctor/converter/composite.rb b/lib/asciidoctor/converter/composite.rb index 94d2973f..d1d6ce65 100644 --- a/lib/asciidoctor/converter/composite.rb +++ b/lib/asciidoctor/converter/composite.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # A {Converter} implementation that delegates to the chain of {Converter} # objects passed to the constructor. Selects the first {Converter} that diff --git a/lib/asciidoctor/converter/docbook45.rb b/lib/asciidoctor/converter/docbook45.rb index 6d6f0f80..de44ff44 100644 --- a/lib/asciidoctor/converter/docbook45.rb +++ b/lib/asciidoctor/converter/docbook45.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 require 'asciidoctor/converter/docbook5' module Asciidoctor diff --git a/lib/asciidoctor/converter/docbook5.rb b/lib/asciidoctor/converter/docbook5.rb index 83fad601..7e2a9327 100644 --- a/lib/asciidoctor/converter/docbook5.rb +++ b/lib/asciidoctor/converter/docbook5.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # A built-in {Converter} implementation that generates DocBook 5 output # similar to the docbook45 backend from AsciiDoc Python, but migrated to the diff --git a/lib/asciidoctor/converter/factory.rb b/lib/asciidoctor/converter/factory.rb index c557ac0f..d182892d 100644 --- a/lib/asciidoctor/converter/factory.rb +++ b/lib/asciidoctor/converter/factory.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor module Converter # A factory for instantiating converters that are used to convert a @@ -51,9 +50,7 @@ module Asciidoctor if initialize_singleton # FIXME this assignment itself may not be thread safe; may need to use a helper here @__default__ ||= begin - unless defined? ::Concurrent::Hash - require ::RUBY_MIN_VERSION_1_9 ? 'concurrent/hash' : 'asciidoctor/core_ext/1.8.7/concurrent/hash' - end + require 'concurrent/hash' unless defined? ::Concurrent::Hash.new new ::Concurrent::Hash.new rescue ::LoadError include Logging unless include? Logging diff --git a/lib/asciidoctor/converter/html5.rb b/lib/asciidoctor/converter/html5.rb index 2857b67c..8f7a1ae3 100644 --- a/lib/asciidoctor/converter/html5.rb +++ b/lib/asciidoctor/converter/html5.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # A built-in {Converter} implementation that generates HTML 5 output # consistent with the html5 backend from AsciiDoc Python. diff --git a/lib/asciidoctor/converter/template.rb b/lib/asciidoctor/converter/template.rb index 1619566e..dfed5c35 100644 --- a/lib/asciidoctor/converter/template.rb +++ b/lib/asciidoctor/converter/template.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # A {Converter} implementation that uses templates composed in template # languages supported by {https://github.com/rtomayko/tilt Tilt} to convert @@ -34,9 +33,7 @@ module Asciidoctor } begin - unless defined? ::Concurrent::Hash - require ::RUBY_MIN_VERSION_1_9 ? 'concurrent/hash' : 'asciidoctor/core_ext/1.8.7/concurrent/hash' - end + require 'concurrent/hash' unless defined? ::Concurrent::Hash @caches = { :scans => ::Concurrent::Hash.new, :templates => ::Concurrent::Hash.new } rescue ::LoadError @caches = { :scans => {}, :templates => {} } diff --git a/lib/asciidoctor/core_ext.rb b/lib/asciidoctor/core_ext.rb index 9edb7b0a..ccf77f29 100644 --- a/lib/asciidoctor/core_ext.rb +++ b/lib/asciidoctor/core_ext.rb @@ -1,18 +1,4 @@ +require 'asciidoctor/core_ext/float/truncate' require 'asciidoctor/core_ext/nil_or_empty' require 'asciidoctor/core_ext/regexp/is_match' -if RUBY_MIN_VERSION_1_9 - require 'asciidoctor/core_ext/string/limit_bytesize' - if RUBY_ENGINE == 'opal' - require 'asciidoctor/core_ext/1.8.7/io/binread' - require 'asciidoctor/core_ext/1.8.7/io/write' - end -elsif RUBY_ENGINE != 'opal' - require 'asciidoctor/core_ext/1.8.7/base64/strict_encode64' - require 'asciidoctor/core_ext/1.8.7/hash/key' - require 'asciidoctor/core_ext/1.8.7/io/binread' - require 'asciidoctor/core_ext/1.8.7/io/write' - require 'asciidoctor/core_ext/1.8.7/string/chr' - require 'asciidoctor/core_ext/1.8.7/string/limit_bytesize' - require 'asciidoctor/core_ext/1.8.7/symbol/empty' - require 'asciidoctor/core_ext/1.8.7/symbol/length' -end +require 'asciidoctor/core_ext/string/limit_bytesize' diff --git a/lib/asciidoctor/core_ext/1.8.7/base64/strict_encode64.rb b/lib/asciidoctor/core_ext/1.8.7/base64/strict_encode64.rb deleted file mode 100644 index 4ad928e1..00000000 --- a/lib/asciidoctor/core_ext/1.8.7/base64/strict_encode64.rb +++ /dev/null @@ -1,6 +0,0 @@ -# Educate Ruby 1.8.7 about the Base64#strict_encode64 method. -module Base64 - def strict_encode64 bin - (self.encode64 bin).delete %(\n) - end -end diff --git a/lib/asciidoctor/core_ext/1.8.7/concurrent/hash.rb b/lib/asciidoctor/core_ext/1.8.7/concurrent/hash.rb deleted file mode 100644 index 7c9b11a4..00000000 --- a/lib/asciidoctor/core_ext/1.8.7/concurrent/hash.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'thread_safe' - -module Concurrent - Hash = ::ThreadSafe::Cache -end diff --git a/lib/asciidoctor/core_ext/1.8.7/hash/key.rb b/lib/asciidoctor/core_ext/1.8.7/hash/key.rb deleted file mode 100644 index ffe32d02..00000000 --- a/lib/asciidoctor/core_ext/1.8.7/hash/key.rb +++ /dev/null @@ -1,4 +0,0 @@ -# Educate Ruby 1.8.7 about the Hash#key method. -class Hash - alias key index -end diff --git a/lib/asciidoctor/core_ext/1.8.7/io/binread.rb b/lib/asciidoctor/core_ext/1.8.7/io/binread.rb deleted file mode 100644 index 40dffa6c..00000000 --- a/lib/asciidoctor/core_ext/1.8.7/io/binread.rb +++ /dev/null @@ -1,6 +0,0 @@ -def IO.binread name, length = nil, offset = 0 - File.open name, 'rb' do |f| - f.seek offset unless offset == 0 - length ? (f.read length) : f.read - end -end unless IO.respond_to? :binread diff --git a/lib/asciidoctor/core_ext/1.8.7/io/write.rb b/lib/asciidoctor/core_ext/1.8.7/io/write.rb deleted file mode 100644 index 864fd9c7..00000000 --- a/lib/asciidoctor/core_ext/1.8.7/io/write.rb +++ /dev/null @@ -1,5 +0,0 @@ -def IO.write name, string, offset = 0, opts = nil - File.open name, 'w' do |f| - f.write string - end -end unless IO.respond_to? :write diff --git a/lib/asciidoctor/core_ext/1.8.7/string/chr.rb b/lib/asciidoctor/core_ext/1.8.7/string/chr.rb deleted file mode 100644 index 7c349163..00000000 --- a/lib/asciidoctor/core_ext/1.8.7/string/chr.rb +++ /dev/null @@ -1,6 +0,0 @@ -# Educate Ruby 1.8.7 about the String#chr method. -class String - def chr - slice 0, 1 - end unless method_defined? :chr -end diff --git a/lib/asciidoctor/core_ext/1.8.7/string/limit_bytesize.rb b/lib/asciidoctor/core_ext/1.8.7/string/limit_bytesize.rb deleted file mode 100644 index 691922ce..00000000 --- a/lib/asciidoctor/core_ext/1.8.7/string/limit_bytesize.rb +++ /dev/null @@ -1,29 +0,0 @@ -if RUBY_ENGINE_JRUBY - class String - # Safely truncate the string to the specified number of bytes. - # If a multibyte char gets split, the dangling fragment is removed. - def limit_bytesize size - return self unless size < bytesize - result = (unpack %(a#{size}))[0] - begin - result.unpack 'U*' - rescue ::ArgumentError - result.chop! - retry - end - result - end unless method_defined? :limit_bytesize - end -else - class String - ValidTrailingCharRx = /.$/u - # Safely truncate the string to the specified number of bytes. - # If a multibyte char gets split, the dangling fragment is removed. - def limit_bytesize size - return self unless size < bytesize - result = (unpack %(a#{size}))[0] - result.chop! until result.empty? || (ValidTrailingCharRx.match? result) - result - end unless method_defined? :limit_bytesize - end -end diff --git a/lib/asciidoctor/core_ext/1.8.7/symbol/empty.rb b/lib/asciidoctor/core_ext/1.8.7/symbol/empty.rb deleted file mode 100644 index 9a37445b..00000000 --- a/lib/asciidoctor/core_ext/1.8.7/symbol/empty.rb +++ /dev/null @@ -1,6 +0,0 @@ -# Educate Ruby 1.8.7 about the Symbol#empty? method. -class Symbol - def empty? - to_s.empty? - end unless method_defined? :empty? -end diff --git a/lib/asciidoctor/core_ext/1.8.7/symbol/length.rb b/lib/asciidoctor/core_ext/1.8.7/symbol/length.rb deleted file mode 100644 index 372a624e..00000000 --- a/lib/asciidoctor/core_ext/1.8.7/symbol/length.rb +++ /dev/null @@ -1,6 +0,0 @@ -# Educate Ruby 1.8.7 about the Symbol#empty? and Symbol#length methods. -class Symbol - def length - to_s.length - end unless method_defined? :length -end diff --git a/lib/asciidoctor/core_ext/float/truncate.rb b/lib/asciidoctor/core_ext/float/truncate.rb new file mode 100644 index 00000000..256d22d3 --- /dev/null +++ b/lib/asciidoctor/core_ext/float/truncate.rb @@ -0,0 +1,19 @@ +if (Float.instance_method :truncate).arity == 0 + Float.prepend(Module.new do + def truncate *args + if args.length == 1 + if (precision = Integer args.shift) == 0 + super + elsif precision > 0 + precision_factor = 10.0 ** precision + (self * precision_factor).to_i / precision_factor + else + precision_factor = 10 ** precision.abs + (self / precision_factor).to_i * precision_factor + end + else + super + end + end + end) +end diff --git a/lib/asciidoctor/document.rb b/lib/asciidoctor/document.rb index 1ae8d7d1..5c548663 100644 --- a/lib/asciidoctor/document.rb +++ b/lib/asciidoctor/document.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # Public: The Document class represents a parsed AsciiDoc document. # @@ -317,9 +316,8 @@ class Document < AbstractBlock # be permissive in case API user wants to define new levels @safe = safe_mode else - # NOTE: not using infix rescue for performance reasons, see https://github.com/jruby/jruby/issues/1816 begin - @safe = SafeMode.value_for_name safe_mode.to_s + @safe = SafeMode.value_for_name safe_mode rescue @safe = SafeMode::SECURE end @@ -504,7 +502,6 @@ class Document < AbstractBlock localyear = (attrs['localyear'] ||= now.year.to_s) end # %Z is OS dependent and may contain characters that aren't UTF-8 encoded (see asciidoctor#2770 and asciidoctor.js#23) - # Ruby 1.8 doesn't support %:z localtime = (attrs['localtime'] ||= now.strftime %(%T #{now.utc_offset == 0 ? 'UTC' : '%z'})) attrs['localdatetime'] ||= %(#{localdate} #{localtime}) @@ -522,7 +519,7 @@ class Document < AbstractBlock if initialize_extensions if (ext_registry = options[:extension_registry]) # QUESTION should we warn if the value type of this option is not a registry - if Extensions::Registry === ext_registry || (::RUBY_ENGINE_JRUBY && + if Extensions::Registry === ext_registry || ((defined? ::AsciidoctorJ::Extensions::ExtensionRegistry) && ::AsciidoctorJ::Extensions::ExtensionRegistry === ext_registry) @extensions = ext_registry.activate self end @@ -1152,7 +1149,7 @@ class Document < AbstractBlock converter_opts[:safe] = @safe end if (converter = @options[:converter]) - converter_factory = Converter::Factory.new ::Hash[backend, converter] + converter_factory = Converter::Factory.new backend => converter else converter_factory = Converter::Factory.default false end @@ -1223,7 +1220,7 @@ class Document < AbstractBlock # ensure there's a trailing endline target.write LF end - elsif COERCE_ENCODING + elsif FORCE_ENCODING ::IO.write target, output, :encoding => ::Encoding::UTF_8 else ::IO.write target, output diff --git a/lib/asciidoctor/extensions.rb b/lib/asciidoctor/extensions.rb index 4c0bed86..70347d9c 100644 --- a/lib/asciidoctor/extensions.rb +++ b/lib/asciidoctor/extensions.rb @@ -1,7 +1,6 @@ # NOTE .to_s hides require from Opal require 'asciidoctor'.to_s unless defined? Asciidoctor.load -# encoding: UTF-8 module Asciidoctor # Extensions provide a way to participate in the parsing and converting # phases of the AsciiDoc processor or extend the AsciiDoc syntax. @@ -66,14 +65,12 @@ module Extensions # # Returns nothing def use_dsl - if self.name.nil_or_empty? - # NOTE contants(false) doesn't exist in Ruby 1.8.7 - #include const_get :DSL if constants(false).include? :DSL - include const_get :DSL if constants.include? :DSL - else - # NOTE contants(false) doesn't exist in Ruby 1.8.7 - #extend const_get :DSL if constants(false).include? :DSL - extend const_get :DSL if constants.include? :DSL + if constants.include? :DSL + if self.name.nil_or_empty? + include const_get :DSL + else + extend const_get :DSL + end end end alias extend_dsl use_dsl @@ -1331,7 +1328,6 @@ module Extensions #class << processor # include_dsl #end - # NOTE kind_class.contants(false) doesn't exist in Ruby 1.8.7 processor.extend kind_class.const_get :DSL if kind_class.constants.include? :DSL processor.instance_exec(&block) processor.freeze @@ -1377,7 +1373,6 @@ module Extensions #class << processor # include_dsl #end - # NOTE kind_class.contants(false) doesn't exist in Ruby 1.8.7 processor.extend kind_class.const_get :DSL if kind_class.constants.include? :DSL if block.arity == 1 yield processor @@ -1548,35 +1543,12 @@ module Extensions # Public: Resolves the Class object for the qualified name. # # Returns Class - if ::RUBY_MIN_VERSION_2 - def class_for_name qualified_name - resolved = ::Object.const_get qualified_name, false - raise unless ::Class === resolved - resolved - rescue - raise ::NameError, %(Could not resolve class for name: #{qualified_name}) - end - elsif ::RUBY_MIN_VERSION_1_9 - def class_for_name qualified_name - resolved = (qualified_name.split '::').reduce ::Object do |current, name| - name.empty? ? current : (current.const_get name, false) - end - raise unless ::Class === resolved - resolved - rescue - raise ::NameError, %(Could not resolve class for name: #{qualified_name}) - end - else - def class_for_name qualified_name - resolved = (qualified_name.split '::').reduce ::Object do |current, name| - # NOTE on Ruby 1.8, const_defined? only checks for constant in current scope - name.empty? ? current : ((current.const_defined? name) ? (current.const_get name) : raise) - end - raise unless ::Class === resolved - resolved - rescue - raise ::NameError, %(Could not resolve class for name: #{qualified_name}) - end + def class_for_name qualified_name + resolved = ::Object.const_get qualified_name, false + raise unless ::Class === resolved + resolved + rescue + raise ::NameError, %(Could not resolve class for name: #{qualified_name}) end end end diff --git a/lib/asciidoctor/helpers.rb b/lib/asciidoctor/helpers.rb index b62645df..b4ac9006 100644 --- a/lib/asciidoctor/helpers.rb +++ b/lib/asciidoctor/helpers.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor module Helpers # Internal: Require the specified library using Kernel#require. @@ -65,27 +64,18 @@ module Helpers # returns a String Array of normalized lines def self.normalize_lines_array data return data if data.empty? - - leading_bytes = (first_line = data[0]).unpack 'C3' - if COERCE_ENCODING - utf8 = ::Encoding::UTF_8 - if (leading_2_bytes = leading_bytes.slice 0, 2) == BOM_BYTES_UTF_16LE - # HACK Ruby messes up trailing whitespace on UTF-16LE, so reencode whole document first - data = data.join - return (((data.force_encoding ::Encoding::UTF_16LE).slice 1, data.length).encode utf8).each_line.map {|line| line.rstrip } - elsif leading_2_bytes == BOM_BYTES_UTF_16BE - data[0] = (first_line.force_encoding ::Encoding::UTF_16BE).slice 1, first_line.length - return data.map {|line| ((line.force_encoding ::Encoding::UTF_16BE).encode utf8).rstrip } - elsif leading_bytes == BOM_BYTES_UTF_8 - data[0] = (first_line.force_encoding utf8).slice 1, first_line.length - end - - data.map {|line| line.encoding == utf8 ? line.rstrip : (line.force_encoding utf8).rstrip } - else - # Ruby 1.8 has no built-in re-encoding, so no point in removing the UTF-16 BOMs - data[0] = first_line.slice 3, first_line.length if leading_bytes == BOM_BYTES_UTF_8 - data.map {|line| line.rstrip } + utf8 = ::Encoding::UTF_8 + if (leading_2_bytes = (leading_bytes = (first_line = data[0]).unpack 'C3').slice 0, 2) == BOM_BYTES_UTF_16LE + # HACK Ruby messes up trailing whitespace on UTF-16LE, so reencode whole document first + data = data.join + return (((data.force_encoding ::Encoding::UTF_16LE).slice 1, data.length).encode utf8).lines.map {|line| line.rstrip } + elsif leading_2_bytes == BOM_BYTES_UTF_16BE + data[0] = (first_line.force_encoding ::Encoding::UTF_16BE).slice 1, first_line.length + return data.map {|line| ((line.force_encoding ::Encoding::UTF_16BE).encode utf8).rstrip } + elsif leading_bytes == BOM_BYTES_UTF_8 + data[0] = (first_line.force_encoding utf8).slice 1, first_line.length end + data.map {|line| line.encoding == utf8 ? line.rstrip : (line.force_encoding utf8).rstrip } end # Public: Normalize the String and split into lines to prepare them for parsing @@ -101,24 +91,17 @@ module Helpers # returns a String Array of normalized lines def self.normalize_lines_from_string data return [] if data.nil_or_empty? - - leading_bytes = data.unpack 'C3' - if COERCE_ENCODING - utf8 = ::Encoding::UTF_8 - if (leading_2_bytes = leading_bytes.slice 0, 2) == BOM_BYTES_UTF_16LE - data = ((data.force_encoding ::Encoding::UTF_16LE).slice 1, data.length).encode utf8 - elsif leading_2_bytes == BOM_BYTES_UTF_16BE - data = ((data.force_encoding ::Encoding::UTF_16BE).slice 1, data.length).encode utf8 - elsif leading_bytes == BOM_BYTES_UTF_8 - data = data.encoding == utf8 ? (data.slice 1, data.length) : ((data.force_encoding utf8).slice 1, data.length) - else - data = data.force_encoding utf8 unless data.encoding == utf8 - end + utf8 = ::Encoding::UTF_8 + if (leading_2_bytes = (leading_bytes = data.unpack 'C3').slice 0, 2) == BOM_BYTES_UTF_16LE + data = ((data.force_encoding ::Encoding::UTF_16LE).slice 1, data.length).encode utf8 + elsif leading_2_bytes == BOM_BYTES_UTF_16BE + data = ((data.force_encoding ::Encoding::UTF_16BE).slice 1, data.length).encode utf8 + elsif leading_bytes == BOM_BYTES_UTF_8 + data = data.encoding == utf8 ? (data.slice 1, data.length) : ((data.force_encoding utf8).slice 1, data.length) else - # Ruby 1.8 has no built-in re-encoding, so no point in removing the UTF-16 BOMs - data = data.slice 3, data.length if leading_bytes == BOM_BYTES_UTF_8 + data = data.force_encoding utf8 unless data.encoding == utf8 end - data.each_line.map {|line| line.rstrip } + data.lines.map {|line| line.rstrip } end # Public: Efficiently checks whether the specified String resembles a URI diff --git a/lib/asciidoctor/inline.rb b/lib/asciidoctor/inline.rb index 13082570..c0f0e6fe 100644 --- a/lib/asciidoctor/inline.rb +++ b/lib/asciidoctor/inline.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # Public: Methods for managing inline elements in AsciiDoc block class Inline < AbstractNode diff --git a/lib/asciidoctor/list.rb b/lib/asciidoctor/list.rb index 1e98e883..bac283c6 100644 --- a/lib/asciidoctor/list.rb +++ b/lib/asciidoctor/list.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # Public: Methods for managing AsciiDoc lists (ordered, unordered and description lists) class List < AbstractBlock diff --git a/lib/asciidoctor/logging.rb b/lib/asciidoctor/logging.rb index 3d9efadc..ded8cf9e 100644 --- a/lib/asciidoctor/logging.rb +++ b/lib/asciidoctor/logging.rb @@ -34,8 +34,7 @@ class Logger < ::Logger end class MemoryLogger < ::Logger - # NOTE Ruby 1.8.7 returns constants as strings instead of symbols - SEVERITY_LABELS = ::Hash[Severity.constants.map {|c| [(Severity.const_get c), c.to_sym] }] + SEVERITY_LABELS = ::Hash[Severity.constants.map {|c| [(Severity.const_get c), c] }] attr_reader :messages @@ -95,7 +94,7 @@ module LoggerManager def memoize_logger class << self alias_method :logger, :logger - if RUBY_ENGINE == 'opal' + if RUBY_ENGINE_OPAL define_method :logger do @logger end else attr_reader :logger diff --git a/lib/asciidoctor/parser.rb b/lib/asciidoctor/parser.rb index 0a4a0579..d65b0e33 100644 --- a/lib/asciidoctor/parser.rb +++ b/lib/asciidoctor/parser.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # Public: Methods to parse lines of AsciiDoc into an object hierarchy # representing the structure of the document. All methods are class methods and @@ -1115,9 +1114,7 @@ class Parser found = false autonum = 0 text.scan(CalloutScanRx) { - # lead with assignments for Ruby 1.8.7 compat - captured, num = $&, $2 - document.callouts.register num == '.' ? (autonum += 1).to_s : num unless captured.start_with? '\\' + document.callouts.register $2 == '.' ? (autonum += 1).to_s : $2 unless $&.start_with? '\\' # we have to mark as found even if it's escaped so it can be unescaped found = true } if text.include? '<' @@ -1152,8 +1149,6 @@ class Parser # Returns nothing def self.catalog_inline_anchors text, block, document, reader text.scan(InlineAnchorScanRx) do - # alias match for Ruby 1.8.7 compat - m = $~ if (id = $1) if (reftext = $2) next if (reftext.include? ATTR_REF_HEAD) && (reftext = document.sub_attributes reftext).empty? @@ -1167,7 +1162,7 @@ class Parser end unless document.register :refs, [id, (Inline.new block, :anchor, reftext, :type => :ref, :id => id), reftext] location = reader.cursor_at_mark - if (offset = (m.pre_match.count LF) + ((m[0].start_with? LF) ? 1 : 0)) > 0 + if (offset = ($`.count LF) + (($&.start_with? LF) ? 1 : 0)) > 0 (location = location.dup).advance offset end logger.warn message_with_context %(id assigned to anchor already in use: #{id}), :source_location => location @@ -1704,7 +1699,7 @@ class Parser def self.setext_section_title? line1, line2 if (level = SETEXT_SECTION_LEVELS[line2_ch1 = line2.chr]) && line2_ch1 * (line2_len = line2.length) == line2 && SetextSectionTitleRx.match?(line1) && - (line_length(line1) - line2_len).abs < 2 + (line1.length - line2_len).abs < 2 level end end @@ -1766,7 +1761,7 @@ class Parser elsif Compliance.underline_style_section_titles && (line2 = reader.peek_line(true)) && (sect_level = SETEXT_SECTION_LEVELS[line2_ch1 = line2.chr]) && line2_ch1 * (line2_len = line2.length) == line2 && (sect_title = SetextSectionTitleRx =~ line1 && $1) && - (line_length(line1) - line2_len).abs < 2 + (line1.length - line2_len).abs < 2 atx = false if sect_title.end_with?(']]') && InlineSectionAnchorRx =~ sect_title && !$1 # escaped sect_title, sect_id, sect_reftext = (sect_title.slice 0, sect_title.length - $&.length), $2, $3 @@ -1779,21 +1774,6 @@ class Parser [sect_id, sect_reftext, sect_title, sect_level, atx] end - # Public: Calculate the number of unicode characters in the line, excluding the endline - # - # line - the String to calculate - # - # returns the number of unicode characters in the line - if FORCE_UNICODE_LINE_LENGTH - def self.line_length(line) - line.scan(UnicodeCharScanRx).size - end - else - def self.line_length(line) - line.length - end - end - # Public: Consume and parse the two header lines (line 1 = author info, line 2 = revision info). # # Returns the Hash of header metadata. If a Document object is supplied, the metadata diff --git a/lib/asciidoctor/path_resolver.rb b/lib/asciidoctor/path_resolver.rb index ea8eaacb..b3c27a39 100644 --- a/lib/asciidoctor/path_resolver.rb +++ b/lib/asciidoctor/path_resolver.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # Public: Handles all operations for resolving, cleaning and joining paths. # This class includes operations for handling both web paths (request URIs) and diff --git a/lib/asciidoctor/reader.rb b/lib/asciidoctor/reader.rb index a3f3a11f..8036ac20 100644 --- a/lib/asciidoctor/reader.rb +++ b/lib/asciidoctor/reader.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # Public: Methods for retrieving lines from AsciiDoc source files class Reader @@ -807,16 +806,14 @@ class PreprocessorReader < Reader # don't honor match if it doesn't meet this criteria return false unless no_target && EvalExpressionRx =~ text.strip - # NOTE save values eagerly for Ruby 1.8.7 compat - lhs, op, rhs = $1, $2, $3 - lhs = resolve_expr_val lhs - rhs = resolve_expr_val rhs + lhs = resolve_expr_val $1 + rhs = resolve_expr_val $3 # regex enforces a restricted set of math-related operations - if op == '!=' + if $2 == '!=' skip = lhs.send :==, rhs else - skip = !(lhs.send op.to_sym, rhs) + skip = !(lhs.send $2.to_sym, rhs) end end end @@ -972,11 +969,11 @@ class PreprocessorReader < Reader begin open(inc_path, 'rb') do |f| dbl_co, dbl_sb = '::', '[]' - encoding = ::Encoding::UTF_8 if COERCE_ENCODING + utf8 = ::Encoding::UTF_8 f.each_line do |l| inc_lineno += 1 # must force encoding since we're performing String operations on line - l.force_encoding encoding if encoding + l.force_encoding utf8 if (l.include? dbl_co) && (l.include? dbl_sb) && TagDirectiveRx =~ l this_tag = $2 if $1 # end tag @@ -1016,7 +1013,7 @@ class PreprocessorReader < Reader logger.warn message_with_context %(detected unclosed tag '#{tag_name}' starting at line #{tag_lineno} of include #{target_type}: #{inc_path}), :source_location => cursor, :include_location => (create_include_cursor inc_path, expanded_target, tag_lineno) end end - unless (missing_tags = inc_tags.keys.to_a - tags_used.to_a).empty? + unless (missing_tags = inc_tags.keys - tags_used.to_a).empty? logger.warn message_with_context %(tag#{missing_tags.size > 1 ? 's' : ''} '#{missing_tags.join ', '}' not found in include #{target_type}: #{inc_path}), :source_location => cursor end shift @@ -1066,7 +1063,7 @@ class PreprocessorReader < Reader # caching requires the open-uri-cached gem to be installed # processing will be automatically aborted if these libraries can't be opened Helpers.require_library 'open-uri/cached', 'open-uri-cached' unless defined? ::OpenURI::Cache - elsif !::RUBY_ENGINE_OPAL + elsif !RUBY_ENGINE_OPAL # autoload open-uri ::OpenURI end @@ -1110,7 +1107,7 @@ class PreprocessorReader < Reader # NOTE if file is not a string, assume it's a URI if ::String === file @dir = ::File.dirname file - elsif ::RUBY_ENGINE_OPAL + elsif RUBY_ENGINE_OPAL @dir = ::URI.parse ::File.dirname(file = file.to_s) else # NOTE this intentionally throws an error if URI has no path @@ -1170,7 +1167,7 @@ class PreprocessorReader < Reader def create_include_cursor file, path, lineno if ::String === file dir = ::File.dirname file - elsif ::RUBY_ENGINE_OPAL + elsif RUBY_ENGINE_OPAL dir = ::File.dirname(file = file.to_s) else dir = (dir = ::File.dirname file.path) == '' ? '/' : dir diff --git a/lib/asciidoctor/section.rb b/lib/asciidoctor/section.rb index b5da4826..ebf2d752 100644 --- a/lib/asciidoctor/section.rb +++ b/lib/asciidoctor/section.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # Public: Methods for managing sections of AsciiDoc content in a document. # The section responds as an Array of content blocks by delegating diff --git a/lib/asciidoctor/stylesheets.rb b/lib/asciidoctor/stylesheets.rb index 482e9900..03e039c4 100644 --- a/lib/asciidoctor/stylesheets.rb +++ b/lib/asciidoctor/stylesheets.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # A utility class for working with the built-in stylesheets. #-- diff --git a/lib/asciidoctor/substitutors.rb b/lib/asciidoctor/substitutors.rb index 02e88f4f..7f642941 100644 --- a/lib/asciidoctor/substitutors.rb +++ b/lib/asciidoctor/substitutors.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor # Public: Methods to perform substitutions on lines of AsciiDoc text. This module # is intented to be mixed-in to Section and Block to provide operations for performing @@ -43,24 +42,17 @@ module Substitutors SUB_HIGHLIGHT = ['coderay', 'pygments'] - if ::RUBY_MIN_VERSION_1_9 - CAN = %(\u0018) - DEL = %(\u007f) + CAN = %(\u0018) + DEL = %(\u007f) - # Delimiters and matchers for the passthrough placeholder - # See http://www.aivosto.com/vbtips/control-characters.html#listabout for characters to use + # Delimiters and matchers for the passthrough placeholder + # See http://www.aivosto.com/vbtips/control-characters.html#listabout for characters to use - # SPA, start of guarded protected area (\u0096) - PASS_START = %(\u0096) + # SPA, start of guarded protected area (\u0096) + PASS_START = %(\u0096) - # EPA, end of guarded protected area (\u0097) - PASS_END = %(\u0097) - else - CAN = 24.chr - DEL = 127.chr - PASS_START = 150.chr - PASS_END = 151.chr - end + # EPA, end of guarded protected area (\u0097) + PASS_END = %(\u0097) # match passthrough slot PassSlotRx = /#{PASS_START}(\d+)#{PASS_END}/ @@ -177,28 +169,24 @@ module Substitutors compat_mode = @document.compat_mode passes = @passthroughs text = text.gsub(InlinePassMacroRx) { - # alias match for Ruby 1.8.7 compat - m = $~ preceding = nil - if (boundary = m[4]) # $$, ++, or +++ + if (boundary = $4) # $$, ++, or +++ # skip ++ in compat mode, handled as normal quoted text if compat_mode && boundary == '++' - next m[2] ? - %(#{m[1]}[#{m[2]}]#{m[3]}++#{extract_passthroughs m[5]}++) : - %(#{m[1]}#{m[3]}++#{extract_passthroughs m[5]}++) + next $2 ? %(#{$1}[#{$2}]#{$3}++#{extract_passthroughs $5}++) : %(#{$1}#{$3}++#{extract_passthroughs $5}++) end - attributes = m[2] - escape_count = m[3].length - content = m[5] + attributes = $2 + escape_count = $3.length + content = $5 old_behavior = false if attributes if escape_count > 0 # NOTE we don't look for nested unconstrained pass macros - next %(#{m[1]}[#{attributes}]#{RS * (escape_count - 1)}#{boundary}#{m[5]}#{boundary}) - elsif m[1] == RS + next %(#{$1}[#{attributes}]#{RS * (escape_count - 1)}#{boundary}#{$5}#{boundary}) + elsif $1 == RS preceding = %([#{attributes}]) attributes = nil else @@ -210,7 +198,7 @@ module Substitutors end elsif escape_count > 0 # NOTE we don't look for nested unconstrained pass macros - next %(#{RS * (escape_count - 1)}#{boundary}#{m[5]}#{boundary}) + next %(#{RS * (escape_count - 1)}#{boundary}#{$5}#{boundary}) end subs = (boundary == '+++' ? [] : BASIC_SUBS) @@ -225,12 +213,12 @@ module Substitutors passes[pass_key] = {:text => content, :subs => subs} end else # pass:[] - if m[6] == RS + if $6 == RS # NOTE we don't look for nested pass:[] macros - next m[0].slice 1, m[0].length + next $&.slice 1, $&.length end - passes[pass_key = passes.size] = {:text => (unescape_brackets m[8]), :subs => (m[7] ? (resolve_pass_subs m[7]) : nil)} + passes[pass_key = passes.size] = {:text => (unescape_brackets $8), :subs => ($7 ? (resolve_pass_subs $7) : nil)} end %(#{preceding}#{PASS_START}#{pass_key}#{PASS_END}) @@ -238,13 +226,11 @@ module Substitutors pass_inline_char1, pass_inline_char2, pass_inline_rx = InlinePassRx[compat_mode] text = text.gsub(pass_inline_rx) { - # alias match for Ruby 1.8.7 compat - m = $~ - preceding = m[1] - attributes = m[2] - escape_mark = RS if (quoted_text = m[3]).start_with? RS - format_mark = m[4] - content = m[5] + preceding = $1 + attributes = $2 + escape_mark = RS if (quoted_text = $3).start_with? RS + format_mark = $4 + content = $5 if compat_mode old_behavior = true @@ -297,18 +283,16 @@ module Substitutors # NOTE we need to do the stem in a subsequent step to allow it to be escaped by the former text = text.gsub(InlineStemMacroRx) { - # alias match for Ruby 1.8.7 compat - m = $~ # honor the escape if $&.start_with? RS - next m[0].slice 1, m[0].length + next $&.slice 1, $&.length end - if (type = m[1].to_sym) == :stem + if (type = $1.to_sym) == :stem type = STEM_TYPE_ALIASES[@document.attributes['stem']].to_sym end - content = unescape_brackets m[3] - subs = m[2] ? (resolve_pass_subs m[2]) : ((@document.basebackend? 'html') ? BASIC_SUBS : nil) + content = unescape_brackets $3 + subs = $2 ? (resolve_pass_subs $2) : ((@document.basebackend? 'html') ? BASIC_SUBS : nil) passes[pass_key = passes.size] = {:text => content, :subs => subs, :type => type} %(#{PASS_START}#{pass_key}#{PASS_END}) } if (text.include? ':') && ((text.include? 'stem:') || (text.include? 'math:')) @@ -420,14 +404,8 @@ module Substitutors # text - The String text to process. # # returns The String text with special characters replaced. - if ::RUBY_MIN_VERSION_1_9 - def sub_specialchars text - (text.include? '<') || (text.include? '&') || (text.include? '>') ? (text.gsub SpecialCharsRx, SpecialCharsTr) : text - end - else - def sub_specialchars text - (text.include? '<') || (text.include? '&') || (text.include? '>') ? (text.gsub(SpecialCharsRx) { SpecialCharsTr[$&] }) : text - end + def sub_specialchars text + (text.include? '<') || (text.include? '&') || (text.include? '>') ? (text.gsub SpecialCharsRx, SpecialCharsTr) : text end alias sub_specialcharacters sub_specialchars @@ -573,16 +551,13 @@ module Substitutors if found_macroish && (text.include? 'menu:') text = text.gsub(InlineMenuMacroRx) { - # alias match for Ruby 1.8.7 compat - m = $~ # honor the escape if $&.start_with? RS - next m[0].slice 1, m[0].length + next $&.slice 1, $&.length end - menu, items = m[1], m[2] - - if items + menu = $1 + if (items = $2) items = items.gsub ESC_R_SB, R_SB if items.include? R_SB if (delim = items.include?('>') ? '>' : (items.include?(',') ? ',' : nil)) submenus = items.split(delim).map {|it| it.strip } @@ -600,16 +575,12 @@ module Substitutors if (text.include? '"') && (text.include? '>') text = text.gsub(InlineMenuRx) { - # alias match for Ruby 1.8.7 compat - m = $~ # honor the escape if $&.start_with? RS - next m[0].slice 1, m[0].length + next $&.slice 1, $&.length end - input = m[1] - - menu, *submenus = input.split('>').map {|it| it.strip } + menu, *submenus = $1.split('>').map {|it| it.strip } menuitem = submenus.pop Inline.new(self, :menu, nil, :attributes => {'menu' => menu, 'submenus' => submenus, 'menuitem' => menuitem}).convert } @@ -621,17 +592,15 @@ module Substitutors if (extensions = doc.extensions) && extensions.inline_macros? # && found_macroish extensions.inline_macros.each do |extension| text = text.gsub(extension.instance.regexp) { - # alias match for Ruby 1.8.7 compat - m = $~ # honor the escape if $&.start_with? RS - next m[0].slice 1, m[0].length + next $&.slice 1, $&.length end - if (m.names rescue []).empty? - target, content, extconf = m[1], m[2], extension.config + if ($~.names rescue []).empty? + target, content, extconf = $1, $2, extension.config else - target, content, extconf = (m[:target] rescue nil), (m[:content] rescue nil), extension.config + target, content, extconf = ($~[:target] rescue nil), ($~[:content] rescue nil), extension.config end attributes = (attributes = extconf[:default_attrs]) ? attributes.dup : {} if content.nil_or_empty? @@ -656,21 +625,19 @@ module Substitutors if found_macroish && ((text.include? 'image:') || (text.include? 'icon:')) # image:filename.png[Alt Text] text = text.gsub(InlineImageMacroRx) { - # alias match for Ruby 1.8.7 compat - m = $~ # honor the escape - if (captured = $&).start_with? RS - next captured.slice 1, captured.length - elsif captured.start_with? 'icon:' + if $&.start_with? RS + next $&.slice 1, $&.length + elsif $&.start_with? 'icon:' type, posattrs = 'icon', ['size'] else type, posattrs = 'image', ['alt', 'width', 'height'] end - if (target = m[1]).include? ATTR_REF_HEAD + if (target = $1).include? ATTR_REF_HEAD # TODO remove this special case once titles use normal substitution order target = sub_attributes target end - attrs = parse_attributes m[2], posattrs, :unescape_input => true + attrs = parse_attributes $2, posattrs, :unescape_input => true doc.register :images, [target, (attrs['imagesdir'] = doc_attrs['imagesdir'])] unless type == 'icon' attrs['alt'] ||= (attrs['default-alt'] = Helpers.basename(target, true).tr('_-', ' ')) Inline.new(self, :image, nil, :type => type, :target => target, :attributes => attrs).convert @@ -683,13 +650,12 @@ module Substitutors # ((Tigers)) # indexterm2:[Tigers] text = text.gsub(InlineIndextermMacroRx) { - captured = $& case $1 when 'indexterm' text = $2 # honor the escape - if captured.start_with? RS - next captured.slice 1, captured.length + if $&.start_with? RS + next $&.slice 1, $&.length end # indexterm:[Tigers,Big cats] terms = split_simple_csv normalize_string text, true @@ -698,8 +664,8 @@ module Substitutors when 'indexterm2' text = $2 # honor the escape - if captured.start_with? RS - next captured.slice 1, captured.length + if $&.start_with? RS + next $&.slice 1, $&.length end # indexterm2:[Tigers] term = normalize_string text, true @@ -708,13 +674,13 @@ module Substitutors else text = $3 # honor the escape - if captured.start_with? RS + if $&.start_with? RS # escape concealed index term, but process nested flow index term if (text.start_with? '(') && (text.end_with? ')') text = text.slice 1, text.length - 2 visible, before, after = true, '(', ')' else - next captured.slice 1, captured.length + next $&.slice 1, $&.length end else visible = true @@ -747,21 +713,19 @@ module Substitutors if found_colon && (text.include? '://') # inline urls, target[text] (optionally prefixed with link: and optionally surrounded by <>) text = text.gsub(InlineLinkRx) { - # alias match for Ruby 1.8.7 compat - m = $~ # honor the escape if (target = $2).start_with? RS - next %(#{m[1]}#{target.slice 1, target.length}#{m[3]}) + next %(#{$1}#{target.slice 1, target.length}#{$3}) end # NOTE if text is non-nil, then we've matched a formal macro (i.e., trailing square brackets) - prefix, text, suffix = m[1], (macro = m[3]) || '', '' + captured, prefix, text, suffix = $&, $1, (macro = $3) || '', '' if prefix == 'link:' if macro prefix = '' else # invalid macro syntax (link: prefix w/o trailing square brackets) # we probably shouldn't even get here...our regex is doing too much - next m[0] + next captured end end unless macro || UriTerminatorRx !~ target @@ -794,7 +758,7 @@ module Substitutors end end # NOTE handle case when remaining target is a URI scheme (e.g., http://) - return m[0] if target.end_with? '://' + return captured if target.end_with? '://' end attrs, link_opts = nil, { :type => :link } @@ -843,15 +807,18 @@ module Substitutors if found_macroish && ((text.include? 'link:') || (text.include? 'mailto:')) # inline link macros, link:target[text] text = text.gsub(InlineLinkMacroRx) { - # alias match for Ruby 1.8.7 compat - m = $~ # honor the escape if $&.start_with? RS - next m[0].slice 1, m[0].length + next $&.slice 1, $&.length + end + if (mailto = $1) + target = %(mailto:#{$2}) + mailto_text = $2 + else + target = $2 end - target = (mailto = m[1]) ? %(mailto:#{m[2]}) : m[2] attrs, link_opts = nil, { :type => :link } - unless (text = m[3]).empty? + unless (text = $3).empty? text = text.gsub ESC_R_SB, R_SB if text.include? R_SB if mailto if !doc.compat_mode && (text.include? ',') @@ -892,7 +859,7 @@ module Substitutors if text.empty? # mailto is a special case, already processed if mailto - text = m[2] + text = mailto_text else if doc_attrs.key? 'hide-uri-scheme' if (text = target.sub UriSniffRx, '').empty? @@ -918,31 +885,28 @@ module Substitutors if text.include? '@' text = text.gsub(InlineEmailRx) { - address, tip = $&, $1 - if tip - next (tip == RS ? (address.slice 1, address.length) : address) + if $1 + next ($1 == RS ? ($&.slice 1, $&.length) : $&) end - target = %(mailto:#{address}) + target = %(mailto:#{$&}) # QUESTION should this be registered as an e-mail address? doc.register(:links, target) - Inline.new(self, :anchor, address, :type => :link, :target => target).convert + Inline.new(self, :anchor, $&, :type => :link, :target => target).convert } end if found_macroish && (text.include? 'tnote') text = text.gsub(InlineFootnoteMacroRx) { - # alias match for Ruby 1.8.7 compat - m = $~ - # honor the escape if $&.start_with? RS - next m[0].slice 1, m[0].length + next $&.slice 1, $&.length end - if m[1] # footnoteref (legacy) - id, text = (m[3] || '').split(',', 2) + if $1 # footnoteref (legacy) + id, text = ($3 || '').split(',', 2) else - id, text = m[2], m[3] + id = $2 + text = $3 end if id if text @@ -967,7 +931,7 @@ module Substitutors doc.register(:footnotes, Document::Footnote.new(index, id, text)) type = target = nil else - next m[0] + next $& end Inline.new(self, :footnote, text, :attributes => {'index' => index}, :id => id, :target => target, :type => type).convert } @@ -1011,20 +975,18 @@ module Substitutors def sub_inline_xrefs(content, found = nil) if ((found ? found[:macroish] : (content.include? '[')) && (content.include? 'xref:')) || ((content.include? '&') && (content.include? 'lt;&')) content = content.gsub(InlineXrefMacroRx) { - # alias match for Ruby 1.8.7 compat - m = $~ # honor the escape if $&.start_with? RS - next m[0].slice 1, m[0].length + next $&.slice 1, $&.length end attrs, doc = {}, @document - if (refid = m[1]) + if (refid = $1) refid, text = refid.split ',', 2 text = text.lstrip if text else macro = true - refid = m[2] - if (text = m[3]) + refid = $2 + if (text = $3) text = text.gsub ESC_R_SB, R_SB if text.include? R_SB # NOTE if an equal sign (=) is present, parse text as attributes text = ((AttributeList.new text, self).parse_into attrs)[1] if !doc.compat_mode && (text.include? '=') diff --git a/lib/asciidoctor/table.rb b/lib/asciidoctor/table.rb index a4b61412..acc9eb89 100644 --- a/lib/asciidoctor/table.rb +++ b/lib/asciidoctor/table.rb @@ -1,10 +1,9 @@ -# encoding: UTF-8 module Asciidoctor # Public: Methods and constants for managing AsciiDoc table content in a document. # It supports all three of AsciiDoc's table formats: psv, dsv and csv. class Table < AbstractBlock - # multipler / divisor for tuning precision of calculated result - DEFAULT_PRECISION_FACTOR = 10000.0 + # precision of column widths + DEFAULT_PRECISION = 4 # Public: A data object that encapsulates the collection of rows (head, foot, body) for a table class Rows @@ -111,7 +110,7 @@ class Table < AbstractBlock # # returns nothing def assign_column_widths width_base = nil, autowidth_cols = nil - pf = DEFAULT_PRECISION_FACTOR + precision = DEFAULT_PRECISION total_width = col_pcwidth = 0 if width_base @@ -120,32 +119,22 @@ class Table < AbstractBlock autowidth = 0 logger.warn %(total column width must not exceed 100% when using autowidth columns; got #{width_base}%) else - autowidth = ((100.0 - width_base) / autowidth_cols.size * pf).to_i / pf + autowidth = ((100.0 - width_base) / autowidth_cols.size).truncate precision autowidth = autowidth.to_i if autowidth.to_i == autowidth width_base = 100 end autowidth_attrs = { 'width' => autowidth, 'autowidth-option' => '' } autowidth_cols.each {|col| col.update_attributes autowidth_attrs } end - @columns.each {|col| total_width += (col_pcwidth = col.assign_width nil, width_base, pf) } + @columns.each {|col| total_width += (col_pcwidth = col.assign_width nil, width_base, precision) } else - col_pcwidth = ((100 * pf / @columns.size).to_i) / pf - # or... - #col_pcwidth = (100.0 / @columns.size).truncate 4 + col_pcwidth = (100.0 / @columns.size).truncate precision col_pcwidth = col_pcwidth.to_i if col_pcwidth.to_i == col_pcwidth - @columns.each {|col| total_width += col.assign_width col_pcwidth } + @columns.each {|col| total_width += col.assign_width col_pcwidth, nil, precision } end # donate balance, if any, to final column (using half up rounding) - unless total_width == 100 - @columns[-1].assign_width(((100 - total_width + col_pcwidth) * pf).round / pf) - # or (manual half up rounding)... - #numerator = (raw_numerator = (100 - total_width + col_pcwidth) * pf).to_i - #numerator += 1 if raw_numerator >= numerator + 0.5 - #@columns[-1].assign_width numerator / pf - # or... - #@columns[-1].assign_width((100 - total_width + col_pcwidth).round 4) - end + @columns[-1].assign_width(((100 - total_width + col_pcwidth).round precision), nil, precision) unless total_width == 100 nil end @@ -201,11 +190,9 @@ class Table::Column < AbstractNode # This method assigns the colpcwidth and colabswidth attributes. # # returns the resolved colpcwidth value - def assign_width col_pcwidth, width_base = nil, pf = 10000.0 + def assign_width col_pcwidth, width_base, precision if width_base - col_pcwidth = ((@attributes['width'].to_f / width_base) * 100 * pf).to_i / pf - # or... - #col_pcwidth = (@attributes['width'].to_f * 100.0 / width_base).truncate 4 + col_pcwidth = (@attributes['width'].to_f * 100.0 / width_base).truncate precision col_pcwidth = col_pcwidth.to_i if col_pcwidth.to_i == col_pcwidth end @attributes['colpcwidth'] = col_pcwidth diff --git a/lib/asciidoctor/timings.rb b/lib/asciidoctor/timings.rb index 4a93a908..1d53f514 100644 --- a/lib/asciidoctor/timings.rb +++ b/lib/asciidoctor/timings.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 module Asciidoctor class Timings def initialize diff --git a/run-tests.sh b/run-tests.sh index 1c6e3c65..5c61da1a 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -4,7 +4,7 @@ # This script will execute against all supported Ruby versions if "all" is the first argument to the script. if [ "$1" = "all" ]; then - rvm 1.8@asciidoctor-dev,jruby@asciidoctor-dev,rbx@asciidoctor-dev,1.9@asciidoctor-dev,2.0@asciidoctor-dev,2.1@asciidoctor-dev "do" ./run-tests.sh + rvm 2.3@asciidoctor-dev,2.4@asciidoctor-dev,2.5@asciidoctor-dev,2.6@asciidoctor-dev,jruby-9.1@asciidoctor-dev,jruby-9.2@asciidoctor-dev "do" ./run-tests.sh else rake test:all > /tmp/asciidoctor-test-results.txt 2>&1; cat /tmp/asciidoctor-test-results.txt fi diff --git a/test/api_test.rb b/test/api_test.rb index f68831d9..a5e2d30e 100644 --- a/test/api_test.rb +++ b/test/api_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' @@ -24,6 +23,39 @@ context 'API' do assert_equal '.asciidoc', doc.attr('docfilesuffix') end + test 'should coerce encoding of file to UTF-8' do + old_external = Encoding.default_external + old_internal = Encoding.default_internal + old_verbose = $VERBOSE + begin + $VERBOSE = nil # disable warnings since we have to modify constants + input_path = fixture_path 'encoding.asciidoc' + Encoding.default_external = Encoding.default_internal = Encoding::IBM437 + output = Asciidoctor.convert_file input_path, :to_file => false, :safe => :safe + assert_equal Encoding::UTF_8, output.encoding + assert_includes output, 'Romé' + ensure + Encoding.default_external = old_external + Encoding.default_internal = old_internal + $VERBOSE = old_verbose + end + end + + test 'should not load file with unrecognized encoding' do + begin + tmp_input = Tempfile.new %w(test- .adoc), encoding: Encoding::IBM437 + # NOTE using a character whose code differs between UTF-8 and IBM437 + tmp_input.write %(ƒ\n) + tmp_input.close + exception = assert_raises ArgumentError do + Asciidoctor.load_file tmp_input.path, :safe => :safe + end + assert_match(/Failed to load AsciiDoc document - invalid byte sequence in UTF-8/, exception.message) + ensure + tmp_input.close! + end + end + test 'should not load invalid file' do sample_input_path = fixture_path('hello-asciidoctor.pdf') exception = assert_raises ArgumentError do @@ -31,8 +63,33 @@ context 'API' do end assert_match(/Failed to load AsciiDoc document/, exception.message) # verify we have the correct backtrace (should be in at least first 5 lines) - assert_match((RUBY_ENGINE == 'rbx' ? /parser\.rb/ : /helpers\.rb/), exception.backtrace[0..4].join("\n")) - end if RUBY_MIN_VERSION_1_9 + assert_match(/helpers\.rb/, exception.backtrace[0..4].join("\n")) + end + + test 'should convert filename that contains non-ASCII characters independent of default encodings' do + old_external = Encoding.default_external + old_internal = Encoding.default_internal + old_verbose = $VERBOSE + begin + $VERBOSE = nil # disable warnings since we have to modify constants + tmp_input = Tempfile.new %w(test-UTF8- .adoc) + tmp_input.write %(UTF8\n) + tmp_input.close + tmp_output = tmp_input.path.sub '.adoc', '.html' + Asciidoctor.convert_file tmp_input.path, :safe => :safe, :attributes => 'linkcss !copycss' + assert File.exist? tmp_output + output = File.read tmp_output, mode: 'rb', encoding: 'utf-8:utf-8' + assert_equal ::Encoding::UTF_8, output.encoding + refute_empty output + assert_includes output, 'UTF8' + ensure + tmp_input.close! + FileUtils.rm tmp_output + Encoding.default_external = old_external + Encoding.default_internal = old_internal + $VERBOSE = old_verbose + end + end test 'should load input IO' do input = StringIO.new(<<-EOS) @@ -67,7 +124,7 @@ Document Title preamble EOS - doc = Asciidoctor.load(input.lines.entries, :safe => Asciidoctor::SafeMode::SAFE) + doc = Asciidoctor.load(input.lines, :safe => Asciidoctor::SafeMode::SAFE) assert_equal 'Document Title', doc.doctitle refute doc.attr?('docfile') assert_equal doc.base_dir, doc.attr('docdir') diff --git a/test/attribute_list_test.rb b/test/attribute_list_test.rb index 40454a64..74f389a7 100644 --- a/test/attribute_list_test.rb +++ b/test/attribute_list_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' diff --git a/test/attributes_test.rb b/test/attributes_test.rb index cc63812f..eb491465 100644 --- a/test/attributes_test.rb +++ b/test/attributes_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' @@ -59,8 +58,7 @@ context 'Attributes' do result = convert_string_to_embedded str assert_includes result, %(<p>#{value}</p>) end - - end if ::RUBY_MIN_VERSION_1_9 + end test 'creates an attribute by fusing a legacy multi-line value' do str = <<-EOS @@ -245,11 +243,7 @@ content {imagesdir} EOS output = convert_inline_string input, :safe => :safe - if RUBY_VERSION >= '1.9' - assert_equal %(#{Dir.home}/etc/images), output - else - assert_equal %(#{ENV['HOME']}/etc/images), output - end + assert_equal %(#{Asciidoctor::USER_HOME}/etc/images), output end test 'user-home attribute resolves to . if safe mode is SERVER or greater' do diff --git a/test/blocks_test.rb b/test/blocks_test.rb index effcee5a..b4ca21ac 100644 --- a/test/blocks_test.rb +++ b/test/blocks_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' @@ -927,9 +926,9 @@ EOS assert_xpath '//pre', output, 1 assert_xpath '//pre/text()', output, 1 text = xmlnodes_at_xpath('//pre/text()', output, 1).text - lines = text.lines.entries + lines = text.lines assert_equal 5, lines.size - expected = "line one\n\nline two\n\nline three".lines.entries + expected = "line one\n\nline two\n\nline three".lines assert_equal expected, lines blank_lines = output.scan(/\n[ \t]*\n/).size assert blank_lines >= 2 @@ -952,9 +951,9 @@ EOS assert_xpath '//pre/code', output, 1 assert_xpath '//pre/code/text()', output, 1 text = xmlnodes_at_xpath('//pre/code/text()', output, 1).text - lines = text.lines.entries + lines = text.lines assert_equal 5, lines.size - expected = "line one\n\nline two\n\nline three".lines.entries + expected = "line one\n\nline two\n\nline three".lines assert_equal expected, lines blank_lines = output.scan(/\n[ \t]*\n/).size assert blank_lines >= 2 @@ -979,9 +978,9 @@ EOS assert_xpath '//*[@class="verseblock"]/pre', output, 1 assert_xpath '//*[@class="verseblock"]/pre/text()', output, 1 text = xmlnodes_at_xpath('//*[@class="verseblock"]/pre/text()', output, 1).text - lines = text.lines.entries + lines = text.lines assert_equal 5, lines.size - expected = "line one\n\nline two\n\nline three".lines.entries + expected = "line one\n\nline two\n\nline three".lines assert_equal expected, lines blank_lines = output.scan(/\n[ \t]*\n/).size assert blank_lines >= 2 diff --git a/test/converter_test.rb b/test/converter_test.rb index cd61c4e1..69a99d30 100644 --- a/test/converter_test.rb +++ b/test/converter_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' diff --git a/test/document_test.rb b/test/document_test.rb index 87e70e52..fbfdec80 100644 --- a/test/document_test.rb +++ b/test/document_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' @@ -275,17 +274,13 @@ content test 'should force encoding of docinfo files to UTF-8' do sample_input_path = fixture_path('basic.asciidoc') - if RUBY_VERSION >= '1.9' - default_external_old = Encoding.default_external - force_encoding_old = Asciidoctor::FORCE_ENCODING - verbose_old = $VERBOSE - end + default_external_old = Encoding.default_external + force_encoding_old = Asciidoctor::FORCE_ENCODING + verbose_old = $VERBOSE begin - if RUBY_VERSION >= '1.9' - $VERBOSE = nil # disable warnings since we have to modify constants - Encoding.default_external = 'US-ASCII' - Asciidoctor::FORCE_ENCODING = true - end + $VERBOSE = nil # disable warnings since we have to modify constants + Encoding.default_external = 'ISO-8859-1' + Asciidoctor::FORCE_ENCODING = true output = Asciidoctor.convert_file sample_input_path, :to_file => false, :header_footer => true, :backend => 'docbook', :safe => Asciidoctor::SafeMode::SERVER, :attributes => {'docinfo2' => ''} refute_empty output @@ -294,11 +289,9 @@ content assert_xpath '//xmlns:edition[text()="1.0"]', output, 1 # verifies substitutions are performed assert_css 'copyright', output, 1 ensure - if RUBY_VERSION >= '1.9' - Encoding.default_external = default_external_old - Asciidoctor::FORCE_ENCODING = force_encoding_old - $VERBOSE = verbose_old - end + Encoding.default_external = default_external_old + Asciidoctor::FORCE_ENCODING = force_encoding_old + $VERBOSE = verbose_old end end diff --git a/test/extensions_test.rb b/test/extensions_test.rb index 37798554..da4a5936 100644 --- a/test/extensions_test.rb +++ b/test/extensions_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' @@ -141,32 +140,13 @@ class TemperatureMacro < Asciidoctor::Extensions::InlineMacroProcessor; use_dsl c = target.to_f case units when 'C' - %(#{round_with_precision c, precision} °C) + %(#{c.round precision} °C) when 'F' - %(#{round_with_precision c * 1.8 + 32, precision} °F) + %(#{(c * 1.8 + 32).round precision} °F) else raise ::ArgumentError, %(Unknown temperature units: #{units}) end end - - if (::Numeric.instance_method :round).arity == 0 - def round_with_precision value, precision = 0 - if precision == 0 - value.round - else - factor = 10 ** precision - if precision < 0 - (value * factor).round.div factor - else - (value * factor).round.fdiv factor - end - end - end - else - def round_with_precision value, precision = 0 - value.round precision - end - end end class MetaRobotsDocinfoProcessor < Asciidoctor::Extensions::DocinfoProcessor diff --git a/test/invoker_test.rb b/test/invoker_test.rb index 060d04ab..b9d23324 100644 --- a/test/invoker_test.rb +++ b/test/invoker_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' @@ -130,7 +129,7 @@ context 'Invoker' do ensure FileUtils.rm_f sample_inpath end - end if RUBY_MIN_VERSION_1_9 && !windows? + end unless windows? test 'should allow docdir to be specified when input is a string' do expected_docdir = fixturedir diff --git a/test/links_test.rb b/test/links_test.rb index b4dee7dd..750c73fd 100644 --- a/test/links_test.rb +++ b/test/links_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' @@ -158,7 +157,7 @@ context 'Links' do test 'qualified url following no-break space' do assert_xpath '//a[@href="http://asciidoc.org"][text()="AsciiDoc"]', convert_string(%(#{[0xa0].pack 'U1'}http://asciidoc.org[AsciiDoc] project page.)), 1 - end if ::RUBY_MIN_VERSION_1_9 + end test 'qualified url following smart apostrophe' do output = convert_string_to_embedded("l’http://www.irit.fr[IRIT]") diff --git a/test/lists_test.rb b/test/lists_test.rb index a779ef8a..1bfa4ff0 100644 --- a/test/lists_test.rb +++ b/test/lists_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' @@ -239,7 +238,7 @@ second wrapped line assert_css 'ul', output, 1 assert_css 'ul li', output, 2 # NOTE for some reason, we're getting an extra line after the indented line - lines = xmlnodes_at_xpath('(//ul/li)[1]/p', output, 1).text.gsub(/\n[[:space:]]*\n/, "\n").lines.entries + lines = xmlnodes_at_xpath('(//ul/li)[1]/p', output, 1).text.gsub(/\n[[:space:]]*\n/, "\n").lines assert_equal 3, lines.size assert_equal 'list item 1', lines[0].chomp assert_equal ' // not line comment', lines[1].chomp @@ -539,7 +538,7 @@ List output = convert_string input assert_xpath '//ul', output, 1 assert_xpath '//ul/li', output, 3 - end if ::RUBY_MIN_VERSION_1_9 + end test 'indented asterisk elements using tabs' do input = <<-EOS @@ -996,7 +995,7 @@ List output = convert_string input assert_xpath '//ul', output, 0 assert_includes output, '•' - end if ::RUBY_MIN_VERSION_1_9 + end test "nested ordered elements (2)" do input = <<-EOS diff --git a/test/logger_test.rb b/test/logger_test.rb index 3f921b0e..fe1be778 100644 --- a/test/logger_test.rb +++ b/test/logger_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' diff --git a/test/manpage_test.rb b/test/manpage_test.rb index 0d56c40b..8102450b 100644 --- a/test/manpage_test.rb +++ b/test/manpage_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' @@ -175,7 +174,7 @@ Does stuff. (C) & (R) are translated to character references, but not the &.) output = Asciidoctor.convert input, :backend => :manpage - assert_equal '\\(co & \\(rg are translated to character references, but not the &.', output.lines.entries.last.chomp + assert_equal '\\(co & \\(rg are translated to character references, but not the &.', output.lines.last.chomp end test 'should replace em dashes' do @@ -194,7 +193,7 @@ go--to) .) output = Asciidoctor.convert input, :backend => :manpage - assert_equal '\&.', output.lines.entries.last.chomp + assert_equal '\&.', output.lines.last.chomp end test 'should escape raw macro' do @@ -205,7 +204,7 @@ AAA this line of text should be show BBB this line and the one above it should be visible) output = Asciidoctor.convert input, :backend => :manpage - assert_equal '\&.if 1 .nx', output.lines.entries[-2].chomp + assert_equal '\&.if 1 .nx', output.lines[-2].chomp end test 'should normalize whitespace in a paragraph' do @@ -261,7 +260,7 @@ Describe this thing.) "`hello`" '`goodbye`' *strong* _weak_ `even`) output = Asciidoctor.convert input, :backend => :manpage - assert_equal '\(lqhello\(rq \(oqgoodbye\(cq \fBstrong\fP \fIweak\fP \f(CReven\fP', output.lines.entries.last.chomp + assert_equal '\(lqhello\(rq \(oqgoodbye\(cq \fBstrong\fP \fIweak\fP \f(CReven\fP', output.lines.last.chomp end test 'should escape backslashes in content' do @@ -270,7 +269,7 @@ Describe this thing.) \\.foo \\ bar\\ baz) output = Asciidoctor.convert input, :backend => :manpage - assert_equal '\(rs.foo \(rs bar\(rs', output.lines.entries[-2].chomp + assert_equal '\(rs.foo \(rs bar\(rs', output.lines[-2].chomp end test 'should escape literal escape sequence' do @@ -289,7 +288,7 @@ After break.) output = Asciidoctor.convert input, :backend => :manpage assert_equal 'Before break. .br -After break.', output.lines.entries[-3..-1].join +After break.', output.lines[-3..-1].join end end @@ -303,7 +302,7 @@ http://asciidoc.org[AsciiDoc]) assert_equal '.sp First paragraph. .sp -.URL "http://asciidoc.org" "AsciiDoc" ""', output.lines.entries[-4..-1].join +.URL "http://asciidoc.org" "AsciiDoc" ""', output.lines[-4..-1].join end test 'should not swallow content following URL' do @@ -312,7 +311,7 @@ First paragraph. http://asciidoc.org[AsciiDoc] can be used to create man pages.) output = Asciidoctor.convert input, :backend => :manpage assert_equal '.URL "http://asciidoc.org" "AsciiDoc" " " -can be used to create man pages.', output.lines.entries[-2..-1].join +can be used to create man pages.', output.lines[-2..-1].join end test 'should pass adjacent character as final argument of URL macro' do @@ -321,7 +320,7 @@ can be used to create man pages.', output.lines.entries[-2..-1].join This is http://asciidoc.org[AsciiDoc].) output = Asciidoctor.convert input, :backend => :manpage assert_equal 'This is \c -.URL "http://asciidoc.org" "AsciiDoc" "."', output.lines.entries[-2..-1].join +.URL "http://asciidoc.org" "AsciiDoc" "."', output.lines[-2..-1].join end test 'should pass adjacent character as final argument of URL macro and move trailing content to next line' do @@ -331,7 +330,7 @@ This is http://asciidoc.org[AsciiDoc], which can be used to write content.) output = Asciidoctor.convert input, :backend => :manpage assert_equal 'This is \c .URL "http://asciidoc.org" "AsciiDoc" "," -which can be used to write content.', output.lines.entries[-3..-1].join +which can be used to write content.', output.lines[-3..-1].join end test 'should not leave blank lines between URLs on contiguous lines of input' do @@ -351,7 +350,7 @@ The corresponding implementations are .URL "http://cmucl.org" "CMUCL" "," .URL "http://ecls.sf.net" "ECL" "," and \c -.URL "http://sbcl.sf.net" "SBCL" "."', output.lines.entries[-8..-1].join +.URL "http://sbcl.sf.net" "SBCL" "."', output.lines[-8..-1].join end test 'should not leave blank lines between URLs on same line of input' do @@ -366,7 +365,7 @@ The corresponding implementations are \c .URL "http://cmucl.org" "CMUCL" "," .URL "http://ecls.sf.net" "ECL" "," and -.URL "http://sbcl.sf.net" "SBCL" "."', output.lines.entries[-8..-1].join +.URL "http://sbcl.sf.net" "SBCL" "."', output.lines[-8..-1].join end test 'should not insert space between link and non-whitespace characters surrounding it' do @@ -377,7 +376,7 @@ Please search |link:http://discuss.asciidoctor.org[the forums]| before asking.) assert_equal '.sp Please search |\c .URL "http://discuss.asciidoctor.org" "the forums" "|" -before asking.', output.lines.entries[-4..-1].join +before asking.', output.lines[-4..-1].join end test 'should be able to use monospaced text inside a link' do @@ -388,7 +387,7 @@ Enter the link:cat[`cat`] command.) assert_equal '.sp Enter the \c .URL "cat" "\f(CRcat\fP" " " -command.', output.lines.entries[-4..-1].join +command.', output.lines[-4..-1].join end end @@ -402,7 +401,7 @@ mailto:doc@example.org[Contact the doc]) assert_equal '.sp First paragraph. .sp -.MTO "doc\\(atexample.org" "Contact the doc" ""', output.lines.entries[-4..-1].join +.MTO "doc\\(atexample.org" "Contact the doc" ""', output.lines[-4..-1].join end test 'should set text of MTO macro to blank for implicit email' do diff --git a/test/options_test.rb b/test/options_test.rb index a2629983..4dd31ce6 100644 --- a/test/options_test.rb +++ b/test/options_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' diff --git a/test/paragraphs_test.rb b/test/paragraphs_test.rb index 62b9cade..35b5eda6 100644 --- a/test/paragraphs_test.rb +++ b/test/paragraphs_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' diff --git a/test/parser_test.rb b/test/parser_test.rb index 2e96bef1..59c67ec6 100644 --- a/test/parser_test.rb +++ b/test/parser_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' @@ -298,7 +297,7 @@ context "Parser" do assert_equal 'Stéphane', metadata['firstname'] assert_equal 'Brontë', metadata['lastname'] assert_equal 'SB', metadata['authorinitials'] - end if ::RUBY_MIN_VERSION_1_9 + end test 'parse ideographic author names' do metadata, _ = parse_header_metadata '李 四 <si.li@example.com>' @@ -310,7 +309,7 @@ context "Parser" do assert_equal '四', metadata['lastname'] assert_equal 'si.li@example.com', metadata['email'] assert_equal '李四', metadata['authorinitials'] - end if ::RUBY_MIN_VERSION_1_9 + end test "parse author condenses whitespace" do metadata, _ = parse_header_metadata ' Stuart Rackham <founder@asciidoc.org>' @@ -710,7 +709,7 @@ devtmpfs 3.9G 0 3.9G 0% /dev expected = input - lines = input.lines.entries + lines = input.lines Asciidoctor::Parser.adjust_indentation! lines, -1 assert_equal expected, lines.join end diff --git a/test/paths_test.rb b/test/paths_test.rb index 459705d5..1340dc76 100644 --- a/test/paths_test.rb +++ b/test/paths_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' diff --git a/test/preamble_test.rb b/test/preamble_test.rb index d9b8127a..3f76edc8 100644 --- a/test/preamble_test.rb +++ b/test/preamble_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' diff --git a/test/reader_test.rb b/test/reader_test.rb index 0c71228c..4364275d 100644 --- a/test/reader_test.rb +++ b/test/reader_test.rb @@ -1,11 +1,10 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' end class ReaderTest < Minitest::Test - DIRNAME = File.expand_path File.dirname __FILE__ + DIRNAME = ASCIIDOCTOR_TEST_DIR SAMPLE_DATA = <<-EOS.chomp.split(::Asciidoctor::LF) first line @@ -40,34 +39,32 @@ third line assert_equal SAMPLE_DATA, reader.lines end - if Asciidoctor::COERCE_ENCODING - test 'should encode UTF-16LE string to UTF-8 when BOM is found' do - data = "\ufeff#{SAMPLE_DATA.join ::Asciidoctor::LF}".encode('UTF-16LE').force_encoding('UTF-8') - reader = Asciidoctor::Reader.new data, nil, :normalize => true - assert_equal 'f', reader.lines.first.chr - assert_equal SAMPLE_DATA, reader.lines - end + test 'should encode UTF-16LE string to UTF-8 when BOM is found' do + data = "\ufeff#{SAMPLE_DATA.join ::Asciidoctor::LF}".encode('UTF-16LE').force_encoding('UTF-8') + reader = Asciidoctor::Reader.new data, nil, :normalize => true + assert_equal 'f', reader.lines.first.chr + assert_equal SAMPLE_DATA, reader.lines + end - test 'should encode UTF-16LE string array to UTF-8 when BOM is found' do - data = "\ufeff#{SAMPLE_DATA.join ::Asciidoctor::LF}".encode('UTF-16LE').force_encoding('UTF-8').lines.to_a - reader = Asciidoctor::Reader.new data, nil, :normalize => true - assert_equal 'f', reader.lines.first.chr - assert_equal SAMPLE_DATA, reader.lines - end + test 'should encode UTF-16LE string array to UTF-8 when BOM is found' do + data = "\ufeff#{SAMPLE_DATA.join ::Asciidoctor::LF}".encode('UTF-16LE').force_encoding('UTF-8').lines.to_a + reader = Asciidoctor::Reader.new data, nil, :normalize => true + assert_equal 'f', reader.lines.first.chr + assert_equal SAMPLE_DATA, reader.lines + end - test 'should encode UTF-16BE string to UTF-8 when BOM is found' do - data = "\ufeff#{SAMPLE_DATA.join ::Asciidoctor::LF}".encode('UTF-16BE').force_encoding('UTF-8') - reader = Asciidoctor::Reader.new data, nil, :normalize => true - assert_equal 'f', reader.lines.first.chr - assert_equal SAMPLE_DATA, reader.lines - end + test 'should encode UTF-16BE string to UTF-8 when BOM is found' do + data = "\ufeff#{SAMPLE_DATA.join ::Asciidoctor::LF}".encode('UTF-16BE').force_encoding('UTF-8') + reader = Asciidoctor::Reader.new data, nil, :normalize => true + assert_equal 'f', reader.lines.first.chr + assert_equal SAMPLE_DATA, reader.lines + end - test 'should encode UTF-16BE string array to UTF-8 when BOM is found' do - data = "\ufeff#{SAMPLE_DATA.join ::Asciidoctor::LF}".encode('UTF-16BE').force_encoding('UTF-8').lines.to_a - reader = Asciidoctor::Reader.new data, nil, :normalize => true - assert_equal 'f', reader.lines.first.chr - assert_equal SAMPLE_DATA, reader.lines - end + test 'should encode UTF-16BE string array to UTF-8 when BOM is found' do + data = "\ufeff#{SAMPLE_DATA.join ::Asciidoctor::LF}".encode('UTF-16BE').force_encoding('UTF-8').lines.to_a + reader = Asciidoctor::Reader.new data, nil, :normalize => true + assert_equal 'f', reader.lines.first.chr + assert_equal SAMPLE_DATA, reader.lines end end @@ -269,7 +266,7 @@ third line context 'Read lines until' do test 'Read lines until until end' do - lines = <<-EOS.lines.entries + lines = <<-EOS.lines This is one paragraph. This is another paragraph. @@ -284,7 +281,7 @@ This is another paragraph. end test 'Read lines until until blank line' do - lines = <<-EOS.lines.entries + lines = <<-EOS.lines This is one paragraph. This is another paragraph. @@ -1207,8 +1204,7 @@ include::fixtures/include-file.asciidoc[tags=no-such-tag-b;no-such-tag-a] using_memory_logger do |logger| convert_string_to_embedded input, :safe => :safe, :base_dir => DIRNAME - # NOTE Ruby 1.8 swaps the order of the list for some silly reason - expected_tags = ::RUBY_MIN_VERSION_1_9 ? 'no-such-tag-b, no-such-tag-a' : 'no-such-tag-a, no-such-tag-b' + expected_tags = 'no-such-tag-b, no-such-tag-a' assert_message logger, :WARN, %(~<stdin>: line 2: tags '#{expected_tags}' not found in include file), Hash end end diff --git a/test/sections_test.rb b/test/sections_test.rb index 513baed3..98f5cbc1 100644 --- a/test/sections_test.rb +++ b/test/sections_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' @@ -594,11 +593,7 @@ endif::[] == Asciidoctor in 中文 EOS output = convert_string input - if ::RUBY_MIN_VERSION_1_9 - assert_xpath '//h2[@id="_asciidoctor_in_中文"][text()="Asciidoctor in 中文"]', output - else - assert_xpath '//h2[@id="_asciidoctor_in"][text()="Asciidoctor in 中文"]', output - end + assert_xpath '//h2[@id="_asciidoctor_in_中文"][text()="Asciidoctor in 中文"]', output end test 'with only multibyte characters' do @@ -607,7 +602,7 @@ endif::[] EOS output = convert_string_to_embedded input assert_xpath '//h2[@id="_视图"][text()="视图"]', output - end if ::RUBY_MIN_VERSION_1_9 + end test 'multiline syntax with only multibyte characters' do input = <<-EOS @@ -624,7 +619,7 @@ content output = convert_string_to_embedded input assert_xpath '//h2[@id="_视图"][text()="视图"]', output assert_xpath '//h2[@id="_连接器"][text()="连接器"]', output - end if ::RUBY_MIN_VERSION_1_9 + end end context "level 2" do diff --git a/test/substitutions_test.rb b/test/substitutions_test.rb index 65d9752d..2e4f6f37 100644 --- a/test/substitutions_test.rb +++ b/test/substitutions_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' @@ -228,7 +227,7 @@ context 'Substitutions' do test 'constrained strong string containing an asterisk and multibyte word chars' do para = block_from_string(%q{*黑*眼圈*}) assert_equal '<strong>黑*眼圈</strong>', para.sub_quotes(para.source) - end if ::RUBY_MIN_VERSION_1_9 + end test 'single-line constrained quote variation emphasized string' do para = block_from_string(%q{_a few emphasized words_}) @@ -1486,12 +1485,12 @@ EOS test 'should process menu macro with items containing multibyte characters' do para = block_from_string('menu:视图[放大, 重置]', :attributes => {'experimental' => ''}) assert_equal %q{<span class="menuseq"><b class="menu">视图</b> <b class="caret">›</b> <b class="submenu">放大</b> <b class="caret">›</b> <b class="menuitem">重置</b></span>}, para.sub_macros(para.source) - end if ::RUBY_MIN_VERSION_1_9 + end test 'should process inline menu with items containing multibyte characters' do para = block_from_string('"视图 > 放大 > 重置"', :attributes => {'experimental' => ''}) assert_equal %q{<span class="menuseq"><b class="menu">视图</b> <b class="caret">›</b> <b class="submenu">放大</b> <b class="caret">›</b> <b class="menuitem">重置</b></span>}, para.sub_macros(para.source) - end if ::RUBY_MIN_VERSION_1_9 + end test 'should process a menu macro with a target that begins with a character reference' do para = block_from_string('menu:⋮[More Tools, Extensions]', :attributes => {'experimental' => ''}) @@ -1879,7 +1878,7 @@ foo — ' para = block_from_string %(富--巴) expected = '富—​巴' assert_equal expected, para.sub_replacements(para.source) - end if ::RUBY_MIN_VERSION_1_9 + end test 'replaces marks' do para = block_from_string '(C) (R) (TM) \(C) \(R) \(TM)' diff --git a/test/tables_test.rb b/test/tables_test.rb index 6e8a8715..816f30a9 100644 --- a/test/tables_test.rb +++ b/test/tables_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' @@ -1101,10 +1100,10 @@ I wouldn't have it any other way. assert_css 'table > tbody > tr > td:nth-child(1).halign-left.valign-top > p.tableblock', output, 7 assert_css 'table > tbody > tr > td:nth-child(2).halign-center.valign-top > div.verse', output, 1 verse = xmlnodes_at_css 'table > tbody > tr > td:nth-child(2).halign-center.valign-top > div.verse', output, 1 - assert_equal 26, verse.text.lines.entries.size + assert_equal 26, verse.text.lines.size assert_css 'table > tbody > tr > td:nth-child(3).halign-center.valign-top > div.literal > pre', output, 1 literal = xmlnodes_at_css 'table > tbody > tr > td:nth-child(3).halign-center.valign-top > div.literal > pre', output, 1 - assert_equal 26, literal.text.lines.entries.size + assert_equal 26, literal.text.lines.size end test 'should strip trailing endline when splitting paragraphs' do diff --git a/test/test_helper.rb b/test/test_helper.rb index 720b9472..87a16a60 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,13 +1,8 @@ -# encoding: UTF-8 ASCIIDOCTOR_TEST_DIR = File.expand_path File.dirname __FILE__ ASCIIDOCTOR_PROJECT_DIR = File.dirname ASCIIDOCTOR_TEST_DIR ASCIIDOCTOR_LIB_DIR = ENV['ASCIIDOCTOR_LIB_DIR'] || File.join(ASCIIDOCTOR_PROJECT_DIR, 'lib') Dir.chdir ASCIIDOCTOR_PROJECT_DIR -if RUBY_VERSION < '1.9' - require 'rubygems' -end - require 'simplecov' if ENV['COVERAGE'] == 'true' require File.join(ASCIIDOCTOR_LIB_DIR, 'asciidoctor') @@ -182,9 +177,9 @@ class Minitest::Test def document_from_string(src, opts = {}) assign_default_test_options opts if opts[:parse] - (Asciidoctor::Document.new src.lines.entries, opts).parse + (Asciidoctor::Document.new src.lines, opts).parse else - Asciidoctor::Document.new src.lines.entries, opts + Asciidoctor::Document.new src.lines, opts end end @@ -301,8 +296,7 @@ class Minitest::Test end def resolve_localhost - (RUBY_VERSION < '1.9' || RUBY_ENGINE == 'rbx') ? Socket.gethostname : - Socket.ip_address_list.find {|addr| addr.ipv4? }.ip_address + Socket.ip_address_list.find {|addr| addr.ipv4? }.ip_address end def using_memory_logger diff --git a/test/text_test.rb b/test/text_test.rb index e3f0d21f..894d3e85 100644 --- a/test/text_test.rb +++ b/test/text_test.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 unless defined? ASCIIDOCTOR_PROJECT_DIR $: << File.dirname(__FILE__); $:.uniq! require 'test_helper' |
