asciidoctor-interdoc-reftext 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c198842d29c3f2d5ec0a2e10465885cd5172d698224f2cc05c2e0d72beda2d73
4
- data.tar.gz: 2241e3587eaf4097ccc9d3a8305f8ce957d88d0d052cc7522401795b1d8d933c
3
+ metadata.gz: cd232240b5668fc37f4552d6ee419f46028815c0aec5015fd7f70dd4a110c3cc
4
+ data.tar.gz: f68b54e05dd2cd7d32fc3f3f0c48b852ab50e3d4ec7cda4bcf54c1891ef0d8f1
5
5
  SHA512:
6
- metadata.gz: 662b5938301f727bba05fd130ad25cd5c101b3cfe43fbc75299f7f5f6fc9e0a5400f9d28997e005885ed04883ab0f68abe522623469c6d102f252998a186a614
7
- data.tar.gz: 1cd4739d61b4d8279b4e24775b702f34425c4f09be3610f3f81885dc7415b3017c105c10be1bea99e1fd0d38ebafb2b28fec2c6d8de78ffca065f1279a94dff4
6
+ metadata.gz: a7ddcf13d9d125c3071507ebaca379af4bfc41933df4f4920c6be70f8e12053e804cd586146da565389d9f29cc8fb7e6f174db2d4db129105a66cbc02447e1d2
7
+ data.tar.gz: 19432fd2fac94e39a36d4c20ac45870d45733b904f3485f4dc37413b8fa47664e6b92bae2603a6a583867ff0fa9385871e289ef139c804b679585445477e51e1
data/README.adoc CHANGED
@@ -11,6 +11,7 @@ image:https://travis-ci.org/{gh-name}.svg?branch={gh-branch}[Build Status, link=
11
11
  image:https://api.codacy.com/project/badge/Coverage/{codacy-id}["Test Coverage", link="https://www.codacy.com/app/{gh-name}"]
12
12
  image:https://api.codacy.com/project/badge/Grade/{codacy-id}["Codacy Code quality", link="https://www.codacy.com/app/{gh-name}"]
13
13
  image:https://img.shields.io/gem/v/{gem-name}.svg?style=flat[Gem Version, link="https://rubygems.org/gems/{gem-name}"]
14
+ image:https://img.shields.io/npm/v/{gem-name}.svg?style=flat[npm Version, link="https://www.npmjs.org/package/{gem-name}"]
14
15
  image:https://img.shields.io/badge/yard-docs-blue.svg[Yard Docs, link="http://www.rubydoc.info/github/{gh-name}/{gh-branch}"]
15
16
  endif::env-github[]
16
17
 
@@ -61,7 +62,9 @@ where “The First Section” is *title* of the section with id _sec1_ in _other
61
62
 
62
63
  == Installation
63
64
 
64
- To install (or update to the latest version):
65
+ === Ruby
66
+
67
+ Install {gem-name} from Rubygems:
65
68
 
66
69
  [source, subs="+attributes"]
67
70
  gem install {gem-name}
@@ -72,21 +75,47 @@ or to install the latest development version:
72
75
  gem install {gem-name} --pre
73
76
 
74
77
 
78
+ === Node.js
79
+
80
+ Install {gem-name} from npmjs.com:
81
+
82
+ [source, sh, subs="+attributes"]
83
+ npm install --save {gem-name}
84
+
85
+
75
86
  == Usage
76
87
 
77
- Just `require '{gem-name}'`.
88
+ === CLI
89
+
78
90
  If you invoke Asciidoctor from command-line, use option `-r` to load the extension:
79
91
 
80
92
  [source, subs="+attributes"]
81
93
  asciidoctor -r {gem-name} README.adoc
82
94
 
83
- If you don’t want the extension to be automatically registered in Asciidoctor, don’t _require_ `{gem-name}`, but `asciidoctor/interdoc_reftext/processor`.
95
+
96
+ === Ruby
97
+
98
+ Just `require '{gem-name}'`.
99
+ However, if you don’t want the extension to be automatically registered in Asciidoctor, `require 'asciidoctor/interdoc_reftext/processor'` instead.
84
100
 
85
101
  IMPORTANT: Bundler automatically _requires_ all the specified gems.
86
102
  To prevent it, use +
87
103
  `gem '{gem-name}', require: false`.
88
104
 
89
105
 
106
+ === Node.js
107
+
108
+ [source, js, subs="+attributes"]
109
+ ----
110
+ // Load asciidoctor.js and {gem-name}.
111
+ const asciidoctor = require('asciidoctor.js')()
112
+ require('{gem-name}')
113
+
114
+ // Convert the file to HTML.
115
+ asciidoctor.convertFile("./README.adoc")
116
+ ----
117
+
118
+
90
119
  == License
91
120
 
92
121
  This project is licensed under http://opensource.org/licenses/MIT/[MIT License].
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
+ require 'asciidoctor' unless RUBY_PLATFORM == 'opal'
2
3
  require 'asciidoctor/interdoc_reftext/version'
3
- require 'asciidoctor'
4
4
 
5
5
  module Asciidoctor::InterdocReftext
6
6
  # Mixin intended to be prepended into `Asciidoctor::Inline`.
@@ -10,29 +10,39 @@ module Asciidoctor::InterdocReftext
10
10
  # represents an inter-document cross reference).
11
11
  module InlineNodeMixin
12
12
 
13
- # Returns text of this inline element.
14
- #
15
- # @note This method will override the same name attribute reader in
16
- # class `Asciidoctor::Inline`.
17
- #
18
- # @return [String, nil]
19
- def text
20
- if (value = super)
21
- value
22
- # If this node is an inter-document cross reference...
23
- elsif @node_name == 'inline_anchor' && @attributes['path']
24
- resolver = interdoc_reftext_resolver
25
- @text = resolver.call(@attributes['refid']) if resolver
13
+ if RUBY_PLATFORM == 'opal'
14
+ # Opal does not support `Module#prepend`, so we have to fallback to
15
+ # `include` with poor alias method chain approach.
16
+ def self.included(base_klass)
17
+ base_klass.send(:alias_method, :text_without_reftext, :text)
18
+ base_klass.send(:define_method, :text) do
19
+ text_without_reftext || interdoc_reftext
20
+ end
21
+ end
22
+ else
23
+ # Returns text of this inline element.
24
+ #
25
+ # @note This method will override the same name attribute reader in
26
+ # class `Asciidoctor::Inline`.
27
+ def text
28
+ super || interdoc_reftext
26
29
  end
27
30
  end
28
31
 
29
32
  private
30
33
 
31
- # @return [#call, nil] an inter-document reftext resolver, or nil if not
32
- # set for the document.
33
- def interdoc_reftext_resolver
34
+ # Returns resolved reftext of this inline node if it is a valid
35
+ # inter-document cross-reference, otherwise returns nil.
36
+ #
37
+ # @return [String, nil]
38
+ def interdoc_reftext
39
+ # If this node is not an inter-document cross reference...
40
+ return if @node_name != 'inline_anchor' || @attributes['path'].nil?
41
+
34
42
  # This variable is injected into the document by {Processor}.
35
- @document.instance_variable_get(Processor::RESOLVER_VAR_NAME)
43
+ if (resolver = @document.instance_variable_get(Processor::RESOLVER_VAR_NAME))
44
+ @text = resolver.call(@attributes['refid'])
45
+ end
36
46
  end
37
47
  end
38
48
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
- require 'asciidoctor'
3
- require 'asciidoctor/extensions'
2
+ require 'asciidoctor' unless RUBY_PLATFORM == 'opal'
3
+ require 'asciidoctor/extensions' unless RUBY_PLATFORM == 'opal'
4
4
  require 'asciidoctor/interdoc_reftext/inline_node_mixin'
5
5
  require 'asciidoctor/interdoc_reftext/resolver'
6
6
  require 'asciidoctor/interdoc_reftext/version'
@@ -34,7 +34,7 @@ module Asciidoctor::InterdocReftext
34
34
  #
35
35
  # Eh, this is really nasty... The least evil way how to achieve the goal
36
36
  # seems to be monkey-patching of the `Asciidoctor::Inline` class. This is
37
- # done via {InlineNodeMixin} which is prepended into the `Inline` class on
37
+ # done via {InlineNodeMixin} which is prepended* into the `Inline` class on
38
38
  # initialization of this processor.
39
39
  #
40
40
  # The actual logic that resolves reflabel for the given *refid* is
@@ -43,11 +43,16 @@ module Asciidoctor::InterdocReftext
43
43
  # it into instance variable {RESOLVER_VAR_NAME} in the document, so
44
44
  # {InlineNodeMixin} can access it.
45
45
  #
46
- # Prepending {InlineNodeMixin} into the `Asciidoctor::Inline` class has
46
+ # Prepending* {InlineNodeMixin} into the `Asciidoctor::Inline` class has
47
47
  # (obviously) a global effect. However, if {RESOLVER_VAR_NAME} is not
48
48
  # injected in the document object (e.g. extension is not active), `Inline`
49
49
  # behaves the same as without {InlineNodeMixin}.
50
50
  #
51
+ # _\* If running under Opal (JavaScript), {InlineNodeMixin} is not prepended
52
+ # into the `Asciidoctor::Inline`, because Opal does not support that. Thus
53
+ # it's included and the `#text` method is overriden using poor alias method
54
+ # chain approach.
55
+ #
51
56
  # NOTE: We use _reftext_ and _reflabel_ as interchangeable terms in this gem.
52
57
  class Processor < ::Asciidoctor::Extensions::TreeProcessor
53
58
 
@@ -65,7 +70,13 @@ module Asciidoctor::InterdocReftext
65
70
 
66
71
  # Monkey-patch Asciidoctor::Inline unless already patched.
67
72
  unless ::Asciidoctor::Inline.include? InlineNodeMixin
68
- ::Asciidoctor::Inline.send(:prepend, InlineNodeMixin)
73
+ if RUBY_PLATFORM == 'opal'
74
+ # Opal does not support `Module#prepend`, so we have to fallback to
75
+ # `include` with poor alias method chain approach.
76
+ ::Asciidoctor::Inline.send(:include, InlineNodeMixin)
77
+ else
78
+ ::Asciidoctor::Inline.send(:prepend, InlineNodeMixin)
79
+ end
69
80
  end
70
81
  end
71
82
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
+ require 'asciidoctor' unless RUBY_PLATFORM == 'opal'
3
+ require 'logger' unless RUBY_PLATFORM == 'opal'
2
4
  require 'asciidoctor/interdoc_reftext/version'
3
- require 'asciidoctor'
4
- require 'logger'
5
5
 
6
6
  module Asciidoctor::InterdocReftext
7
7
  # Resolver of inter-document cross reference texts.
@@ -20,8 +20,17 @@ module Asciidoctor::InterdocReftext
20
20
 
21
21
  logger ||= if defined? ::Asciidoctor::LoggerManager
22
22
  ::Asciidoctor::LoggerManager.logger
23
- else
23
+ elsif defined? ::Logger
24
24
  ::Logger.new(STDERR)
25
+ else
26
+ # Fake logger for Opal.
27
+ # TODO: Remove after update to Asciidoctor 1.5.7 or Opal with Logger.
28
+ Object.new.tap do |o|
29
+ # rubocop: disable MethodMissing
30
+ def o.method_missing(_, *args)
31
+ STDERR.puts(*args)
32
+ end
33
+ end
25
34
  end
26
35
 
27
36
  @document = document
@@ -48,8 +57,9 @@ module Asciidoctor::InterdocReftext
48
57
  path = resolve_target_path(path) or return nil
49
58
 
50
59
  @cache["#{path}##{fragment}".freeze] ||= begin
51
- lines = read_file(path) or return nil
52
- parse_reftext(lines, fragment)
60
+ read_file(path) do |lines|
61
+ parse_reftext(lines, fragment)
62
+ end
53
63
  rescue => e # rubocop: disable RescueWithoutErrorClass
54
64
  raise if @raise_exceptions
55
65
  @logger.error "interdoc-reftext: #{e}"
@@ -93,9 +103,11 @@ module Asciidoctor::InterdocReftext
93
103
  end
94
104
 
95
105
  # @param path [String] path of the file to read.
96
- # @return [Enumerable<String>] lines of the file.
106
+ # @yield [Enumerable<String>] gives lines of the file.
97
107
  def read_file(path)
98
- ::IO.foreach(path)
108
+ ::File.open(path) do |f|
109
+ yield f.each_line
110
+ end
99
111
  end
100
112
 
101
113
  # @param input [Enumerable<String>] lines of the AsciiDoc document.
@@ -3,6 +3,6 @@
3
3
  module Asciidoctor
4
4
  module InterdocReftext
5
5
  # Version of the asciidoctor-interdoc-reftext gem.
6
- VERSION = '0.2.0'.freeze
6
+ VERSION = '0.3.0'.freeze
7
7
  end
8
8
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
- require 'asciidoctor'
3
- require 'asciidoctor/extensions'
2
+ require 'asciidoctor' unless RUBY_PLATFORM == 'opal'
3
+ require 'asciidoctor/extensions' unless RUBY_PLATFORM == 'opal'
4
4
  require 'asciidoctor/interdoc_reftext/version'
5
5
  require 'asciidoctor/interdoc_reftext/processor'
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asciidoctor-interdoc-reftext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakub Jirutka
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-09 00:00:00.000000000 Z
11
+ date: 2018-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -171,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
171
  version: '0'
172
172
  requirements: []
173
173
  rubyforge_project:
174
- rubygems_version: 2.7.6
174
+ rubygems_version: 2.7.7
175
175
  signing_key:
176
176
  specification_version: 4
177
177
  summary: Asciidoctor extension providing implicit (automatic) reference text (label)