asciidoctor-reducer 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3aad6e916f11307d608d015b5befc14567f71638ed6baf9023416846ee8d2136
4
- data.tar.gz: e51e12a72a7b4acaa6335e69cf489a356e109d57640dff8b1c7f4bfdfedaed53
3
+ metadata.gz: d79dee7d3fe04a10fac4e4ea7ff50a564bc915cd20be9ccc9bfa21b41e2ab836
4
+ data.tar.gz: a830fd2dd521f0e2154a8d46917278254eec564d6ede76ba07d147581a2a7414
5
5
  SHA512:
6
- metadata.gz: 46f16ea6198d0aa04dc00c890522b8014a2f5e9bad8c684acf7a39dc275bc339ddc587409c8bc34bee1227aa976ba7f4d25c3a08e2412070a7963219483acec8
7
- data.tar.gz: b5c42486dcccd3e178d1cbda2b09f1c7656746a830087f64a9ece7d79ab99ef9e67e40bacd19b9310063915a3bf76ea26af73b6ba18e957edc9580cdbc13773b
6
+ metadata.gz: 77dbc3cd2f39503f85093edce50bb32d9da44c700de3c61558ddfbd9c2d3b7bfc5e0cb4e05d9705e460d92d4da30555c1bbf9d90a54cb19176ffdceb2711cf8c
7
+ data.tar.gz: 3228ea887ee92bf585258dae1ec3dc120309f3d1274c8ddc90558a25473a72e1bfa2c20074fdb8b1033db90730f558c45967457279660d6782b6fa4567d7e1ee
data/CHANGELOG.adoc CHANGED
@@ -4,6 +4,27 @@
4
4
  This document provides a curated view of the changes to Asciidoctor Reducer in each 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.1.2 (2025-05-09) - @mojavelinux
8
+
9
+ === Fixed
10
+
11
+ * Don't define reader for source_header_attributes instance variable if already defined
12
+ * Only catch RangeError when flattening list when using Opal runtime
13
+
14
+ === Details
15
+
16
+ {url-repo}/releases/tag/v1.1.2[git tag] | {url-repo}/compare/v1.1.1\...v1.1.2[full diff]
17
+
18
+ == 1.1.1 (2025-03-27) - @mojavelinux
19
+
20
+ === Fixed
21
+
22
+ * Fall back to iteration-based strategy in compiled JavaScript if built-in Array#flatten method throws RangeError (#63)
23
+
24
+ === Details
25
+
26
+ {url-repo}/releases/tag/v1.1.1[git tag] | {url-repo}/compare/v1.1.0\...v1.1.1[full diff]
27
+
7
28
  == 1.1.0 (2024-11-24) - @mojavelinux
8
29
 
9
30
  === Added
data/README.adoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = {project-name}
2
2
  Dan Allen <https://github.com/mojavelinux[@mojavelinux]>
3
- v1.1.0, 2024-11-24
3
+ v1.1.2, 2025-05-09
4
4
  :idprefix:
5
5
  :idseparator: -
6
6
  ifndef::env-github[:icons: font]
@@ -115,21 +115,30 @@ require 'asciidoctor/reducer/api'
115
115
  ----
116
116
 
117
117
  Next, reduce a parent document that contains includes.
118
- (This works without having to specify the safe mode since the default safe mode when using this API is `:safe`).
119
118
 
120
119
  [,ruby]
121
120
  ----
122
121
  doc = Asciidoctor::Reducer.reduce_file 'sample.adoc'
123
122
  ----
124
123
 
125
- Finally, you can retrieve the reduced source from the returned document.
124
+ NOTE: The previous call works without having to specify the safe mode since the default safe mode when using this API is `:safe`.
125
+ However, if any include target points to a file in an ancestor directory of `docdir`, you'll need to set the safe mode to `:unsafe` by passing the option `safe: :unsafe`.
126
+ The options that the `reduce_file` method accepts are a superset of the Asciidoctor API options.
127
+
128
+ The benefit of this return value is that you can access the reduced source as well as the parsed document that corresponds to it.
129
+ Use the following call to retrieve the reduced source from the returned document.
126
130
 
127
131
  [,ruby]
128
132
  ----
129
133
  puts doc.source
130
134
  ----
131
135
 
132
- The benefit of this approach is that you can access the reduced source and the parsed document that corresponds to it.
136
+ If you want to retrieve the source split into an array of lines, use the following call instead.
137
+
138
+ [,ruby]
139
+ ----
140
+ puts doc.source_lines
141
+ ----
133
142
 
134
143
  If you only want AsciiDoctor Reducer to process include directives, leaving preprocessor conditional directives untouched, set the `:preserve_conditionals` option:
135
144
 
@@ -152,7 +161,37 @@ You can write the reduced source directly to a file by passing a file path to th
152
161
  Asciidoctor::Reducer.reduce_file 'sample.adoc', to: 'sample-reduced.adoc'
153
162
  ----
154
163
 
155
- == Extension
164
+ === In Preprocessor
165
+
166
+ It's generally not safe to read the lines from the reader in an Asciidoctor preprocessor extension because it introduces side effects.
167
+ However, Asciidoctor Reducer offers a workaround for that problem.
168
+ You can use Asciidoctor Reducer to safely retrieve the source lines of the document (with or without resolving preprocessor directives) in order to analyze them or even modify and replace the lines on the reader.
169
+
170
+ Let's look at how we can retrieve the source lines in an Asciidoctor preprocessor.
171
+ What you do with those lines is then up to you.
172
+
173
+ [,ruby]
174
+ ----
175
+ require 'asciidoctor/reducer/api'
176
+
177
+ Asciidoctor::Extensions.register do
178
+ preprocessor do
179
+ process do |doc, reader|
180
+ unless doc.options[:extension_registry]&.groups&.include? :reducer
181
+ reducer_opts = { safe: doc.options[:safe], attributes: doc.options[:attributes].dup }
182
+ reduced_doc = Asciidoctor::Reducer.reduce_file reader.file, reducer_opts
183
+ reduced_source_lines = reduced_doc.source_lines
184
+ ...
185
+ end
186
+ reader
187
+ end
188
+ end
189
+ end
190
+ ----
191
+
192
+ Since the extension is registered globally, it's necessary to short-circuit it when called by reducer.
193
+
194
+ === Extension
156
195
 
157
196
  Instead of using the API for this library, you can use the load API provided by Asciidoctor.
158
197
  If you want to register the extension globally, require the library as follows:
@@ -407,6 +446,10 @@ When running the `asciidoctor-reducer` command from source, you must prefix the
407
446
 
408
447
  To avoid having to do this, or to make the `asciidoctor-reducer` command available from anywhere, you need to build the development gem and install it.
409
448
 
449
+ == Authors
450
+
451
+ Asciidoctor Reducer was written by Dan Allen of OpenDevise Inc. and contributed to the Asciidoctor project.
452
+
410
453
  == Copyright and License
411
454
 
412
455
  Copyright (C) 2021-present Dan Allen.
@@ -50,7 +50,7 @@ module Asciidoctor::Reducer
50
50
  private
51
51
 
52
52
  def write doc, to
53
- return doc unless to && to != '/dev/null'
53
+ return doc unless to && to != ::File::NULL
54
54
  output = doc.source
55
55
  return output if to == ::String
56
56
  output += LF unless output.empty?
@@ -3,10 +3,11 @@
3
3
  module Asciidoctor::Reducer
4
4
  module HeaderAttributeTracker
5
5
  def self.extended instance
6
+ return if instance.singleton_class.method_defined? :source_header_attributes
6
7
  instance.singleton_class.send :attr_reader, :source_header_attributes
7
8
  end
8
9
 
9
- def finalize_header(*) # rubocop:disable Style/MethodDefParentheses
10
+ def finalize_header(*)
10
11
  @source_header_attributes = @attributes_modified.each_with_object({}) do |name, accum|
11
12
  accum[name] = @attributes[name]
12
13
  end
@@ -5,7 +5,7 @@ module Asciidoctor::Reducer
5
5
  def process doc
6
6
  if doc.extensions.groups[:reducer]
7
7
  unless (includes = doc.catalog[:includes].map {|name, val| val ? name : %(~#{name}) }).empty?
8
- doc.source_lines.concat ['', %(//# includes=#{includes.join ','})]
8
+ doc.source_lines.push '', %(//# includes=#{includes.join ','})
9
9
  end
10
10
  elsif (last_line = doc.source_lines[-1])&.start_with? '//# includes='
11
11
  doc.catalog[:includes].update ((last_line.slice 13, last_line.length).split ',')
@@ -23,7 +23,11 @@ module Asciidoctor::Reducer
23
23
  end
24
24
  target_lines[idx] = lines if target_lines
25
25
  end
26
- reduced_source_lines = inc_replacements[0][:lines].flatten
26
+ if RUBY_ENGINE == 'opal'
27
+ reduced_source_lines = flatten inc_replacements[0][:lines]
28
+ else
29
+ reduced_source_lines = inc_replacements[0][:lines].flatten
30
+ end
27
31
  if doc.sourcemap
28
32
  logger = doc.logger
29
33
  opts = doc.options.merge logger: nil, parse: false, reduced: true
@@ -42,5 +46,37 @@ module Asciidoctor::Reducer
42
46
  end
43
47
  doc
44
48
  end
49
+
50
+ private
51
+
52
+ if RUBY_ENGINE == 'opal'
53
+ def flatten input_list
54
+ input_list.flatten
55
+ rescue ::Exception => e # rubocop:disable Lint/RescueException
56
+ raise unless e && %x(e.name) == 'RangeError'
57
+ result = []
58
+ stack = [[0, input_list, input_list.length]]
59
+ until stack.empty?
60
+ idx, list, len = stack.pop
61
+ while idx < len
62
+ if Array === (item = list[idx])
63
+ if (next_idx = idx + 1) < len
64
+ stack << [next_idx, list, len]
65
+ end
66
+ idx = 0
67
+ len = (list = item).length
68
+ else
69
+ result << item
70
+ idx += 1
71
+ end
72
+ end
73
+ end
74
+ result
75
+ end
76
+ else
77
+ def flatten input_list
78
+ input_list.flatten
79
+ end
80
+ end
45
81
  end
46
82
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Asciidoctor
4
4
  module Reducer
5
- VERSION = '1.1.0'
5
+ VERSION = '1.1.2'
6
6
  end
7
7
  end
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.1.0
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Allen
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-11-24 00:00:00.000000000 Z
11
+ date: 2025-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -87,7 +87,7 @@ metadata:
87
87
  changelog_uri: https://github.com/asciidoctor/asciidoctor-reducer/blob/main/CHANGELOG.adoc
88
88
  mailing_list_uri: https://chat.asciidoctor.org
89
89
  source_code_uri: https://github.com/asciidoctor/asciidoctor-reducer
90
- post_install_message:
90
+ post_install_message:
91
91
  rdoc_options: []
92
92
  require_paths:
93
93
  - lib
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  version: '0'
104
104
  requirements: []
105
105
  rubygems_version: 3.5.22
106
- signing_key:
106
+ signing_key:
107
107
  specification_version: 4
108
108
  summary: Reduces an AsciiDoc document containing includes and conditionals to a single
109
109
  AsciiDoc document.