defmastership 1.0.5 → 1.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -1
- data/bin/defmastership +33 -22
- data/cucumber.yml +1 -1
- data/defmastership.gemspec +12 -6
- data/features/changeref.feature +82 -129
- data/features/definition_checksum.feature +298 -0
- data/features/definition_version.feature +204 -0
- data/features/export.feature +35 -34
- data/features/modify.feature +165 -0
- data/features/rename_included_files.feature +121 -0
- data/lib/defmastership.rb +17 -3
- data/lib/defmastership/batch_modifier.rb +35 -0
- data/lib/defmastership/{ref_changer.rb → change_ref_line_modifier.rb} +18 -35
- data/lib/defmastership/change_ref_modifier.rb +15 -0
- data/lib/defmastership/constants.rb +14 -1
- data/lib/defmastership/csv_formatter.rb +22 -17
- data/lib/defmastership/csv_formatter_body.rb +19 -11
- data/lib/defmastership/csv_formatter_header.rb +15 -10
- data/lib/defmastership/definition.rb +14 -3
- data/lib/defmastership/definition_parser.rb +46 -0
- data/lib/defmastership/document.rb +59 -85
- data/lib/defmastership/filters.rb +30 -0
- data/lib/defmastership/line_modifier_base.rb +29 -0
- data/lib/defmastership/modifier_base.rb +29 -0
- data/lib/defmastership/rename_included_files_line_modifier.rb +126 -0
- data/lib/defmastership/rename_included_files_modifier.rb +15 -0
- data/lib/defmastership/update_def_checksum_line_modifier.rb +38 -0
- data/lib/defmastership/update_def_checksum_modifier.rb +21 -0
- data/lib/defmastership/update_def_version_line_modifier.rb +58 -0
- data/lib/defmastership/update_def_version_modifier.rb +25 -0
- data/lib/defmastership/version.rb +1 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/unit/defmastership/batch_modifier_spec.rb +123 -0
- data/spec/unit/defmastership/{ref_changer_spec.rb → change_ref_line_modifier_spec.rb} +48 -26
- data/spec/unit/defmastership/change_ref_modifier_spec.rb +76 -0
- data/spec/unit/defmastership/comment_filter_spec.rb +8 -4
- data/spec/unit/defmastership/csv_formatter_body_spec.rb +88 -82
- data/spec/unit/defmastership/csv_formatter_header_spec.rb +68 -22
- data/spec/unit/defmastership/csv_formatter_spec.rb +207 -109
- data/spec/unit/defmastership/definition_parser_spec.rb +63 -0
- data/spec/unit/defmastership/definition_spec.rb +45 -4
- data/spec/unit/defmastership/document_spec.rb +236 -35
- data/spec/unit/defmastership/rename_included_files_line_modifier_spec.rb +203 -0
- data/spec/unit/defmastership/rename_included_files_modifier_spec.rb +67 -0
- data/spec/unit/defmastership/update_def_checksum_line_modifier_spec.rb +78 -0
- data/spec/unit/defmastership/update_def_checksum_modifier_spec.rb +75 -0
- data/spec/unit/defmastership/update_def_version_line_modifier_spec.rb +127 -0
- data/spec/unit/defmastership/update_def_version_modifier_spec.rb +80 -0
- metadata +44 -9
- data/lib/defmastership/batch_changer.rb +0 -41
- data/lib/defmastership/project_ref_changer.rb +0 -28
- data/spec/unit/defmastership/batch_changer_spec.rb +0 -109
- data/spec/unit/defmastership/project_ref_changer_spec.rb +0 -80
data/lib/defmastership.rb
CHANGED
@@ -7,10 +7,24 @@ require('defmastership/version')
|
|
7
7
|
# you just need to require this one file in your bin file
|
8
8
|
require('defmastership/constants')
|
9
9
|
require('defmastership/definition')
|
10
|
+
require('defmastership/definition_parser')
|
11
|
+
require('defmastership/filters')
|
10
12
|
require('defmastership/document')
|
11
13
|
require('defmastership/comment_filter')
|
12
14
|
require('defmastership/csv_formatter')
|
13
15
|
|
14
|
-
require('defmastership/
|
15
|
-
require('defmastership/
|
16
|
-
require('defmastership/
|
16
|
+
require('defmastership/modifier_base')
|
17
|
+
require('defmastership/line_modifier_base')
|
18
|
+
require('defmastership/batch_modifier')
|
19
|
+
|
20
|
+
require('defmastership/change_ref_modifier')
|
21
|
+
require('defmastership/change_ref_line_modifier')
|
22
|
+
|
23
|
+
require('defmastership/rename_included_files_modifier')
|
24
|
+
require('defmastership/rename_included_files_line_modifier')
|
25
|
+
|
26
|
+
require('defmastership/update_def_checksum_modifier')
|
27
|
+
require('defmastership/update_def_checksum_line_modifier')
|
28
|
+
|
29
|
+
require('defmastership/update_def_version_modifier')
|
30
|
+
require('defmastership/update_def_version_line_modifier')
|
@@ -0,0 +1,35 @@
|
|
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 BatchModifier
|
7
|
+
attr_reader :config, :adoc_texts, :changes
|
8
|
+
|
9
|
+
def initialize(config, adoc_texts)
|
10
|
+
@config = config
|
11
|
+
@adoc_texts = adoc_texts
|
12
|
+
@changes = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def apply(modifs)
|
16
|
+
modifs.split(/\s*,\s*/).each do |modif|
|
17
|
+
modifier = modifier_from(modif)
|
18
|
+
@adoc_texts = modifier.do_modifications(@adoc_texts)
|
19
|
+
@config[modif.to_sym][:config] = modifier.config
|
20
|
+
modifier.changes.each do |change|
|
21
|
+
@changes << [modif] + change
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def modifier_from(modif)
|
29
|
+
raise(ArgumentError, "#{modif} is not a known modification") if @config[modif.to_sym].nil?
|
30
|
+
|
31
|
+
class_name = "#{@config[modif.to_sym][:type].split('_').map(&:capitalize).join}Modifier"
|
32
|
+
DefMastership.const_get(class_name).new(@config[modif.to_sym][:config])
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -2,12 +2,10 @@
|
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
module DefMastership
|
5
|
-
# Change references from temporary to definitive
|
6
|
-
class
|
7
|
-
|
8
|
-
|
9
|
-
def self.from_h(hash)
|
10
|
-
new.from_h(hash)
|
5
|
+
# Change references from temporary to definitive with multiple RefChangers
|
6
|
+
class ChangeRefLineModifier < LineModifierBase
|
7
|
+
def self.from_config(hash)
|
8
|
+
new.from_config(hash)
|
11
9
|
end
|
12
10
|
|
13
11
|
REGEXP_FROM = {
|
@@ -24,32 +22,17 @@ module DefMastership
|
|
24
22
|
private_constant :REGEXP_FROM
|
25
23
|
|
26
24
|
def initialize
|
27
|
-
|
25
|
+
super
|
26
|
+
@config = {
|
28
27
|
from_regexp: '',
|
29
28
|
to_template: '',
|
30
29
|
next_ref: 0
|
31
30
|
}
|
32
|
-
@changes = []
|
33
31
|
@in_literal = false
|
34
32
|
end
|
35
33
|
|
36
|
-
def
|
37
|
-
|
38
|
-
|
39
|
-
super
|
40
|
-
end
|
41
|
-
|
42
|
-
def respond_to_missing?(method_name, *args)
|
43
|
-
@h[method_name] || super
|
44
|
-
end
|
45
|
-
|
46
|
-
def from_h(hash)
|
47
|
-
@h.merge!(hash)
|
48
|
-
self
|
49
|
-
end
|
50
|
-
|
51
|
-
def to_h
|
52
|
-
@h
|
34
|
+
def replace(method, line)
|
35
|
+
public_send("replace_#{method}".to_sym, line)
|
53
36
|
end
|
54
37
|
|
55
38
|
def replace_refdef(line)
|
@@ -70,6 +53,15 @@ module DefMastership
|
|
70
53
|
|
71
54
|
private
|
72
55
|
|
56
|
+
def do_replace_refdef(line)
|
57
|
+
line.gsub(regexp_from(:definition, from_regexp)) do
|
58
|
+
replacement = to_template % hmerge(Regexp.last_match)
|
59
|
+
changes << [Regexp.last_match[:from], replacement]
|
60
|
+
@config[:next_ref] += 1
|
61
|
+
text_with(Regexp.last_match, replacement)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
73
65
|
def regexp_from(const, from)
|
74
66
|
regexp_str =
|
75
67
|
"(?<before>#{REGEXP_FROM[const][:before]})" \
|
@@ -83,21 +75,12 @@ module DefMastership
|
|
83
75
|
end
|
84
76
|
|
85
77
|
def hmerge(match)
|
86
|
-
@
|
78
|
+
@config.merge(match.names.map(&:to_sym).zip(match.captures).to_h)
|
87
79
|
end
|
88
80
|
|
89
81
|
def in_literal(line)
|
90
82
|
@in_literal ^= true if line.chomp == '....'
|
91
83
|
@in_literal
|
92
84
|
end
|
93
|
-
|
94
|
-
def do_replace_refdef(line)
|
95
|
-
line.gsub(regexp_from(:definition, from_regexp)) do
|
96
|
-
replacement = to_template % hmerge(Regexp.last_match)
|
97
|
-
changes << [Regexp.last_match[:from], replacement]
|
98
|
-
@h[:next_ref] += 1
|
99
|
-
text_with(Regexp.last_match, replacement)
|
100
|
-
end
|
101
|
-
end
|
102
85
|
end
|
103
86
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# Copyright (c) 2020 Jerome Arbez-Gindre
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module DefMastership
|
5
|
+
# Change all refs of a given project
|
6
|
+
class ChangeRefModifier < ModifierBase
|
7
|
+
def replacements
|
8
|
+
%i[replace_refdef replace_irefs]
|
9
|
+
end
|
10
|
+
|
11
|
+
def new_line_modifier(config, _adoc_texts)
|
12
|
+
ChangeRefLineModifier.from_config(config)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -19,8 +19,11 @@ module DefMastership
|
|
19
19
|
\s*
|
20
20
|
(,\s*\[\s*(?<labels>.*?)\s*\])?\s*\]
|
21
21
|
AFT
|
22
|
+
|
23
|
+
DEF_VERSION_AND_CHECKSUM = '(\((?<explicit_version>[^~]+)?(?<explicit_checksum>~[[:alnum:]]+)?\))?'
|
24
|
+
|
22
25
|
DEFINITION = Regexp.new(
|
23
|
-
"#{DEF_BEFORE_REF}(?<reference>[\\w:_-]+)#{DEF_AFTER_REF}",
|
26
|
+
"#{DEF_BEFORE_REF}(?<reference>[\\w:_-]+)#{DEF_VERSION_AND_CHECKSUM}#{DEF_AFTER_REF}",
|
24
27
|
Regexp::EXTENDED
|
25
28
|
)
|
26
29
|
|
@@ -56,6 +59,16 @@ module DefMastership
|
|
56
59
|
|
57
60
|
WHATEVER = //.freeze
|
58
61
|
|
62
|
+
INCLUDE_KEYWORD = '\binclude::'
|
63
|
+
INCLUDE_PATH = '(?<path>.*/)?'
|
64
|
+
INCLUDE_FILENAME = '(?<filename>[^\\/]+)'
|
65
|
+
INCLUDE_OPTIONS = '\[(?<options>[\]]*)\]'
|
66
|
+
|
67
|
+
INCLUDE = Regexp.new(
|
68
|
+
INCLUDE_KEYWORD + INCLUDE_PATH + INCLUDE_FILENAME + INCLUDE_OPTIONS,
|
69
|
+
Regexp::EXTENDED
|
70
|
+
)
|
71
|
+
|
59
72
|
public_constant :SINGLE_LINE_COMMENT,
|
60
73
|
:MULTI_LINE_COMMENT_DELIM,
|
61
74
|
:DEF_BEFORE_REF,
|
@@ -8,37 +8,42 @@ require('defmastership/csv_formatter_body')
|
|
8
8
|
module DefMastership
|
9
9
|
# to export a CSV file
|
10
10
|
class CSVFormatter
|
11
|
-
COLUMN_LIST = %w[fixed labels eref iref attributes].freeze
|
12
|
-
private_constant :COLUMN_LIST
|
13
|
-
|
14
11
|
def initialize(doc, sep = ',')
|
15
12
|
@doc = doc
|
16
13
|
@sep = sep
|
17
|
-
@header_formatter = CSVFormatterHeader.new(@doc)
|
18
|
-
@body_formatter = CSVFormatterBody.new(@doc)
|
19
14
|
end
|
20
15
|
|
21
16
|
def export_to(output_file)
|
17
|
+
column_list = build_column_list
|
22
18
|
CSV.open(output_file, 'w:ISO-8859-1', { col_sep: @sep }) do |csv|
|
23
|
-
csv << header
|
24
|
-
@doc.definitions.each { |definition| csv << body(definition) }
|
19
|
+
csv << header(column_list)
|
20
|
+
@doc.definitions.each { |definition| csv << body(definition, column_list) }
|
25
21
|
end
|
26
22
|
end
|
27
23
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
24
|
+
private
|
25
|
+
|
26
|
+
def header(column_list)
|
27
|
+
header_formatter = CSVFormatterHeader.new(@doc)
|
28
|
+
header_line = column_list.map { |part| header_formatter.public_send(part) }
|
33
29
|
header_line.reduce(:+)
|
34
30
|
end
|
35
31
|
|
36
|
-
def body(definition)
|
37
|
-
|
38
|
-
|
39
|
-
@body_formatter.public_send("#{part}_body".to_sym, definition)
|
40
|
-
end
|
32
|
+
def body(definition, column_list)
|
33
|
+
body_formatter = CSVFormatterBody.new(@doc)
|
34
|
+
body_line = column_list.map { |part| body_formatter.public_send(part, definition) }
|
41
35
|
body_line.reduce(:+)
|
42
36
|
end
|
37
|
+
|
38
|
+
def build_column_list
|
39
|
+
column_list = [:fixed]
|
40
|
+
column_list += [:wrong_explicit_checksum] if @doc.wrong_explicit_checksum?
|
41
|
+
column_list += [:explicit_version] if @doc.explicit_version?
|
42
|
+
column_list += [:labels] unless @doc.labels.empty?
|
43
|
+
column_list += [:eref] unless @doc.eref.empty?
|
44
|
+
column_list += [:iref] if @doc.iref
|
45
|
+
column_list += [:attributes] unless @doc.attributes.empty?
|
46
|
+
column_list
|
47
|
+
end
|
43
48
|
end
|
44
49
|
end
|
@@ -10,26 +10,34 @@ module DefMastership
|
|
10
10
|
@doc = doc
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
14
|
-
[definition.type, definition.reference, definition.value]
|
13
|
+
def fixed(definition)
|
14
|
+
[definition.type, definition.reference, definition.value, definition.sha256]
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
18
|
-
|
17
|
+
def wrong_explicit_checksum(definition)
|
18
|
+
wrong_explicit_checksum = definition.wrong_explicit_checksum
|
19
|
+
wrong_explicit_checksum ? [wrong_explicit_checksum] : ['']
|
19
20
|
end
|
20
21
|
|
21
|
-
def
|
22
|
+
def explicit_version(definition)
|
23
|
+
explicit_version = definition.explicit_version
|
24
|
+
explicit_version ? [explicit_version] : ['']
|
25
|
+
end
|
26
|
+
|
27
|
+
def labels(definition)
|
28
|
+
[definition.labels.to_a.join("\n")]
|
29
|
+
end
|
30
|
+
|
31
|
+
def eref(definition)
|
22
32
|
@doc.eref.map { |key, _| definition.eref[key].join("\n") }
|
23
33
|
end
|
24
34
|
|
25
|
-
def
|
26
|
-
|
35
|
+
def iref(definition)
|
36
|
+
[definition.iref.join("\n")]
|
27
37
|
end
|
28
38
|
|
29
|
-
def
|
30
|
-
@doc.attributes.map
|
31
|
-
definition.attributes[key]
|
32
|
-
end
|
39
|
+
def attributes(definition)
|
40
|
+
@doc.attributes.map { |key, _| definition.attributes[key] }
|
33
41
|
end
|
34
42
|
end
|
35
43
|
end
|
@@ -10,26 +10,31 @@ module DefMastership
|
|
10
10
|
@doc = doc
|
11
11
|
end
|
12
12
|
|
13
|
-
def
|
14
|
-
%w[Type Reference Value]
|
13
|
+
def fixed
|
14
|
+
%w[Type Reference Value Checksum]
|
15
15
|
end
|
16
16
|
|
17
|
-
def
|
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
|
18
26
|
@doc.labels.empty? ? [] : %w[Labels]
|
19
27
|
end
|
20
28
|
|
21
|
-
def
|
22
|
-
@doc.eref.map
|
23
|
-
ref[:prefix] +
|
24
|
-
(ref[:url].nil? || ref[:url] == 'none' ? '' : " #{ref[:url]}")
|
25
|
-
end
|
29
|
+
def eref
|
30
|
+
@doc.eref.map { |_, ref| ref[:prefix] }
|
26
31
|
end
|
27
32
|
|
28
|
-
def
|
33
|
+
def iref
|
29
34
|
@doc.iref ? ['Internal links'] : []
|
30
35
|
end
|
31
36
|
|
32
|
-
def
|
37
|
+
def attributes
|
33
38
|
@doc.attributes.map { |_, value| value }
|
34
39
|
end
|
35
40
|
end
|
@@ -1,21 +1,24 @@
|
|
1
1
|
# Copyright (c) 2020 Jerome Arbez-Gindre
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
+
require 'digest'
|
5
|
+
|
4
6
|
module DefMastership
|
5
7
|
# DefMastership definition: contains all data of a definition
|
6
8
|
class Definition
|
7
|
-
attr_reader :type, :reference, :lines, :labels, :eref, :iref, :attributes
|
9
|
+
attr_reader :type, :reference, :lines, :labels, :eref, :iref, :attributes, :explicit_version
|
8
10
|
|
9
11
|
def initialize(match)
|
10
12
|
@type = match[:type]
|
11
13
|
@reference = match[:reference]
|
12
14
|
@lines = []
|
13
15
|
@labels = Set.new
|
14
|
-
labels
|
15
|
-
@labels.merge(labels.split(/\s*,\s*/).to_set) if labels
|
16
|
+
@labels.merge(match[:labels].split(/\s*,\s*/).to_set) if match[:labels]
|
16
17
|
@eref = Hash.new([])
|
17
18
|
@iref = []
|
18
19
|
@attributes = {}
|
20
|
+
@explicit_checksum = match[:explicit_checksum]
|
21
|
+
@explicit_version = match[:explicit_version]
|
19
22
|
end
|
20
23
|
|
21
24
|
def <<(new_line)
|
@@ -27,6 +30,14 @@ module DefMastership
|
|
27
30
|
@lines.join("\n")
|
28
31
|
end
|
29
32
|
|
33
|
+
def sha256
|
34
|
+
"~#{Digest::SHA2.new(256).hexdigest(value).split(//).last(8).join}"
|
35
|
+
end
|
36
|
+
|
37
|
+
def wrong_explicit_checksum
|
38
|
+
@explicit_checksum if @explicit_checksum != sha256
|
39
|
+
end
|
40
|
+
|
30
41
|
def add_eref(refname, extrefs)
|
31
42
|
@eref[refname] = extrefs.strip.split(/\s*,\s*/)
|
32
43
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# Copyright (c) 2020 Jerome Arbez-Gindre
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require('aasm')
|
5
|
+
|
6
|
+
module DefMastership
|
7
|
+
# DefMastership definition: contains all data of a definition
|
8
|
+
class DefinitionParser
|
9
|
+
include AASM
|
10
|
+
|
11
|
+
def initialize(document)
|
12
|
+
@document = document
|
13
|
+
end
|
14
|
+
|
15
|
+
aasm do
|
16
|
+
state :idle, initial: true
|
17
|
+
state :wait_content
|
18
|
+
state :in_block
|
19
|
+
state :single_para
|
20
|
+
|
21
|
+
event :new_definition do
|
22
|
+
transitions from: :idle, to: :wait_content, after: ->(*args) { @document.add_new_definition(*args) }
|
23
|
+
end
|
24
|
+
|
25
|
+
event :block_delimiter do
|
26
|
+
transitions from: :wait_content, to: :in_block
|
27
|
+
transitions from: %i[in_block idle single_para], to: :idle
|
28
|
+
end
|
29
|
+
|
30
|
+
event :new_line do
|
31
|
+
transitions from: %i[wait_content single_para],
|
32
|
+
to: :single_para,
|
33
|
+
after: ->(*args) { @document.add_line(*args) }
|
34
|
+
transitions from: :in_block,
|
35
|
+
to: :in_block,
|
36
|
+
after: ->(*args) { @document.add_line(*args) }
|
37
|
+
transitions from: :idle, to: :idle
|
38
|
+
end
|
39
|
+
|
40
|
+
event :empty_line do
|
41
|
+
transitions from: %i[wait_content single_para idle], to: :idle
|
42
|
+
transitions from: :in_block, to: :in_block
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -1,41 +1,13 @@
|
|
1
1
|
# Copyright (c) 2020 Jerome Arbez-Gindre
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
require('
|
4
|
+
require('asciidoctor')
|
5
5
|
|
6
6
|
module DefMastership
|
7
7
|
# Contains the content of a DefMastership document: mainly definitions
|
8
|
-
# TODO: make this class smaller by defining a separated parser
|
9
|
-
|
10
|
-
# Contains regexp / action couples
|
11
|
-
Filter = Struct.new(:regexp, :event, :consumed_line)
|
12
|
-
private_constant :Filter
|
13
|
-
|
14
|
-
FILTERS_IN_LITERAL = [
|
15
|
-
Filter.new(DMRegexp::LITERAL_BLOCK, :code_block_delimiter, false),
|
16
|
-
Filter.new(DMRegexp::WHATEVER, :new_line, true)
|
17
|
-
].freeze
|
18
|
-
private_constant :FILTERS_IN_LITERAL
|
19
|
-
|
20
|
-
FILTERS = [
|
21
|
-
Filter.new(DMRegexp::VARIABLE_DEF, :new_variable_def, false),
|
22
|
-
Filter.new(DMRegexp::VARIABLE_USE, :new_variable_use, false),
|
23
|
-
Filter.new(DMRegexp::DEFINITION, :new_definition, true),
|
24
|
-
Filter.new(DMRegexp::EREF_CONFIG, :new_eref_setup, true),
|
25
|
-
Filter.new(DMRegexp::EREF_DEF, :new_eref_def, false),
|
26
|
-
Filter.new(DMRegexp::IREF_DEF, :new_iref_def, false),
|
27
|
-
Filter.new(DMRegexp::ATTR_CONFIG, :new_attribute_conf, true),
|
28
|
-
Filter.new(DMRegexp::ATTR_SET, :new_attribute_value, false),
|
29
|
-
Filter.new(DMRegexp::BLOCK, :block_delimiter, true),
|
30
|
-
Filter.new(DMRegexp::LITERAL_BLOCK, :code_block_delimiter, true),
|
31
|
-
Filter.new(DMRegexp::EMPTY_LINE, :empty_line, false),
|
32
|
-
Filter.new(DMRegexp::WHATEVER, :new_line, true)
|
33
|
-
].freeze
|
34
|
-
private_constant :FILTERS
|
35
8
|
|
36
9
|
# Reflects document structure from a definition point of view
|
37
10
|
class Document
|
38
|
-
include AASM
|
39
11
|
attr_reader :definitions, :labels, :eref, :iref, :attributes, :variables
|
40
12
|
|
41
13
|
def initialize
|
@@ -45,110 +17,112 @@ module DefMastership
|
|
45
17
|
@iref = false
|
46
18
|
@attributes = {}
|
47
19
|
@in_literal = true
|
48
|
-
@current_line = nil
|
49
20
|
@variables = {}
|
21
|
+
@definition_parser = DefinitionParser.new(self)
|
50
22
|
end
|
51
23
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
state :single_para
|
24
|
+
def parse(lines)
|
25
|
+
lines.reject(&:commented?).each do |line|
|
26
|
+
(@in_literal ? FILTERS : FILTERS_IN_LITERAL).each do |filter|
|
27
|
+
next unless line.match(filter.regexp)
|
57
28
|
|
58
|
-
|
59
|
-
transitions from: :idle, to: :wait_content, after: :add_new_definition
|
60
|
-
end
|
29
|
+
line = generate_event(filter.event, Regexp.last_match, line)
|
61
30
|
|
62
|
-
|
63
|
-
|
64
|
-
transitions from: %i[in_block idle], to: :idle
|
65
|
-
transitions from: :single_para, to: :idle
|
31
|
+
break if filter.consumed_line
|
32
|
+
end
|
66
33
|
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def parse_file_with_preprocessor(adoc_file)
|
37
|
+
parse(Asciidoctor.load_file(adoc_file, safe: :unsafe, parse: false).reader.read_lines)
|
38
|
+
end
|
67
39
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
transitions from: :in_block, to: :in_block, after: :add_line
|
72
|
-
transitions from: :idle, to: :idle
|
40
|
+
def wrong_explicit_checksum?
|
41
|
+
@definitions.reduce(false) do |res, definition|
|
42
|
+
res || !definition.wrong_explicit_checksum.nil?
|
73
43
|
end
|
44
|
+
end
|
74
45
|
|
75
|
-
|
76
|
-
|
77
|
-
|
46
|
+
def explicit_version?
|
47
|
+
@definitions.reduce(false) do |res, definition|
|
48
|
+
res || !definition.explicit_version.nil?
|
78
49
|
end
|
79
50
|
end
|
80
51
|
|
81
|
-
def code_block_delimiter(_match)
|
52
|
+
def code_block_delimiter(_match, line)
|
82
53
|
@in_literal ^= true
|
54
|
+
line
|
83
55
|
end
|
84
56
|
|
85
|
-
def add_new_definition(match)
|
57
|
+
def add_new_definition(match, line)
|
86
58
|
definition = Definition.new(match)
|
87
59
|
@labels.merge(definition.labels)
|
88
60
|
@definitions << definition
|
61
|
+
line
|
89
62
|
end
|
90
63
|
|
91
|
-
def
|
92
|
-
@definitions.
|
64
|
+
def ref_to_def(ref)
|
65
|
+
@definitions.find { |definition| definition.reference == ref }
|
93
66
|
end
|
94
67
|
|
95
|
-
def
|
96
|
-
@
|
68
|
+
def add_line(_match, line)
|
69
|
+
@definitions.last << line
|
70
|
+
line
|
71
|
+
end
|
97
72
|
|
98
|
-
|
99
|
-
|
73
|
+
def new_eref_setup(match, line)
|
74
|
+
@eref[match[:refname].to_sym] ||= {}
|
75
|
+
@eref[match[:refname].to_sym][match[:symb].to_sym] = match[:value]
|
76
|
+
line
|
100
77
|
end
|
101
78
|
|
102
|
-
def new_eref_def(match)
|
103
|
-
@definitions.last.add_eref(
|
104
|
-
|
105
|
-
match[:extrefs]
|
106
|
-
)
|
79
|
+
def new_eref_def(match, line)
|
80
|
+
@definitions.last.add_eref(match[:refname].to_sym, match[:extrefs])
|
81
|
+
line
|
107
82
|
end
|
108
83
|
|
109
|
-
def new_iref_def(_match)
|
84
|
+
def new_iref_def(_match, line)
|
110
85
|
@iref = true
|
111
|
-
|
86
|
+
line.scan(DMRegexp::IREF_DEF) do |_|
|
112
87
|
@definitions.last.add_iref(Regexp.last_match[:intref])
|
113
88
|
end
|
89
|
+
line
|
114
90
|
end
|
115
91
|
|
116
|
-
def new_attribute_conf(match)
|
92
|
+
def new_attribute_conf(match, line)
|
117
93
|
@attributes[match[:attr].to_sym] = match[:prefix]
|
94
|
+
line
|
118
95
|
end
|
119
96
|
|
120
|
-
def new_attribute_value(match)
|
121
|
-
@definitions.last.set_attribute(
|
122
|
-
|
123
|
-
match[:value]
|
124
|
-
)
|
97
|
+
def new_attribute_value(match, line)
|
98
|
+
@definitions.last.set_attribute(match[:attr].to_sym, match[:value])
|
99
|
+
line
|
125
100
|
end
|
126
101
|
|
127
|
-
def new_variable_def(match)
|
102
|
+
def new_variable_def(match, line)
|
128
103
|
@variables[match[:varname].to_sym] = match[:value]
|
104
|
+
line
|
129
105
|
end
|
130
106
|
|
131
|
-
def new_variable_use(_match)
|
132
|
-
|
107
|
+
def new_variable_use(_match, line)
|
108
|
+
line.scan(DMRegexp::VARIABLE_USE) do |_|
|
133
109
|
varname = Regexp.last_match[:varname]
|
134
110
|
next if @variables[varname.to_sym].nil?
|
135
111
|
|
136
|
-
|
137
|
-
"{#{varname}}", @variables[varname.to_sym]
|
138
|
-
)
|
112
|
+
line = line.gsub("{#{varname}}", @variables[varname.to_sym])
|
139
113
|
end
|
114
|
+
line
|
140
115
|
end
|
141
116
|
|
142
|
-
|
143
|
-
lines.reject(&:commented?).each do |line|
|
144
|
-
@current_line = line
|
145
|
-
(@in_literal ? FILTERS : FILTERS_IN_LITERAL).each do |filter|
|
146
|
-
next unless line.match(filter.regexp)
|
117
|
+
private
|
147
118
|
|
148
|
-
|
149
|
-
|
150
|
-
|
119
|
+
def generate_event(event, match, line)
|
120
|
+
if respond_to?(event)
|
121
|
+
line = public_send(event, match, line)
|
122
|
+
else
|
123
|
+
@definition_parser.public_send(event, match, line)
|
151
124
|
end
|
125
|
+
line
|
152
126
|
end
|
153
127
|
end
|
154
128
|
end
|