asciidoctor-reducer 1.0.0.alpha.6 → 1.0.0.alpha.7

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: 30089ea8ab059ef848713ada56dd9457f4d6681c80de8028b92a9d02b21b0544
4
- data.tar.gz: f38c68d1f15627a4d74fd5376fb3ec9a5b6ad29345c10ea882ff988c875922a9
3
+ metadata.gz: 2a685ae408de9db32a7ec96055b70120ea6f7476436dca071cc37155cc680d56
4
+ data.tar.gz: 39d783c66481423417d64a232ee01bdaa88bef517474d22e8ec9fdeb670d09d9
5
5
  SHA512:
6
- metadata.gz: a3ec843b92513164762d9647d963238073bff89c66619205166b26199a23e65d85ee5aaf23c2a60c953eae52d17ed36cbb7499dd7afb2040692912065171db12
7
- data.tar.gz: df40e3f0c244ed21ea052536844df17f536c84a12252008d1c7dbdc04ad85ec00f3aabae81242550eda9a394497db386bd12b34f5110f323f61a8bdbc8ec30c6
6
+ metadata.gz: a3376645764a9d608f3e3aad5f29f2080748d9ecd20d90a06e2335263cdd7c88c0450a72ea061f8f36c17fa19af0f985a89002ee70e12ce950f4320c483b8140
7
+ data.tar.gz: a7899259cf2ff2daf619d13b97d88dd460a013b0bc85d2c8402e1283f1b3d6fff3780bc3c112e4c281463ad06e0bcb035385998771528a01e3aac487dd7c6e09
data/CHANGELOG.adoc CHANGED
@@ -4,6 +4,25 @@
4
4
  This document provides a high-level view of the changes to the Asciidoctor Reducer by release.
5
5
  For a detailed view of what has changed, refer to the {url-repo}/commits/main[commit history] on GitHub.
6
6
 
7
+ == 1.0.0.alpha.7 (2022-02-14) - @mojavelinux
8
+
9
+ === Added
10
+
11
+ * Add asciidoctor/reducer/api to require main API (#3)
12
+ * Add `Asciidoctor::Reducer.reduce` and `Asciidoctor::Reducer.reduce_file` API methods (#3)
13
+ * Add asciidoctor/reducer/extensions to require extensions API (#3)
14
+ * Add `Asciidoctor::Reducer::Extensions` API (#3)
15
+
16
+ === Changed
17
+
18
+ * Scope extensions to single call instead of registering them globally (#3)
19
+ * Use `:safe` as the default safe mode when using the API
20
+ * Make `CurrentPosition` module private to the `PreprocessorDirectiveTracker` module
21
+
22
+ === Fixed
23
+
24
+ * Require asciidoctor/reducer/version automatically when `Asciidoctor::Reducer::VERSION` is accessed
25
+
7
26
  == 1.0.0.alpha.6 (2022-02-10) - @mojavelinux
8
27
 
9
28
  === Added
@@ -98,3 +117,7 @@ Initial release.
98
117
  === Details
99
118
 
100
119
  {url-repo}/releases/tag/v1.0.0.alpha.6[git tag]
120
+
121
+ === Details
122
+
123
+ {url-repo}/releases/tag/v1.0.0.alpha.7[git tag]
data/README.adoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = {project-name}
2
2
  Dan Allen <https://github.com/mojavelinux[@mojavelinux]>
3
- v1.0.0.alpha.6, 2022-02-10
3
+ v1.0.0.alpha.7, 2022-02-14
4
4
  :idprefix:
5
5
  :idseparator: -
6
6
  ifndef::env-github[:icons: font]
@@ -22,10 +22,10 @@ If the document does not contain any preprocessor directives, the tool returns t
22
22
 
23
23
  == Prerequisites
24
24
 
25
- {project-name} is a Ruby program that you install using Ruby packaging.
25
+ {project-name} is a Ruby application that you install using Ruby packaging.
26
26
  To install and run {project-name}, you need Ruby 2.5 or better.
27
27
 
28
- Run the following command to check whether you have Ruby installed and to check which version:
28
+ Run the following command to check which version of Ruby you have installed, if any:
29
29
 
30
30
  $ ruby -v
31
31
 
@@ -41,7 +41,9 @@ You can install the latest version of the gem using the following command:
41
41
  $ gem install asciidoctor-reducer --pre
42
42
 
43
43
  Installing this gem makes the `asciidoctor-reducer` command available on your $PATH.
44
- You can also require the gem into the Ruby runtime to use it as an Asciidoctor extension.
44
+ You can also require the gem into the Ruby runtime to use it as a library or Asciidoctor extension.
45
+
46
+ === Project-scoped
45
47
 
46
48
  If you prefer to manage the application as a project-scoped dependency, you can declare the gem in the project's [.path]_Gemfile_:
47
49
 
@@ -61,7 +63,7 @@ Installing the gem this way makes the `bundle exec asciidoctor-reducer` command
61
63
 
62
64
  == Usage
63
65
 
64
- === As command
66
+ === Command
65
67
 
66
68
  You can run this tool using the provided command (i.e., CLI), named `asciidoctor-reducer`.
67
69
  To learn how to use the command, and to verify it's available, run the command with the `-h` option:
@@ -75,7 +77,7 @@ asciidoctor-reducer [OPTION]... FILE
75
77
  ....
76
78
 
77
79
  The argument `FILE` is the AsciiDoc file you want to reduce.
78
- The options, represented by `[OPTION]...`, are optional, as the name suggestions.
80
+ The options, represented by `+[OPTION]...+`, are optional, as the name suggestions.
79
81
 
80
82
  Thus, to use the command, pass the AsciiDoc file as the sole argument:
81
83
 
@@ -91,27 +93,26 @@ To use the command in this way, pass `-` as the first argument:
91
93
 
92
94
  $ cat input.adoc | asciidoctor-reducer -
93
95
 
94
- To write the output to a file instead of stdout, also specify an output file using the `-o` option:
96
+ To write the output to a file in this case, specify an output file using the `-o` option:
95
97
 
96
98
  $ cat input.adoc | asciidoctor-reducer -o output.adoc -
97
99
 
98
- Note that top-level include files in the input AsciiDoc document are resolved relative to current working directory.
99
-
100
- === As extension
100
+ === API
101
101
 
102
- You can use this tool as an Asciidoctor extension when using the Asciidoctor API.
103
- To do so, require it before calling the `Asciidoctor.load` method.
102
+ You can also use this tool from a Ruby application using the provided API.
103
+ To begin, require the API for this library.
104
104
 
105
105
  [,ruby]
106
106
  ----
107
- require 'asciidoctor/reducer'
107
+ require 'asciidoctor/reducer/api'
108
108
  ----
109
109
 
110
- Next, load a parent document that contains includes.
110
+ Next, reduce a parent document that contains includes.
111
+ This works without having to specify the safe mode since the default safe mode when using the API is `:safe`.
111
112
 
112
113
  [,ruby]
113
114
  ----
114
- doc = Asciidoctor.load_file 'sample.adoc', safe: :safe
115
+ doc = Asciidoctor::Reducer.reduce_file 'sample.adoc'
115
116
  ----
116
117
 
117
118
  Finally, you can retrieve the reduced source from the returned document.
@@ -121,11 +122,60 @@ Finally, you can retrieve the reduced source from the returned document.
121
122
  puts doc.source
122
123
  ----
123
124
 
124
- You can write this source to a file to save the reduced document.
125
+ The benefit of this approach is that you can access the reduced source and the parsed document that corresponds to it.
126
+
127
+ If you don't need the parsed document, you can retrieve the reduced source directly by passing the `String` type to the `:to` option:
128
+
129
+ [,ruby]
130
+ ----
131
+ puts Asciidoctor::Reducer.reduce_file 'sample.adoc', to: String
132
+ ----
133
+
134
+ You can write the reduced source directly to a file by passing a file path to the `:to` option:
135
+
136
+ [,ruby]
137
+ ----
138
+ Asciidoctor::Reducer.reduce_file 'sample.adoc', to: 'sample-reduced.adoc'
139
+ ----
140
+
141
+ == Extension
142
+
143
+ Instead of using the API for this library, you can use the load API provided by Asciidoctor.
144
+ If you want to register the extension globally, require the library as follows:
145
+
146
+ [,ruby]
147
+ ----
148
+ require 'asciidoctor/reducer'
149
+ ----
150
+
151
+ When you use the Asciidoctor load API, the document will automatically be reduced.
152
+
153
+ [,ruby]
154
+ ----
155
+ puts (Asciidoctor.load_file 'sample.adoc', safe: :safe).source
156
+ ----
157
+
158
+ If you want to keep the extension scoped to the call, require the library as follows:
159
+
160
+ [,ruby]
161
+ ----
162
+ require 'asciidoctor/reducer/extensions'
163
+ ----
164
+
165
+ Next, use the extensions API to prepare an extension registry and pass it to the Asciidoctor load API:
166
+
167
+ [,ruby]
168
+ ----
169
+ puts (Asciidoctor.load_file 'sample.adoc', safe: :safe, extension_registry: Asciidoctor::Reducer.prepare_registry).source
170
+ ----
171
+
172
+ Working with the extension directly is intended for low-level operations.
173
+ Most of the time, you should use the API provided by this library.
125
174
 
126
175
  == How it Works
127
176
 
128
177
  {project-name} uses a collection of Asciidoctor extensions to rebuild the AsciiDoc source as a single document.
178
+ Top-level include files in the input AsciiDoc document are resolved relative to current working directory.
129
179
 
130
180
  It starts by using a preprocessor extension to enhance the PreprocessorReader class to be notified each time an include is entered (pushed) or exited (popped).
131
181
  When an include directive is encountered, the enhanced reader stores the resolved lines and location of the include directive, thus keeping track of where those lines should be inserted in the original source.
@@ -191,7 +241,7 @@ For more fine-grained control, you can also run the tests directly using RSpec:
191
241
 
192
242
  To run all tests in a single spec, point RSpec at the spec file:
193
243
 
194
- $ bundle exec rspec spec/asciidoctor_reducer_spec.rb
244
+ $ bundle exec rspec spec/reducer_spec.rb
195
245
 
196
246
  ==== Run specific tests
197
247
 
@@ -35,5 +35,5 @@ Gem::Specification.new do |s|
35
35
  s.add_runtime_dependency 'asciidoctor', '~> 2.0'
36
36
 
37
37
  s.add_development_dependency 'rake', '~> 13.0.0'
38
- s.add_development_dependency 'rspec', '~> 3.10.0'
38
+ s.add_development_dependency 'rspec', '~> 3.11.0'
39
39
  end
@@ -1,13 +1,11 @@
1
1
  #!/usr/bin/env ruby
2
2
  # frozen_string_literal: true
3
3
 
4
- asciidoctor_reducer = File.absolute_path '../lib/asciidoctor/reducer', __dir__
5
- if File.exist? asciidoctor_reducer
6
- require asciidoctor_reducer
4
+ asciidoctor_reducer_cli = File.absolute_path '../lib/asciidoctor/reducer/cli', __dir__
5
+ if File.exist? asciidoctor_reducer_cli
6
+ require asciidoctor_reducer_cli
7
7
  else
8
- require 'asciidoctor/reducer'
8
+ require 'asciidoctor/reducer/cli'
9
9
  end
10
10
 
11
- require 'asciidoctor/reducer/cli'
12
-
13
11
  exit Asciidoctor::Reducer::Cli.run
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+ require_relative 'extensions'
5
+
6
+ module Asciidoctor::Reducer
7
+ autoload :VERSION, (::File.join __dir__, 'version.rb')
8
+
9
+ class << self
10
+ def reduce input, opts = {}
11
+ opts = opts.merge extension_registry: (Extensions.prepare_registry opts[:extension_registry] || opts[:extensions])
12
+ opts[:safe] = ::Asciidoctor::SafeMode::SAFE unless opts.key? :safe
13
+ case input
14
+ when ::File
15
+ doc = ::Asciidoctor.load_file input, opts
16
+ when ::Pathname
17
+ doc = ::Asciidoctor.load_file input.to_path, opts
18
+ else
19
+ doc = ::Asciidoctor.load input, opts
20
+ end
21
+ write doc, opts[:to]
22
+ end
23
+
24
+ def reduce_file input_file, opts = {}
25
+ reduce (::Pathname.new input_file), opts
26
+ end
27
+
28
+ private
29
+
30
+ def write doc, to
31
+ if to && to != '/dev/null'
32
+ output = doc.source
33
+ return output if to == ::String
34
+ output += LF unless output.empty?
35
+ if ::Pathname === to || (!(to.respond_to? :write) && (to = ::Pathname.new to.to_s))
36
+ to.dirname.mkpath
37
+ to.write output, encoding: UTF_8
38
+ else
39
+ to.write output
40
+ end
41
+ end
42
+ doc
43
+ end
44
+ end
45
+
46
+ LF = ?\n
47
+ UTF_8 = ::Encoding::UTF_8
48
+
49
+ private_constant :LF, :UTF_8
50
+ end
@@ -1,11 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'api'
3
4
  autoload :OptionParser, 'optparse'
4
- autoload :Pathname, 'pathname'
5
5
 
6
6
  module Asciidoctor::Reducer
7
- autoload :VERSION, (::File.join __dir__, 'version.rb')
8
-
9
7
  class Cli
10
8
  LOG_LEVELS = (::Logger::Severity.constants false).each_with_object({}) do |level, accum|
11
9
  accum[level.to_s.downcase] = (::Logger::Severity.const_get level) unless level == :UNKNOWN
@@ -105,17 +103,9 @@ module Asciidoctor::Reducer
105
103
  else
106
104
  options[:logger] = nil
107
105
  end
108
- if (output_file = options.delete :output_file) == '-'
109
- to = $stdout
110
- else
111
- (to = ::Pathname.new output_file).dirname.mkpath
112
- end
113
- if (input_file = options.delete :input_file) == '-'
114
- reduced = (::Asciidoctor.load $stdin, options).source + ?\n
115
- else
116
- reduced = (::Asciidoctor.load_file input_file, options).source + ?\n
117
- end
118
- ::Pathname === to ? (to.write reduced, encoding: ::Encoding::UTF_8) : (to.write reduced)
106
+ options[:to] = (output_file = options.delete :output_file) == '-' ? $stdout : (::Pathname.new output_file)
107
+ input = (input_file = options.delete :input_file) == '-' ? $stdin : (::Pathname.new input_file)
108
+ ::Asciidoctor::Reducer.reduce input, options
119
109
  0
120
110
  rescue
121
111
  $stderr.write %(asciidoctor-reducer: #{$!.message}\n)
@@ -1,4 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'asciidoctor' unless defined? Asciidoctor.load
3
4
  require_relative 'preprocessor'
4
5
  require_relative 'tree_processor'
6
+
7
+ module Asciidoctor::Reducer
8
+ module Extensions
9
+ module_function
10
+
11
+ def group
12
+ proc do
13
+ next if document.options[:reduced]
14
+ preprocessor Preprocessor
15
+ tree_processor TreeProcessor
16
+ end
17
+ end
18
+
19
+ def prepare_registry registry = nil
20
+ registry = ::Asciidoctor::Extensions.create(&registry) if ::Proc === registry
21
+ unless ::Asciidoctor::Extensions.groups[:reducer]
22
+ if registry
23
+ registry.groups[:reducer] = group
24
+ else
25
+ registry = ::Asciidoctor::Extensions.create :reducer, &group
26
+ end
27
+ end
28
+ registry
29
+ end
30
+
31
+ def register
32
+ ::Asciidoctor::Extensions.register :reducer, &group
33
+ end
34
+
35
+ def unregister
36
+ ::Asciidoctor::Extensions.unregister :reducer
37
+ end
38
+ end
39
+ end
@@ -86,4 +86,6 @@ module Asciidoctor::Reducer
86
86
  self[@pos]
87
87
  end
88
88
  end
89
+
90
+ private_constant :CurrentPosition
89
91
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Asciidoctor
4
4
  module Reducer
5
- VERSION = '1.0.0.alpha.6'
5
+ VERSION = '1.0.0.alpha.7'
6
6
  end
7
7
  end
@@ -1,10 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'asciidoctor'
4
3
  require_relative 'reducer/extensions'
5
4
 
6
- Asciidoctor::Extensions.register :reducer do
7
- next if document.options[:reduced]
8
- preprocessor Asciidoctor::Reducer::Preprocessor
9
- tree_processor Asciidoctor::Reducer::TreeProcessor
10
- end
5
+ Asciidoctor::Reducer::Extensions.register
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asciidoctor-reducer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.alpha.6
4
+ version: 1.0.0.alpha.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Allen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-10 00:00:00.000000000 Z
11
+ date: 2022-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 3.10.0
47
+ version: 3.11.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 3.10.0
54
+ version: 3.11.0
55
55
  description: A tool that reduces a composite AsciiDoc document containing preprocessor
56
56
  directives (includes and conditionals) to a single AsciiDoc document by expanding
57
57
  the includes and applying the conditionals.
@@ -68,6 +68,7 @@ files:
68
68
  - bin/asciidoctor-reducer
69
69
  - lib/asciidoctor-reducer.rb
70
70
  - lib/asciidoctor/reducer.rb
71
+ - lib/asciidoctor/reducer/api.rb
71
72
  - lib/asciidoctor/reducer/cli.rb
72
73
  - lib/asciidoctor/reducer/extensions.rb
73
74
  - lib/asciidoctor/reducer/preprocessor.rb