Permalink
Browse files

Simplify changes for immutable string literals

  • Loading branch information...
mvz committed Aug 26, 2017
1 parent f2e1dfa commit 9ba21805efe5745ea6dfba9e3c1c4eda14db5490
Showing with 27 additions and 34 deletions.
  1. +4 −3 lib/redcloth/formatters/base.rb
  2. +2 −2 lib/redcloth/formatters/latex.rb
  3. +17 −17 spec/custom_tags_spec.rb
  4. +4 −12 spec/parser_spec.rb
@@ -28,6 +28,7 @@ def pba(opts)
opts.delete(:class) if filter_classes
opts.delete(:id) if filter_ids

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

def method_missing(method, opts)
@@ -165,15 +165,15 @@ def table_open(opts)

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

# code blocks
@@ -4,31 +4,31 @@ module FigureTag
def fig( opts )
label, img = opts[:text].split('|').map! {|str| str.strip}

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

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

html = %Q{<p>The first line of text.</p>\n}
html += %Q{<div class="img" id="figure-1-1">\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{ </a>\n}
html += %Q{ <p>Figure 1.1</p>\n}
html += %Q{<div>\n}
html += %Q{<p>The last line of text.</p>}
html = %Q{<p>The first line of text.</p>\n}.dup
html << %Q{<div class="img" id="figure-1-1">\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{ </a>\n}
html << %Q{ <p>Figure 1.1</p>\n}
html << %Q{<div>\n}
html << %Q{<p>The last line of text.</p>}

r.to_html.should == html
end
@@ -85,12 +85,8 @@

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

output = RedCloth.new(input).to_html

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

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

0 comments on commit 9ba2180

Please sign in to comment.