defmastership 1.0.19 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.gitlab-ci.yml +27 -0
- data/Gemfile +27 -16
- data/LICENSE +1 -1
- data/README.adoc +24 -0
- data/Rakefile +0 -1
- data/bin/defmastership +5 -5
- data/config/mutant.yml +18 -16
- data/config/rubocop.yml +5 -10
- data/defmastership.gemspec +3 -3
- data/lib/defmastership/batch_modifier.rb +23 -5
- data/lib/defmastership/comment_filter.rb +2 -1
- data/lib/defmastership/definition.rb +24 -4
- data/lib/defmastership/definition_parser.rb +2 -2
- data/lib/defmastership/document.rb +39 -19
- data/lib/defmastership/export/body_formatter.rb +55 -0
- data/lib/defmastership/export/csv/formatter.rb +64 -0
- data/lib/defmastership/export/header_formatter.rb +51 -0
- data/lib/defmastership/filters.rb +15 -12
- data/lib/defmastership/matching_line.rb +3 -2
- data/lib/defmastership/modifier/change_ref.rb +117 -0
- data/lib/defmastership/modifier/factory.rb +17 -0
- data/lib/defmastership/modifier/modifier_common.rb +71 -0
- data/lib/defmastership/modifier/rename_included_files.rb +223 -0
- data/lib/defmastership/modifier/update_def.rb +72 -0
- data/lib/defmastership/modifier/update_def_checksum.rb +17 -0
- data/lib/defmastership/modifier/update_def_version.rb +110 -0
- data/lib/defmastership/set_join_hack.rb +2 -0
- data/lib/defmastership/version.rb +3 -2
- data/lib/defmastership.rb +6 -8
- data/spec/unit/{def_mastership → defmastership}/batch_modifier_spec.rb +13 -13
- data/spec/unit/{def_mastership → defmastership}/definition_parser_spec.rb +1 -1
- data/spec/unit/{def_mastership → defmastership}/definition_spec.rb +1 -1
- data/spec/unit/{def_mastership → defmastership}/document_spec.rb +57 -57
- data/spec/unit/{def_mastership/csv_formatter_body_spec.rb → defmastership/export/body_formatter_spec.rb} +4 -4
- data/spec/unit/{def_mastership/csv_formatter_spec.rb → defmastership/export/csv/formatter_spec.rb} +13 -8
- data/spec/unit/{def_mastership/csv_formatter_header_spec.rb → defmastership/export/header_formatter_spec.rb} +3 -3
- data/spec/unit/{def_mastership → defmastership}/matching_line_spec.rb +1 -1
- data/spec/unit/{def_mastership/change_ref_modifier_spec.rb → defmastership/modifier/change_ref_spec.rb} +19 -44
- data/spec/unit/defmastership/modifier/factory_spec.rb +45 -0
- data/spec/unit/{def_mastership/modifier_spec.rb → defmastership/modifier/modifier_common_spec.rb} +3 -12
- data/spec/unit/{def_mastership/rename_included_files_modifier_spec.rb → defmastership/modifier/rename_included_files_spec.rb} +3 -3
- data/spec/unit/{def_mastership/update_def_checksum_modifier_spec.rb → defmastership/modifier/update_def_checksum_spec.rb} +10 -10
- data/spec/unit/{def_mastership/update_def_modifier_spec.rb → defmastership/modifier/update_def_spec.rb} +22 -20
- data/spec/unit/{def_mastership/update_def_version_modifier_spec.rb → defmastership/modifier/update_def_version_spec.rb} +38 -38
- data/spec/unit/{def_mastership_spec.rb → defmastership_spec.rb} +2 -2
- data/tasks/documentation.rake +19 -0
- data/tasks/package.rake +4 -0
- metadata +63 -43
- data/README.rdoc +0 -6
- data/defmastership.rdoc +0 -5
- data/lib/defmastership/change_ref_modifier.rb +0 -99
- data/lib/defmastership/constants.rb +0 -91
- data/lib/defmastership/csv_formatter.rb +0 -53
- data/lib/defmastership/csv_formatter_body.rb +0 -46
- data/lib/defmastership/csv_formatter_header.rb +0 -41
- data/lib/defmastership/modifier.rb +0 -42
- data/lib/defmastership/modifier_factory.rb +0 -12
- data/lib/defmastership/parsing_state.rb +0 -31
- data/lib/defmastership/rename_included_files_modifier.rb +0 -182
- data/lib/defmastership/update_def_checksum_modifier.rb +0 -16
- data/lib/defmastership/update_def_modifier.rb +0 -49
- data/lib/defmastership/update_def_version_modifier.rb +0 -91
- data/spec/unit/def_mastership/modifier_factory_spec.rb +0 -38
- data/spec/unit/def_mastership/parsing_state_spec.rb +0 -62
- data/tasks/package.task +0 -9
- /data/spec/unit/{def_mastership → defmastership}/string_spec.rb +0 -0
@@ -1,99 +0,0 @@
|
|
1
|
-
# Copyright (c) 2020 Jerome Arbez-Gindre
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require 'defmastership/constants'
|
5
|
-
require 'defmastership/modifier'
|
6
|
-
|
7
|
-
module DefMastership
|
8
|
-
# Change references from temporary to definitive with multiple RefChangers
|
9
|
-
class ChangeRefModifier
|
10
|
-
include Modifier
|
11
|
-
|
12
|
-
REGEXP_FROM = {
|
13
|
-
definition: {
|
14
|
-
before: DMRegexp::DEF_BEFORE_REF,
|
15
|
-
after: DMRegexp::DEF_AFTER_REF
|
16
|
-
},
|
17
|
-
iref: {
|
18
|
-
before: DMRegexp::IREF_DEF_BEF,
|
19
|
-
after: DMRegexp::IREF_DEF_AFT
|
20
|
-
}
|
21
|
-
}.freeze
|
22
|
-
|
23
|
-
private_constant :REGEXP_FROM
|
24
|
-
|
25
|
-
def self.replacement_methods
|
26
|
-
%i[replace_refdef replace_irefs]
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.default_config
|
30
|
-
{
|
31
|
-
from_regexp: '',
|
32
|
-
to_template: '',
|
33
|
-
next_ref: 0
|
34
|
-
}
|
35
|
-
end
|
36
|
-
|
37
|
-
def initialize(config)
|
38
|
-
@parsing_state = ParsingState.new
|
39
|
-
|
40
|
-
setup_modifier_module(config)
|
41
|
-
end
|
42
|
-
|
43
|
-
def replace(method, line)
|
44
|
-
public_send(:"replace_#{method}", line)
|
45
|
-
end
|
46
|
-
|
47
|
-
def replace_refdef(line)
|
48
|
-
if @parsing_state.enabled?(line)
|
49
|
-
do_replace_refdef(line)
|
50
|
-
else
|
51
|
-
line
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def replace_irefs(line)
|
56
|
-
changes.reduce(line) do |res_line, (from, to)|
|
57
|
-
res_line.gsub(Helper.regexp_from(:iref, from)) do
|
58
|
-
Helper.text_with(Regexp.last_match, to)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
private
|
64
|
-
|
65
|
-
def do_replace_refdef(line)
|
66
|
-
line.sub(Helper.regexp_from(:definition, from_regexp)) do
|
67
|
-
replacement_from(Regexp.last_match)
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def replacement_from(match)
|
72
|
-
replacement = to_template % hmerge(match)
|
73
|
-
changes << [match[:from], replacement]
|
74
|
-
config[:next_ref] += 1
|
75
|
-
Helper.text_with(match, replacement)
|
76
|
-
end
|
77
|
-
|
78
|
-
def hmerge(match)
|
79
|
-
config.merge(match.named_captures.transform_keys(&:to_sym))
|
80
|
-
end
|
81
|
-
|
82
|
-
# Helper functions
|
83
|
-
module Helper
|
84
|
-
def self.regexp_from(const, from)
|
85
|
-
regexps = REGEXP_FROM.fetch(const)
|
86
|
-
regexp_str =
|
87
|
-
"(?<before>#{regexps[:before]})" \
|
88
|
-
"(?<from>#{from})" \
|
89
|
-
"#{DMRegexp::DEF_VERSION_AND_CHECKSUM}" \
|
90
|
-
"(?<after>#{regexps[:after]})"
|
91
|
-
Regexp.new(regexp_str, Regexp::EXTENDED)
|
92
|
-
end
|
93
|
-
|
94
|
-
def self.text_with(match, replacement)
|
95
|
-
match[:before] + replacement + (match[:version_and_checksum] || '') + match[:after]
|
96
|
-
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
@@ -1,91 +0,0 @@
|
|
1
|
-
# Copyright (c) 2020 Jerome Arbez-Gindre
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
module DefMastership
|
5
|
-
# set of regexp of added asciidoctor constructions
|
6
|
-
# This module smells of :reek:TooManyConstants
|
7
|
-
module DMRegexp
|
8
|
-
SINGLE_LINE_COMMENT = %r{^//[^/]}.freeze
|
9
|
-
|
10
|
-
DEF_BEFORE_REF = <<~'BEF'
|
11
|
-
^\s*
|
12
|
-
\[\s*define\s*,
|
13
|
-
\s*(?<type>[\w:_-]+)\s*,
|
14
|
-
\s*
|
15
|
-
BEF
|
16
|
-
|
17
|
-
DEF_AFTER_REF = <<~'AFT'
|
18
|
-
\s*
|
19
|
-
(,\s*\[\s*(?<labels>.*?)\s*\])?\s*\]
|
20
|
-
AFT
|
21
|
-
|
22
|
-
DEF_VERSION_AND_CHECKSUM = '(?<version_and_checksum>' \
|
23
|
-
'\((?<explicit_version>[^~]+)?(?<explicit_checksum>~[[:alnum:]]+)?\)' \
|
24
|
-
')?'
|
25
|
-
|
26
|
-
REFERENCE = '(?<reference>[\\w:_-]+)'
|
27
|
-
|
28
|
-
DEFINITION = Regexp.new(
|
29
|
-
"#{DEF_BEFORE_REF}#{REFERENCE}#{DEF_VERSION_AND_CHECKSUM}#{DEF_AFTER_REF}",
|
30
|
-
Regexp::EXTENDED
|
31
|
-
)
|
32
|
-
|
33
|
-
VARIABLE_DEF = /^\s*:(?<varname>[\w:_-]+):\s+
|
34
|
-
(?<value>\S.*\S)\s*$/x.freeze
|
35
|
-
|
36
|
-
VARIABLE_USE = /{(?<varname>[\w:_-]+)}/x.freeze
|
37
|
-
|
38
|
-
EREF_CONFIG = /^\s*:eref-(?<reference>[\w:_-]+)-(?<symb>prefix|url):\s*
|
39
|
-
\s*(?<value>\S.*\S)\s*/x.freeze
|
40
|
-
EREF_DEF = /^\s*
|
41
|
-
defs:eref\[
|
42
|
-
\s*(?<reference>[\w:_-]+)\s*,
|
43
|
-
\s*\[\s*(?<extrefs>[^\]]+?)\s*\]\s*\]/x.freeze
|
44
|
-
BLOCK = /^--\s*$/.freeze
|
45
|
-
|
46
|
-
IREF_DEF_BEF = 'defs:iref\[\s*'
|
47
|
-
IREF_DEF_AFT = '\s*\]'
|
48
|
-
IREF_DEF = Regexp.new(
|
49
|
-
"#{IREF_DEF_BEF}(?<intref>[\\w:_-]+)#{IREF_DEF_AFT}",
|
50
|
-
Regexp::EXTENDED
|
51
|
-
)
|
52
|
-
|
53
|
-
ATTR_CONFIG = /\s*:attr-(?<attr>[\w:_-]+)-prefix:
|
54
|
-
\s+(?<prefix>.+?)\s*$/x.freeze
|
55
|
-
ATTR_SET = /\s*
|
56
|
-
defs:attribute\[
|
57
|
-
\s*(?<attr>[\w:_-]+)\s*,
|
58
|
-
\s*(?<value>.+?)\s*\]/x.freeze
|
59
|
-
|
60
|
-
EMPTY_LINE = /^\s*$/.freeze
|
61
|
-
|
62
|
-
WHATEVER = //.freeze
|
63
|
-
|
64
|
-
INCLUDE_KEYWORD = '\binclude::'
|
65
|
-
INCLUDE_PATH = '(?<path>.*/)?'
|
66
|
-
INCLUDE_FILENAME = '(?<filename>[^\\/]+)'
|
67
|
-
INCLUDE_OPTIONS = '\[(?<options>[^\]]*)\]'
|
68
|
-
|
69
|
-
INCLUDE = Regexp.new(
|
70
|
-
INCLUDE_KEYWORD + INCLUDE_PATH + INCLUDE_FILENAME + INCLUDE_OPTIONS,
|
71
|
-
Regexp::EXTENDED
|
72
|
-
)
|
73
|
-
|
74
|
-
public_constant :SINGLE_LINE_COMMENT,
|
75
|
-
:DEF_BEFORE_REF,
|
76
|
-
:DEF_AFTER_REF,
|
77
|
-
:DEFINITION,
|
78
|
-
:VARIABLE_DEF,
|
79
|
-
:VARIABLE_USE,
|
80
|
-
:EREF_CONFIG,
|
81
|
-
:EREF_DEF,
|
82
|
-
:BLOCK,
|
83
|
-
:IREF_DEF_BEF,
|
84
|
-
:IREF_DEF_AFT,
|
85
|
-
:IREF_DEF,
|
86
|
-
:ATTR_CONFIG,
|
87
|
-
:ATTR_SET,
|
88
|
-
:EMPTY_LINE,
|
89
|
-
:WHATEVER
|
90
|
-
end
|
91
|
-
end
|
@@ -1,53 +0,0 @@
|
|
1
|
-
# Copyright (c) 2020 Jerome Arbez-Gindre
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require('csv')
|
5
|
-
require('defmastership/csv_formatter_body')
|
6
|
-
require('defmastership/csv_formatter_header')
|
7
|
-
|
8
|
-
module DefMastership
|
9
|
-
# to export a CSV file
|
10
|
-
class CSVFormatter
|
11
|
-
def initialize(doc, sep)
|
12
|
-
@doc = doc
|
13
|
-
@sep = sep
|
14
|
-
end
|
15
|
-
|
16
|
-
def export_to(output_file)
|
17
|
-
column_list = build_column_list
|
18
|
-
CSV.open(output_file, 'w:ISO-8859-1', col_sep: @sep) do |csv|
|
19
|
-
csv << header(column_list)
|
20
|
-
export_definitions_in(csv, column_list)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def export_definitions_in(csv, column_list)
|
27
|
-
@doc.definitions.each { |definition| csv << body(definition, column_list) }
|
28
|
-
end
|
29
|
-
|
30
|
-
def header(column_list)
|
31
|
-
header_formatter = CSVFormatterHeader.new(@doc)
|
32
|
-
header_line = column_list.map { |part| header_formatter.public_send(part) }
|
33
|
-
header_line.flatten
|
34
|
-
end
|
35
|
-
|
36
|
-
def body(definition, column_list)
|
37
|
-
body_formatter = CSVFormatterBody.new(@doc, definition)
|
38
|
-
body_line = column_list.map { |part| body_formatter.public_send(part) }
|
39
|
-
body_line.flatten
|
40
|
-
end
|
41
|
-
|
42
|
-
def build_column_list
|
43
|
-
[
|
44
|
-
[:wrong_explicit_checksum, @doc.wrong_explicit_checksum?],
|
45
|
-
[:explicit_version, @doc.explicit_version?],
|
46
|
-
[:labels, !@doc.labels.empty?],
|
47
|
-
[:eref, !@doc.eref.empty?],
|
48
|
-
[:iref, @doc.iref],
|
49
|
-
[:attributes, !@doc.attributes.empty?]
|
50
|
-
].reduce([:fixed]) { |acc, elem| acc + (elem.fetch(1) ? [elem.first] : []) }
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
@@ -1,46 +0,0 @@
|
|
1
|
-
# Copyright (c) 2020 Jerome Arbez-Gindre
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require('csv')
|
5
|
-
|
6
|
-
require('defmastership/set_join_hack')
|
7
|
-
|
8
|
-
module DefMastership
|
9
|
-
# format lines per definition
|
10
|
-
class CSVFormatterBody
|
11
|
-
def initialize(doc, definition)
|
12
|
-
@doc = doc
|
13
|
-
@definition = definition
|
14
|
-
end
|
15
|
-
|
16
|
-
def fixed
|
17
|
-
[@definition.type, @definition.reference, @definition.value, @definition.sha256_short]
|
18
|
-
end
|
19
|
-
|
20
|
-
def wrong_explicit_checksum
|
21
|
-
wrong_explicit_checksum = @definition.wrong_explicit_checksum
|
22
|
-
wrong_explicit_checksum ? [wrong_explicit_checksum] : ['']
|
23
|
-
end
|
24
|
-
|
25
|
-
def explicit_version
|
26
|
-
explicit_version = @definition.explicit_version
|
27
|
-
explicit_version ? [explicit_version] : ['']
|
28
|
-
end
|
29
|
-
|
30
|
-
def labels
|
31
|
-
[@definition.labels.join("\n")]
|
32
|
-
end
|
33
|
-
|
34
|
-
def eref
|
35
|
-
@doc.eref.map { |key,| @definition.eref.fetch(key, []).join("\n") }
|
36
|
-
end
|
37
|
-
|
38
|
-
def iref
|
39
|
-
[@definition.iref.join("\n")]
|
40
|
-
end
|
41
|
-
|
42
|
-
def attributes
|
43
|
-
@doc.attributes.map { |key,| @definition.attributes.fetch(key, '') }
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
# Copyright (c) 2020 Jerome Arbez-Gindre
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require('csv')
|
5
|
-
|
6
|
-
module DefMastership
|
7
|
-
# format header for one document
|
8
|
-
class CSVFormatterHeader
|
9
|
-
def initialize(doc)
|
10
|
-
@doc = doc
|
11
|
-
end
|
12
|
-
|
13
|
-
def fixed
|
14
|
-
%w[Type Reference Value Checksum]
|
15
|
-
end
|
16
|
-
|
17
|
-
def wrong_explicit_checksum
|
18
|
-
@doc.wrong_explicit_checksum? ? ['Wrong explicit checksum'] : []
|
19
|
-
end
|
20
|
-
|
21
|
-
def explicit_version
|
22
|
-
@doc.explicit_version? ? ['Version'] : []
|
23
|
-
end
|
24
|
-
|
25
|
-
def labels
|
26
|
-
@doc.labels.empty? ? [] : %w[Labels]
|
27
|
-
end
|
28
|
-
|
29
|
-
def eref
|
30
|
-
@doc.eref.map { |_, ref| ref.fetch(:prefix) }
|
31
|
-
end
|
32
|
-
|
33
|
-
def iref
|
34
|
-
@doc.iref ? ['Internal links'] : []
|
35
|
-
end
|
36
|
-
|
37
|
-
def attributes
|
38
|
-
@doc.attributes.map { |_, value| value }
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
# Copyright (c) 2020 Jerome Arbez-Gindre
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
module DefMastership
|
5
|
-
# define methods for line modifiers
|
6
|
-
module Modifier
|
7
|
-
attr_reader :config, :changes
|
8
|
-
|
9
|
-
def setup_modifier_module(config)
|
10
|
-
@config = self.class.default_config.merge(config)
|
11
|
-
@changes = []
|
12
|
-
end
|
13
|
-
|
14
|
-
def method_missing(method_name, *args)
|
15
|
-
config_method_name = config[method_name]
|
16
|
-
return config_method_name if config_method_name
|
17
|
-
|
18
|
-
super
|
19
|
-
end
|
20
|
-
|
21
|
-
def respond_to_missing?(method_name, *args)
|
22
|
-
config.key?(method_name) || super
|
23
|
-
end
|
24
|
-
|
25
|
-
def apply_to_all(texts, method)
|
26
|
-
texts.transform_values do |text|
|
27
|
-
apply_to_one(text, method)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def apply_to_one(text, method)
|
32
|
-
text.lines.map { |line| public_send(method, line) }
|
33
|
-
.join
|
34
|
-
end
|
35
|
-
|
36
|
-
def do_modifications(adoc_sources)
|
37
|
-
self.class.replacement_methods.reduce(adoc_sources) do |texts, method|
|
38
|
-
apply_to_all(texts, method)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
# Copyright (c) 2023 Jerome Arbez-Gindre
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
module DefMastership
|
5
|
-
# build midifers from a piece of configuration
|
6
|
-
module ModifierFactory
|
7
|
-
def self.from_config(config)
|
8
|
-
class_name = "#{config.fetch(:type).split('_').map(&:capitalize).join}Modifier"
|
9
|
-
DefMastership.const_get(class_name).new(config.fetch(:config))
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
# Copyright (c) 2020 Jerome Arbez-Gindre
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
module DefMastership
|
5
|
-
# Allow to know if we need to parse the line or simply ignore it
|
6
|
-
class ParsingState
|
7
|
-
# mutant:disable (for mutant, aatribute initialization is useless)
|
8
|
-
def initialize
|
9
|
-
@last_disabling_line = nil
|
10
|
-
end
|
11
|
-
|
12
|
-
def enabled?(line)
|
13
|
-
return false if line.match?(DMRegexp::SINGLE_LINE_COMMENT)
|
14
|
-
|
15
|
-
line = line.chomp
|
16
|
-
possibly_invert_last_disabling_line(line) if ['....', '----', '////'].include?(line)
|
17
|
-
|
18
|
-
!@last_disabling_line
|
19
|
-
end
|
20
|
-
|
21
|
-
private
|
22
|
-
|
23
|
-
def possibly_invert_last_disabling_line(line)
|
24
|
-
if @last_disabling_line == line
|
25
|
-
@last_disabling_line = nil
|
26
|
-
elsif !@last_disabling_line
|
27
|
-
@last_disabling_line = line
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
@@ -1,182 +0,0 @@
|
|
1
|
-
# Copyright (c) 2020 Jerome Arbez-Gindre
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require 'defmastership/constants'
|
5
|
-
require('defmastership/matching_line')
|
6
|
-
require 'defmastership/modifier'
|
7
|
-
|
8
|
-
# defintion of the Rename Included Files Modifier
|
9
|
-
module DefMastership
|
10
|
-
LOCAL_FILTERS = [
|
11
|
-
Filter.new(DMRegexp::VARIABLE_DEF, :new_variable_def),
|
12
|
-
Filter.new(DMRegexp::DEFINITION, :new_definition),
|
13
|
-
Filter.new(DMRegexp::BLOCK, :block_delimiter),
|
14
|
-
Filter.new(DMRegexp::EMPTY_LINE, :empty_line),
|
15
|
-
Filter.new(DMRegexp::WHATEVER, :new_line)
|
16
|
-
].freeze
|
17
|
-
private_constant :LOCAL_FILTERS
|
18
|
-
|
19
|
-
# Change included filenames on one line at a time
|
20
|
-
class RenameIncludedFilesModifier
|
21
|
-
include Modifier
|
22
|
-
|
23
|
-
PARSER_ACTIONS = {
|
24
|
-
new_variable_def: lambda { |matching_line|
|
25
|
-
@variables.merge!(Helper.variable_def_hash(matching_line.match))
|
26
|
-
},
|
27
|
-
add_line: proc { |_| },
|
28
|
-
add_new_definition: lambda { |matching_line|
|
29
|
-
config[:reference] = matching_line.match[:reference]
|
30
|
-
}
|
31
|
-
}.freeze
|
32
|
-
|
33
|
-
private_constant :PARSER_ACTIONS
|
34
|
-
|
35
|
-
def self.replacement_methods
|
36
|
-
%i[replace]
|
37
|
-
end
|
38
|
-
|
39
|
-
def self.default_config
|
40
|
-
{
|
41
|
-
from_regexp: '',
|
42
|
-
to_template: ''
|
43
|
-
}
|
44
|
-
end
|
45
|
-
|
46
|
-
def initialize(config)
|
47
|
-
@variables = {}
|
48
|
-
@definition_parser = DefinitionParser.new(self)
|
49
|
-
|
50
|
-
setup_modifier_module(config)
|
51
|
-
end
|
52
|
-
|
53
|
-
def method_missing(method_name, *args)
|
54
|
-
action = PARSER_ACTIONS[method_name]
|
55
|
-
return instance_exec(*args, &action) if action
|
56
|
-
|
57
|
-
super
|
58
|
-
end
|
59
|
-
|
60
|
-
def respond_to_missing?(method_name, *args)
|
61
|
-
PARSER_ACTIONS.key?(method_name) || super
|
62
|
-
end
|
63
|
-
|
64
|
-
def replace(line)
|
65
|
-
match = matched?(line)
|
66
|
-
|
67
|
-
return line unless match
|
68
|
-
|
69
|
-
new_line = build_new_include_line(match, line)
|
70
|
-
|
71
|
-
rename_file(line, new_line)
|
72
|
-
|
73
|
-
new_line
|
74
|
-
end
|
75
|
-
|
76
|
-
private
|
77
|
-
|
78
|
-
def build_new_include_line(match, line)
|
79
|
-
line.sub(Helper.complete_regexp_from(from_regexp)) do
|
80
|
-
Helper.text_with(match, to_template % Helper.hmerge(config, match))
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
def matched?(line)
|
85
|
-
return false unless concerned_line?(line)
|
86
|
-
return false unless line =~ Helper.complete_regexp_from(from_regexp)
|
87
|
-
|
88
|
-
match = Regexp.last_match
|
89
|
-
|
90
|
-
return false if config.key?(:cancel_if_match) && match[:filename].match?(cancel_if_match)
|
91
|
-
|
92
|
-
match
|
93
|
-
end
|
94
|
-
|
95
|
-
def concerned_line?(line)
|
96
|
-
return false if line.commented?
|
97
|
-
|
98
|
-
parse(line)
|
99
|
-
|
100
|
-
return false if @definition_parser.idle?
|
101
|
-
|
102
|
-
true
|
103
|
-
end
|
104
|
-
|
105
|
-
def parse(line)
|
106
|
-
Helper.apply_filters_until_consumed(line) do |event, match|
|
107
|
-
generate_event(event, MatchingLine.new(match))
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
def rename_file(from, to)
|
112
|
-
filename_from = Helper.extract_filename(from, @variables)
|
113
|
-
filename_to = Helper.extract_filename(to, @variables)
|
114
|
-
changes << [filename_from, filename_to]
|
115
|
-
File.rename(filename_from, filename_to)
|
116
|
-
end
|
117
|
-
|
118
|
-
def generate_event(event, matching_line)
|
119
|
-
if PARSER_ACTIONS.key?(event)
|
120
|
-
public_send(event, matching_line)
|
121
|
-
else
|
122
|
-
@definition_parser.public_send(event, matching_line)
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
# Helper functions
|
127
|
-
module Helper
|
128
|
-
def self.extract_filename(include_statement, variables)
|
129
|
-
filename = filename_from_include_statement(include_statement)
|
130
|
-
|
131
|
-
filename_replace_all_variables(filename, variables).chomp
|
132
|
-
end
|
133
|
-
|
134
|
-
def self.filename_from_include_statement(include_statement)
|
135
|
-
include_statement
|
136
|
-
.sub(Regexp.new(DMRegexp::INCLUDE_KEYWORD), '')
|
137
|
-
.sub(Regexp.new(DMRegexp::INCLUDE_OPTIONS), '')
|
138
|
-
end
|
139
|
-
|
140
|
-
def self.filename_replace_all_variables(filename, variables)
|
141
|
-
filename.scan(DMRegexp::VARIABLE_USE) do
|
142
|
-
varname = Regexp.last_match[:varname]
|
143
|
-
value = variables.fetch(varname.to_sym)
|
144
|
-
filename = filename_replace_one_variable(filename, varname, value)
|
145
|
-
end
|
146
|
-
filename
|
147
|
-
end
|
148
|
-
|
149
|
-
def self.filename_replace_one_variable(filename, varname, value)
|
150
|
-
filename.sub("{#{varname}}", value)
|
151
|
-
end
|
152
|
-
|
153
|
-
def self.complete_regexp_from(from)
|
154
|
-
Regexp.new(
|
155
|
-
DMRegexp::INCLUDE_KEYWORD + DMRegexp::INCLUDE_PATH +
|
156
|
-
"(?<filename>#{from})" + DMRegexp::INCLUDE_OPTIONS
|
157
|
-
)
|
158
|
-
end
|
159
|
-
|
160
|
-
def self.text_with(match, to)
|
161
|
-
"include::#{match[:path]}#{to}[#{match[:options]}]"
|
162
|
-
end
|
163
|
-
|
164
|
-
def self.hmerge(config, match)
|
165
|
-
config.merge(match.names.map(&:to_sym).zip(match.captures).to_h)
|
166
|
-
end
|
167
|
-
|
168
|
-
def self.variable_def_hash(match)
|
169
|
-
{ match[:varname].to_sym => match[:value] }
|
170
|
-
end
|
171
|
-
|
172
|
-
def self.apply_filters_until_consumed(line)
|
173
|
-
LOCAL_FILTERS.each do |filter|
|
174
|
-
next unless line.match(filter.regexp)
|
175
|
-
|
176
|
-
yield(filter.event, Regexp.last_match)
|
177
|
-
break
|
178
|
-
end
|
179
|
-
end
|
180
|
-
end
|
181
|
-
end
|
182
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
# Copyright (c) 2020 Jerome Arbez-Gindre
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
require 'defmastership/constants'
|
5
|
-
require 'defmastership/update_def_modifier'
|
6
|
-
|
7
|
-
module DefMastership
|
8
|
-
# modify one line after another
|
9
|
-
class UpdateDefChecksumModifier < UpdateDefModifier
|
10
|
-
private
|
11
|
-
|
12
|
-
def reference_replacement(reference, match)
|
13
|
-
"#{reference}(#{match[:explicit_version]}#{document.ref_to_def(reference).sha256_short})"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
# Copyright (c) 2024 Jerome Arbez-Gindre
|
2
|
-
# frozen_string_literal: true
|
3
|
-
|
4
|
-
module DefMastership
|
5
|
-
# absttrac class for modififier that need o update defintion references
|
6
|
-
class UpdateDefModifier
|
7
|
-
include Modifier
|
8
|
-
|
9
|
-
attr_reader :document
|
10
|
-
|
11
|
-
def self.default_config
|
12
|
-
{
|
13
|
-
def_type: ''
|
14
|
-
}
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.replacement_methods
|
18
|
-
%i[replace_reference]
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.reference_regexp(reference)
|
22
|
-
Regexp.new("#{reference}#{DMRegexp::DEF_VERSION_AND_CHECKSUM}")
|
23
|
-
end
|
24
|
-
|
25
|
-
def initialize(config)
|
26
|
-
@document = Document.new
|
27
|
-
|
28
|
-
setup_modifier_module(config)
|
29
|
-
end
|
30
|
-
|
31
|
-
def do_modifications(adoc_sources)
|
32
|
-
adoc_sources.each_key do |adoc_file|
|
33
|
-
document.parse_file_with_preprocessor(adoc_file)
|
34
|
-
end
|
35
|
-
|
36
|
-
super
|
37
|
-
end
|
38
|
-
|
39
|
-
def replace_reference(line)
|
40
|
-
match = line.match(DMRegexp::DEFINITION)
|
41
|
-
|
42
|
-
return line unless match
|
43
|
-
return line unless match[:type] == def_type
|
44
|
-
|
45
|
-
reference = match[:reference]
|
46
|
-
line.sub(self.class.reference_regexp(reference), reference_replacement(reference, match))
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|