varar-core 0.8.0 → 0.9.0
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 +4 -4
- data/lib/varar/core/ast.rb +4 -1
- data/lib/varar/core/cell_diff.rb +23 -9
- data/lib/varar/core/conformance.rb +12 -6
- data/lib/varar/core/diagnostics.rb +57 -2
- data/lib/varar/core/drift.rb +158 -84
- data/lib/varar/core/execute.rb +12 -8
- data/lib/varar/core/failure.rb +2 -1
- data/lib/varar/core/failure_anchor.rb +17 -0
- data/lib/varar/core/hash.rb +1 -1
- data/lib/varar/core/matcher.rb +10 -13
- data/lib/varar/core/parse.rb +2 -1
- data/lib/varar/core/plan.rb +232 -50
- data/lib/varar/core/reference.rb +142 -0
- data/lib/varar/core/registry.rb +7 -3
- data/lib/varar/core/result.rb +22 -6
- data/lib/varar/core/scanner.rb +54 -37
- data/lib/varar/core/sentences.rb +41 -34
- data/lib/varar/core/span.rb +111 -38
- data/lib/varar/core/step_index.rb +124 -0
- data/lib/varar/core/structurer.rb +6 -3
- data/lib/varar/core/table_cells.rb +4 -3
- data/lib/varar/core.rb +1 -2
- metadata +6 -5
- data/lib/varar/core/canonical_json.rb +0 -47
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'varar/core/ast'
|
|
4
|
+
|
|
5
|
+
module Varar
|
|
6
|
+
module Core
|
|
7
|
+
# Reuse is a link (ADR 0016). A candidate block whose entire content is a
|
|
8
|
+
# single Markdown link to an oath section is a REFERENCE BLOCK: it splices
|
|
9
|
+
# that section's steps in at its own position instead of being prose.
|
|
10
|
+
#
|
|
11
|
+
# Everything here is pure text and path arithmetic — no filesystem. The
|
|
12
|
+
# shell reads the documents; `references` tells it which ones to read, and
|
|
13
|
+
# `build_workspace` turns the collection into what `plan` needs.
|
|
14
|
+
module Reference
|
|
15
|
+
# The referenced oath's path (resolved against the referring doc's own
|
|
16
|
+
# path), the GFM slug of the heading, and the link's visible text.
|
|
17
|
+
Ref = Data.define(:path, :slug, :text)
|
|
18
|
+
|
|
19
|
+
# What `plan` needs to resolve references: every oath by path, plus which
|
|
20
|
+
# sections a reference block consumes somewhere in the project. A section
|
|
21
|
+
# that is referenced stops being a standalone example, so this is
|
|
22
|
+
# whole-project knowledge — see ADR 0016 on why each runner builds it at
|
|
23
|
+
# its once-per-run discovery pass.
|
|
24
|
+
Workspace = Data.define(:docs, :referenced)
|
|
25
|
+
|
|
26
|
+
# A candidate is a reference block iff its whole text is one Markdown link
|
|
27
|
+
# whose target is oath-shaped. Anything else — a link with surrounding
|
|
28
|
+
# words, a link to https://…, to a .rb file, to a mailto: — is ordinary
|
|
29
|
+
# content, so existing documents keep their meaning.
|
|
30
|
+
LINK_ONLY = /\A\[([^\]]*)\]\(\s*([^\s)]+)\s*\)\z/
|
|
31
|
+
PROTOCOL = /\A[a-z][a-z0-9+.-]*:/i
|
|
32
|
+
|
|
33
|
+
module_function
|
|
34
|
+
|
|
35
|
+
def reference_of(text, from_path)
|
|
36
|
+
m = LINK_ONLY.match(text.strip)
|
|
37
|
+
return nil if m.nil?
|
|
38
|
+
|
|
39
|
+
link_text = m[1]
|
|
40
|
+
target = m[2]
|
|
41
|
+
return Ref.new(path: from_path, slug: normalize_slug(target[1..]), text: link_text) if target.start_with?('#')
|
|
42
|
+
|
|
43
|
+
hash_at = target.index('#')
|
|
44
|
+
file_part = hash_at.nil? ? target : target[0...hash_at]
|
|
45
|
+
fragment = hash_at.nil? ? '' : target[(hash_at + 1)..]
|
|
46
|
+
# Only a relative Markdown path is a reference. A protocol (https:,
|
|
47
|
+
# mailto:) or any other extension is left alone — remote references are
|
|
48
|
+
# deliberately out of scope (ADR 0016).
|
|
49
|
+
return nil unless file_part.end_with?('.md')
|
|
50
|
+
return nil if PROTOCOL.match?(file_part) || file_part.start_with?('/')
|
|
51
|
+
|
|
52
|
+
Ref.new(path: join_posix(dirname_posix(from_path), file_part), slug: normalize_slug(fragment),
|
|
53
|
+
text: link_text)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# GitHub's heading anchors: inline markup dropped, lowercased, spaces to
|
|
57
|
+
# hyphens, everything else that isn't a word character or hyphen removed.
|
|
58
|
+
# The same function produces the slug of a heading and normalizes the slug
|
|
59
|
+
# written in a link, so the two meet in the middle.
|
|
60
|
+
def slugify(heading_text)
|
|
61
|
+
stripped = heading_text.gsub(/`([^`]*)`/, '\1')
|
|
62
|
+
.gsub(/\*\*([^*]*)\*\*/, '\1')
|
|
63
|
+
.gsub(/\*([^*]*)\*/, '\1')
|
|
64
|
+
.gsub(/_([^_]*)_/, '\1')
|
|
65
|
+
normalize_slug(stripped)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# One hyphen per space, not per run of them: GitHub leaves the gap where
|
|
69
|
+
# it dropped punctuation, so "Fees, VAT & rounding" slugs with a double
|
|
70
|
+
# hyphen.
|
|
71
|
+
def normalize_slug(str)
|
|
72
|
+
str.strip.downcase.gsub(/[^[[:word:]] -]/, '').tr(' ', '-')
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def dirname_posix(path)
|
|
76
|
+
i = path.rindex('/')
|
|
77
|
+
i.nil? ? '' : path[0...i]
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# POSIX path arithmetic on oath paths (always '/'-separated, relative to
|
|
81
|
+
# the workspace root). The core may not touch the filesystem. A link that
|
|
82
|
+
# climbs above the root keeps its leading `../`, as the oath-path
|
|
83
|
+
# convention does for an oath outside the root.
|
|
84
|
+
def join_posix(dir, rel)
|
|
85
|
+
segments = dir.empty? ? [] : dir.split('/')
|
|
86
|
+
rel.split('/').each do |segment|
|
|
87
|
+
next if segment.empty? || segment == '.'
|
|
88
|
+
|
|
89
|
+
if segment != '..'
|
|
90
|
+
segments << segment
|
|
91
|
+
elsif !segments.empty? && segments.last != '..'
|
|
92
|
+
segments.pop
|
|
93
|
+
else
|
|
94
|
+
segments << '..'
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
segments.join('/')
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# Every reference block in a document, in document order. The shell uses
|
|
101
|
+
# this to walk the closure of documents it must read before planning.
|
|
102
|
+
def references(doc)
|
|
103
|
+
doc.examples.filter_map do |ex|
|
|
104
|
+
primary = ex.body.first
|
|
105
|
+
next nil unless primary.respond_to?(:text)
|
|
106
|
+
|
|
107
|
+
reference_of(primary.text, doc.path)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def section_key(path, slug)
|
|
112
|
+
"#{path}##{slug}"
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# The workspace with no references at all: what a caller planning a single
|
|
116
|
+
# document in isolation passes.
|
|
117
|
+
def empty_workspace
|
|
118
|
+
Workspace.new(docs: {}, referenced: Set.new)
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def build_workspace(docs)
|
|
122
|
+
by_path = docs.to_h { |doc| [doc.path, doc] }
|
|
123
|
+
referenced = Set.new
|
|
124
|
+
docs.each do |doc|
|
|
125
|
+
references(doc).each { |ref| referenced << section_key(ref.path, ref.slug) }
|
|
126
|
+
end
|
|
127
|
+
Workspace.new(docs: by_path, referenced: referenced)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# The candidates that make up a section: those whose heading chain
|
|
131
|
+
# contains the slug. A whole-file reference ('' slug) is every candidate.
|
|
132
|
+
# Section membership follows the document outline exactly — a heading's
|
|
133
|
+
# section runs until the next heading of the same or higher level, which
|
|
134
|
+
# is precisely the range over which it stays on the scope stack.
|
|
135
|
+
def section_candidates(doc, slug)
|
|
136
|
+
return doc.examples if slug.empty?
|
|
137
|
+
|
|
138
|
+
doc.examples.select { |ex| ex.scope_stack.any? { |h| slugify(h) == slug } }
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|
data/lib/varar/core/registry.rb
CHANGED
|
@@ -3,13 +3,16 @@
|
|
|
3
3
|
require 'cucumber/cucumber_expressions/cucumber_expression'
|
|
4
4
|
require 'cucumber/cucumber_expressions/parameter_type'
|
|
5
5
|
require 'cucumber/cucumber_expressions/parameter_type_registry'
|
|
6
|
+
require 'varar/core/step_index'
|
|
6
7
|
|
|
7
8
|
module Varar
|
|
8
9
|
module Core
|
|
9
|
-
# One registered step definition. `compiled` is the CucumberExpression
|
|
10
|
+
# One registered step definition. `compiled` is the CucumberExpression;
|
|
11
|
+
# `scan` (a StepScan) is derived from it when the step is registered:
|
|
12
|
+
# what the matcher scans sentences with.
|
|
10
13
|
StepRegistration = Data.define(
|
|
11
14
|
:expression, :expression_source_file, :expression_source_line,
|
|
12
|
-
:handler, :compiled, :kind
|
|
15
|
+
:handler, :compiled, :kind, :scan
|
|
13
16
|
)
|
|
14
17
|
|
|
15
18
|
# A registry of step definitions plus the shared cucumber ParameterTypeRegistry
|
|
@@ -74,7 +77,8 @@ module Varar
|
|
|
74
77
|
expression_source_line: expression_source_line,
|
|
75
78
|
handler: handler,
|
|
76
79
|
compiled: compiled,
|
|
77
|
-
kind: kind
|
|
80
|
+
kind: kind,
|
|
81
|
+
scan: StepIndex.step_scan(expression, compiled)
|
|
78
82
|
)
|
|
79
83
|
registry.with(steps: registry.steps + [reg])
|
|
80
84
|
end
|
data/lib/varar/core/result.rb
CHANGED
|
@@ -21,8 +21,11 @@ module Varar
|
|
|
21
21
|
# nil when they do not apply, and serialize as absent (not null), so a
|
|
22
22
|
# reader that predates them still parses the file. `stack` is deliberately
|
|
23
23
|
# runtime-shaped — no consumer parses it.
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
# `doc_path` is the document `line`, `cells` and `anchor` are offsets INTO.
|
|
25
|
+
# nil — the overwhelming majority — means the oath itself; set only for a
|
|
26
|
+
# step a reference block spliced in from another oath (ADR 0016).
|
|
27
|
+
ExampleFailure = Data.define(:line, :message, :stack, :cells, :anchor, :doc_path) do
|
|
28
|
+
def initialize(line:, message:, stack:, cells: nil, anchor: nil, doc_path: nil)
|
|
26
29
|
super
|
|
27
30
|
end
|
|
28
31
|
end
|
|
@@ -39,7 +42,15 @@ module Varar
|
|
|
39
42
|
# separators and is relative to the workspace root; `source_hash` is
|
|
40
43
|
# Hashing.hash_source over the oath as it was run, so a reader can tell
|
|
41
44
|
# whether the offsets still apply to the buffer in front of it.
|
|
42
|
-
|
|
45
|
+
# An oath other than this one that contributed steps to the run, with its
|
|
46
|
+
# source hash as run (ADR 0016).
|
|
47
|
+
ReferencedDocument = Data.define(:path, :source_hash)
|
|
48
|
+
|
|
49
|
+
OathResults = Data.define(:version, :oath_path, :source_hash, :examples, :documents) do
|
|
50
|
+
def initialize(version:, oath_path:, source_hash:, examples:, documents: [])
|
|
51
|
+
super
|
|
52
|
+
end
|
|
53
|
+
end
|
|
43
54
|
|
|
44
55
|
# Projection of OathResults onto the JSON shape of .varar/<oath_path>.json.
|
|
45
56
|
#
|
|
@@ -51,12 +62,16 @@ module Varar
|
|
|
51
62
|
module_function
|
|
52
63
|
|
|
53
64
|
def to_wire(results)
|
|
54
|
-
{
|
|
65
|
+
out = {
|
|
55
66
|
'version' => results.version,
|
|
56
67
|
'oathPath' => results.oath_path,
|
|
57
|
-
'sourceHash' => results.source_hash
|
|
58
|
-
'examples' => results.examples.map { |e| example_to_wire(e) }
|
|
68
|
+
'sourceHash' => results.source_hash
|
|
59
69
|
}
|
|
70
|
+
unless results.documents.empty?
|
|
71
|
+
out['documents'] = results.documents.map { |d| { 'path' => d.path, 'sourceHash' => d.source_hash } }
|
|
72
|
+
end
|
|
73
|
+
out['examples'] = results.examples.map { |e| example_to_wire(e) }
|
|
74
|
+
out
|
|
60
75
|
end
|
|
61
76
|
|
|
62
77
|
def example_to_wire(example)
|
|
@@ -71,6 +86,7 @@ module Varar
|
|
|
71
86
|
out['cells'] = failure.cells.map { |c| { 'from' => c.from, 'to' => c.to, 'actual' => c.actual } }
|
|
72
87
|
end
|
|
73
88
|
out['anchor'] = { 'from' => failure.anchor.from, 'to' => failure.anchor.to } if failure.anchor
|
|
89
|
+
out['docPath'] = failure.doc_path if failure.doc_path
|
|
74
90
|
out
|
|
75
91
|
end
|
|
76
92
|
end
|
data/lib/varar/core/scanner.rb
CHANGED
|
@@ -26,8 +26,9 @@ module Varar
|
|
|
26
26
|
|
|
27
27
|
module_function
|
|
28
28
|
|
|
29
|
-
# Scan +source+ into an immutable Array of Block nodes.
|
|
30
|
-
|
|
29
|
+
# Scan +source+ into an immutable Array of Block nodes. +idx+ is the
|
|
30
|
+
# source's Offsets.index, built here unless the caller already has one.
|
|
31
|
+
def scan(source, plugins = [], idx = Offsets.index(source))
|
|
31
32
|
blocks = []
|
|
32
33
|
lines = split_lines(source)
|
|
33
34
|
|
|
@@ -39,56 +40,56 @@ module Varar
|
|
|
39
40
|
next
|
|
40
41
|
end
|
|
41
42
|
|
|
42
|
-
matched = run_plugins(
|
|
43
|
+
matched = run_plugins(idx, lines, i, plugins)
|
|
43
44
|
if matched
|
|
44
45
|
blocks << matched[0]
|
|
45
46
|
i = matched[1]
|
|
46
47
|
next
|
|
47
48
|
end
|
|
48
49
|
|
|
49
|
-
fence_result = try_fence(
|
|
50
|
+
fence_result = try_fence(idx, lines, i)
|
|
50
51
|
if fence_result
|
|
51
52
|
blocks << fence_result[0]
|
|
52
53
|
i = fence_result[1]
|
|
53
54
|
next
|
|
54
55
|
end
|
|
55
56
|
|
|
56
|
-
table_result = try_table(
|
|
57
|
+
table_result = try_table(idx, lines, i)
|
|
57
58
|
if table_result
|
|
58
59
|
blocks << table_result[0]
|
|
59
60
|
i = table_result[1]
|
|
60
61
|
next
|
|
61
62
|
end
|
|
62
63
|
|
|
63
|
-
thematic = try_thematic(
|
|
64
|
+
thematic = try_thematic(idx, line)
|
|
64
65
|
if thematic
|
|
65
66
|
blocks << thematic
|
|
66
67
|
i += 1
|
|
67
68
|
next
|
|
68
69
|
end
|
|
69
70
|
|
|
70
|
-
bq_result = try_blockquote(
|
|
71
|
+
bq_result = try_blockquote(idx, lines, i)
|
|
71
72
|
if bq_result
|
|
72
73
|
blocks << bq_result[0]
|
|
73
74
|
i = bq_result[1]
|
|
74
75
|
next
|
|
75
76
|
end
|
|
76
77
|
|
|
77
|
-
heading = try_heading(
|
|
78
|
+
heading = try_heading(idx, line)
|
|
78
79
|
if heading
|
|
79
80
|
blocks << heading
|
|
80
81
|
i += 1
|
|
81
82
|
next
|
|
82
83
|
end
|
|
83
84
|
|
|
84
|
-
list_item = try_list_item(
|
|
85
|
+
list_item = try_list_item(idx, line)
|
|
85
86
|
if list_item
|
|
86
87
|
blocks << list_item
|
|
87
88
|
i += 1
|
|
88
89
|
next
|
|
89
90
|
end
|
|
90
91
|
|
|
91
|
-
paragraph, next_i = consume_paragraph(
|
|
92
|
+
paragraph, next_i = consume_paragraph(idx, lines, i, plugins)
|
|
92
93
|
blocks << paragraph
|
|
93
94
|
i = next_i
|
|
94
95
|
end
|
|
@@ -96,9 +97,9 @@ module Varar
|
|
|
96
97
|
blocks
|
|
97
98
|
end
|
|
98
99
|
|
|
99
|
-
def run_plugins(
|
|
100
|
+
def run_plugins(idx, lines, start_idx, plugins)
|
|
100
101
|
plugins.each do |p|
|
|
101
|
-
r = p.try_scan(source: source, lines: lines, start_idx: start_idx)
|
|
102
|
+
r = p.try_scan(source: idx.source, lines: lines, start_idx: start_idx)
|
|
102
103
|
return r if r
|
|
103
104
|
end
|
|
104
105
|
nil
|
|
@@ -106,6 +107,8 @@ module Varar
|
|
|
106
107
|
|
|
107
108
|
# Split +source+ into RawLines with UTF-16 start/end offsets.
|
|
108
109
|
def split_lines(source)
|
|
110
|
+
return split_ascii_lines(source) if source.ascii_only?
|
|
111
|
+
|
|
109
112
|
out = []
|
|
110
113
|
start_u16 = 0
|
|
111
114
|
current_u16 = 0
|
|
@@ -124,24 +127,38 @@ module Varar
|
|
|
124
127
|
out
|
|
125
128
|
end
|
|
126
129
|
|
|
127
|
-
|
|
130
|
+
# split_lines for a source whose characters are each one UTF-16 unit:
|
|
131
|
+
# String#split finds the newlines without visiting every character from
|
|
132
|
+
# Ruby. A source with no newline (even an empty one) is one line.
|
|
133
|
+
def split_ascii_lines(source)
|
|
134
|
+
texts = source.split("\n", -1)
|
|
135
|
+
texts = [''] if texts.empty?
|
|
136
|
+
start = 0
|
|
137
|
+
texts.map do |text|
|
|
138
|
+
line = RawLine.new(text: text, start_offset: start, end_offset: start + text.length)
|
|
139
|
+
start += text.length + 1
|
|
140
|
+
line
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def try_thematic(idx, line)
|
|
128
145
|
return nil unless THEMATIC_RE.match?(line.text)
|
|
129
146
|
|
|
130
|
-
ThematicBreak.new(span: Offsets.
|
|
147
|
+
ThematicBreak.new(span: Offsets.span_at(idx, line.start_offset, line.end_offset))
|
|
131
148
|
end
|
|
132
149
|
|
|
133
|
-
def try_heading(
|
|
150
|
+
def try_heading(idx, line)
|
|
134
151
|
m = HEADING_RE.match(line.text)
|
|
135
152
|
return nil unless m
|
|
136
153
|
|
|
137
154
|
Heading.new(
|
|
138
155
|
level: m[1].length,
|
|
139
156
|
text: (m[2] || '').strip,
|
|
140
|
-
span: Offsets.
|
|
157
|
+
span: Offsets.span_at(idx, line.start_offset, line.end_offset)
|
|
141
158
|
)
|
|
142
159
|
end
|
|
143
160
|
|
|
144
|
-
def try_list_item(
|
|
161
|
+
def try_list_item(idx, line)
|
|
145
162
|
if (ul = UL_RE.match(line.text))
|
|
146
163
|
text = ul[3] || ''
|
|
147
164
|
marker_start = line.start_offset + Offsets.utf16_len(ul[1] || '')
|
|
@@ -150,10 +167,10 @@ module Varar
|
|
|
150
167
|
text_start = line.start_offset + Offsets.to_utf16_offset(line.text, cp_idx)
|
|
151
168
|
return ListItem.new(
|
|
152
169
|
text: text,
|
|
153
|
-
span: Offsets.
|
|
170
|
+
span: Offsets.span_at(idx, line.start_offset, line.end_offset),
|
|
154
171
|
segment_map: [SegmentOffset.new(text_offset: 0, source_offset: text_start)],
|
|
155
172
|
ordered: false,
|
|
156
|
-
marker_span: Offsets.
|
|
173
|
+
marker_span: Offsets.span_at(idx, marker_start, marker_end)
|
|
157
174
|
)
|
|
158
175
|
end
|
|
159
176
|
|
|
@@ -165,17 +182,17 @@ module Varar
|
|
|
165
182
|
text_start = line.start_offset + Offsets.to_utf16_offset(line.text, cp_idx)
|
|
166
183
|
return ListItem.new(
|
|
167
184
|
text: text,
|
|
168
|
-
span: Offsets.
|
|
185
|
+
span: Offsets.span_at(idx, line.start_offset, line.end_offset),
|
|
169
186
|
segment_map: [SegmentOffset.new(text_offset: 0, source_offset: text_start)],
|
|
170
187
|
ordered: true,
|
|
171
|
-
marker_span: Offsets.
|
|
188
|
+
marker_span: Offsets.span_at(idx, marker_start, marker_end)
|
|
172
189
|
)
|
|
173
190
|
end
|
|
174
191
|
|
|
175
192
|
nil
|
|
176
193
|
end
|
|
177
194
|
|
|
178
|
-
def try_blockquote(
|
|
195
|
+
def try_blockquote(idx, lines, start_idx)
|
|
179
196
|
return nil if start_idx >= lines.length
|
|
180
197
|
|
|
181
198
|
first = lines[start_idx]
|
|
@@ -216,14 +233,14 @@ module Varar
|
|
|
216
233
|
[
|
|
217
234
|
Blockquote.new(
|
|
218
235
|
text: segments.join("\n"),
|
|
219
|
-
span: Offsets.
|
|
236
|
+
span: Offsets.span_at(idx, first.start_offset, end_offset),
|
|
220
237
|
segment_map: segment_map
|
|
221
238
|
),
|
|
222
239
|
i
|
|
223
240
|
]
|
|
224
241
|
end
|
|
225
242
|
|
|
226
|
-
def consume_paragraph(
|
|
243
|
+
def consume_paragraph(idx, lines, start_idx, plugins)
|
|
227
244
|
raise 'invariant: start_idx out of range' if start_idx >= lines.length
|
|
228
245
|
|
|
229
246
|
first = lines[start_idx]
|
|
@@ -239,7 +256,7 @@ module Varar
|
|
|
239
256
|
break if FENCE_RE.match?(candidate.text)
|
|
240
257
|
break if ROW_RE.match?(candidate.text)
|
|
241
258
|
break if THEMATIC_RE.match?(candidate.text)
|
|
242
|
-
break if run_plugins(
|
|
259
|
+
break if run_plugins(idx, lines, candidate_idx, plugins)
|
|
243
260
|
|
|
244
261
|
end_idx += 1
|
|
245
262
|
end
|
|
@@ -249,15 +266,15 @@ module Varar
|
|
|
249
266
|
end_offset = last.end_offset
|
|
250
267
|
[
|
|
251
268
|
Paragraph.new(
|
|
252
|
-
text: Offsets.
|
|
253
|
-
span: Offsets.
|
|
269
|
+
text: Offsets.slice_at(idx, start_offset, end_offset),
|
|
270
|
+
span: Offsets.span_at(idx, start_offset, end_offset),
|
|
254
271
|
segment_map: [SegmentOffset.new(text_offset: 0, source_offset: start_offset)]
|
|
255
272
|
),
|
|
256
273
|
end_idx + 1
|
|
257
274
|
]
|
|
258
275
|
end
|
|
259
276
|
|
|
260
|
-
def try_fence(
|
|
277
|
+
def try_fence(idx, lines, start_idx)
|
|
261
278
|
return nil if start_idx >= lines.length
|
|
262
279
|
|
|
263
280
|
start = lines[start_idx]
|
|
@@ -284,22 +301,22 @@ module Varar
|
|
|
284
301
|
i += 1
|
|
285
302
|
end
|
|
286
303
|
|
|
287
|
-
body = body_start.nil? || body_end.nil? ? '' : Offsets.
|
|
304
|
+
body = body_start.nil? || body_end.nil? ? '' : Offsets.slice_at(idx, body_start, body_end)
|
|
288
305
|
|
|
289
306
|
fallback = start.end_offset
|
|
290
|
-
body_span = Offsets.
|
|
307
|
+
body_span = Offsets.span_at(idx, body_start || fallback, body_end || fallback)
|
|
291
308
|
[
|
|
292
309
|
Fence.new(
|
|
293
310
|
info: info,
|
|
294
311
|
body: body,
|
|
295
312
|
body_span: body_span,
|
|
296
|
-
span: Offsets.
|
|
313
|
+
span: Offsets.span_at(idx, start.start_offset, end_offset)
|
|
297
314
|
),
|
|
298
315
|
i + 1
|
|
299
316
|
]
|
|
300
317
|
end
|
|
301
318
|
|
|
302
|
-
def try_table(
|
|
319
|
+
def try_table(idx, lines, start_idx)
|
|
303
320
|
return nil if start_idx + 1 >= lines.length
|
|
304
321
|
|
|
305
322
|
header_line = lines[start_idx]
|
|
@@ -308,11 +325,11 @@ module Varar
|
|
|
308
325
|
return nil unless DELIM_RE.match?(delim_line.text)
|
|
309
326
|
|
|
310
327
|
header_cells, header_cell_spans = TableCells.parse_row_cells(header_line.text, header_line.start_offset,
|
|
311
|
-
|
|
328
|
+
idx)
|
|
312
329
|
header = Row.new(
|
|
313
330
|
cells: header_cells,
|
|
314
331
|
cell_spans: header_cell_spans,
|
|
315
|
-
span: Offsets.
|
|
332
|
+
span: Offsets.span_at(idx, header_line.start_offset, header_line.end_offset)
|
|
316
333
|
)
|
|
317
334
|
|
|
318
335
|
rows = []
|
|
@@ -321,11 +338,11 @@ module Varar
|
|
|
321
338
|
ln = lines[i]
|
|
322
339
|
break unless ROW_RE.match?(ln.text)
|
|
323
340
|
|
|
324
|
-
cells, cell_spans = TableCells.parse_row_cells(ln.text, ln.start_offset,
|
|
341
|
+
cells, cell_spans = TableCells.parse_row_cells(ln.text, ln.start_offset, idx)
|
|
325
342
|
rows << Row.new(
|
|
326
343
|
cells: cells,
|
|
327
344
|
cell_spans: cell_spans,
|
|
328
|
-
span: Offsets.
|
|
345
|
+
span: Offsets.span_at(idx, ln.start_offset, ln.end_offset)
|
|
329
346
|
)
|
|
330
347
|
i += 1
|
|
331
348
|
end
|
|
@@ -334,7 +351,7 @@ module Varar
|
|
|
334
351
|
end_offset = last_row ? last_row.span.end_offset : delim_line.end_offset
|
|
335
352
|
[
|
|
336
353
|
Table.new(
|
|
337
|
-
span: Offsets.
|
|
354
|
+
span: Offsets.span_at(idx, header_line.start_offset, end_offset),
|
|
338
355
|
header: header,
|
|
339
356
|
rows: rows
|
|
340
357
|
),
|
data/lib/varar/core/sentences.rb
CHANGED
|
@@ -10,57 +10,62 @@ module Varar
|
|
|
10
10
|
|
|
11
11
|
module Sentences
|
|
12
12
|
ABBREVIATIONS = Set.new(['e.g.', 'i.e.', 'etc.', 'cf.', 'vs.']).freeze
|
|
13
|
+
TERMINATOR = /[\n.!?]/
|
|
14
|
+
QUOTE = /[`"]/
|
|
13
15
|
|
|
14
16
|
module_function
|
|
15
17
|
|
|
18
|
+
# Jumps from one terminator to the next with a regexp search rather than
|
|
19
|
+
# visiting every character: the rules are the same, but planning a large
|
|
20
|
+
# project splits tens of thousands of blocks.
|
|
16
21
|
def split_sentences(text)
|
|
17
|
-
|
|
22
|
+
# An ASCII string's code points are its UTF-16 units.
|
|
23
|
+
cp_to_u16 = text.ascii_only? ? nil : build_cp_to_u16(text)
|
|
18
24
|
n = text.length
|
|
19
25
|
out = []
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
skip = Array.new(n, false)
|
|
23
|
-
j = 0
|
|
24
|
-
while j < n
|
|
25
|
-
c = text[j]
|
|
26
|
-
if ['`', '"'].include?(c)
|
|
27
|
-
close = text.index(c, j + 1)
|
|
28
|
-
break if close.nil?
|
|
29
|
-
|
|
30
|
-
(j..close).each { |k| skip[k] = true }
|
|
31
|
-
j = close + 1
|
|
32
|
-
next
|
|
33
|
-
end
|
|
34
|
-
j += 1
|
|
35
|
-
end
|
|
26
|
+
zones = no_split_zones(text)
|
|
27
|
+
zone = 0
|
|
36
28
|
|
|
37
29
|
i = 0
|
|
38
30
|
segment_start = 0
|
|
39
31
|
while i < n
|
|
40
|
-
|
|
32
|
+
i = text.index(TERMINATOR, i)
|
|
33
|
+
break if i.nil?
|
|
34
|
+
|
|
35
|
+
zone += 1 while zone < zones.length && zones[zone].end < i
|
|
36
|
+
if (zone < zones.length && zones[zone].cover?(i)) ||
|
|
37
|
+
(text[i] == '.' && inside_number_or_abbrev?(text, i))
|
|
41
38
|
i += 1
|
|
42
39
|
next
|
|
43
40
|
end
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
end
|
|
50
|
-
stop = i + 1
|
|
51
|
-
push_segment(out, text, segment_start, stop, cp_to_u16)
|
|
52
|
-
i = stop
|
|
53
|
-
i += 1 while i < n && [' ', "\n"].include?(text[i])
|
|
54
|
-
segment_start = i
|
|
55
|
-
next
|
|
56
|
-
end
|
|
57
|
-
i += 1
|
|
41
|
+
stop = i + 1
|
|
42
|
+
push_segment(out, text, segment_start, stop, cp_to_u16)
|
|
43
|
+
i = stop
|
|
44
|
+
i += 1 while i < n && [' ', "\n"].include?(text[i])
|
|
45
|
+
segment_start = i
|
|
58
46
|
end
|
|
59
47
|
|
|
60
48
|
push_segment(out, text, segment_start, n, cp_to_u16)
|
|
61
49
|
out
|
|
62
50
|
end
|
|
63
51
|
|
|
52
|
+
# Backtick spans and double-quoted strings, as ascending inclusive
|
|
53
|
+
# ranges of code-point indexes: terminators inside them don't split. An
|
|
54
|
+
# opening mark with no closing one ends the search — nothing after it
|
|
55
|
+
# is protected.
|
|
56
|
+
def no_split_zones(text)
|
|
57
|
+
zones = []
|
|
58
|
+
j = 0
|
|
59
|
+
while (j = text.index(QUOTE, j))
|
|
60
|
+
close = text.index(text[j], j + 1)
|
|
61
|
+
break if close.nil?
|
|
62
|
+
|
|
63
|
+
zones << (j..close)
|
|
64
|
+
j = close + 1
|
|
65
|
+
end
|
|
66
|
+
zones
|
|
67
|
+
end
|
|
68
|
+
|
|
64
69
|
# cp_to_u16[cp_i] is the UTF-16 offset of text[cp_i].
|
|
65
70
|
def build_cp_to_u16(text)
|
|
66
71
|
result = Array.new(text.length + 1, 0)
|
|
@@ -94,10 +99,12 @@ module Varar
|
|
|
94
99
|
|
|
95
100
|
lead = raw.length - raw.lstrip.length
|
|
96
101
|
trail = raw.length - raw.rstrip.length
|
|
102
|
+
start = start_cp + lead
|
|
103
|
+
stop = end_cp - trail
|
|
97
104
|
out << Sentence.new(
|
|
98
105
|
text: stripped,
|
|
99
|
-
start_offset: cp_to_u16[
|
|
100
|
-
end_offset: cp_to_u16[
|
|
106
|
+
start_offset: cp_to_u16 ? cp_to_u16[start] : start,
|
|
107
|
+
end_offset: cp_to_u16 ? cp_to_u16[stop] : stop
|
|
101
108
|
)
|
|
102
109
|
end
|
|
103
110
|
|