# -*- coding: utf-8 -*-
require File.join(File.expand_path(File.dirname(__FILE__)), "spec_helper")
describe "Text::Formatted::Parser#format" do
it "should handle sup" do
string = "superscript"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "superscript",
:styles => [:superscript],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "should handle sub" do
string = "subscript"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "subscript",
:styles => [:subscript],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "should handle rgb" do
string = "red text"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "red text",
:styles => [],
:color => "ff0000",
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "# should be optional in rgb" do
string = "red text"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "red text",
:styles => [],
:color => "ff0000",
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "should handle cmyk" do
string = "magenta text"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "magenta text",
:styles => [],
:color => [0, 100, 0, 0],
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "should handle fonts" do
string = "Courier text"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "Courier text",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => "Courier",
:size => nil,
:character_spacing => nil)
end
it "should handle size" do
string = "14 point text"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "14 point text",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => 14,
:character_spacing => nil)
end
it "should handle character_spacing" do
string = "extra character spacing"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "extra character spacing",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => 2.5)
end
it "should handle links" do
string = "external link"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "external link",
:styles => [],
:color => nil,
:link => "http://example.com",
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "should handle local links" do
string = "local link"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "local link",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => "/home/example/foo.bar",
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "should handle anchors" do
string = "internal link"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "internal link",
:styles => [],
:color => nil,
:link => nil,
:anchor => "ToC",
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "should handle higher order characters properly" do
string = "©\n©"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "©",
:styles => [:bold],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
expect(array[1]).to eq(:text => "\n",
:styles => [:bold],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
expect(array[2]).to eq(:text => "©",
:styles => [:bold],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "should convert < >, and & to <, >, and &, respectively" do
string = "hello <, >, and &"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[1]).to eq(:text => "<, >, and &",
:styles => [:bold],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "should handle double qoutes around tag attributes" do
string = 'some sized text'
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[1]).to eq(:text => "sized",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => 14,
:character_spacing => nil)
end
it "should handle single qoutes around tag attributes" do
string = "some sized text"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[1]).to eq(:text => "sized",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => 14,
:character_spacing => nil)
end
it "should construct a formatted text array from a string" do
string = "hello world\nhow are you?"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "hello ",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
expect(array[1]).to eq(:text => "world",
:styles => [:bold],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
expect(array[2]).to eq(:text => "\n",
:styles => [:bold],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
expect(array[3]).to eq(:text => "how ",
:styles => [:bold],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
expect(array[4]).to eq(:text => "are",
:styles => [:bold, :italic],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
expect(array[5]).to eq(:text => " you?",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "should accept as an alternative to " do
string = "bold not bold"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "bold",
:styles => [:bold],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
expect(array[1]).to eq(:text => " not bold",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "should accept as an alternative to " do
string = "italic not italic"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "italic",
:styles => [:italic],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
expect(array[1]).to eq(:text => " not italic",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "should accept as an alternative to " do
string = "link not a link"
array = Prawn::Text::Formatted::Parser.format(string)
expect(array[0]).to eq(:text => "link",
:styles => [],
:color => nil,
:link => "http://example.com",
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
expect(array[1]).to eq(:text => " not a link",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil)
end
it "should turn
,
into newline" do
array = Prawn::Text::Formatted::Parser.format("hello
big
world")
expect(array.map { |frag| frag[:text] }.join).to eq("hello\nbig\nworld")
end
end
describe "Text::Formatted::Parser#to_string" do
it "should handle sup" do
string = "superscript"
array = [{ :text => "superscript",
:styles => [:superscript],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil }]
expect(Prawn::Text::Formatted::Parser.to_string(array)).to eq(string)
end
it "should handle sub" do
string = "subscript"
array = [{ :text => "subscript",
:styles => [:subscript],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil }]
expect(Prawn::Text::Formatted::Parser.to_string(array)).to eq(string)
end
it "should handle rgb" do
string = "red text"
array = [{ :text => "red text",
:styles => [],
:color => "ff0000",
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil }]
expect(Prawn::Text::Formatted::Parser.to_string(array)).to eq(string)
end
it "should handle cmyk" do
string = "magenta text"
array = [{ :text => "magenta text",
:styles => [],
:color => [0, 100, 0, 0],
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil }]
expect(Prawn::Text::Formatted::Parser.to_string(array)).to eq(string)
end
it "should handle fonts" do
string = "Courier text"
array = [{ :text => "Courier text",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => "Courier",
:size => nil,
:character_spacing => nil }]
expect(Prawn::Text::Formatted::Parser.to_string(array)).to eq(string)
end
it "should handle size" do
string = "14 point text"
array = [{ :text => "14 point text",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => 14,
:character_spacing => nil }]
expect(Prawn::Text::Formatted::Parser.to_string(array)).to eq(string)
end
it "should handle character spacing" do
string = "2.5 extra character spacing"
array = [{ :text => "2.5 extra character spacing",
:styles => [],
:color => nil,
:link => nil,
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => 2.5 }]
expect(Prawn::Text::Formatted::Parser.to_string(array)).to eq(string)
end
it "should handle links" do
array = [{ :text => "external link",
:styles => [],
:color => nil,
:link => "http://example.com",
:anchor => nil,
:local => nil,
:font => nil,
:size => nil,
:character_spacing => nil }]
string = "external link"
expect(Prawn::Text::Formatted::Parser.to_string(array)).to eq(string)
end
it "should handle anchors" do
array = [{ :text => "internal link",
:styles => [],
:color => nil,
:link => nil,
:anchor => "ToC",
:font => nil,
:size => nil,
:character_spacing => nil }]
string = "internal link"
expect(Prawn::Text::Formatted::Parser.to_string(array)).to eq(string)
end
it "should convert <, >, and & to < >, and &, respectively" do
array = [
{
:text => "hello ",
:styles => [],
:color => nil,
:link => nil,
:font => nil,
:size => nil,
:character_spacing => nil
},
{
:text => "<, >, and &",
:styles => [:bold],
:color => nil,
:link => nil,
:font => nil,
:size => nil,
:character_spacing => nil
}
]
string = "hello <, >, and &"
expect(Prawn::Text::Formatted::Parser.to_string(array)).to eq(string)
end
it "should construct an HTML-esque string from a formatted text array" do
array = [
{ :text => "hello ",
:styles => [],
:color => nil,
:link => nil,
:font => nil,
:size => 14,
:character_spacing => nil },
{ :text => "world",
:styles => [:bold],
:color => nil,
:link => nil,
:font => nil,
:size => nil,
:character_spacing => nil },
{ :text => "\n",
:styles => [:bold],
:color => nil,
:link => nil,
:font => nil,
:size => nil,
:character_spacing => nil },
{ :text => "how ",
:styles => [:bold],
:color => nil,
:link => nil,
:font => nil,
:size => nil,
:character_spacing => nil },
{ :text => "are",
:styles => [:bold, :italic],
:color => nil,
:link => nil,
:font => nil,
:size => nil,
:character_spacing => nil },
{ :text => " you?",
:styles => [],
:color => nil,
:link => nil,
:font => nil,
:size => nil,
:character_spacing => nil }
]
string = "hello world\nhow are you?"
expect(Prawn::Text::Formatted::Parser.to_string(array)).to eq(string)
end
end
describe "Text::Formatted::Parser#array_paragraphs" do
it "should group fragments separated by newlines" do
array = [{ :text => "\nhello\nworld" },
{ :text => "\n\n" },
{ :text => "how" },
{ :text => "are" },
{ :text => "you" }]
target = [[{ :text => "\n" }],
[{ :text => "hello" }],
[{ :text => "world" }],
[{ :text => "\n" }],
[{ :text => "how" },
{ :text => "are" },
{ :text => "you" }]]
expect(Prawn::Text::Formatted::Parser.array_paragraphs(array)).to eq(target)
end
it "should work properly if ending in an empty paragraph" do
array = [{ :text => "\nhello\nworld\n" }]
target = [[{ :text => "\n" }],
[{ :text => "hello" }],
[{ :text => "world" }]]
expect(Prawn::Text::Formatted::Parser.array_paragraphs(array)).to eq(target)
end
end