suada 0.1.0 → 0.2.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/README.md +51 -2
- data/lib/suada/assets/motion.js +287 -0
- data/lib/suada/assets/presenter.html +1 -1
- data/lib/suada/assets/presenter.js +4 -4
- data/lib/suada/assets/shell.js +12 -11
- data/lib/suada/assets/viewer.js +103 -45
- data/lib/suada/attributes.rb +57 -0
- data/lib/suada/browser_output.rb +23 -10
- data/lib/suada/checker.rb +2 -2
- data/lib/suada/cli.rb +8 -4
- data/lib/suada/defaults/css/base.css +22 -0
- data/lib/suada/diagnostics.rb +1 -1
- data/lib/suada/emitter/distribution.rb +10 -5
- data/lib/suada/errors.rb +5 -4
- data/lib/suada/fragment.rb +18 -1
- data/lib/suada/frontmatter.rb +5 -2
- data/lib/suada/layout_parser.rb +12 -1
- data/lib/suada/manifest.rb +2 -1
- data/lib/suada/mapper.rb +32 -29
- data/lib/suada/motion/diff.rb +57 -0
- data/lib/suada/motion/glyphs.rb +158 -0
- data/lib/suada/motion/keyer.rb +111 -0
- data/lib/suada/parser/block_classifier.rb +10 -7
- data/lib/suada/parser/code_attributes.rb +41 -0
- data/lib/suada/parser/inline_spans.rb +112 -0
- data/lib/suada/parser/page_settings.rb +3 -1
- data/lib/suada/parser/slot_fence.rb +25 -13
- data/lib/suada/parser/step_splitter.rb +38 -0
- data/lib/suada/parser.rb +68 -14
- data/lib/suada/pipeline.rb +17 -13
- data/lib/suada/renderer.rb +54 -14
- data/lib/suada/server.rb +2 -2
- data/lib/suada/slot_contract.rb +1 -1
- data/lib/suada/theme.rb +26 -6
- data/lib/suada/version.rb +1 -1
- data/sig/compiler.rbs +25 -11
- data/sig/motion.rbs +56 -0
- data/sig/parser.rbs +28 -2
- metadata +10 -1
data/lib/suada/mapper.rb
CHANGED
|
@@ -4,64 +4,67 @@ require_relative "checker"
|
|
|
4
4
|
|
|
5
5
|
module Suada
|
|
6
6
|
module Mapper
|
|
7
|
-
|
|
7
|
+
Frame = Data.define(:source, :step, :layout, :assigned, :unassigned)
|
|
8
8
|
module_function
|
|
9
9
|
|
|
10
|
-
def assign(source, layout, path)
|
|
11
|
-
assigned = layout.slots.transform_values {
|
|
10
|
+
def assign(source, step, layout, path, previous)
|
|
11
|
+
assigned = layout.slots.transform_values { nil }
|
|
12
|
+
fragments = step.fragments
|
|
13
|
+
inherited = {}
|
|
14
|
+
if step.attrs[:carry] && previous
|
|
15
|
+
# Only the preceding assignment knows the name of an implicitly mapped image slot.
|
|
16
|
+
inherited = previous.assigned
|
|
17
|
+
fragments = fragments.drop(step.carried.size)
|
|
18
|
+
end
|
|
12
19
|
unassigned = []
|
|
13
|
-
slide =
|
|
14
|
-
title =
|
|
15
|
-
|
|
20
|
+
slide = Frame.new(source:, step:, layout:, assigned:, unassigned:)
|
|
21
|
+
title = Fragment.title(fragments)
|
|
22
|
+
fragments.each do |part|
|
|
16
23
|
if part.kind == :slot_group
|
|
17
24
|
unless assigned.key?(part.slot)
|
|
18
25
|
Checker.fail_slide!(slide, path, :layout, part.line, "unknown slot '#{part.slot}' in layout '#{layout.name}'",
|
|
19
26
|
"use one of these slots: #{assigned.keys.join(', ')}")
|
|
20
27
|
end
|
|
21
|
-
assigned
|
|
28
|
+
(assigned[part.slot] ||= []).concat(part.children)
|
|
22
29
|
else
|
|
23
30
|
name = destination(part, title, slide, path)
|
|
24
|
-
assigned.key?(name) ? assigned
|
|
31
|
+
assigned.key?(name) ? (assigned[name] ||= []) << part : unassigned << [name, part]
|
|
25
32
|
end
|
|
26
33
|
end
|
|
27
|
-
assigned.
|
|
34
|
+
assigned.each { |name, items| assigned[name] = (items || inherited.fetch(name, [])).freeze }
|
|
28
35
|
assigned.freeze
|
|
29
36
|
unassigned.freeze
|
|
30
37
|
slide
|
|
31
38
|
end
|
|
32
39
|
|
|
33
40
|
def destination(part, title, slide, path)
|
|
34
|
-
|
|
41
|
+
name = Fragment.destination(part, title)
|
|
42
|
+
return name unless name == :image
|
|
35
43
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
names = slide.layout.slots.values.select { |slot| slot.accepts == :image }.map(&:name)
|
|
41
|
-
if names.size > 1
|
|
42
|
-
Checker.fail_slide!(slide, path, :layout, part.line, "multiple image slots in layout '#{slide.layout.name}'",
|
|
43
|
-
"use an explicit slot group to choose one of: #{names.join(', ')}")
|
|
44
|
-
end
|
|
45
|
-
names.first || :unassigned_image
|
|
44
|
+
names = slide.layout.slots.values.select { |slot| slot.accepts == :image }.map(&:name)
|
|
45
|
+
if names.size > 1
|
|
46
|
+
Checker.fail_slide!(slide, path, :layout, part.line, "multiple image slots in layout '#{slide.layout.name}'",
|
|
47
|
+
"use an explicit slot group to choose one of: #{names.join(', ')}")
|
|
46
48
|
end
|
|
49
|
+
names.first || :unassigned_image
|
|
47
50
|
end
|
|
48
51
|
|
|
49
|
-
def select(source, layouts, path)
|
|
50
|
-
request = source.settings[:layout]
|
|
51
|
-
header = "slide #{source.number} ('#{source.key}'), #{File.basename(path)}:#{
|
|
52
|
+
def select(source, step, layouts, path, previous = nil)
|
|
53
|
+
request = step.attrs[:layout] || source.settings[:layout]
|
|
54
|
+
header = "slide #{source.number} ('#{source.key}'), step #{step.number}, #{File.basename(path)}:#{step.line}"
|
|
52
55
|
if request || layouts.one?
|
|
53
56
|
selected = request ? layouts.find { |layout| layout.name == request } : layouts.first
|
|
54
57
|
unless selected
|
|
55
|
-
raise BuildError.new(kind: :dispatch, file: path, line:
|
|
56
|
-
slide_key: source.key, message: "unknown layout '#{request}'",
|
|
58
|
+
raise BuildError.new(kind: :dispatch, file: path, line: step.line, slide_number: source.number,
|
|
59
|
+
slide_key: source.key, step_number: step.number, message: "unknown layout '#{request}'",
|
|
57
60
|
help: "choose one of: #{layouts.map(&:name).join(', ')}")
|
|
58
61
|
end
|
|
59
|
-
return [assign(source, selected, path), "#{header}\n=> #{selected.name} (#{request ? 'explicit' : 'only layout'})"]
|
|
62
|
+
return [assign(source, step, selected, path, previous), "#{header}\n=> #{selected.name} (#{request ? 'explicit' : 'only layout'})"]
|
|
60
63
|
end
|
|
61
64
|
|
|
62
65
|
matches = []
|
|
63
66
|
rows = layouts.map do |layout|
|
|
64
|
-
slide = assign(source, layout, path)
|
|
67
|
+
slide = assign(source, step, layout, path, previous)
|
|
65
68
|
Checker.check_slide!(slide, path)
|
|
66
69
|
matches << slide
|
|
67
70
|
" #{layout.name} matched"
|
|
@@ -78,8 +81,8 @@ module Suada
|
|
|
78
81
|
else
|
|
79
82
|
"multiple layouts match: #{matches.map { |slide| slide.layout.name }.join(', ')}"
|
|
80
83
|
end
|
|
81
|
-
raise BuildError.new(kind: :dispatch, file: path, line:
|
|
82
|
-
slide_key: source.key, message:, help: 'choose a layout with <!-- {"layout":"name"} --> or adjust the slot contracts')
|
|
84
|
+
raise BuildError.new(kind: :dispatch, file: path, line: step.line, slide_number: source.number,
|
|
85
|
+
slide_key: source.key, step_number: step.number, message:, help: 'choose a layout with <!-- {"layout":"name"} --> or adjust the slot contracts')
|
|
83
86
|
end
|
|
84
87
|
end
|
|
85
88
|
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Suada
|
|
4
|
+
module Motion
|
|
5
|
+
module Diff
|
|
6
|
+
# Myers' frontier stores the furthest x reachable on each diagonal at edit distance d.
|
|
7
|
+
def self.pairs(a, b)
|
|
8
|
+
return [] if a.empty? || b.empty? || !a.intersect?(b)
|
|
9
|
+
|
|
10
|
+
frontier = [0]
|
|
11
|
+
# @type var history: Array[String]
|
|
12
|
+
history = []
|
|
13
|
+
d = 0
|
|
14
|
+
loop do
|
|
15
|
+
next_frontier = Array.new(d + 1, 0)
|
|
16
|
+
(0..d).each do |i|
|
|
17
|
+
k = (2 * i) - d
|
|
18
|
+
x = if i.zero? || (i < d && frontier[i - 1] < frontier[i])
|
|
19
|
+
frontier[i]
|
|
20
|
+
else
|
|
21
|
+
frontier[i - 1] + 1
|
|
22
|
+
end
|
|
23
|
+
y = x - k
|
|
24
|
+
while x < a.size && y < b.size && a[x] == b[y]
|
|
25
|
+
x += 1
|
|
26
|
+
y += 1
|
|
27
|
+
end
|
|
28
|
+
next_frontier[i] = x
|
|
29
|
+
return backtrack(history, x, y) if x == a.size && y == b.size
|
|
30
|
+
end
|
|
31
|
+
history << next_frontier.pack("L*")
|
|
32
|
+
frontier = next_frontier
|
|
33
|
+
d += 1
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.backtrack(history, x, y)
|
|
38
|
+
# @type var pairs: pairs
|
|
39
|
+
pairs = []
|
|
40
|
+
history.size.downto(1) do |d|
|
|
41
|
+
frontier = history[d - 1].unpack("L*") #: Array[Integer]
|
|
42
|
+
i = (x - y + d) / 2
|
|
43
|
+
down = i.zero? || (i < d && frontier[i - 1] < frontier[i])
|
|
44
|
+
previous_x = down ? frontier[i] : frontier[i - 1]
|
|
45
|
+
previous_y = previous_x - (x - y + (down ? 1 : -1))
|
|
46
|
+
while x > previous_x && y > previous_y
|
|
47
|
+
pairs << [x -= 1, y -= 1]
|
|
48
|
+
end
|
|
49
|
+
x, y = previous_x, previous_y
|
|
50
|
+
end
|
|
51
|
+
pairs << [x -= 1, y -= 1] while x.positive? && y.positive?
|
|
52
|
+
pairs.reverse
|
|
53
|
+
end
|
|
54
|
+
private_class_method :backtrack
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Suada
|
|
4
|
+
module Motion
|
|
5
|
+
module Glyphs
|
|
6
|
+
COLUMNS = /\A\d+:\d+(?:-\d+)?(?:\s+\d+:\d+(?:-\d+)?)*\z/
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
def render(code, frame, path, part)
|
|
10
|
+
if part.source.size > 2000
|
|
11
|
+
Checker.fail_slide!(frame, path, :motion, part.line, "code block exceeds 2000 characters", "split the code into smaller blocks")
|
|
12
|
+
end
|
|
13
|
+
# @type var marks: Hash[Integer, String]
|
|
14
|
+
marks = {}
|
|
15
|
+
part.attrs.each do |name, value|
|
|
16
|
+
next unless name.to_s.start_with?("mark-")
|
|
17
|
+
|
|
18
|
+
tone = name.to_s.delete_prefix("mark-")
|
|
19
|
+
positions(part.source, value).each do |index|
|
|
20
|
+
raise ArgumentError, "overlapping markers at character #{index + 1}" if marks[index] && marks[index] != tone
|
|
21
|
+
marks[index] = tone
|
|
22
|
+
end
|
|
23
|
+
rescue ArgumentError => e
|
|
24
|
+
Checker.fail_slide!(frame, path, :motion, part.line, "#{name} #{e.message}", "specify a unique substring or a line:column range")
|
|
25
|
+
end
|
|
26
|
+
wipe = part.attrs.fetch(:wipe, "").split
|
|
27
|
+
index = 0
|
|
28
|
+
code.xpath(".//text()").each do |node|
|
|
29
|
+
html = node.text.each_char.map do |char|
|
|
30
|
+
i = index
|
|
31
|
+
index += 1
|
|
32
|
+
next "\n" if char == "\n"
|
|
33
|
+
|
|
34
|
+
tone = marks[i]
|
|
35
|
+
mark = tone ? %( data-mark="#{tone}") : ""
|
|
36
|
+
mark += ' data-mark-in="wipe"' if wipe.include?(tone)
|
|
37
|
+
%(<span class="suada-g" data-i="#{i}"#{mark}>#{CGI.escapeHTML(char)}</span>)
|
|
38
|
+
end.join
|
|
39
|
+
node.replace(html)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def positions(text, spec)
|
|
44
|
+
lines = text.split("\n", -1)
|
|
45
|
+
offsets = [0]
|
|
46
|
+
lines.each { |line| offsets << (offsets.last + line.size + 1) }
|
|
47
|
+
if COLUMNS.match?(spec)
|
|
48
|
+
return spec.split.flat_map do |range|
|
|
49
|
+
coordinates = range.split(/[:-]/).map(&:to_i)
|
|
50
|
+
row = coordinates.fetch(0)
|
|
51
|
+
first = coordinates.fetch(1)
|
|
52
|
+
last = coordinates.fetch(2, first)
|
|
53
|
+
unless row.between?(1, lines.size) && first.between?(1, last) && last <= lines[row - 1].size
|
|
54
|
+
raise ArgumentError, "'#{range}' is out of range"
|
|
55
|
+
end
|
|
56
|
+
(first..last).map { |col| offsets[row - 1] + col - 1 }
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# @type var hits: Array[Integer]
|
|
61
|
+
hits = []
|
|
62
|
+
start = 0
|
|
63
|
+
while (i = text.index(spec, start))
|
|
64
|
+
hits << i
|
|
65
|
+
start = i + 1
|
|
66
|
+
end
|
|
67
|
+
raise ArgumentError, "'#{spec}' does not occur" if hits.empty?
|
|
68
|
+
if hits.size > 1
|
|
69
|
+
locations = hits.map do |i|
|
|
70
|
+
row = offsets.bsearch_index { |offset| offset > i } || lines.size
|
|
71
|
+
"#{row}:#{i - offsets[row - 1] + 1}"
|
|
72
|
+
end
|
|
73
|
+
raise ArgumentError, "'#{spec}' occurs #{hits.size} times (at #{locations.join(', ')})"
|
|
74
|
+
end
|
|
75
|
+
hits.first...(hits.first + spec.size)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def assign(blocks, previous, origins, frame, path)
|
|
79
|
+
# Each old block can supply continuity once. Named, identical, then same-slot matches take priority.
|
|
80
|
+
available = previous.dup
|
|
81
|
+
# @type var matched: Hash[_Node, _Node]
|
|
82
|
+
matched = {}
|
|
83
|
+
[:key, :text, :slot].each do |group|
|
|
84
|
+
blocks.each do |block|
|
|
85
|
+
next if matched.key?(block)
|
|
86
|
+
next if group != :key && origins.key?(block)
|
|
87
|
+
signature = block_group(block, group)
|
|
88
|
+
next unless signature
|
|
89
|
+
i = available.index { |old| block_group(old, group) == signature }
|
|
90
|
+
matched[block] = available.delete_at(i) if i
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
blocks.each_with_index do |block, index|
|
|
95
|
+
previous_block = matched[block]
|
|
96
|
+
# @type var inherited: indices
|
|
97
|
+
inherited = previous_block ? align(previous_block, block) : {}
|
|
98
|
+
from = origins[block]
|
|
99
|
+
if from && !from.key?("data-suada-glyphs")
|
|
100
|
+
Checker.fail_slide!(frame, path, :motion, block["data-suada-line"].to_i,
|
|
101
|
+
"from='#{block["data-from"]}' does not name a glyph block", "choose a code block with the glyphs attribute")
|
|
102
|
+
end
|
|
103
|
+
derived = from ? align(from, block) : nil
|
|
104
|
+
flow = block.key?("data-suada-flow")
|
|
105
|
+
basis = from || previous_block
|
|
106
|
+
# @type var between: indices
|
|
107
|
+
between = flow && basis ? replacements(derived || inherited, basis.text.size, block.text.size) : {}
|
|
108
|
+
old_keys = glyph_keys(previous_block)
|
|
109
|
+
source_keys = from ? glyph_keys(from) : old_keys
|
|
110
|
+
prefix = "#{frame.source.number}.#{frame.step.number}.#{index + 1}"
|
|
111
|
+
block["data-plate-key"] = previous_block&.[]("data-plate-key") || "~p#{prefix}" unless block["data-k"]
|
|
112
|
+
block.css(".suada-g").each do |node|
|
|
113
|
+
i = node["data-i"].to_i
|
|
114
|
+
keep = old_keys[inherited[i]]
|
|
115
|
+
node["data-k"] = keep || "~g#{prefix}.#{i}"
|
|
116
|
+
next if keep
|
|
117
|
+
origin_key = source_keys[derived&.[](i) || between[i]]
|
|
118
|
+
next unless origin_key
|
|
119
|
+
node["data-from"] = origin_key
|
|
120
|
+
source_step = block["data-from-step"]
|
|
121
|
+
node["data-from-step"] = source_step if source_step
|
|
122
|
+
end
|
|
123
|
+
block.remove_attribute("data-suada-flow")
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def replacements(alignment, source_length, target_length)
|
|
128
|
+
# @type var origins: indices
|
|
129
|
+
origins = {}
|
|
130
|
+
old_start = new_start = 0
|
|
131
|
+
alignment.merge(target_length => source_length).each do |new_end, old_end|
|
|
132
|
+
count = [old_end - old_start, new_end - new_start].min
|
|
133
|
+
count.times { |i| origins[new_start + i] = old_start + i }
|
|
134
|
+
old_start = old_end + 1
|
|
135
|
+
new_start = new_end + 1
|
|
136
|
+
end
|
|
137
|
+
origins
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def align(previous, block)
|
|
141
|
+
Diff.pairs(previous.text.chars, block.text.chars).to_h { |a, b| [b, a] }
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def glyph_keys(block)
|
|
145
|
+
return {} unless block
|
|
146
|
+
block.css(".suada-g").to_h { |g| [g["data-i"].to_i, g["data-k"].to_s] }
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def block_group(block, group)
|
|
150
|
+
case group
|
|
151
|
+
when :key then block["data-k"]
|
|
152
|
+
when :text then block.text
|
|
153
|
+
when :slot then block.classes.find { |name| name.start_with?("slot-") }
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "diff"
|
|
4
|
+
|
|
5
|
+
module Suada
|
|
6
|
+
module Motion
|
|
7
|
+
class Keyer
|
|
8
|
+
attr_reader :keys, :tones
|
|
9
|
+
|
|
10
|
+
def initialize(path)
|
|
11
|
+
@path = path
|
|
12
|
+
@keys = {}
|
|
13
|
+
@tones = []
|
|
14
|
+
@previous_blocks = []
|
|
15
|
+
@previous_units = []
|
|
16
|
+
@previous_named = {}
|
|
17
|
+
@history = {}
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def apply(root, frame, transition)
|
|
21
|
+
slide, step = frame.source, frame.step
|
|
22
|
+
if step.number == 1
|
|
23
|
+
@history.clear
|
|
24
|
+
@previous_named.clear unless transition == "morph"
|
|
25
|
+
end
|
|
26
|
+
# @type var named: Hash[String, _Node]
|
|
27
|
+
named = {}
|
|
28
|
+
([root] + root.css("[data-k]").to_a).each do |node|
|
|
29
|
+
key = node["data-k"]
|
|
30
|
+
next unless key
|
|
31
|
+
if named.key?(key)
|
|
32
|
+
fail_at(frame, node, "duplicate key '#{key}' in step #{step.number}", "use a unique key for each element")
|
|
33
|
+
end
|
|
34
|
+
named[key] = node
|
|
35
|
+
end
|
|
36
|
+
@keys[slide.key] = @keys.fetch(slide.key, []) | named.keys
|
|
37
|
+
|
|
38
|
+
units = automatic_units(root)
|
|
39
|
+
if transition == "morph"
|
|
40
|
+
Diff.pairs(@previous_units.map { |n| signature(n) }, units.map { |n| signature(n) }).each do |a, b|
|
|
41
|
+
units[b]["data-k"] = @previous_units[a]["data-k"].to_s
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
units.each_with_index { |node, i| node["data-k"] ||= "~#{slide.number}.#{step.number}.#{i + 1}" }
|
|
45
|
+
|
|
46
|
+
# @type var origins: Hash[_Node, _Node]
|
|
47
|
+
origins = {}
|
|
48
|
+
root.css("[data-from]").each do |node|
|
|
49
|
+
key = node["data-from"].to_s
|
|
50
|
+
if @previous_named.key?(key)
|
|
51
|
+
origins[node] = @previous_named.fetch(key)
|
|
52
|
+
elsif @history.key?(key)
|
|
53
|
+
number, origins[node] = @history.fetch(key)
|
|
54
|
+
node["data-from-step"] = number.to_s
|
|
55
|
+
else
|
|
56
|
+
fail_at(frame, node, "from='#{key}' refers to no element in the previous step",
|
|
57
|
+
"give the source element the key '#{key}' in an earlier step, or remove from")
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
blocks = root.css("[data-suada-glyphs]").to_a
|
|
61
|
+
if blocks.sum { |block| block.text.size } > 5000
|
|
62
|
+
fail_at(frame, blocks.last, "step #{step.number} exceeds 5000 glyphs", "split the slide into smaller steps")
|
|
63
|
+
end
|
|
64
|
+
Glyphs.assign(blocks, transition == "morph" ? @previous_blocks : [], origins, frame, @path)
|
|
65
|
+
root.css(".suada-call").each do |call|
|
|
66
|
+
call["data-on"].to_s.split.each do |target|
|
|
67
|
+
next if named.key?(target)
|
|
68
|
+
fail_at(frame, call, "call target '#{target}' is not in this step", "use an explicit key present in this step")
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
@tones |= root.css("[data-mark],[data-tone]").flat_map { |n| [n["data-mark"], n["data-tone"]].compact }
|
|
72
|
+
named.each { |key, node| @history[key] = [step.number, node] }
|
|
73
|
+
@previous_blocks = blocks
|
|
74
|
+
@previous_units = units
|
|
75
|
+
@previous_named = named
|
|
76
|
+
root.css("[data-suada-line]").each { |node| node.remove_attribute("data-suada-line") }
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
def automatic_units(root)
|
|
82
|
+
candidates = root.css("h1,h2,h3,h4,h5,h6,p,li,blockquote,table,pre,img,span[class],[data-suada-line]")
|
|
83
|
+
# The candidates arrive in document order. Once selected, an outer item moves its entire subtree.
|
|
84
|
+
# @type var selected: Set[_Node]
|
|
85
|
+
selected = Set.new
|
|
86
|
+
candidates.each do |node|
|
|
87
|
+
next if node.key?("data-k") || node.key?("data-suada-glyphs") || node.classes.include?("suada-g")
|
|
88
|
+
ancestors = node.ancestors
|
|
89
|
+
next if ancestors.any? { |parent| selected.include?(parent) || (parent.element? && (parent.key?("data-k") || parent.key?("data-suada-glyphs"))) }
|
|
90
|
+
next if node.at_css("[data-k], [data-suada-line]")
|
|
91
|
+
next if node.name == "span" && !slot?(node) && !node.key?("data-suada-line")
|
|
92
|
+
|
|
93
|
+
selected << node if node.key?("data-suada-line") || slot?(node) || ancestors.any? { |parent| slot?(parent) }
|
|
94
|
+
end
|
|
95
|
+
selected.to_a
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def slot?(node)
|
|
99
|
+
node.element? && node.classes.any? { |name| name.start_with?("slot-") }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def signature(node)
|
|
103
|
+
[node.name, node.text.split.join(" "), node["src"]]
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def fail_at(frame, node, message, help)
|
|
107
|
+
Checker.fail_slide!(frame, @path, :motion, (node["data-suada-line"] || frame.step.line).to_i, message, help)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
@@ -4,6 +4,8 @@ require "commonmarker"
|
|
|
4
4
|
require "rouge"
|
|
5
5
|
require_relative "../fragment"
|
|
6
6
|
require_relative "page_settings"
|
|
7
|
+
require_relative "inline_spans"
|
|
8
|
+
require_relative "code_attributes"
|
|
7
9
|
|
|
8
10
|
module Suada
|
|
9
11
|
class Parser
|
|
@@ -12,8 +14,9 @@ module Suada
|
|
|
12
14
|
table: Fragment::Table }.freeze
|
|
13
15
|
RAW_HELP = "rewrite the content using Markdown headings, paragraphs, lists, quotes, tables or code blocks"
|
|
14
16
|
|
|
15
|
-
def initialize(
|
|
16
|
-
@
|
|
17
|
+
def initialize(source, options)
|
|
18
|
+
@source = source
|
|
19
|
+
@path = source.path
|
|
17
20
|
@options = options
|
|
18
21
|
end
|
|
19
22
|
|
|
@@ -21,6 +24,7 @@ module Suada
|
|
|
21
24
|
line = node.source_position[:start_line] + offset
|
|
22
25
|
return comment(node.literal, line) if node.type == :html_block
|
|
23
26
|
|
|
27
|
+
spans = InlineSpans.new(node, @path, offset)
|
|
24
28
|
children = node.to_a
|
|
25
29
|
if node.type == :paragraph && children.size == 1 && children.first.type == :image
|
|
26
30
|
image = children.first
|
|
@@ -31,18 +35,17 @@ module Suada
|
|
|
31
35
|
case node.type
|
|
32
36
|
when :heading
|
|
33
37
|
inline = children.map { |child| child.to_html(options: @options) }.join
|
|
34
|
-
Fragment::Heading.new(line, node.header_level, inline, plain_text(node))
|
|
38
|
+
Fragment::Heading.new(line, node.header_level, spans.html(inline), spans.text(plain_text(node)))
|
|
35
39
|
when :code_block
|
|
36
|
-
language,
|
|
37
|
-
error(line, "unsupported code fence options", "write a single language name after the fence") if extra
|
|
40
|
+
language, attrs = node.fenced? ? CodeAttributes.parse(@source.lines.fetch(line - 1), @path, line) : [nil, {}]
|
|
38
41
|
if language && !Rouge::Lexer.find(language)
|
|
39
42
|
error(line, "unknown code language '#{language}'", "use a Rouge language name or omit it")
|
|
40
43
|
end
|
|
41
|
-
Fragment::Code.new(line, language, node.string_content)
|
|
44
|
+
Fragment::Code.new(line, language, node.string_content, attrs)
|
|
42
45
|
else
|
|
43
46
|
klass = BLOCKS[node.type]
|
|
44
47
|
error(line, "unsupported Markdown construct '#{node.type}'", RAW_HELP) unless klass
|
|
45
|
-
klass.new(line, node.to_html(options: @options))
|
|
48
|
+
klass.new(line, spans.html(node.to_html(options: @options)))
|
|
46
49
|
end
|
|
47
50
|
end
|
|
48
51
|
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../attributes"
|
|
4
|
+
|
|
5
|
+
module Suada
|
|
6
|
+
class Parser
|
|
7
|
+
module CodeAttributes
|
|
8
|
+
OPTIONS = { glyphs: :flag, k: :key, from: :key, flow: :flag, wipe: :string,
|
|
9
|
+
in: %w[fade rise draw none], delay: 0..5000 }.freeze
|
|
10
|
+
|
|
11
|
+
def self.parse(raw, path, line)
|
|
12
|
+
info = raw.sub(/\A {0,3}(?:\x60{3,}|~{3,})[ \t]*/, "").strip
|
|
13
|
+
match = /\A([^\s{}]+)?\s*(?:\{(.*)\})?\z/.match(info)
|
|
14
|
+
error(path, line, "unsupported code fence options") unless match
|
|
15
|
+
language, body = match.captures
|
|
16
|
+
return [language, {}] unless body
|
|
17
|
+
|
|
18
|
+
allowed = OPTIONS.dup
|
|
19
|
+
allowed.default_proc = ->(_, key) { :string if /\Amark-[a-z0-9-]+\z/.match?(key) }
|
|
20
|
+
attrs = Attributes.parse(body, allowed:, file: path, line:)
|
|
21
|
+
required = attrs.keys.find { |name| name.to_s.start_with?("mark-") || %i[from flow].include?(name) }
|
|
22
|
+
if required && !attrs[:glyphs]
|
|
23
|
+
raise BuildError.new(kind: :motion, file: path, line:, message: "#{required} needs the glyphs attribute",
|
|
24
|
+
help: "add glyphs to the code fence or remove #{required}")
|
|
25
|
+
end
|
|
26
|
+
attrs.fetch(:wipe, "").split.each do |tone|
|
|
27
|
+
next if attrs.key?(:"mark-#{tone}")
|
|
28
|
+
|
|
29
|
+
raise BuildError.new(kind: :motion, file: path, line:, message: "wipe needs a mark-#{tone} attribute",
|
|
30
|
+
help: "add mark-#{tone} to the code fence or remove the tone from wipe")
|
|
31
|
+
end
|
|
32
|
+
[language, attrs]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def self.error(path, line, message)
|
|
36
|
+
raise BuildError.new(kind: :parse, file: path, line:, message:, help: "write a language and optional code attributes after the opening fence")
|
|
37
|
+
end
|
|
38
|
+
private_class_method :error
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "cgi/escape"
|
|
4
|
+
require_relative "../attributes"
|
|
5
|
+
|
|
6
|
+
module Suada
|
|
7
|
+
class Parser
|
|
8
|
+
class InlineSpans
|
|
9
|
+
OPEN = "\u{F0001}"
|
|
10
|
+
MID = "\u{F0002}"
|
|
11
|
+
CLOSE = "\u{F0003}"
|
|
12
|
+
RESERVED = /[\u{F0001}-\u{F0003}]/
|
|
13
|
+
MARKERS = /#{OPEN}(\d+)#{MID}|#{CLOSE}/
|
|
14
|
+
OPTIONS = { k: :key, from: :key, in: %w[fade rise draw none], delay: 0..5000, mark: :key, wipe: :flag }.freeze
|
|
15
|
+
BODY = /\{((?:[^{}"']|"(?:\\.|[^"\\])*"|'[^']*')*)\}/
|
|
16
|
+
|
|
17
|
+
def initialize(node, file, offset)
|
|
18
|
+
@file = file
|
|
19
|
+
@offset = offset
|
|
20
|
+
@tags = []
|
|
21
|
+
node.walk do |child|
|
|
22
|
+
if reserved?(child)
|
|
23
|
+
error(@offset + child.source_position[:start_line], "reserved motion marker in source", "remove the private-use character")
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
mark(node)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def html(value)
|
|
30
|
+
value.gsub(MARKERS) { |marker| marker == CLOSE ? "</span>" : @tags.fetch(Regexp.last_match(1).to_i) }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def text(value)
|
|
34
|
+
value.gsub(MARKERS, "")
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def reserved?(node)
|
|
40
|
+
case node.type
|
|
41
|
+
when :text, :code then RESERVED.match?(node.string_content)
|
|
42
|
+
when :image, :link then RESERVED.match?(node.url) || RESERVED.match?(node.title)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def mark(parent)
|
|
47
|
+
stack = []
|
|
48
|
+
buffers = []
|
|
49
|
+
last_type = nil
|
|
50
|
+
parent.each do |child|
|
|
51
|
+
if child.type == :text
|
|
52
|
+
input = child.string_content
|
|
53
|
+
line = @offset + child.source_position[:start_line]
|
|
54
|
+
if %i[link image].include?(last_type) && input.match?(/\A\{\s*[a-z][a-z0-9-]*(?:[=\s}])/)
|
|
55
|
+
error(line, "attributes after a link or image", "wrap the entire link: [[label](url)]{k=name}")
|
|
56
|
+
end
|
|
57
|
+
buffers << [child, scan(input, stack, line)]
|
|
58
|
+
elsif !%i[code html_inline escaped image].include?(child.type)
|
|
59
|
+
mark(child)
|
|
60
|
+
end
|
|
61
|
+
last_type = child.type
|
|
62
|
+
end
|
|
63
|
+
buffers.each { |node, buffer| node.string_content = buffer unless node.string_content == buffer }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def scan(input, stack, line)
|
|
67
|
+
scanner = StringScanner.new(input)
|
|
68
|
+
out = +""
|
|
69
|
+
until scanner.eos?
|
|
70
|
+
if scanner.scan(/\[/)
|
|
71
|
+
stack << [out, out.length, line]
|
|
72
|
+
out << "["
|
|
73
|
+
elsif scanner.check(/\]\{/)
|
|
74
|
+
scanner.getch
|
|
75
|
+
unless scanner.scan(BODY)
|
|
76
|
+
error(line, "expected a closing brace for inline attributes", "complete the attributes or escape the closing bracket")
|
|
77
|
+
end
|
|
78
|
+
body = scanner[1]
|
|
79
|
+
error(line, "no opening bracket at this nesting level", "keep both brackets within the same emphasis or link") if stack.empty?
|
|
80
|
+
opener, index, opening_line = stack.pop
|
|
81
|
+
opener[index, 1] = "#{OPEN}#{@tags.size}#{MID}"
|
|
82
|
+
@tags << open_tag(body, line, opening_line)
|
|
83
|
+
out << CLOSE
|
|
84
|
+
elsif scanner.scan(/\]/)
|
|
85
|
+
stack.pop
|
|
86
|
+
out << "]"
|
|
87
|
+
else
|
|
88
|
+
out << scanner.scan(/[^\[\]]+/)
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
out
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def open_tag(body, line, opening_line)
|
|
95
|
+
attrs = Attributes.parse(body, allowed: OPTIONS, file: @file, line:)
|
|
96
|
+
if attrs.delete(:wipe)
|
|
97
|
+
unless attrs[:mark]
|
|
98
|
+
raise BuildError.new(kind: :motion, file: @file, line:, message: "wipe needs a mark on this span",
|
|
99
|
+
help: "add a mark tone or remove wipe")
|
|
100
|
+
end
|
|
101
|
+
attrs["mark-in"] = "wipe"
|
|
102
|
+
end
|
|
103
|
+
values = attrs.merge("suada-line" => opening_line)
|
|
104
|
+
"<span" + values.map { |name, value| %( data-#{name}="#{CGI.escapeHTML(value.to_s)}") }.join + ">"
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def error(line, message, help)
|
|
108
|
+
raise BuildError.new(kind: :parse, file: @file, line:, message:, help:)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "json"
|
|
4
|
+
require_relative "../attributes"
|
|
4
5
|
|
|
5
6
|
module Suada
|
|
6
7
|
class Parser
|
|
7
8
|
module PageSettings
|
|
8
|
-
KEYS = %w[key layout section time skip draft].freeze
|
|
9
|
+
KEYS = %w[key layout section time skip draft transition].freeze
|
|
9
10
|
|
|
10
11
|
def self.parse(comment, file, line)
|
|
11
12
|
fields = JSON.parse(comment)
|
|
@@ -14,6 +15,7 @@ module Suada
|
|
|
14
15
|
fields.each do |name, value|
|
|
15
16
|
valid = case name
|
|
16
17
|
when "key" then value.is_a?(String) && /\A[a-z0-9-]+\z/.match?(value)
|
|
18
|
+
when "transition" then Attributes::TRANSITIONS.include?(value)
|
|
17
19
|
when "section", "layout" then value.is_a?(String) && !value.strip.empty?
|
|
18
20
|
when "skip", "draft" then [true, false].include?(value)
|
|
19
21
|
when "time" then !PlannedTime.parse(value).nil?
|