defmastership 1.0.17 → 1.0.19

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.
Files changed (79) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.gitlab-ci.yml +37 -11
  4. data/Gemfile +71 -1
  5. data/Guardfile +44 -0
  6. data/Rakefile +16 -61
  7. data/bin/defmastership +9 -6
  8. data/config/cucumber.yml +3 -0
  9. data/config/mutant.yml +27 -3
  10. data/config/reek.yml +129 -105
  11. data/config/rubocop.yml +72 -28
  12. data/defmastership.gemspec +5 -13
  13. data/features/changeref.feature +0 -8
  14. data/features/definition_checksum.feature +30 -10
  15. data/features/definition_version.feature +168 -10
  16. data/features/export.feature +64 -17
  17. data/features/modify.feature +1 -5
  18. data/features/rename_included_files.feature +27 -4
  19. data/features/step_definitions/git_steps.rb +19 -0
  20. data/lib/defmastership/batch_modifier.rb +17 -12
  21. data/lib/defmastership/change_ref_modifier.rb +88 -6
  22. data/lib/defmastership/comment_filter.rb +1 -1
  23. data/lib/defmastership/constants.rb +10 -7
  24. data/lib/defmastership/csv_formatter.rb +16 -12
  25. data/lib/defmastership/csv_formatter_body.rb +18 -15
  26. data/lib/defmastership/csv_formatter_header.rb +1 -1
  27. data/lib/defmastership/definition.rb +59 -20
  28. data/lib/defmastership/document.rb +101 -74
  29. data/lib/defmastership/matching_line.rb +17 -0
  30. data/lib/defmastership/modifier.rb +42 -0
  31. data/lib/defmastership/modifier_factory.rb +12 -0
  32. data/lib/defmastership/parsing_state.rb +15 -9
  33. data/lib/defmastership/rename_included_files_modifier.rb +172 -5
  34. data/lib/defmastership/set_join_hack.rb +11 -0
  35. data/lib/defmastership/update_def_checksum_modifier.rb +8 -13
  36. data/lib/defmastership/update_def_modifier.rb +49 -0
  37. data/lib/defmastership/update_def_version_modifier.rb +81 -15
  38. data/lib/defmastership/version.rb +1 -1
  39. data/lib/defmastership.rb +1 -6
  40. data/spec/spec_helper.rb +3 -1
  41. data/spec/unit/def_mastership/batch_modifier_spec.rb +40 -36
  42. data/spec/unit/def_mastership/change_ref_modifier_spec.rb +196 -51
  43. data/spec/unit/def_mastership/csv_formatter_body_spec.rb +60 -31
  44. data/spec/unit/def_mastership/csv_formatter_header_spec.rb +1 -1
  45. data/spec/unit/def_mastership/csv_formatter_spec.rb +79 -87
  46. data/spec/unit/def_mastership/definition_parser_spec.rb +1 -1
  47. data/spec/unit/def_mastership/definition_spec.rb +16 -6
  48. data/spec/unit/def_mastership/document_spec.rb +81 -38
  49. data/spec/unit/def_mastership/matching_line_spec.rb +37 -0
  50. data/spec/unit/def_mastership/modifier_factory_spec.rb +38 -0
  51. data/spec/unit/def_mastership/modifier_spec.rb +85 -0
  52. data/spec/unit/def_mastership/parsing_state_spec.rb +1 -1
  53. data/spec/unit/def_mastership/rename_included_files_modifier_spec.rb +219 -47
  54. data/spec/unit/def_mastership/string_spec.rb +1 -1
  55. data/spec/unit/def_mastership/update_def_checksum_modifier_spec.rb +82 -50
  56. data/spec/unit/def_mastership/update_def_modifier_spec.rb +121 -0
  57. data/spec/unit/def_mastership/update_def_version_modifier_spec.rb +327 -56
  58. data/tasks/code_quality.rake +74 -0
  59. data/tasks/console.rake +8 -0
  60. data/tasks/package.task +9 -0
  61. data/tasks/test.rake +30 -0
  62. metadata +33 -145
  63. data/.rubocop.yml +0 -76
  64. data/config/devtools.yml +0 -2
  65. data/config/flay.yml +0 -3
  66. data/config/flog.yml +0 -2
  67. data/config/yardstick.yml +0 -2
  68. data/cucumber.yml +0 -2
  69. data/lib/defmastership/change_ref_line_modifier.rb +0 -85
  70. data/lib/defmastership/line_modifier_base.rb +0 -29
  71. data/lib/defmastership/modifier_base.rb +0 -36
  72. data/lib/defmastership/rename_included_files_line_modifier.rb +0 -126
  73. data/lib/defmastership/update_def_checksum_line_modifier.rb +0 -38
  74. data/lib/defmastership/update_def_version_line_modifier.rb +0 -58
  75. data/spec/unit/def_mastership/change_ref_line_modifier_spec.rb +0 -250
  76. data/spec/unit/def_mastership/rename_included_files_line_modifier_spec.rb +0 -207
  77. data/spec/unit/def_mastership/update_def_checksum_line_modifier_spec.rb +0 -82
  78. data/spec/unit/def_mastership/update_def_version_line_modifier_spec.rb +0 -131
  79. /data/{.rspec → config/rspec} +0 -0
metadata CHANGED
@@ -1,183 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: defmastership
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.17
4
+ version: 1.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jérôme Arbez-Gindre
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-09 00:00:00.000000000 Z
11
+ date: 2024-07-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: aruba
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '2'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '2'
27
- - !ruby/object:Gem::Dependency
28
- name: cucumber
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '8'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '8'
41
- - !ruby/object:Gem::Dependency
42
- name: rake
14
+ name: aasm
43
15
  requirement: !ruby/object:Gem::Requirement
44
16
  requirements:
45
17
  - - "~>"
46
18
  - !ruby/object:Gem::Version
47
- version: '13'
48
- type: :development
19
+ version: '5'
20
+ type: :runtime
49
21
  prerelease: false
50
22
  version_requirements: !ruby/object:Gem::Requirement
51
23
  requirements:
52
24
  - - "~>"
53
25
  - !ruby/object:Gem::Version
54
- version: '13'
26
+ version: '5'
55
27
  - !ruby/object:Gem::Dependency
56
- name: rdoc
28
+ name: asciidoctor
57
29
  requirement: !ruby/object:Gem::Requirement
58
30
  requirements:
59
31
  - - "~>"
60
32
  - !ruby/object:Gem::Version
61
- version: '6'
62
- type: :development
33
+ version: '2'
34
+ type: :runtime
63
35
  prerelease: false
64
36
  version_requirements: !ruby/object:Gem::Requirement
65
37
  requirements:
66
38
  - - "~>"
67
39
  - !ruby/object:Gem::Version
68
- version: '6'
40
+ version: '2'
69
41
  - !ruby/object:Gem::Dependency
70
- name: rspec
42
+ name: csv
71
43
  requirement: !ruby/object:Gem::Requirement
72
44
  requirements:
73
45
  - - "~>"
74
46
  - !ruby/object:Gem::Version
75
47
  version: '3'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '3'
83
- - !ruby/object:Gem::Dependency
84
- name: rubocop
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - '='
88
- - !ruby/object:Gem::Version
89
- version: '1.50'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - '='
95
- - !ruby/object:Gem::Version
96
- version: '1.50'
97
- - !ruby/object:Gem::Dependency
98
- name: rubocop-performance
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: '1.17'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: '1.17'
111
- - !ruby/object:Gem::Dependency
112
- name: rubocop-rake
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - "~>"
116
- - !ruby/object:Gem::Version
117
- version: '0.6'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - "~>"
123
- - !ruby/object:Gem::Version
124
- version: '0.6'
125
- - !ruby/object:Gem::Dependency
126
- name: rubocop-rspec
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - "~>"
130
- - !ruby/object:Gem::Version
131
- version: '2.22'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - "~>"
137
- - !ruby/object:Gem::Version
138
- version: '2.22'
139
- - !ruby/object:Gem::Dependency
140
- name: simplecov
141
- requirement: !ruby/object:Gem::Requirement
142
- requirements:
143
- - - "~>"
144
- - !ruby/object:Gem::Version
145
- version: '0'
146
- type: :development
147
- prerelease: false
148
- version_requirements: !ruby/object:Gem::Requirement
149
- requirements:
150
- - - "~>"
151
- - !ruby/object:Gem::Version
152
- version: '0'
153
- - !ruby/object:Gem::Dependency
154
- name: aasm
155
- requirement: !ruby/object:Gem::Requirement
156
- requirements:
157
- - - "~>"
158
- - !ruby/object:Gem::Version
159
- version: '5'
160
48
  type: :runtime
161
49
  prerelease: false
162
50
  version_requirements: !ruby/object:Gem::Requirement
163
51
  requirements:
164
52
  - - "~>"
165
53
  - !ruby/object:Gem::Version
166
- version: '5'
54
+ version: '3'
167
55
  - !ruby/object:Gem::Dependency
168
- name: asciidoctor
56
+ name: git
169
57
  requirement: !ruby/object:Gem::Requirement
170
58
  requirements:
171
59
  - - "~>"
172
60
  - !ruby/object:Gem::Version
173
- version: '2'
61
+ version: '1'
174
62
  type: :runtime
175
63
  prerelease: false
176
64
  version_requirements: !ruby/object:Gem::Requirement
177
65
  requirements:
178
66
  - - "~>"
179
67
  - !ruby/object:Gem::Version
180
- version: '2'
68
+ version: '1'
181
69
  - !ruby/object:Gem::Dependency
182
70
  name: gli
183
71
  requirement: !ruby/object:Gem::Requirement
@@ -203,21 +91,17 @@ extra_rdoc_files:
203
91
  files:
204
92
  - ".gitignore"
205
93
  - ".gitlab-ci.yml"
206
- - ".rspec"
207
- - ".rubocop.yml"
208
94
  - Gemfile
95
+ - Guardfile
209
96
  - LICENSE
210
97
  - README.rdoc
211
98
  - Rakefile
212
99
  - bin/defmastership
213
- - config/devtools.yml
214
- - config/flay.yml
215
- - config/flog.yml
100
+ - config/cucumber.yml
216
101
  - config/mutant.yml
217
102
  - config/reek.yml
103
+ - config/rspec
218
104
  - config/rubocop.yml
219
- - config/yardstick.yml
220
- - cucumber.yml
221
105
  - defmastership.gemspec
222
106
  - defmastership.rdoc
223
107
  - features/changeref.feature
@@ -228,10 +112,10 @@ files:
228
112
  - features/modify.feature
229
113
  - features/rename_included_files.feature
230
114
  - features/step_definitions/defmastership_steps.rb
115
+ - features/step_definitions/git_steps.rb
231
116
  - features/support/env.rb
232
117
  - lib/defmastership.rb
233
118
  - lib/defmastership/batch_modifier.rb
234
- - lib/defmastership/change_ref_line_modifier.rb
235
119
  - lib/defmastership/change_ref_modifier.rb
236
120
  - lib/defmastership/comment_filter.rb
237
121
  - lib/defmastership/constants.rb
@@ -242,19 +126,18 @@ files:
242
126
  - lib/defmastership/definition_parser.rb
243
127
  - lib/defmastership/document.rb
244
128
  - lib/defmastership/filters.rb
245
- - lib/defmastership/line_modifier_base.rb
246
- - lib/defmastership/modifier_base.rb
129
+ - lib/defmastership/matching_line.rb
130
+ - lib/defmastership/modifier.rb
131
+ - lib/defmastership/modifier_factory.rb
247
132
  - lib/defmastership/parsing_state.rb
248
- - lib/defmastership/rename_included_files_line_modifier.rb
249
133
  - lib/defmastership/rename_included_files_modifier.rb
250
- - lib/defmastership/update_def_checksum_line_modifier.rb
134
+ - lib/defmastership/set_join_hack.rb
251
135
  - lib/defmastership/update_def_checksum_modifier.rb
252
- - lib/defmastership/update_def_version_line_modifier.rb
136
+ - lib/defmastership/update_def_modifier.rb
253
137
  - lib/defmastership/update_def_version_modifier.rb
254
138
  - lib/defmastership/version.rb
255
139
  - spec/spec_helper.rb
256
140
  - spec/unit/def_mastership/batch_modifier_spec.rb
257
- - spec/unit/def_mastership/change_ref_line_modifier_spec.rb
258
141
  - spec/unit/def_mastership/change_ref_modifier_spec.rb
259
142
  - spec/unit/def_mastership/csv_formatter_body_spec.rb
260
143
  - spec/unit/def_mastership/csv_formatter_header_spec.rb
@@ -262,15 +145,20 @@ files:
262
145
  - spec/unit/def_mastership/definition_parser_spec.rb
263
146
  - spec/unit/def_mastership/definition_spec.rb
264
147
  - spec/unit/def_mastership/document_spec.rb
148
+ - spec/unit/def_mastership/matching_line_spec.rb
149
+ - spec/unit/def_mastership/modifier_factory_spec.rb
150
+ - spec/unit/def_mastership/modifier_spec.rb
265
151
  - spec/unit/def_mastership/parsing_state_spec.rb
266
- - spec/unit/def_mastership/rename_included_files_line_modifier_spec.rb
267
152
  - spec/unit/def_mastership/rename_included_files_modifier_spec.rb
268
153
  - spec/unit/def_mastership/string_spec.rb
269
- - spec/unit/def_mastership/update_def_checksum_line_modifier_spec.rb
270
154
  - spec/unit/def_mastership/update_def_checksum_modifier_spec.rb
271
- - spec/unit/def_mastership/update_def_version_line_modifier_spec.rb
155
+ - spec/unit/def_mastership/update_def_modifier_spec.rb
272
156
  - spec/unit/def_mastership/update_def_version_modifier_spec.rb
273
157
  - spec/unit/def_mastership_spec.rb
158
+ - tasks/code_quality.rake
159
+ - tasks/console.rake
160
+ - tasks/package.task
161
+ - tasks/test.rake
274
162
  homepage: https://gitlab.com/jjag/defmastership/
275
163
  licenses:
276
164
  - MIT
@@ -295,7 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
295
183
  - !ruby/object:Gem::Version
296
184
  version: '0'
297
185
  requirements: []
298
- rubygems_version: 3.4.5
186
+ rubygems_version: 3.5.9
299
187
  signing_key:
300
188
  specification_version: 4
301
189
  summary: Handling of references and definitions with asciidoctor
data/.rubocop.yml DELETED
@@ -1,76 +0,0 @@
1
- # The behavior of RuboCop can be controlled via the .rubocop.yml
2
- # configuration file. It makes it possible to enable/disable
3
- # certain cops (checks) and to alter their behavior if they accept
4
- # any parameters. The file can be placed either in your home
5
- # directory or in some project directory.
6
- #
7
- # RuboCop will start looking for the configuration file in the directory
8
- # where the inspected file is and continue its way up to the root directory.
9
- #
10
- # See https://github.com/rubocop-hq/rubocop/blob/master/manual/configuration.md
11
-
12
- require:
13
- - rubocop-performance
14
- - rubocop-rspec
15
- - rubocop-rake
16
-
17
- AllCops:
18
- TargetRubyVersion: 2.7
19
- EnabledByDefault: true
20
- DisplayCopNames: true
21
-
22
- Style/Copyright:
23
- Enabled: true
24
- Notice: 'Copyright (\(c\) )?202[0-9] Jerome Arbez-Gindre'
25
- AutocorrectNotice: '# Copyright (c) 2023 Jerome Arbez-Gindre'
26
-
27
- Lint/ConstantResolution: # Not available ins rubocop 0.81
28
- Enabled: false
29
-
30
- Style/DocumentationMethod:
31
- Enabled: false
32
-
33
- Style/StringHashKeys :
34
- Enabled: true
35
- Exclude:
36
- - '*.gemspec'
37
- - 'spec/**/*'
38
-
39
- Style/MissingElse:
40
- EnforcedStyle: case
41
-
42
- Metrics/ModuleLength :
43
- Exclude:
44
- - 'spec/**/*'
45
-
46
- Metrics/BlockLength :
47
- Exclude:
48
- - 'spec/**/*'
49
- - '*.gemspec'
50
-
51
- Security/Eval :
52
- Exclude:
53
- - 'Rakefile'
54
-
55
- Style/ConstantVisibility :
56
- Exclude:
57
- # there is one unique explicit constant visibility for all
58
- # constants
59
- - 'lib/defmastership/constants.rb'
60
-
61
- # rubocop-rspec options
62
- RSpec/MessageExpectation :
63
- Enabled: true
64
-
65
- RSpec/FilePath :
66
- Enabled: true
67
-
68
- RSpec/NestedGroups:
69
- Max: 4
70
-
71
- Layout/RedundantLineBreak:
72
- Enabled: false
73
-
74
- Layout/EndOfLine:
75
- EnforcedStyle: lf
76
-
data/config/devtools.yml DELETED
@@ -1,2 +0,0 @@
1
- ---
2
- unit_test_timeout: 0.2
data/config/flay.yml DELETED
@@ -1,3 +0,0 @@
1
- ---
2
- threshold: 8
3
- total_score: 119
data/config/flog.yml DELETED
@@ -1,2 +0,0 @@
1
- ---
2
- threshold: 13.5
data/config/yardstick.yml DELETED
@@ -1,2 +0,0 @@
1
- ---
2
- threshold: 100
data/cucumber.yml DELETED
@@ -1,2 +0,0 @@
1
- ---
2
- default: --publish --format pretty
@@ -1,85 +0,0 @@
1
- # Copyright (c) 2020 Jerome Arbez-Gindre
2
- # frozen_string_literal: true
3
-
4
- require 'defmastership/constants'
5
- require 'defmastership/line_modifier_base'
6
-
7
- module DefMastership
8
- # Change references from temporary to definitive with multiple RefChangers
9
- class ChangeRefLineModifier < LineModifierBase
10
- def self.from_config(hash)
11
- new.from_config(hash)
12
- end
13
-
14
- REGEXP_FROM = {
15
- definition: {
16
- before: DMRegexp::DEF_BEFORE_REF,
17
- after: DMRegexp::DEF_AFTER_REF
18
- },
19
- iref: {
20
- before: DMRegexp::IREF_DEF_BEF,
21
- after: DMRegexp::IREF_DEF_AFT
22
- }
23
- }.freeze
24
-
25
- private_constant :REGEXP_FROM
26
-
27
- def initialize
28
- super
29
- @config = {
30
- from_regexp: '',
31
- to_template: '',
32
- next_ref: 0
33
- }
34
- @parsing_state = ParsingState.new
35
- end
36
-
37
- def replace(method, line)
38
- public_send("replace_#{method}".to_sym, line)
39
- end
40
-
41
- def replace_refdef(line)
42
- if @parsing_state.enabled?(line)
43
- do_replace_refdef(line)
44
- else
45
- line
46
- end
47
- end
48
-
49
- def replace_irefs(line)
50
- changes.reduce(line) do |res_line, (from, to)|
51
- res_line.gsub(regexp_from(:iref, from)) do
52
- text_with(Regexp.last_match, to)
53
- end
54
- end
55
- end
56
-
57
- private
58
-
59
- def do_replace_refdef(line)
60
- line.gsub(regexp_from(:definition, from_regexp)) do
61
- replacement = to_template % hmerge(Regexp.last_match)
62
- changes << [Regexp.last_match[:from], replacement]
63
- @config[:next_ref] += 1
64
- text_with(Regexp.last_match, replacement)
65
- end
66
- end
67
-
68
- def regexp_from(const, from)
69
- regexp_str =
70
- "(?<before>#{REGEXP_FROM[const][:before]})" \
71
- "(?<from>#{from})" \
72
- "#{DMRegexp::DEF_VERSION_AND_CHECKSUM}" \
73
- "(?<after>#{REGEXP_FROM[const][:after]})"
74
- Regexp.new(regexp_str, Regexp::EXTENDED)
75
- end
76
-
77
- def text_with(match, replacement)
78
- match[:before] + replacement + (match[:version_and_checksum] || '') + match[:after]
79
- end
80
-
81
- def hmerge(match)
82
- @config.merge(match.names.map(&:to_sym).zip(match.captures).to_h)
83
- end
84
- end
85
- end
@@ -1,29 +0,0 @@
1
- # Copyright (c) 2020 Jerome Arbez-Gindre
2
- # frozen_string_literal: true
3
-
4
- module DefMastership
5
- # Change references from temporary to definitive with multiple RefChangers
6
- class LineModifierBase
7
- attr_reader :changes, :config
8
-
9
- def initialize
10
- @config = {}
11
- @changes = []
12
- end
13
-
14
- def method_missing(method_name, *args, &block)
15
- return @config[method_name] if @config[method_name]
16
-
17
- super
18
- end
19
-
20
- def respond_to_missing?(method_name, *args)
21
- @config[method_name] || super
22
- end
23
-
24
- def from_config(config)
25
- @config.merge!(config)
26
- self
27
- end
28
- end
29
- end
@@ -1,36 +0,0 @@
1
- # Copyright (c) 2020 Jerome Arbez-Gindre
2
- # frozen_string_literal: true
3
-
4
- module DefMastership
5
- # Change references from temporary to definitive with multiple RefChangers
6
- class ModifierBase
7
- attr_reader :config, :changes
8
-
9
- def initialize(config)
10
- @config = config
11
- @changes = []
12
- end
13
-
14
- def do_modifications(adoc_texts)
15
- line_modifier = new_line_modifier(@config, adoc_texts)
16
-
17
- adoc_texts =
18
- replacements.reduce(adoc_texts) do |texts, method|
19
- transform_values(texts, line_modifier, method)
20
- end
21
-
22
- @config = line_modifier.config
23
- @changes = line_modifier.changes
24
- adoc_texts
25
- end
26
-
27
- private
28
-
29
- def transform_values(texts, line_modifier, method)
30
- texts.transform_values do |text|
31
- text.lines.map { |line| line_modifier.public_send(method, line) }
32
- .join
33
- end
34
- end
35
- end
36
- end
@@ -1,126 +0,0 @@
1
- # Copyright (c) 2020 Jerome Arbez-Gindre
2
- # frozen_string_literal: true
3
-
4
- module DefMastership
5
- # Change included filenames on one line at a time
6
- class RenameIncludedFilesLineModifier < LineModifierBase
7
- LOCAL_FILTERS = [
8
- Filter.new(DMRegexp::VARIABLE_DEF, :new_variable_def, false),
9
- Filter.new(DMRegexp::DEFINITION, :new_definition, true),
10
- Filter.new(DMRegexp::BLOCK, :block_delimiter, true),
11
- Filter.new(DMRegexp::EMPTY_LINE, :empty_line, false),
12
- Filter.new(DMRegexp::WHATEVER, :new_line, true)
13
- ].freeze
14
- private_constant :LOCAL_FILTERS
15
-
16
- def self.from_config(config)
17
- new.from_config(config)
18
- end
19
-
20
- def initialize
21
- super
22
- @config = {
23
- from_regexp: '',
24
- to_template: ''
25
- }
26
- @variables = {}
27
- @definition_parser = DefinitionParser.new(self)
28
- end
29
-
30
- def replace(line)
31
- match = matched?(line)
32
-
33
- return line unless match
34
-
35
- new_line =
36
- line.gsub(complete_regexp_from(from_regexp)) do
37
- text_with(match, to_template % hmerge(match))
38
- end
39
-
40
- rename_file(line, new_line)
41
-
42
- new_line
43
- end
44
-
45
- def add_new_definition(match, _line)
46
- @config[:reference] = match[:reference]
47
- end
48
-
49
- def add_line(_match, _line) end
50
-
51
- def new_variable_def(match, line)
52
- @variables[match[:varname].to_sym] = match[:value]
53
- line
54
- end
55
-
56
- private
57
-
58
- def matched?(line)
59
- return if line.commented?
60
-
61
- parse(line)
62
-
63
- return if @definition_parser.idle?
64
- return unless line =~ complete_regexp_from(from_regexp)
65
-
66
- match = Regexp.last_match
67
-
68
- return if @config[:cancel_if_match] && match[:filename] =~ Regexp.new(cancel_if_match)
69
-
70
- match
71
- end
72
-
73
- def parse(line)
74
- LOCAL_FILTERS.each do |filter|
75
- next unless line.match(filter.regexp)
76
-
77
- line = generate_event(filter.event, Regexp.last_match, line)
78
-
79
- break if filter.consumed_line
80
- end
81
- end
82
-
83
- def rename_file(from, to)
84
- @changes << [extract_filename(from), extract_filename(to)]
85
- File.rename(extract_filename(from), extract_filename(to))
86
- end
87
-
88
- def extract_filename(include_statement)
89
- filename = include_statement
90
- .gsub(Regexp.new(DMRegexp::INCLUDE_KEYWORD), '')
91
- .gsub(Regexp.new(DMRegexp::INCLUDE_OPTIONS), '')
92
-
93
- filename.dup.scan(DMRegexp::VARIABLE_USE) do |_|
94
- varname = Regexp.last_match[:varname]
95
- next if @variables[varname.to_sym].nil?
96
-
97
- filename.gsub!("{#{varname}}", @variables[varname.to_sym])
98
- end
99
- filename.chomp
100
- end
101
-
102
- def complete_regexp_from(from)
103
- Regexp.new(
104
- DMRegexp::INCLUDE_KEYWORD + DMRegexp::INCLUDE_PATH +
105
- "(?<filename>#{from})" + DMRegexp::INCLUDE_OPTIONS
106
- )
107
- end
108
-
109
- def text_with(match, to)
110
- "include::#{match[:path] || ''}#{to}[#{match[:options]}]"
111
- end
112
-
113
- def hmerge(match)
114
- @config.merge(match.names.map(&:to_sym).zip(match.captures).to_h)
115
- end
116
-
117
- def generate_event(event, match, line)
118
- if respond_to?(event)
119
- public_send(event, match, line)
120
- else
121
- @definition_parser.public_send(event, match, line)
122
- end
123
- line
124
- end
125
- end
126
- end