asciidoctor-reducer 1.0.0.alpha.2 → 1.0.0.alpha.3

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: 4345ff2d6a81a560e1ed5bca19109d14ae5b230e087021876377ad4ded4e80e6
4
- data.tar.gz: 2c52ac147e3388f7f04fb2adfa89b1653b890e4a208f2eac698821853e6a0648
3
+ metadata.gz: 4b0c9530e403be351428ec664c9a5d763a0f27686505e8328d831f46f9ad580b
4
+ data.tar.gz: ea5116a19ea8e1199415f5e50ee9339dcec66b87b2c574f60306c8b68e9083b8
5
5
  SHA512:
6
- metadata.gz: 35cbaa540aa6b5a01b830f3e7a687718ae48cdc005321d0a90025427c32a5e06d5d6a9ff6c1b670d659508a95e16bce58550b0d90b8f844dd435c044fd14a309
7
- data.tar.gz: 42808122280abc28294df1c0498e4a33fd2943c638202fa5fa4978a762b58cdb05d5d029b555826009ac89c507ed793925368fee0c287eddfdfd5d4799a7d51d
6
+ metadata.gz: c3f8025d47b14f4701c4883151403e7fc1aa498cb3d82b9180f7935e14e2aa95a149517e9b037f92d4086d01bd1133aae3088158018ecd088de8f0dc221d5602
7
+ data.tar.gz: 79ee7b026d7de3f74b2423e193b85c3e0d8d1e1a783e27af4e838522f13c7712871c1379556757ebc432999a0625590d8e03c80d6ef5259104c088a412fa8d25
data/CHANGELOG.adoc CHANGED
@@ -4,6 +4,15 @@
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.3 (2022-02-02) - @mojavelinux
8
+
9
+ === Changed
10
+
11
+ * Rename PreprocessorReader ext module to PreprocessorReaderTracker
12
+ * Encapsulate logic to enhance PreprocessorReader inside PreprocessorReaderTracker module
13
+ * Only reload document if source lines have changed; otherwise, update source lines on reader directly
14
+ * Change default safe mode for CLI to :unsafe
15
+
7
16
  == 1.0.0.alpha.2 (2022-01-27) - @mojavelinux
8
17
 
9
18
  === Added
@@ -34,3 +43,7 @@ Initial release.
34
43
  === Details
35
44
 
36
45
  {url-repo}/releases/tag/v1.0.0.alpha.2[git tag]
46
+
47
+ === Details
48
+
49
+ {url-repo}/releases/tag/v1.0.0.alpha.3[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.2, 2022-01-27
3
+ v1.0.0.alpha.3, 2022-02-02
4
4
  :idprefix:
5
5
  :idseparator: -
6
6
  ifndef::env-github[:icons: font]
@@ -80,7 +80,7 @@ To use the command, pass the AsciiDoc file as the sole argument:
80
80
  $ asciidoctor-reducer input.adoc
81
81
 
82
82
  By default, the command will output the reduced AsciiDoc document to the terminal (via stdout).
83
- To write the output to a file, specify an output file using the `-o` option.
83
+ To write the output to a file, specify an output file using the `-o` option:
84
84
 
85
85
  $ asciidoctor-reducer -o output.adoc input.adoc
86
86
 
@@ -89,7 +89,7 @@ To use the command in this way, pass `-` as the first argument:
89
89
 
90
90
  $ cat input.adoc | asciidoctor-reducer -
91
91
 
92
- To write the output to a file, also specify the `-o` option:
92
+ To write the output to a file instead of stdout, also specify an output file using the `-o` option:
93
93
 
94
94
  $ cat input.adoc | asciidoctor-reducer -o output.adoc -
95
95
 
@@ -137,6 +137,14 @@ As it goes, it also removes lines that have been excluded by the preprocessor co
137
137
  Finally, it loads the document again and returns it.
138
138
  The reduced source is available on the reconstructed document (via `Document#source` or `Document#source_lines`).
139
139
 
140
+ === Impact on Extensions
141
+
142
+ If the sourcemap is enabled, and the reducer finds lines to replace or filter, the reducer will load the document again using `Asciidoctor.load`.
143
+ This step is necessary to synchronize the sourcemap with the reduced source.
144
+ This call will cause extensions that run during the load phase to be invoked again.
145
+ An extension can check for this secondary load by checking for the `:reduced` option in the `Document#options` hash.
146
+ If this option is set (the value of which will be `true`), then Asciidoctor is loading the reduced document.
147
+
140
148
  == Development
141
149
 
142
150
  Follow the instructions below to learn how to help develop the project or test-drive the development version.
@@ -8,7 +8,7 @@ module Asciidoctor::Reducer
8
8
 
9
9
  class Cli
10
10
  def parse args
11
- options = { attributes: {}, safe: :safe }
11
+ options = { attributes: {}, safe: :unsafe }
12
12
 
13
13
  opt_parser = ::OptionParser.new do |opts|
14
14
  opts.program_name = 'asciidoctor-reducer'
@@ -91,7 +91,7 @@ module Asciidoctor::Reducer
91
91
  end
92
92
  ::Pathname === to ? (to.write reduced, encoding: ::Encoding::UTF_8) : (to.write reduced)
93
93
  0
94
- rescue ::IOError
94
+ rescue
95
95
  $stderr.write %(asciidoctor-reducer: #{$!.message}\n)
96
96
  1
97
97
  ensure
@@ -1,23 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'ext/asciidoctor/preprocessor_reader'
3
+ require_relative 'preprocessor_directive_tracker'
4
4
 
5
5
  module Asciidoctor::Reducer
6
6
  class Preprocessor < ::Asciidoctor::Extensions::Preprocessor
7
7
  def process doc, reader
8
- return if doc.options[:reduced]
9
- reader.singleton_class.prepend AsciidoctorExt::PreprocessorReader
10
- reader.instance_variable_set :@x_include_replacements, ([{ drop: [] }].extend Current)
11
- reader.instance_variable_set :@x_parents, [0]
12
- nil
13
- end
14
- end
15
-
16
- module Current
17
- attr_accessor :current
18
-
19
- def self.extended obj
20
- obj.current = obj[-1]
8
+ doc.options[:reduced] ? reader : (reader.extend PreprocessorDirectiveTracker)
21
9
  end
22
10
  end
23
11
  end
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Asciidoctor::Reducer
4
+ module PreprocessorDirectiveTracker
5
+ attr_writer :source_lines
6
+ attr_reader :x_include_replacements
7
+
8
+ def self.extended instance
9
+ instance.instance_variable_set :@x_include_replacements, ([{ drop: [] }].extend CurrentPointer)
10
+ instance.instance_variable_set :@x_parents, [0]
11
+ end
12
+
13
+ def preprocess_conditional_directive keyword, target, delimiter, text
14
+ return super if (opts = @document.options)[:preserve_conditionals] || opts[:reduced]
15
+ skip_active = @skipping
16
+ depth = @conditional_stack.length
17
+ cond_lineno = @lineno - 1
18
+ result = super
19
+ return result if @skipping && skip_active
20
+ drop = @x_include_replacements.current[:drop]
21
+ if (depth_change = @conditional_stack.length - depth) < 0
22
+ if skip_active
23
+ drop.push(*(drop.pop..cond_lineno))
24
+ else
25
+ drop << cond_lineno
26
+ end
27
+ elsif depth_change > 0 || cond_lineno == @lineno - 1
28
+ drop << cond_lineno
29
+ else
30
+ drop << [cond_lineno, text]
31
+ end
32
+ result
33
+ end
34
+
35
+ def preprocess_include_directive target, attrlist
36
+ @x_include_directive_line = %(include::#{target}[#{attrlist}])
37
+ @x_push_include_called = false
38
+ inc_lineno = @lineno - 1 # we're currently on the include line, which is 1-based
39
+ result = super
40
+ return result if @x_push_include_called
41
+ parent_depth = (parents = @x_parents).length
42
+ if (depth_change = @include_stack.length - (parent_depth - 1)) < 0
43
+ parent_depth -= (parents.slice! parent_depth + depth_change, -depth_change).length
44
+ end
45
+ inc_lines = ((line = lines[0].to_s).start_with? 'Unresolved directive in ') && (line.end_with? ']') ? [line] : []
46
+ push_include_replacement inc_lines, parent_depth, inc_lineno
47
+ result
48
+ end
49
+
50
+ def push_include data, file, path, lineno, attrs
51
+ @x_push_include_called = true
52
+ inc_lineno = @lineno - 2 # we're below the include line, which is 1-based
53
+ prev_inc_depth = @include_stack.length
54
+ # Q: can we do this without resetting the lineno?
55
+ lineno = 1 # rubocop:disable Lint/ShadowedArgument
56
+ super
57
+ parent_depth = (parents = @x_parents).length
58
+ # push_include did not push to the stack
59
+ if (inc_depth = @include_stack.length) == prev_inc_depth
60
+ if (depth_change = inc_depth - (parent_depth - 1)) < 0
61
+ parent_depth -= (parents.slice! parent_depth + depth_change, -depth_change).length
62
+ end
63
+ else
64
+ if (depth_change = inc_depth - parent_depth) > 0
65
+ parents << @x_include_replacements.length.pred
66
+ parent_depth += 1
67
+ elsif depth_change < 0
68
+ parent_depth -= (parents.slice! parent_depth + depth_change, -depth_change).length
69
+ end
70
+ inc_lines = lines
71
+ end
72
+ push_include_replacement inc_lines, parent_depth, inc_lineno
73
+ self
74
+ end
75
+
76
+ def pop_include
77
+ @x_include_replacements.current = @x_include_replacements[@x_include_replacements.current[:into] || 0]
78
+ super
79
+ end
80
+
81
+ private
82
+
83
+ def push_include_replacement inc_lines, parent_depth, inc_lineno
84
+ @x_include_replacements << {
85
+ lines: inc_lines || [],
86
+ drop: [],
87
+ into: @x_parents[parent_depth - 1],
88
+ index: inc_lineno,
89
+ replace: @x_include_directive_line,
90
+ }
91
+ @x_include_replacements.current = @x_include_replacements[-1] if inc_lines
92
+ nil
93
+ end
94
+ end
95
+
96
+ module CurrentPointer
97
+ attr_accessor :current
98
+
99
+ def self.extended instance
100
+ instance.current = instance[-1]
101
+ end
102
+ end
103
+ end
@@ -4,7 +4,7 @@ module Asciidoctor::Reducer
4
4
  class TreeProcessor < ::Asciidoctor::Extensions::TreeProcessor
5
5
  def process doc
6
6
  return if doc.options[:reduced]
7
- inc_replacements = doc.reader.instance_variable_get :@x_include_replacements
7
+ inc_replacements = doc.reader.x_include_replacements
8
8
  unless inc_replacements.length == 1 && inc_replacements[0][:drop].empty?
9
9
  inc_replacements[0][:lines] = doc.source_lines.dup
10
10
  inc_replacements.reverse_each do |it|
@@ -19,8 +19,13 @@ module Asciidoctor::Reducer
19
19
  end
20
20
  target_lines[index] = lines if target_lines
21
21
  end
22
- # WARNING: if include directives remain that can still be resolved, the sourcemap won't match the source lines
23
- doc = ::Asciidoctor.load inc_replacements[0][:lines].flatten, (doc.options.merge reduced: true)
22
+ source_lines = inc_replacements[0][:lines].flatten
23
+ if doc.sourcemap
24
+ # WARNING: if include directives remain that can still be resolved, the sourcemap won't match the source lines
25
+ doc = ::Asciidoctor.load source_lines, (doc.options.merge reduced: true)
26
+ else
27
+ doc.reader.source_lines = source_lines
28
+ end
24
29
  end
25
30
  doc
26
31
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Asciidoctor
4
4
  module Reducer
5
- VERSION = '1.0.0.alpha.2'
5
+ VERSION = '1.0.0.alpha.3'
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.0.0.alpha.2
4
+ version: 1.0.0.alpha.3
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-01-27 00:00:00.000000000 Z
11
+ date: 2022-02-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor
@@ -69,9 +69,9 @@ files:
69
69
  - lib/asciidoctor-reducer.rb
70
70
  - lib/asciidoctor/reducer.rb
71
71
  - lib/asciidoctor/reducer/cli.rb
72
- - lib/asciidoctor/reducer/ext/asciidoctor/preprocessor_reader.rb
73
72
  - lib/asciidoctor/reducer/extensions.rb
74
73
  - lib/asciidoctor/reducer/preprocessor.rb
74
+ - lib/asciidoctor/reducer/preprocessor_directive_tracker.rb
75
75
  - lib/asciidoctor/reducer/tree_processor.rb
76
76
  - lib/asciidoctor/reducer/version.rb
77
77
  homepage: https://asciidoctor.org
@@ -1,89 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Asciidoctor::Reducer
4
- module AsciidoctorExt
5
- module PreprocessorReader
6
- def preprocess_conditional_directive keyword, target, delimiter, text
7
- return super if (opts = @document.options)[:preserve_conditionals] || opts[:reduced]
8
- skip_active = @skipping
9
- depth = @conditional_stack.length
10
- cond_lineno = @lineno - 1
11
- result = super
12
- return result if @skipping && skip_active
13
- drop = @x_include_replacements.current[:drop]
14
- if (depth_change = @conditional_stack.length - depth) < 0
15
- if skip_active
16
- drop.push(*(drop.pop..cond_lineno))
17
- else
18
- drop << cond_lineno
19
- end
20
- elsif depth_change > 0 || cond_lineno == @lineno - 1
21
- drop << cond_lineno
22
- else
23
- drop << [cond_lineno, text]
24
- end
25
- result
26
- end
27
-
28
- def preprocess_include_directive target, attrlist
29
- @x_include_directive_line = %(include::#{target}[#{attrlist}])
30
- @x_push_include_called = false
31
- inc_lineno = @lineno - 1 # we're currently on the include line, which is 1-based
32
- result = super
33
- return result if @x_push_include_called
34
- parent_depth = (parents = @x_parents).length
35
- if (depth_change = @include_stack.length - (parent_depth - 1)) < 0
36
- parent_depth -= (parents.slice! parent_depth + depth_change, -depth_change).length
37
- end
38
- lines = ((line = @lines[-1].to_s).start_with? 'Unresolved directive in ') && (line.end_with? ']') ? [line] : []
39
- push_include_replacement lines, parent_depth, inc_lineno
40
- result
41
- end
42
-
43
- def push_include data, file, path, lineno, attrs
44
- @x_push_include_called = true
45
- inc_lineno = @lineno - 2 # we're below the include line, which is 1-based
46
- prev_inc_depth = @include_stack.length
47
- # Q: can we do this without resetting the lineno?
48
- lineno = 1 # rubocop:disable Lint/ShadowedArgument
49
- super
50
- parent_depth = (parents = @x_parents).length
51
- # push_include did not push to the stack
52
- if (inc_depth = @include_stack.length) == prev_inc_depth
53
- if (depth_change = inc_depth - (parent_depth - 1)) < 0
54
- parent_depth -= (parents.slice! parent_depth + depth_change, -depth_change).length
55
- end
56
- else
57
- if (depth_change = inc_depth - parent_depth) > 0
58
- parents << @x_include_replacements.length.pred
59
- parent_depth += 1
60
- elsif depth_change < 0
61
- parent_depth -= (parents.slice! parent_depth + depth_change, -depth_change).length
62
- end
63
- lines = @lines.reverse
64
- end
65
- push_include_replacement lines, parent_depth, inc_lineno
66
- self
67
- end
68
-
69
- def pop_include
70
- @x_include_replacements.current = @x_include_replacements[@x_include_replacements.current[:into] || 0]
71
- super
72
- end
73
-
74
- private
75
-
76
- def push_include_replacement lines, parent_depth, inc_lineno
77
- @x_include_replacements << {
78
- lines: lines || [],
79
- drop: [],
80
- into: @x_parents[parent_depth - 1],
81
- index: inc_lineno,
82
- replace: @x_include_directive_line,
83
- }
84
- @x_include_replacements.current = @x_include_replacements[-1] if lines
85
- nil
86
- end
87
- end
88
- end
89
- end