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

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: 8e5bfad3f3b343a1fd2741938d31812feac3218e8b6998d43b3614b6800874b0
4
- data.tar.gz: 443bcb83342bb51cf3537b9b5cba497b7a1f5340eb7e7fb0956e85746344f917
3
+ metadata.gz: 30089ea8ab059ef848713ada56dd9457f4d6681c80de8028b92a9d02b21b0544
4
+ data.tar.gz: f38c68d1f15627a4d74fd5376fb3ec9a5b6ad29345c10ea882ff988c875922a9
5
5
  SHA512:
6
- metadata.gz: b52ca3132349b059d672bab08dd1b1c5a2586d88e8f4f31eb8d2ae7f0f9891917f0ee0cd46bd63826e7127cb8a64fda650bc5a13815760d299c06c6e5181e02b
7
- data.tar.gz: 6bdb49618d5adb7b1ce702d266ee8d6a229545b6ca8676931083df9362b21aaedabf539c7d1c4ea5c2fabecbb25426a9bcc140d80ffce93cd9b6da52bdc0e4cd
6
+ metadata.gz: a3ec843b92513164762d9647d963238073bff89c66619205166b26199a23e65d85ee5aaf23c2a60c953eae52d17ed36cbb7499dd7afb2040692912065171db12
7
+ data.tar.gz: df40e3f0c244ed21ea052536844df17f536c84a12252008d1c7dbdc04ad85ec00f3aabae81242550eda9a394497db386bd12b34f5110f323f61a8bdbc8ec30c6
data/CHANGELOG.adoc CHANGED
@@ -4,6 +4,26 @@
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.6 (2022-02-10) - @mojavelinux
8
+
9
+ === Added
10
+
11
+ * Add `-S`, `--safe-mode` option to CLI to set safe mode (#13)
12
+ * Add `-r`, `--require` option to CLI to specify additional libraries to require before running (#17)
13
+
14
+ === Changed
15
+
16
+ * Sort CLI options in help text, except for the `-h`, `--help` option
17
+ * Update CLI to always use a new logger instance
18
+ * Defer initializing logger until run method is called
19
+
20
+ === Fixed
21
+
22
+ * Replace include directives that follow an unresolved include (#19)
23
+ * Don't activate reducer extensions on reduced document
24
+ * Prevent custom extension registry from activating extensions twice during reload (#21)
25
+ * Retain includes table in document catalog when reloading document (#23)
26
+
7
27
  == 1.0.0.alpha.5 (2022-02-06) - @mojavelinux
8
28
 
9
29
  === Changed
@@ -36,15 +56,15 @@ For a detailed view of what has changed, refer to the {url-repo}/commits/main[co
36
56
 
37
57
  === Added
38
58
 
39
- * Add `-a` / `--attribute` option to CLI for setting an AsciiDoc document attribute at runtime (#6)
59
+ * Add `-a`, `--attribute` option to CLI for setting an AsciiDoc document attribute at runtime (#6)
40
60
 
41
61
  === Changed
42
62
 
43
- * Reduce preprocessor conditionals by default; add option (`--preserve-conditionals` / `:preserve_conditionals`) to preserve them (#8)
63
+ * Reduce preprocessor conditionals by default; add option (`--preserve-conditionals`, `:preserve_conditionals`) to preserve them (#8)
44
64
  * Don't enable sourcemap automatically (#4)
45
65
  * Don't override logger by default; instead, rely on `:logger` API option to change logger
46
66
  * Add `--log-level` option to CLI to set severity level on logger (#9)
47
- * Add `-q` / `--quiet` option to CLI to suppress log messages (#9)
67
+ * Add `-q`, `--quiet` option to CLI to suppress log messages (#9)
48
68
  * Reserve zero index in include replacements for top-level document
49
69
 
50
70
  === Fixed
@@ -74,3 +94,7 @@ Initial release.
74
94
  === Details
75
95
 
76
96
  {url-repo}/releases/tag/v1.0.0.alpha.5[git tag]
97
+
98
+ === Details
99
+
100
+ {url-repo}/releases/tag/v1.0.0.alpha.6[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.5, 2022-02-06
3
+ v1.0.0.alpha.6, 2022-02-10
4
4
  :idprefix:
5
5
  :idseparator: -
6
6
  ifndef::env-github[:icons: font]
@@ -23,14 +23,14 @@ If the document does not contain any preprocessor directives, the tool returns t
23
23
  == Prerequisites
24
24
 
25
25
  {project-name} is a Ruby program that you install using Ruby packaging.
26
- Therefore, to install and run {project-name}, you need Ruby 2.5 or better installed.
26
+ To install and run {project-name}, you need Ruby 2.5 or better.
27
27
 
28
- To check whether you have Ruby installed, and which version, run the following command:
28
+ Run the following command to check whether you have Ruby installed and to check which version:
29
29
 
30
30
  $ ruby -v
31
31
 
32
32
  If Ruby is not installed, you can install it using {url-rvm}[RVM] (or, if you prefer, the package manager for your system).
33
- We generally recommend using RVM because it allows you to install gems without requiring elevated privileges or messing with system libraries.
33
+ We generally recommend using RVM as it allows you to install gems without requiring elevated privileges or messing with system libraries.
34
34
 
35
35
  == Installation
36
36
 
@@ -75,7 +75,9 @@ asciidoctor-reducer [OPTION]... FILE
75
75
  ....
76
76
 
77
77
  The argument `FILE` is the AsciiDoc file you want to reduce.
78
- To use the command, pass the AsciiDoc file as the sole argument:
78
+ The options, represented by `[OPTION]...`, are optional, as the name suggestions.
79
+
80
+ Thus, to use the command, pass the AsciiDoc file as the sole argument:
79
81
 
80
82
  $ asciidoctor-reducer input.adoc
81
83
 
@@ -234,7 +236,7 @@ When running the `asciidoctor-reducer` command from source, you must prefix the
234
236
  [subs=attributes+]
235
237
  $ bundle exec asciidoctor-reducer sample.adoc
236
238
 
237
- To avoid having to do this, or make the `asciidoctor-reducer` command available from anywhere, you need to build the development gem and install it.
239
+ 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.
238
240
 
239
241
  == Copyright and License
240
242
 
@@ -7,8 +7,12 @@ module Asciidoctor::Reducer
7
7
  autoload :VERSION, (::File.join __dir__, 'version.rb')
8
8
 
9
9
  class Cli
10
+ LOG_LEVELS = (::Logger::Severity.constants false).each_with_object({}) do |level, accum|
11
+ accum[level.to_s.downcase] = (::Logger::Severity.const_get level) unless level == :UNKNOWN
12
+ end
13
+
10
14
  def parse args
11
- options = { attributes: {}, safe: :unsafe }
15
+ options = { attributes: {}, log_level: LOG_LEVELS['warn'], safe: :unsafe }
12
16
 
13
17
  opt_parser = ::OptionParser.new do |opts|
14
18
  opts.program_name = 'asciidoctor-reducer'
@@ -19,10 +23,6 @@ module Asciidoctor::Reducer
19
23
 
20
24
  EOS
21
25
 
22
- opts.on '-o FILE', '--output=FILE', 'set the output filename or stream' do |file|
23
- options[:output_file] = file
24
- end
25
-
26
26
  opts.on '-a KEY[=VALUE]', '--attribute=KEY[=VALUE]',
27
27
  'set a document attribute in the AsciiDoc document: [key, key!, key=value]' do |attr|
28
28
  key, val = attr.split '=', 2
@@ -30,28 +30,41 @@ module Asciidoctor::Reducer
30
30
  options[:attributes][key] = val
31
31
  end
32
32
 
33
+ opts.on '--log-level LEVEL', %w(debug info warn error fatal),
34
+ 'set the minimum level of messages to log: [debug, info, warn, error, fatal] (default: warn)' do |level|
35
+ options[:log_level] = level
36
+ end
37
+
38
+ opts.on '-o FILE', '--output=FILE', 'set the output filename or stream' do |file|
39
+ options[:output_file] = file
40
+ end
41
+
33
42
  opts.on '--preserve-conditionals', 'preserve preprocessor conditional directives in the reduced source' do
34
43
  options[:preserve_conditionals] = true
35
44
  end
36
45
 
37
- opts.on '--log-level LEVEL', %w(debug info warn error fatal),
38
- 'set the minimum level of messages to log: [debug, info, warn, error, fatal] (default: warn)' do |level|
39
- (options[:logger] = ::Asciidoctor::Logger.new $stderr).level = level unless level == 'warn'
46
+ opts.on '-q', '--quiet', 'suppress all application log messages' do
47
+ options[:log_level] = nil
40
48
  end
41
49
 
42
- opts.on '-q', '--quiet', 'suppress all application log messages' do
43
- options[:logger] = ::Asciidoctor::NullLogger.new
50
+ opts.on '-rLIBRARY', '--require LIBRARY', 'require the specified library or libraries before running' do |path|
51
+ (options[:requires] ||= []).concat path.split ','
44
52
  end
45
53
 
46
- opts.on '-h', '--help', 'display this help text and exit' do
47
- $stdout.write opts.help
48
- return 0
54
+ opts.on '-S', '--safe-mode SAFE_MODE', ['unsafe', 'safe', 'server'],
55
+ 'set supported safe mode level: [unsafe, safe, server] (default: unsafe)' do |name|
56
+ options[:safe] = ::Asciidoctor::SafeMode.value_for_name name
49
57
  end
50
58
 
51
59
  opts.on '-v', '--version', 'display the version information and exit' do
52
60
  $stdout.write %(#{opts.program_name} #{VERSION}\n)
53
61
  return 0
54
62
  end
63
+
64
+ opts.on '-h', '--help', 'display this help text and exit' do
65
+ $stdout.write opts.help
66
+ return 0
67
+ end
55
68
  end
56
69
 
57
70
  args = opt_parser.parse args
@@ -61,13 +74,21 @@ module Asciidoctor::Reducer
61
74
  $stdout.write opt_parser.help
62
75
  1
63
76
  elsif args.size == 1
77
+ if (requires = options.delete :requires)
78
+ requires.uniq.each do |path|
79
+ require path
80
+ rescue ::LoadError
81
+ $stderr.write %(#{opt_parser.program_name}: '#{path}' could not be required (reason: #{$!.message})\n)
82
+ return 1
83
+ end
84
+ end
64
85
  options[:input_file] = args[0]
65
86
  options[:output_file] = '-' unless options[:output_file]
66
87
  [0, options]
67
88
  else
68
89
  opt_parser.warn %(extra arguments detected (unparsed arguments: #{(args.drop 1).join ' '}))
69
90
  $stdout.write opt_parser.help
70
- [1, options]
91
+ 1
71
92
  end
72
93
  rescue ::OptionParser::InvalidOption
73
94
  $stderr.write %(#{opt_parser.program_name}: #{$!.message}\n)
@@ -76,9 +97,14 @@ module Asciidoctor::Reducer
76
97
  end
77
98
 
78
99
  def self.run args = ARGV
79
- old_logger = ::Asciidoctor::LoggerManager.logger
80
100
  code, options = new.parse (Array args)
81
101
  return code unless code == 0 && options
102
+ old_logger = ::Asciidoctor::LoggerManager.logger
103
+ if (log_level = options.delete :log_level)
104
+ (options[:logger] = ::Asciidoctor::Logger.new $stderr).level = log_level
105
+ else
106
+ options[:logger] = nil
107
+ end
82
108
  if (output_file = options.delete :output_file) == '-'
83
109
  to = $stdout
84
110
  else
@@ -87,7 +113,7 @@ module Asciidoctor::Reducer
87
113
  if (input_file = options.delete :input_file) == '-'
88
114
  reduced = (::Asciidoctor.load $stdin, options).source + ?\n
89
115
  else
90
- reduced = (::Asciidoctor.load_file input_file, (options.merge to_file: false)).source + ?\n
116
+ reduced = (::Asciidoctor.load_file input_file, options).source + ?\n
91
117
  end
92
118
  ::Pathname === to ? (to.write reduced, encoding: ::Encoding::UTF_8) : (to.write reduced)
93
119
  0
@@ -95,7 +121,7 @@ module Asciidoctor::Reducer
95
121
  $stderr.write %(asciidoctor-reducer: #{$!.message}\n)
96
122
  1
97
123
  ensure
98
- ::Asciidoctor::LoggerManager.logger = old_logger
124
+ ::Asciidoctor::LoggerManager.logger = old_logger if old_logger
99
125
  end
100
126
  end
101
127
  end
@@ -4,8 +4,8 @@ require_relative 'preprocessor_directive_tracker'
4
4
 
5
5
  module Asciidoctor::Reducer
6
6
  class Preprocessor < ::Asciidoctor::Extensions::Preprocessor
7
- def process doc, reader
8
- doc.options[:reduced] ? reader : (reader.extend PreprocessorDirectiveTracker)
7
+ def process _, reader
8
+ reader.extend PreprocessorDirectiveTracker
9
9
  end
10
10
  end
11
11
  end
@@ -12,7 +12,7 @@ module Asciidoctor::Reducer
12
12
  end
13
13
 
14
14
  def preprocess_conditional_directive keyword, target, delimiter, text
15
- return super if (opts = @document.options)[:preserve_conditionals] || opts[:reduced]
15
+ return super if @document.options[:preserve_conditionals]
16
16
  skip_active = @skipping
17
17
  depth = @conditional_stack.length
18
18
  cond_lineno = @lineno - 1
@@ -38,12 +38,10 @@ module Asciidoctor::Reducer
38
38
  @x_include_pushed = false
39
39
  inc_lineno = @lineno - 1 # we're currently on the include line, which is 1-based
40
40
  result = super
41
- if @x_include_pushed
42
- @x_include_directive_line = @x_include_pushed = nil
43
- return result
41
+ unless @x_include_pushed
42
+ unresolved = (l = peek_line true) && (l.start_with? 'Unresolved directive in ') && (l.end_with? ']') || nil
43
+ push_include_replacement inc_lineno, (unresolved ? [l] : []), unresolved
44
44
  end
45
- inc_lines = ((line = lines[0].to_s).start_with? 'Unresolved directive in ') && (line.end_with? ']') ? [line] : []
46
- push_include_replacement inc_lineno, inc_lines
47
45
  @x_include_directive_line = @x_include_pushed = nil
48
46
  result
49
47
  end
@@ -53,8 +51,7 @@ module Asciidoctor::Reducer
53
51
  inc_lineno = @lineno - 2 # we're below the include line, which is 1-based
54
52
  prev_inc_depth = @include_stack.length
55
53
  result = super
56
- inc_lines = lines if @include_stack.length > prev_inc_depth
57
- push_include_replacement inc_lineno, inc_lines
54
+ push_include_replacement inc_lineno, (@include_stack.length > prev_inc_depth ? lines : [])
58
55
  result
59
56
  end
60
57
 
@@ -65,15 +62,15 @@ module Asciidoctor::Reducer
65
62
 
66
63
  private
67
64
 
68
- def push_include_replacement inc_lineno, inc_lines
65
+ def push_include_replacement inc_lineno, inc_lines, unresolved = false
69
66
  @x_include_replacements << {
70
67
  into: @x_include_replacements.pos,
71
68
  lineno: inc_lineno,
72
69
  line: @x_include_directive_line,
73
- lines: inc_lines || [],
70
+ lines: inc_lines,
74
71
  drop: [],
75
72
  }
76
- @x_include_replacements.pos = @x_include_replacements.length - 1 if inc_lines
73
+ @x_include_replacements.pos = @x_include_replacements.length - 1 unless unresolved || inc_lines.empty?
77
74
  nil
78
75
  end
79
76
  end
@@ -3,7 +3,6 @@
3
3
  module Asciidoctor::Reducer
4
4
  class TreeProcessor < ::Asciidoctor::Extensions::TreeProcessor
5
5
  def process doc
6
- return if doc.options[:reduced]
7
6
  unless (inc_replacements = doc.reader.x_include_replacements).length == 1 && inc_replacements[0][:drop].empty?
8
7
  inc_replacements[0][:lines] = doc.source_lines.dup
9
8
  inc_replacements.reverse_each do |it|
@@ -21,7 +20,14 @@ module Asciidoctor::Reducer
21
20
  source_lines = inc_replacements[0][:lines].flatten
22
21
  if doc.sourcemap
23
22
  logger = ::Asciidoctor::LoggerManager.logger
24
- doc = ::Asciidoctor.load source_lines, (doc.options.merge logger: false, reduced: true)
23
+ opts = doc.options.merge logger: nil, parse: false, reduced: true
24
+ if (ext_reg = opts[:extension_registry])
25
+ opts[:extension_registry] = ::Asciidoctor::Extensions::Registry.new ext_reg.groups
26
+ end
27
+ includes = doc.catalog[:includes]
28
+ doc = ::Asciidoctor.load source_lines, opts
29
+ doc.catalog[:includes] = includes
30
+ doc.parse
25
31
  ::Asciidoctor::LoggerManager.logger = logger
26
32
  else
27
33
  source_lines.pop while (last = source_lines[-1]) && last.empty?
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Asciidoctor
4
4
  module Reducer
5
- VERSION = '1.0.0.alpha.5'
5
+ VERSION = '1.0.0.alpha.6'
6
6
  end
7
7
  end
@@ -4,6 +4,7 @@ require 'asciidoctor'
4
4
  require_relative 'reducer/extensions'
5
5
 
6
6
  Asciidoctor::Extensions.register :reducer do
7
+ next if document.options[:reduced]
7
8
  preprocessor Asciidoctor::Reducer::Preprocessor
8
9
  tree_processor Asciidoctor::Reducer::TreeProcessor
9
10
  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.5
4
+ version: 1.0.0.alpha.6
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-06 00:00:00.000000000 Z
11
+ date: 2022-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciidoctor