diff options
| author | Dan Allen <dan.j.allen@gmail.com> | 2014-05-15 19:27:57 -0600 |
|---|---|---|
| committer | Dan Allen <dan.j.allen@gmail.com> | 2014-05-15 19:49:15 -0600 |
| commit | 1919b8f2fcb27eecb6e95a9b877cd8c7d31813e6 (patch) | |
| tree | 1a0611a0457916882766fe44958f9633c354750b | |
| parent | 842e6594452aeeeb3941bbc520fc82271f71269f (diff) | |
resolves #836 and #846
- use new Opal preprocessor guards (i.e., unless RUBY_ENGINE == 'opal')
- use unicode escape sequence for passthrough placeholders and null character
- fix several checks where a value is empty string instead of null in JavaScript
- consolidate opal_ext require to a single line
- move autoload declarations outside of Asciidoctor module block
- use qualified name for top-level classes in AbstractNode
| -rw-r--r-- | lib/asciidoctor.rb | 33 | ||||
| -rw-r--r-- | lib/asciidoctor/abstract_node.rb | 16 | ||||
| -rw-r--r-- | lib/asciidoctor/core_ext.rb | 10 | ||||
| -rw-r--r-- | lib/asciidoctor/opal_ext.rb | 4 | ||||
| -rw-r--r-- | lib/asciidoctor/substitutors.rb | 29 | ||||
| -rw-r--r-- | test/document_test.rb | 1 |
6 files changed, 49 insertions, 44 deletions
diff --git a/lib/asciidoctor.rb b/lib/asciidoctor.rb index 8d0b3569..633c3090 100644 --- a/lib/asciidoctor.rb +++ b/lib/asciidoctor.rb @@ -5,13 +5,16 @@ RUBY_MIN_VERSION_1_9 = (RUBY_VERSION >= '1.9') require 'set' -if RUBY_ENGINE_OPAL +# NOTE "RUBY_ENGINE == 'opal'" conditional blocks are filtered by the Opal preprocessor +if RUBY_ENGINE == 'opal' require 'encoding' # needed for String.bytes method require 'strscan' - require 'asciidoctor/opal_ext/comparable' - require 'asciidoctor/opal_ext/dir' - require 'asciidoctor/opal_ext/error' - require 'asciidoctor/opal_ext/file' + require 'asciidoctor/opal_ext' +else + autoload :Base64, 'base64' + autoload :FileUtils, 'fileutils' + autoload :OpenURI, 'open-uri' + autoload :StringScanner, 'strscan' end # ideally we should use require_relative instead of modifying the LOAD_PATH @@ -48,15 +51,6 @@ $:.unshift File.dirname __FILE__ # module Asciidoctor - unless ::RUBY_ENGINE_OPAL - # .chomp keeps Opal from trying to load the library - ::Object.autoload :Base64, 'base64'.chomp - ::Object.autoload :FileUtils, 'fileutils'.chomp - ::Object.autoload :OpenURI, 'open-uri'.chomp - #::Object.autoload :Set, 'set'.chomp - ::Object.autoload :StringScanner, 'strscan'.chomp - end - module SafeMode # A safe mode level that disables any of the security features enforced @@ -200,7 +194,7 @@ module Asciidoctor EOL = "\n" # The null character to use for splitting attribute values - NULL = ::RUBY_ENGINE_OPAL ? 0.chr : "\0" + NULL = "\0" # String for matching tab character TAB = "\t" @@ -343,7 +337,7 @@ module Asciidoctor # character classes for JavaScript Regexp engine # NOTE use of double quotes are intentional to work around Opal issue - if ::RUBY_ENGINE_OPAL + if RUBY_ENGINE == 'opal' CC_ALPHA = 'a-zA-Z' CC_ALNUM = 'a-zA-Z0-9' CC_BLANK = "[ \\t]" @@ -1449,8 +1443,11 @@ module Asciidoctor end - # autoload - unless ::RUBY_ENGINE_OPAL + if RUBY_ENGINE == 'opal' + require 'asciidoctor/debug' + require 'asciidoctor/version' + require 'asciidoctor/timings' + else autoload :Debug, 'asciidoctor/debug' autoload :VERSION, 'asciidoctor/version' autoload :Timings, 'asciidoctor/timings' diff --git a/lib/asciidoctor/abstract_node.rb b/lib/asciidoctor/abstract_node.rb index 200da01a..013f620b 100644 --- a/lib/asciidoctor/abstract_node.rb +++ b/lib/asciidoctor/abstract_node.rb @@ -323,7 +323,7 @@ class AbstractNode # # Returns A String data URI containing the content of the target image def generate_data_uri(target_image, asset_dir_key = nil) - ext = File.extname(target_image)[1..-1] + ext = ::File.extname(target_image)[1..-1] mimetype = 'image/' + ext mimetype = "#{mimetype}+xml" if ext == 'svg' if asset_dir_key @@ -334,7 +334,7 @@ class AbstractNode image_path = normalize_system_path(target_image) end - if !File.readable? image_path + unless ::File.readable? image_path warn "asciidoctor: WARNING: image to embed not found or not readable: #{image_path}" return "data:#{mimetype}:base64," # uncomment to return 1 pixel white dot instead @@ -342,12 +342,12 @@ class AbstractNode end bindata = nil - if IO.respond_to? :binread - bindata = IO.binread(image_path) + if ::IO.respond_to? :binread + bindata = ::IO.binread(image_path) else - bindata = File.open(image_path, 'rb') {|file| file.read } + bindata = ::File.open(image_path, 'rb') {|file| file.read } end - "data:#{mimetype};base64,#{Base64.encode64(bindata).delete EOL}" + "data:#{mimetype};base64,#{::Base64.encode64(bindata).delete EOL}" end # Public: Read the contents of the file at the specified path. @@ -361,9 +361,9 @@ class AbstractNode # Returns the [String] content of the file at the specified path, or nil # if the file does not exist. def read_asset(path, warn_on_failure = false) - if File.readable? path + if ::File.readable? path # QUESTION should we use strip or rstrip instead of chomp here? - File.read(path).chomp + ::File.read(path).chomp else warn "asciidoctor: WARNING: file does not exist or cannot be read: #{path}" if warn_on_failure nil diff --git a/lib/asciidoctor/core_ext.rb b/lib/asciidoctor/core_ext.rb index 60bc5faa..5f2fdbcf 100644 --- a/lib/asciidoctor/core_ext.rb +++ b/lib/asciidoctor/core_ext.rb @@ -1,7 +1,7 @@ require 'asciidoctor/core_ext/object/nil_or_empty' -# Opal barfs here if we use ::RUBY_VERSION_MIN_1_9 -unless RUBY_VERSION >= '1.9' - require 'asciidoctor/core_ext/string/chr' - # we append .to_s to keep Opal from processing the next require - require 'asciidoctor/core_ext/symbol/length'.to_s +unless RUBY_ENGINE == 'opal' + unless RUBY_MIN_VERSION_1_9 + require 'asciidoctor/core_ext/string/chr' + require 'asciidoctor/core_ext/symbol/length' + end end diff --git a/lib/asciidoctor/opal_ext.rb b/lib/asciidoctor/opal_ext.rb new file mode 100644 index 00000000..457b116d --- /dev/null +++ b/lib/asciidoctor/opal_ext.rb @@ -0,0 +1,4 @@ +require 'asciidoctor/opal_ext/comparable' +require 'asciidoctor/opal_ext/dir' +require 'asciidoctor/opal_ext/error' +require 'asciidoctor/opal_ext/file' diff --git a/lib/asciidoctor/substitutors.rb b/lib/asciidoctor/substitutors.rb index 0cfb7452..677506dd 100644 --- a/lib/asciidoctor/substitutors.rb +++ b/lib/asciidoctor/substitutors.rb @@ -48,13 +48,12 @@ module Substitutors # Delimiters and matchers for the passthrough placeholder # See http://www.aivosto.com/vbtips/control-characters.html#listabout for characters to use - # Opal hasn't yet defined the constant ::RUBY_ENGINE_OPAL at this point # SPA, start of guarded protected area (\u0096) - PASS_START = ::RUBY_ENGINE == 'opal' ? 150.chr : "\u0096" + PASS_START = "\u0096" # EPA, end of guarded protected area (\u0097) - PASS_END = ::RUBY_ENGINE == 'opal' ? 151.chr : "\u0097" + PASS_END = "\u0097" # match placeholder record PASS_MATCH = /\u0096(\d+)\u0097/ @@ -822,14 +821,8 @@ module Substitutors else id, text = m[2].split(',', 2) id = id.strip - if text - # REVIEW it's a dirty job, but somebody's gotta do it - text = restore_passthroughs(sub_inline_xrefs(sub_inline_anchors(normalize_string text, true))) - index = @document.counter('footnote-number') - @document.register(:footnotes, Document::Footnote.new(index, id, text)) - type = :ref - target = nil - else + # NOTE In Opal, text is set to empty string if comma is missing + if text.nil_or_empty? if (footnote = @document.references[:footnotes].find {|fn| fn.id == id }) index = footnote.index text = footnote.text @@ -840,6 +833,13 @@ module Substitutors target = id id = nil type = :xref + else + # REVIEW it's a dirty job, but somebody's gotta do it + text = restore_passthroughs(sub_inline_xrefs(sub_inline_anchors(normalize_string text, true))) + index = @document.counter('footnote-number') + @document.register(:footnotes, Document::Footnote.new(index, id, text)) + type = :ref + target = nil end end Inline.new(self, :footnote, text, :attributes => {'index' => index}, :id => id, :target => target, :type => type).convert @@ -920,7 +920,12 @@ module Substitutors if m[1] id, reftext = m[1].split(',', 2).map {|it| it.strip } id = id.sub(DoubleQuotedRx, ::RUBY_ENGINE_OPAL ? '$2' : '\2') - reftext = reftext.sub(DoubleQuotedMultiRx, ::RUBY_ENGINE_OPAL ? '$2' : '\2') if reftext + # NOTE In Opal, reftext is set to empty string if comma is missing + reftext = if reftext.nil_or_empty? + nil + else + reftext.sub(DoubleQuotedMultiRx, ::RUBY_ENGINE_OPAL ? '$2' : '\2') + end else id = m[2] reftext = m[3] unless m[3].nil_or_empty? diff --git a/test/document_test.rb b/test/document_test.rb index 837016d0..e4834f84 100644 --- a/test/document_test.rb +++ b/test/document_test.rb @@ -1301,7 +1301,6 @@ two content EOS result = render_string input, :safe => :safe, :backend => :xhtml, :keep_namespaces => true - doc = xmldoc_from_string(result) assert_xpath '//*[not(namespace-uri() = "http://www.w3.org/1999/xhtml")]', result, 0 end |
