Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Enhance description formatting

This translates | to non breaking spaces and still keeps these spaces
properly formatted.
  • Loading branch information...
commit 55bf2c3b548aa88a96e79f77878ec113281af276 1 parent 0fa3cbd
@saulius saulius authored
View
2  app/formatters/description_formatter.rb
@@ -4,8 +4,8 @@ def self.format(opts = {})
str = opts.values.first
str.gsub!("|%", "%")
- str.gsub!(/&(\w+|\s+)/, '&\1')
str.gsub!("|", " ")
+ str.gsub!(/&(?!#|nbsp)/, '&')
str.gsub!("!1!", "<br />")
str.gsub!("!X!", "&times;")
str.gsub!("!x!", "&times;")
View
2  app/models/footnote.rb
@@ -8,7 +8,7 @@ class Footnote
attr_accessor :code, :description
format :formatted_description, with: DescriptionFormatter,
- using: :description
+ using: :description
def id
@id ||= "#{casted_by.destination}-#{casted_by.id}-footnote-#{code}"
View
38 spec/formatters/description_formatter_spec.rb
@@ -3,44 +3,62 @@
describe DescriptionFormatter do
describe '.format' do
+ it 'replaces & with ampersands' do
+ description = 'bread & butter'
+
+ DescriptionFormatter.format(description: description).should eq 'bread &amp; butter'
+ end
+
+ it 'does not replace & followed with # (html entity)' do
+ description = '&#39;A&#39; with an &#39;X&#39;'
+
+ DescriptionFormatter.format(description: description).should eq description
+ end
+
+ it 'does not replace & followed by nbsp (non breaking space entity)' do
+ description = 'a&nbsp;paragraph'
+
+ DescriptionFormatter.format(description: description).should eq description
+ end
+
it 'replaces | with non breaking space html entity' do
- DescriptionFormatter.format(description: ' | ').should == ' &nbsp; '
+ DescriptionFormatter.format(description: ' | ').should eq ' &nbsp; '
end
it 'replaces !1! with breaking space tags' do
- DescriptionFormatter.format(description: ' !1! ').should == ' <br /> '
+ DescriptionFormatter.format(description: ' !1! ').should eq ' <br /> '
end
it 'replaces !X! with times html entity' do
- DescriptionFormatter.format(description: ' !X! ').should == ' &times; '
+ DescriptionFormatter.format(description: ' !X! ').should eq ' &times; '
end
it 'replaces !x! with times html entity' do
- DescriptionFormatter.format(description: ' !x! ').should == ' &times; '
+ DescriptionFormatter.format(description: ' !x! ').should eq ' &times; '
end
it 'replaces !o! with deg html entity' do
- DescriptionFormatter.format(description: ' !o! ').should == ' &deg; '
+ DescriptionFormatter.format(description: ' !o! ').should eq ' &deg; '
end
it 'replaces !O! with deg html entity' do
- DescriptionFormatter.format(description: ' !O! ').should == ' &deg; '
+ DescriptionFormatter.format(description: ' !O! ').should eq ' &deg; '
end
it 'replaces !>=! with greater or equals html entity' do
- DescriptionFormatter.format(description: ' !>=! ').should == ' &ge; '
+ DescriptionFormatter.format(description: ' !>=! ').should eq ' &ge; '
end
it 'replaces !<=! with less or equal html entity' do
- DescriptionFormatter.format(description: ' !<=! ').should == ' &le; '
+ DescriptionFormatter.format(description: ' !<=! ').should eq ' &le; '
end
it 'replaces and wraps @<anycharacter> with html sub tag' do
- DescriptionFormatter.format(description: ' @1 ').should == ' <sub>1</sub> '
+ DescriptionFormatter.format(description: ' @1 ').should eq ' <sub>1</sub> '
end
it 'replaces and wraps $<anycharacter> with html sup tag' do
- DescriptionFormatter.format(description: ' $1 ').should == ' <sup>1</sup> '
+ DescriptionFormatter.format(description: ' $1 ').should eq ' <sup>1</sup> '
end
end
end
View
20 spec/formatters/description_trim_formatter_spec.rb
@@ -4,43 +4,43 @@
describe DescriptionTrimFormatter do
describe '.format' do
it 'replaces | with empty space' do
- DescriptionTrimFormatter.format(description: '|').should == ' '
+ DescriptionTrimFormatter.format(description: '|').should eq ' '
end
it 'strips !1!' do
- DescriptionTrimFormatter.format(description: '!1!').should == ''
+ DescriptionTrimFormatter.format(description: '!1!').should eq ''
end
it 'strips !X!' do
- DescriptionTrimFormatter.format(description: '!X!').should == ''
+ DescriptionTrimFormatter.format(description: '!X!').should eq ''
end
it 'strips !x!' do
- DescriptionTrimFormatter.format(description: '!x!').should == ''
+ DescriptionTrimFormatter.format(description: '!x!').should eq ''
end
it 'strips !o!' do
- DescriptionTrimFormatter.format(description: '!o!').should == ''
+ DescriptionTrimFormatter.format(description: '!o!').should eq ''
end
it 'strips !O!' do
- DescriptionTrimFormatter.format(description: '!O!').should == ''
+ DescriptionTrimFormatter.format(description: '!O!').should eq ''
end
it 'strips !>=!' do
- DescriptionTrimFormatter.format(description: '!>=!').should == ''
+ DescriptionTrimFormatter.format(description: '!>=!').should eq ''
end
it 'strips !<=!' do
- DescriptionTrimFormatter.format(description: '!<=!').should == ''
+ DescriptionTrimFormatter.format(description: '!<=!').should eq ''
end
it 'replaces @<anycharacter> with <anycharacter>' do
- DescriptionTrimFormatter.format(description: '@1').should == '1'
+ DescriptionTrimFormatter.format(description: '@1').should eq '1'
end
it 'replaces $<anycharacter> with <anycharacter>' do
- DescriptionTrimFormatter.format(description: '$1').should == '1'
+ DescriptionTrimFormatter.format(description: '$1').should eq '1'
end
end
end
View
6 spec/formatters/duty_expression_formatter_spec.rb
@@ -6,7 +6,7 @@
context 'for duty expression 99' do
it 'return the measurement unit' do
DutyExpressionFormatter.format(duty_expression_id: '99',
- measurement_unit: 'abc').should == 'abc'
+ measurement_unit: 'abc').should eq 'abc'
end
end
@@ -15,14 +15,14 @@
it 'returns duty expression abbreviation' do
DutyExpressionFormatter.format(duty_expression_id: '12',
duty_expression_abbreviation: 'abc',
- duty_expression_description: 'def').should == 'abc'
+ duty_expression_description: 'def').should eq 'abc'
end
end
context 'duty expression abbreviation missing' do
it 'returns duty expression description' do
DutyExpressionFormatter.format(duty_expression_id: '12',
- duty_expression_description: 'def').should == 'def'
+ duty_expression_description: 'def').should eq 'def'
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.