| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 
 | #!/usr/bin/env ruby
# encoding: utf-8
BEGIN {
	require 'pathname'
	basedir = Pathname.new( __FILE__ ).dirname.parent
	libdir = basedir + 'lib'
#	$LOAD_PATH.unshift( basedir ) unless $LOAD_PATH.include?( basedir )
#	$LOAD_PATH.unshift( libdir ) unless $LOAD_PATH.include?( libdir )
}
require 'rspec'
require 'bluecloth'
require 'spec/lib/helpers'
### Output some debugging if $DEBUG is true
def debug_msg( *args )
	$stderr.puts( *args ) if $DEBUG
end
#####################################################################
###	C O N T E X T S
#####################################################################
describe BlueCloth do
	include BlueCloth::TestConstants,
		BlueCloth::Matchers
	it "defines the top-level Markdown constant" do
		Object.const_defined?('Markdown').should be_true()
		# :FIXME: This is probably a fragile test, as anything else that defines it before
		# the BlueCloth tests run could lead to a false failure. I at least know that it'll
		# work in my environment, and I'm not sure how else to test it.
		::Markdown.should be_equal( ::BlueCloth )
	end
	it "knows what version of Discount was used to build it" do
		BlueCloth.discount_version.should =~ /^\d+\.\d+\.\d+.*GITHUB-TAGS/
	end
	it "can build a flags bitmask out of an options hash" do
		flags = BlueCloth.flags_from_opthash(
			:remove_links => true,
			:header_labels => true,
			:pandoc_headers => false
		  )
		( flags & BlueCloth::MKD_NOLINKS ).should be_nonzero()
		( flags & BlueCloth::MKD_TOC ).should be_nonzero()
		( flags & BlueCloth::MKD_NOHEADER ).should be_nonzero()
	end
	it "handles non-string content safely" do
		BlueCloth.new( nil ).text.should == ''
	end
	it "inherits the taintedness of its input" do
		str = "a string"
		BlueCloth.new( str ).should_not be_tainted()
		str.taint
		BlueCloth.new( str ).should be_tainted()
	end
	it "allows output to be rendered several times" do
		bc = BlueCloth.new( "Some text" )
		bc.to_html.should == bc.to_html
	end
	it "correctly applies the :remove_links option to the output" do
		input = "An [example](http://url.com/). A <a href='http://example.com/'>link</a>."
		expected = "<p>An [example](http://url.com/). A <a href='http://example.com/'>link</a>.</p>"
		the_markdown( input, :remove_links => true ).should be_transformed_into( expected )
	end
	it "correctly applies the :remove_images option to the output" do
		input = %{An . An <img href='http://example.com/1.jpg' />.}
		expected = %{<p>An . An <img href='http://example.com/1.jpg' />.</p>}
		the_markdown( input, :remove_images => true ).should be_transformed_into( expected )
	end
	it "correctly applies the :smartypants option to the output" do
		input = %{He was known to frequent that "other establishment"...}
		expected = %{<p>He was known to frequent that “other establishment”…</p>}
		the_markdown( input, :smartypants => true ).should be_transformed_into( expected )
	end
	it "correctly applies the :auto_links option to the output" do
		the_indented_markdown( <<-"---", :auto_links => true ).should be_transformed_into(<<-"---").without_indentation
		I wonder how many people have
		http://google.com/ as their home page.
		---
		<p>I wonder how many people have
		<a href="http://google.com/">http://google.com/</a> as their home page.</p>
		---
	end
	it "doesn't form links for protocols it doesn't know about under :safe_links mode" do
		the_indented_markdown( <<-"---", :safe_links => true ).should be_transformed_into(<<-"---").without_indentation
		This is an example 
		[of something](javascript:do_something_bad(\\)) 
		you might want to prevent.
		---
		<p>This is an example
		[of something](javascript:do<em>something</em>bad())
		you might want to prevent.</p>
		---
	end
	it "forms links for protocols it doesn't know about when not under :safe_links mode" do
		the_indented_markdown( <<-"---", :safe_links => false ).should be_transformed_into(<<-"---").without_indentation
		This is an example 
		[of something](javascript:do_something_benign(\\)) 
		you might want to allow.
		---
		<p>This is an example
		<a href="javascript:do_something_benign()">of something</a>
		you might want to allow.</p>
		---
	end
	describe "Discount extensions" do
		it "correctly applies the :pandoc_headers option" do
			input = "% title\n% author1, author2\n% date\n\nStuff."
			bc = BlueCloth.new( input, :pandoc_headers => true )
			bc.header.should == {
				:title => 'title',
				:author => 'author1, author2',
				:date => 'date'
			}
			bc.to_html.should == '<p>Stuff.</p>'
		end
		it "correctly expands id: links when :pseudoprotocols are enabled" do
			input = "It was [just as he said](id:foo) it would be."
			expected = %{<p>It was <span id="foo">just as he said</span> it would be.</p>}
			the_markdown( input, :pseudoprotocols => true ).should be_transformed_into( expected )
		end
		it "correctly expands class: links when :pseudoprotocols are enabled" do
			input = "It was [just as he said](class:foo) it would be."
			expected = %{<p>It was <span class="foo">just as he said</span> it would be.</p>}
			the_markdown( input, :pseudoprotocols => true ).should be_transformed_into( expected )
		end
		it "correctly expands raw: links when :pseudoprotocols are enabled" do
			input = %{I have node idea [what this is for](raw:really "but") it's here.}
			expected = %{<p>I have node idea really it's here.</p>}
			the_markdown( input, :pseudoprotocols => true ).should be_transformed_into( expected )
		end
		it "correctly adds IDs to headers when :header_labels is enabled" do
			input = %{# A header\n\nSome stuff\n\n## Another header\n\nMore stuff.\n\n}
			expected = %{<a name=\"A.header\"></a>\n<h1>A header</h1>\n\n<p>Some stuff</p>\n\n} +
			           %{<a name=\"Another.header\"></a>\n<h2>Another header</h2>\n\n<p>More stuff.</p>}
			the_markdown( input, :header_labels => true ).should be_transformed_into( expected )
		end
		it "expands superscripts only when :superscript is enabled" do
			input = %{It used to be that E = mc^2 used to be the province of physicists.}
			expected = %{<p>It used to be that E = mc<sup>2</sup> used to be the province} +
			           %{ of physicists.</p>}
			disabled = %{<p>It used to be that E = mc^2 used to be the province} +
			         %{ of physicists.</p>}
			the_markdown( input, :superscript => false ).should be_transformed_into( disabled )
			the_markdown( input, :superscript => true ).should be_transformed_into( expected )
		end
		it "uses relaxed emphasis when :relaxed is enabled" do
			input = %{If you use size_t instead, you _won't_ have to worry as much about portability.}
			relaxed = %{<p>If you use size_t instead, you <em>won't</em> have to worry as much about portability.</p>}
			strict = %{<p>If you use size<em>t instead, you </em>won't_ have to worry as much about portability.</p>}
			the_markdown( input, :relaxed => true ).should be_transformed_into( relaxed )
			the_markdown( input, :relaxed => false ).should be_transformed_into( strict )
		end
	end
	### Test email address output
	describe " email obfuscation" do
		TESTING_EMAILS = %w[
			address@example.com
			foo-list-admin@bar.com
			fu@bar.COM
			baz@ruby-lang.org
			foo-tim-bazzle@bar-hop.co.uk
			littlestar@twinkle.twinkle.band.CO.ZA
			ll@lll.lllll.ll
			Ull@Ulll.Ulllll.ll
			UUUU1@UU1.UU1UUU.UU
			l@ll.ll
			Ull.Ullll@llll.ll
			Ulll-Ull.Ulllll@ll.ll
			1@111.ll
		]
		# I can't see a way to handle IDNs clearly yet, so these will have to wait.
		#	info@öko.de
		#	jemand@büro.de
		#	irgendwo-interreßant@dÅgta.se
		#]
		def decode( str )
			str.gsub( /&#(x[a-f0-9]+|\d{1,3});/i ) do |match|
				code = $1
				debug_msg "Decoding &##{code};"
				case code
				when /^x([a-f0-9]+)/i
					debug_msg "-> #{$1.to_i(16).chr}"
					$1.to_i(16).chr
				when /^\d+$/
					debug_msg "-> #{code.to_i.chr}"
					code.to_i.chr
				else
					raise "Hmmm... malformed entity %p" % code
				end
			end
		end
		TESTING_EMAILS.each do |addr|
			it( "obfuscates the email address %p" % addr ) do
				html = BlueCloth.new( "<#{addr}>" ).to_html
				expected_output = %r{<p><a href="([^"]+)">[^<]+</a></p>}
				match = expected_output.match( html )
				match.should be_an_instance_of( MatchData )
				match[1].should_not == addr
				decoded_href = decode( match[1] )
				debug_msg "Unencoded href = %p" % [ decoded_href ]
				decoded_href.should == "mailto:#{addr}"
			end
		end
	end
	describe "encoding under Ruby 1.9.x", :ruby_19_only => true do
		before( :each ) do
			pending "only valid under a version of Ruby that has the Encoding class" unless
				Object.const_defined?( :Encoding )
		end
		it "outputs HTML in UTF8 if given a UTF8 string" do
			input = "a ∫‡®îñg".encode( Encoding::UTF_8 )
			output = BlueCloth.new( input ).to_html
			output.encoding.should == Encoding::UTF_8
		end
		it "outputs HTML in KOI8-U if given a KOI8-U string" do
			input = "Почему Молчишь".encode( Encoding::KOI8_U )
			output = BlueCloth.new( input ).to_html
			output.should == "<p>\xF0\xCF\xDE\xC5\xCD\xD5 \xED\xCF\xCC\xDE\xC9\xDB\xD8</p>".
				force_encoding( Encoding::KOI8_U )
		end
		it "outputs HTML in Shift-JIS if given a Shift-JIS string" do
			input = "日本語".encode( Encoding::SHIFT_JIS )
			output = BlueCloth.new( input ).to_html
			output.should == "<p>\x93\xFA\x96{\x8C\xEA</p>".
				force_encoding( Encoding::SHIFT_JIS )
		end
	end
end
# vim: set nosta noet ts=4 sw=4:
 |