Sha256: 65d2c6ec6414ad03250e4402f4dce516eff055fa1d9c3a9906b3895d549967af

Contents?: true

Size: 949 Bytes

Versions: 8

Compression:

Stored size: 949 Bytes

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2020, by Samuel Williams.

module Decode
	module Syntax
		class Rewriter
			def initialize(text)
				@text = text
				@matches = []
			end
			
			attr :text
			
			attr :matches
			
			def << match
				@matches << match
			end
			
			# Returns a chunk of raw text with no formatting.
			def text_for(range)
				@text[range]
			end
			
			def apply(output = [])
				offset = 0
				
				@matches.sort.each do |match|
					if match.offset > offset
						output << text_for(offset...match.offset)
						
						offset = match.offset
					elsif match.offset < offset
						# Match intersects last output buffer.
						next
					end
					
					offset += match.apply(output, self)
				end
				
				if offset < @text.size
					output << text_for(offset...@text.size)
				end
				
				return output
			end
			
			def link_to(definition, text)
				"[#{text}]"
			end
		end
	end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
decode-0.21.3 lib/decode/syntax/rewriter.rb
decode-0.21.2 lib/decode/syntax/rewriter.rb
decode-0.21.1 lib/decode/syntax/rewriter.rb
decode-0.21.0 lib/decode/syntax/rewriter.rb
decode-0.20.2 lib/decode/syntax/rewriter.rb
decode-0.20.1 lib/decode/syntax/rewriter.rb
decode-0.20.0 lib/decode/syntax/rewriter.rb
decode-0.19.0 lib/decode/syntax/rewriter.rb