Class: Prosereflect::Transform::ReplaceAroundStep
- Defined in:
- lib/prosereflect/transform/replace_around_step.rb
Overview
Replaces a range of the document with a slice of content, but also replaces the content before and after the gap. Used by lift and wrap operations.
Instance Attribute Summary collapse
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#gap_from ⇒ Object
readonly
Returns the value of attribute gap_from.
-
#gap_to ⇒ Object
readonly
Returns the value of attribute gap_to.
-
#insert ⇒ Object
readonly
Returns the value of attribute insert.
-
#slice ⇒ Object
readonly
Returns the value of attribute slice.
-
#structure ⇒ Object
readonly
Returns the value of attribute structure.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Class Method Summary collapse
Instance Method Summary collapse
- #apply(doc) ⇒ Object
- #get_map ⇒ Object
-
#initialize(from, to, gap_from, gap_to, slice, insert, structure: false) ⇒ ReplaceAroundStep
constructor
A new instance of ReplaceAroundStep.
- #invert(doc) ⇒ Object
- #map(mapping) ⇒ Object
- #step_type ⇒ Object
- #to_json(*_args) ⇒ Object
Methods inherited from Step
Constructor Details
#initialize(from, to, gap_from, gap_to, slice, insert, structure: false) ⇒ ReplaceAroundStep
Returns a new instance of ReplaceAroundStep.
13 14 15 16 17 18 19 20 21 22 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 13 def initialize(from, to, gap_from, gap_to, slice, insert, structure: false) super() @from = from @to = to @gap_from = gap_from @gap_to = gap_to @slice = slice @insert = insert @structure = structure end |
Instance Attribute Details
#from ⇒ Object (readonly)
Returns the value of attribute from.
11 12 13 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 11 def from @from end |
#gap_from ⇒ Object (readonly)
Returns the value of attribute gap_from.
11 12 13 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 11 def gap_from @gap_from end |
#gap_to ⇒ Object (readonly)
Returns the value of attribute gap_to.
11 12 13 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 11 def gap_to @gap_to end |
#insert ⇒ Object (readonly)
Returns the value of attribute insert.
11 12 13 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 11 def insert @insert end |
#slice ⇒ Object (readonly)
Returns the value of attribute slice.
11 12 13 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 11 def slice @slice end |
#structure ⇒ Object (readonly)
Returns the value of attribute structure.
11 12 13 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 11 def structure @structure end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
11 12 13 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 11 def to @to end |
Class Method Details
.from_json(_schema, json) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 124 def self.from_json(_schema, json) from_val = json["from"] to_val = json["to"] gap_from_val = json["gapFrom"] gap_to_val = json["gapTo"] insert_val = json["insert"] structure_val = json["structure"] || false slice_json = json["slice"] || [] slice_content = slice_json.map { |h| Prosereflect::Node.from_h(h) } slice = Slice.new(Fragment.new(slice_content)) new(from_val, to_val, gap_from_val, gap_to_val, slice, insert_val, structure: structure_val) end |
Instance Method Details
#apply(doc) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 24 def apply(doc) # Check structure constraint if @structure && (content_between(doc, @from, @gap_from) || content_between(doc, @gap_to, @to)) return Result.fail("Structure gap-replace would overwrite content") end # Get the gap content gap = doc.slice(@gap_from, @gap_to) if gap.open_start || gap.open_end return Result.fail("Gap is not a flat range") end # Try to insert slice into gap inserted = @slice.insert_at(@insert, gap.content) unless inserted return Result.fail("Content does not fit in gap") end # Apply the replacement new_doc = apply_replace_around(doc, inserted) Result.ok(new_doc) rescue StandardError => e Result.fail(e.) end |
#get_map ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 49 def get_map StepMap.new([ @from, @gap_from - @from, @insert, @gap_to, @to - @gap_to, @slice.size - @insert, ]) end |
#invert(doc) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 60 def invert(doc) gap = @gap_to - @gap_from removed = doc.slice(@from, @to).remove_between( @gap_from - @from, @gap_to - @from, ) ReplaceAroundStep.new( @from, @from + @slice.size + gap, @from + @insert, @from + @insert + gap, removed, @gap_from - @from, @structure, ) end |
#map(mapping) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 77 def map(mapping) from_mapped = mapping.map_result(@from, 1) to_mapped = mapping.map_result(@to, -1) gap_from_mapped = if @from == @gap_from from_mapped.pos else mapping.map(@gap_from, -1) end gap_to_mapped = if @to == @gap_to to_mapped.pos else mapping.map(@gap_to, 1) end if (from_mapped.deleted && to_mapped.deleted) || gap_from_mapped < from_mapped.pos || gap_to_mapped > to_mapped.pos return nil end ReplaceAroundStep.new( from_mapped.pos, to_mapped.pos, gap_from_mapped, gap_to_mapped, @slice, @insert, @structure, ) end |
#step_type ⇒ Object
108 109 110 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 108 def step_type "replaceAround" end |
#to_json(*_args) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/prosereflect/transform/replace_around_step.rb', line 112 def to_json(*_args) json = super json["from"] = @from json["to"] = @to json["gapFrom"] = @gap_from json["gapTo"] = @gap_to json["slice"] = @slice.content.to_a.map(&:to_h) json["insert"] = @insert json["structure"] = @structure json end |