Skip to content

Commit

Permalink
Auto-correct rubocop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
fdebijl committed Mar 11, 2024
1 parent 5d32e4a commit 2792a5c
Show file tree
Hide file tree
Showing 18 changed files with 333 additions and 322 deletions.
12 changes: 6 additions & 6 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
require "bundler/gem_tasks"
require "rake/testtask"
require "yard"
require 'bundler/gem_tasks'
require 'rake/testtask'
require 'yard'

Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.libs << 'test'
t.libs << 'lib'
t.test_files = FileList['test/**/*_test.rb']
end

YARD::Rake::YardocTask.new(:doc)

task :default => :test
task default: :test
6 changes: 3 additions & 3 deletions bin/console
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby

require "bundler/setup"
require "rich-text"
require 'bundler/setup'
require 'rich-text'

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
Expand All @@ -10,5 +10,5 @@ require "rich-text"
# require "pry"
# Pry.start

require "irb"
require 'irb'
IRB.start
14 changes: 7 additions & 7 deletions bin/rake
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
# this file is here to facilitate running it.
#

require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)
require 'pathname'
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
Pathname.new(__FILE__).realpath)

bundle_binstub = File.expand_path("../bundle", __FILE__)
bundle_binstub = File.expand_path('bundle', __dir__)

if File.file?(bundle_binstub)
if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
Expand All @@ -23,7 +23,7 @@ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this
end
end

require "rubygems"
require "bundler/setup"
require 'rubygems'
require 'bundler/setup'

load Gem.bin_path("rake", "rake")
load Gem.bin_path('rake', 'rake')
23 changes: 11 additions & 12 deletions lib/rich-text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@ def self.configure
require 'rich-text/html'

RichText.configure do |c|

c.html_inline_formats = {
bold: { tag: 'strong' },
br: { tag: 'br' },
hr: { tag: 'hr', block_format: false },
italic: { tag: 'em' },
link: { tag: 'a', apply: ->(el, op, ctx){ el[:href] = op.attributes[:link] } }
bold: { tag: 'strong' },
br: { tag: 'br' },
hr: { tag: 'hr', block_format: false },
italic: { tag: 'em' },
link: { tag: 'a', apply: ->(el, op, _ctx) { el[:href] = op.attributes[:link] } }
}

c.html_block_formats = {
firstheader: { tag: 'h1' },
secondheader: { tag: 'h2' },
thirdheader: { tag: 'h3' },
bullet: { tag: 'li', parent: 'ul' },
list: { tag: 'li', parent: 'ol' },
id: { apply: ->(el, op, ctx){ el[:id] = op.attributes[:id] } }
firstheader: { tag: 'h1' },
secondheader: { tag: 'h2' },
thirdheader: { tag: 'h3' },
bullet: { tag: 'li', parent: 'ul' },
list: { tag: 'li', parent: 'ol' },
id: { apply: ->(el, op, _ctx) { el[:id] = op.attributes[:id] } }
}

c.html_default_block_format = 'p'
Expand Down
7 changes: 5 additions & 2 deletions lib/rich-text/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@ class << self
def compose(a, b, keep_nil)
return b if a.nil?
return a if b.nil?
result = b.merge(a) { |k,vb,va| vb }
result.delete_if { |k,v| v.nil? } unless keep_nil

result = b.merge(a) { |_k, vb, _va| vb }
result.delete_if { |_k, v| v.nil? } unless keep_nil
result
end

def diff(a, b)
return b if a.nil?
return a if b.nil?

(a.keys | b.keys).each_with_object({}) do |key, memo|
memo[key] = b[key] if a[key] != b[key]
end
end

def transform(a, b, priority)
return b if a.nil? || a.empty? || b.nil? || b.empty? || !priority

(b.keys - a.keys).each_with_object({}) do |key, memo|
memo[key] = b[key]
end
Expand Down
62 changes: 32 additions & 30 deletions lib/rich-text/delta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def initialize(data = [])
else
ArgumentError.new("Please provide either String, Array or Hash with an 'ops' key containing an Array")
end

@ops
end

# Appends an insert operation. A no-op if the provided value is the empty string.
Expand All @@ -43,6 +41,7 @@ def initialize(data = [])
# delta.insert({ image: 'http://i.imgur.com/FUCb95Y.gif' })
def insert(value, attributes = {})
return self if value.is_a?(String) && value.length == 0

push(Op.new(:insert, value, attributes))
end

Expand All @@ -53,6 +52,7 @@ def insert(value, attributes = {})
# delta.delete(5)
def delete(value)
return self if value <= 0

push(Op.new(:delete, value))
end

Expand All @@ -64,6 +64,7 @@ def delete(value)
# delta.retain(4).retain(5, { color: '#0c6' })
def retain(value, attributes = {})
return self if value <= 0

push(Op.new(:retain, value, attributes))
end

Expand All @@ -85,7 +86,7 @@ def push(op)
if last_op.delete? && op.insert?
index -= 1
last_op = @ops[index - 1]
if !last_op
unless last_op
@ops.unshift(op)
return self
end
Expand All @@ -108,40 +109,39 @@ def push(op)
@ops[index, 0] = op
end

return self
self
end
alias :<< :push
alias << push

# Modifies self by removing the last op if it was a retain without attributes.
# @return [Delta] `self` for chainability
def chop!
last_op = @ops.last
if last_op && last_op.retain? && !last_op.attributes?
@ops.pop
end
return self
@ops.pop if last_op && last_op.retain? && !last_op.attributes?
self
end

# Returns true if all operations are inserts, i.e. a fully-composed document
# @return [Boolean]
def insert_only?
@ops.all?(&:insert?)
end
alias :document? :insert_only?
alias document? insert_only?

# Returns true if the last operation is a string insert that ends with a `\n` character.
# @return [Boolean]
def trailing_newline?
return false unless @ops.last && @ops.last.insert?(String)

@ops.last.value.end_with?("\n")
end

# Returns true if `other` is a substring of `self`
# @param other [Delta]
# @return [Boolean]
# @todo Not implemented yet
def include?(other)
raise NotImplementedError.new("TODO")
def include?(_other)
raise NotImplementedError, 'TODO'
end

# Yields ops of at most `size` length to the block, or returns an enumerator which will do the same
Expand All @@ -151,9 +151,10 @@ def include?(other)
# @example
# delta = RichText::Delta.new.insert('abc')
# delta.each_slice(2).to_a # => [#<RichText::Op insert="ab">, #<RichText::Op insert="c">]
def each_slice(size = 1)
def each_slice(size = 1, &block)
return enum_for(:each_slice, size) unless block_given?
Iterator.new(@ops).each(size) { |op| yield op }

Iterator.new(@ops).each(size, &block)
self
end

Expand All @@ -167,6 +168,7 @@ def each_slice(size = 1)
# delta.each_char.to_a # => [["a", { bold: true }], ["b", {}], [{ image: "http://i.imgur.com/YtQPTnw.gif" }, {}]]
def each_char
return enum_for(:each_char) unless block_given?

each_slice(1) { |op| yield op.value, op.attributes }
self
end
Expand All @@ -186,7 +188,7 @@ def each_line

while iter.next?
op = iter.next
if !op.insert?(String)
unless op.insert?(String)
line.push(op)
next
end
Expand All @@ -199,9 +201,7 @@ def each_line
offset = idx + 1
end

if offset < op.value.length
line.push op.slice(offset)
end
line.push op.slice(offset) if offset < op.value.length
end

yield line if line.length > 0
Expand All @@ -210,9 +210,10 @@ def each_line
# Yields each operation in the delta, as-is.
# @yield [op] an {Op} object
# @return [Enumerator, Delta] if no block given, returns an {Enumerator}, else returns `self` for chainability
def each_op
def each_op(&block)
return enum_for(:each_op) unless block_given?
@ops.each { |op| yield op }

@ops.each(&block)
self
end

Expand Down Expand Up @@ -254,9 +255,9 @@ def slice(start = 0, len = length)
end
idx += op.length
end
return delta
delta
end
alias :[] :slice
alias [] slice

# Returns a Delta that is equivalent to first applying the operations of `self`, then applying the operations of `other` on top of that.
# @param other [Delta]
Expand Down Expand Up @@ -293,7 +294,7 @@ def compose(other)
end
delta.chop!
end
alias :| :compose
alias | compose

# Modifies `self` by the concatenating this and another document Delta's operations.
# Correctly handles the case of merging the last operation of `self` with the first operation of `other`, if possible.
Expand Down Expand Up @@ -360,7 +361,7 @@ def diff(other)

delta.chop!
end
alias :- :diff
alias - diff

# Transform other Delta against own operations, such that [transformation property 1 (TP1)](https://en.wikipedia.org/wiki/Operational_transformation#Convergence_properties) holds:
#
Expand All @@ -384,6 +385,7 @@ def diff(other)
# a.transform(b, false) # => #<RichText::Delta [retain=1 {:color=>"#fff", :bold=>true}]>
def transform(other, priority)
return transform_position(other, priority) if other.is_a?(Integer)

iter = Iterator.new(@ops)
other_iter = Iterator.new(other.ops)
delta = Delta.new
Expand All @@ -409,7 +411,7 @@ def transform(other, priority)
end
delta.chop!
end
alias :^ :transform
alias ^ transform

# Transform an index against the current delta. Useful for shifting cursor & selection positions in response to remote changes.
# @param index [Integer] an offset position that may be shifted by inserts and deletes happening beforehand
Expand All @@ -435,12 +437,12 @@ def transform_position(index, priority)
end
offset += op.length
end
return index
index
end

# @return [Hash] the Hash representation of this object, by converting each contained op into a Hash
def to_h
{ :ops => @ops.map(&:to_h) }
{ ops: @ops.map(&:to_h) }
end

# @return [String] the JSON representation of this object, by delegating to {#to_h}
Expand Down Expand Up @@ -477,8 +479,8 @@ def to_html(options = {})
# '#<RichText::Delta [retain=3, delete=1, insert="abc" {:bold=>true}, insert={:image=>"http://i.imgur.com/vwGN6.gif"}]>'
def inspect
str = "#<#{self.class.name} ["
str << @ops.map { |o| o.inspect(false) }.join(", ")
str << "]>"
str << @ops.map { |o| o.inspect(false) }.join(', ')
str << ']>'
end

# A Delta is equal to another if all the ops are equal.
Expand All @@ -487,6 +489,6 @@ def inspect
def ==(other)
other.is_a?(RichText::Delta) && @ops == other.ops
end
alias_method :eql?, :==
alias eql? ==
end
end
10 changes: 5 additions & 5 deletions lib/rich-text/diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ module RichText
class Diff
attr_reader :chunks

def initialize(left, right)
def initialize(left, right, &block)
@chunks = []
::Diff::LCS.traverse_sequences(left.to_plaintext, right.to_plaintext, self)
@chunks.each { |c| yield c } if block_given?
@chunks.each(&block) if block_given?
end

def push(type)
Expand All @@ -19,15 +19,15 @@ def push(type)
end
end

def match(args)
def match(_args)
push :retain
end

def discard_a(args)
def discard_a(_args)
push :delete
end

def discard_b(args)
def discard_b(_args)
push :insert
end
end
Expand Down
Loading

0 comments on commit 2792a5c

Please sign in to comment.