mirror of
https://github.com/discourse/discourse.git
synced 2024-11-30 06:23:44 +08:00
Merge pull request #852 from avdi/avoid-exceptions-for-flow-control
Replace exceptions for flow control with idiomatic throw/catch
This commit is contained in:
commit
8b63c82f64
|
@ -235,8 +235,6 @@ module PrettyText
|
||||||
|
|
||||||
class ExcerptParser < Nokogiri::XML::SAX::Document
|
class ExcerptParser < Nokogiri::XML::SAX::Document
|
||||||
|
|
||||||
class DoneException < StandardError; end
|
|
||||||
|
|
||||||
attr_reader :excerpt
|
attr_reader :excerpt
|
||||||
|
|
||||||
def initialize(length,options)
|
def initialize(length,options)
|
||||||
|
@ -249,10 +247,8 @@ module PrettyText
|
||||||
def self.get_excerpt(html, length, options)
|
def self.get_excerpt(html, length, options)
|
||||||
me = self.new(length,options)
|
me = self.new(length,options)
|
||||||
parser = Nokogiri::HTML::SAX::Parser.new(me)
|
parser = Nokogiri::HTML::SAX::Parser.new(me)
|
||||||
begin
|
catch(:done) do
|
||||||
parser.parse(html) unless html.nil?
|
parser.parse(html) unless html.nil?
|
||||||
rescue DoneException
|
|
||||||
# we are done
|
|
||||||
end
|
end
|
||||||
me.excerpt
|
me.excerpt
|
||||||
end
|
end
|
||||||
|
@ -303,7 +299,7 @@ module PrettyText
|
||||||
@excerpt << encode.call(string[0..length]) if truncate
|
@excerpt << encode.call(string[0..length]) if truncate
|
||||||
@excerpt << "…"
|
@excerpt << "…"
|
||||||
@excerpt << "</a>" if @in_a
|
@excerpt << "</a>" if @in_a
|
||||||
raise DoneException.new
|
throw :done
|
||||||
end
|
end
|
||||||
@excerpt << encode.call(string)
|
@excerpt << encode.call(string)
|
||||||
@current_length += string.length if count_it
|
@current_length += string.length if count_it
|
||||||
|
|
Loading…
Reference in New Issue
Block a user