Skip to content

Commit

Permalink
Simplify changes for immutable string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
mvz committed Aug 26, 2017
1 parent f2e1dfa commit 9ba2180
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 34 deletions.
7 changes: 4 additions & 3 deletions lib/redcloth/formatters/base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def pba(opts)
opts.delete(:class) if filter_classes opts.delete(:class) if filter_classes
opts.delete(:id) if filter_ids opts.delete(:id) if filter_ids


atts = ''.dup
opts[:"text-align"] = opts.delete(:align) opts[:"text-align"] = opts.delete(:align)
opts[:style] += ';' if opts[:style] && (opts[:style][-1..-1] != ';') opts[:style] += ';' if opts[:style] && (opts[:style][-1..-1] != ';')
[:float, :"text-align", :"vertical-align"].each do |a| [:float, :"text-align", :"vertical-align"].each do |a|
Expand All @@ -36,10 +37,10 @@ def pba(opts)
[:"padding-right", :"padding-left"].each do |a| [:"padding-right", :"padding-left"].each do |a|
opts[:style] = "#{a}:#{opts[a]}em;#{opts[:style]}" if opts[a] opts[:style] = "#{a}:#{opts[a]}em;#{opts[:style]}" if opts[a]
end end
atts = [:style, :class, :lang, :id, :colspan, :rowspan, :title, :start, :align].map do |a| [:style, :class, :lang, :id, :colspan, :rowspan, :title, :start, :align].each do |a|
" #{a}=\"#{ html_esc(opts[a].to_s, :html_escape_attributes) }\"" if opts[a] atts << " #{a}=\"#{ html_esc(opts[a].to_s, :html_escape_attributes) }\"" if opts[a]
end end
atts.compact.join atts
end end


def method_missing(method, opts) def method_missing(method, opts)
Expand Down
4 changes: 2 additions & 2 deletions lib/redcloth/formatters/latex.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ def table_open(opts)


# FIXME: need caption and label elements similar to image -> figure # FIXME: need caption and label elements similar to image -> figure
def table_close(opts) def table_close(opts)
output = ["\\begin{table}\n"] output = "\\begin{table}\n".dup
output << " \\centering\n" output << " \\centering\n"
output << " \\begin{tabular}{ #{"l " * @table[0].size }}\n" output << " \\begin{tabular}{ #{"l " * @table[0].size }}\n"
@table.each do |row| @table.each do |row|
output << " #{row.join(" & ")} \\\\\n" output << " #{row.join(" & ")} \\\\\n"
end end
output << " \\end{tabular}\n" output << " \\end{tabular}\n"
output << "\\end{table}\n" output << "\\end{table}\n"
output.join output
end end


# code blocks # code blocks
Expand Down
34 changes: 17 additions & 17 deletions spec/custom_tags_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@ module FigureTag
def fig( opts ) def fig( opts )
label, img = opts[:text].split('|').map! {|str| str.strip} label, img = opts[:text].split('|').map! {|str| str.strip}


html = %Q{<div class="img" id="figure-#{label.tr('.', '-')}">\n} html = %Q{<div class="img" id="figure-#{label.tr('.', '-')}">\n}.dup
html += %Q{ <a class="fig" href="/images/#{img}">\n} html << %Q{ <a class="fig" href="/images/#{img}">\n}
html += %Q{ <img src="/images/thumbs/#{img}" alt="Figure #{label}" />\n} html << %Q{ <img src="/images/thumbs/#{img}" alt="Figure #{label}" />\n}
html += %Q{ </a>\n} html << %Q{ </a>\n}
html += %Q{ <p>Figure #{label}</p>\n} html << %Q{ <p>Figure #{label}</p>\n}
html += %Q{<div>\n} html << %Q{<div>\n}
end end
end end


describe "custom tags" do describe "custom tags" do
it "should recognize the custom tag" do it "should recognize the custom tag" do
input = %Q{The first line of text.\n\n} input = %Q{The first line of text.\n\n}.dup
input += %Q{fig. 1.1 | img.jpg\n\n} input << %Q{fig. 1.1 | img.jpg\n\n}
input += %Q{The last line of text.\n} input << %Q{The last line of text.\n}
r = RedCloth.new input r = RedCloth.new input
r.extend FigureTag r.extend FigureTag


html = %Q{<p>The first line of text.</p>\n} html = %Q{<p>The first line of text.</p>\n}.dup
html += %Q{<div class="img" id="figure-1-1">\n} html << %Q{<div class="img" id="figure-1-1">\n}
html += %Q{ <a class="fig" href="/images/img.jpg">\n} html << %Q{ <a class="fig" href="/images/img.jpg">\n}
html += %Q{ <img src="/images/thumbs/img.jpg" alt="Figure 1.1" />\n} html << %Q{ <img src="/images/thumbs/img.jpg" alt="Figure 1.1" />\n}
html += %Q{ </a>\n} html << %Q{ </a>\n}
html += %Q{ <p>Figure 1.1</p>\n} html << %Q{ <p>Figure 1.1</p>\n}
html += %Q{<div>\n} html << %Q{<div>\n}
html += %Q{<p>The last line of text.</p>} html << %Q{<p>The last line of text.</p>}


r.to_html.should == html r.to_html.should == html
end end
Expand Down
16 changes: 4 additions & 12 deletions spec/parser_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -85,12 +85,8 @@


if RUBY_VERSION > "1.9.0" if RUBY_VERSION > "1.9.0"
it "should preserve character encoding" do it "should preserve character encoding" do
if RUBY_VERSION > "2.3.0" input = "This is an ISO-8859-1 string".dup
input = String.new("This is an ISO-8859-1 string", encoding: 'iso-8859-1') input.force_encoding 'iso-8859-1'
else
input = "This is an ISO-8859-1 string"
input.force_encoding 'iso-8859-1'
end


output = RedCloth.new(input).to_html output = RedCloth.new(input).to_html


Expand All @@ -99,12 +95,8 @@
end end


it "should not raise ArgumentError: invalid byte sequence" do it "should not raise ArgumentError: invalid byte sequence" do
if RUBY_VERSION > "2.3.0" s = "\xa3".dup
s = String.new("\xa3", encoding: 'iso-8859-1') s.force_encoding 'iso-8859-1'
else
s = "\xa3"
s.force_encoding 'iso-8859-1'
end
lambda { RedCloth.new(s).to_html }.should_not raise_error lambda { RedCloth.new(s).to_html }.should_not raise_error
end end
end end
Expand Down

0 comments on commit 9ba2180

Please sign in to comment.