Class: BlueCloth

Inherits:
String show all
Defined in:
lib/bluecloth_tweaked.rb

Overview

BlueCloth is a Ruby implementation of Markdown, a text-to-HTML conversion tool.

Defined Under Namespace

Classes: FormatError, RenderState

Constant Summary collapse

Version =

Release Version

'0.0.3'
SvnRev =

SVN Revision

%q$Rev: 37 $
SvnId =

SVN Id tag

%q$Id: bluecloth.rb,v 1.3 2004/05/02 15:56:33 webster132 Exp $
SvnUrl =

SVN URL

%q$URL: svn+ssh://cvs.faeriemud.org/var/svn/BlueCloth/trunk/lib/bluecloth.rb $
TabWidth =

Tab width for #detab! if none is specified

4
EmptyElementSuffix =

The tag-closing string – set to ‘>’ for HTML

"/>"
EscapeTable =

Table of MD5 sums for escaped characters

{}
BlockTags =

The list of tags which are considered block-level constructs and an alternation pattern suitable for use in regexps made from the list

%w[ p div h[1-6] blockquote pre table dl ol ul script ]
BlockTagPattern =
BlockTags.join('|')
StrictBlockRegex =

Nested blocks: <div> <div> tags for inner block must be indented. </div> </div>

%r{
		^						# Start of line
		<(#{BlockTagPattern})	# Start tag: \2
		\b						# word break
		(.*\n)*?				# Any number of lines, minimal match
		</\1>					# Matching end tag
		[ ]*					# trailing spaces
		(?=\n+|\Z)				# End of line or document
}ix
LooseBlockRegex =

More-liberal block-matching

%r{
		^						# Start of line
		<(#{BlockTagPattern})	# start tag: \2
		\b						# word break
		(.*\n)*?				# Any number of lines, minimal match
		.*</\1>					# Anything + Matching end tag
		[ ]*					# trailing spaces
		(?=\n+|\Z)				# End of line or document
}ix
HruleBlockRegex =

Special case for <hr />.

%r{
		(						# $1
\A\n?				# Start of doc + optional \n
|					# or
.*\n\n				# anything + blank line
		)
		(						# save in $2
[ ]*				# Any spaces
<hr					# Tag open
\b					# Word break
([^<>])*?			# Attributes
/?>					# Tag close
(?=\n\n|\Z)			# followed by a blank line or end of document
		)
}ix
LinkRegex =

Link defs are in the form: ^[id]: url “optional title”

%r{
		^[ ]*\[(.+)\]:		# id = $1
 [ ]*
 \n?				# maybe *one* newline
 [ ]*
		(\S+)				# url = $2
 [ ]*
 \n?				# maybe one newline
 [ ]*
		(?:
# Titles are delimited by "quotes" or (parens).
["(]
(.+?)			# title = $3
[")]			# Matching ) or "
[ ]*
		)?	# title is optional
		(?:\n+|\Z)
}x
ListRegexp =

Pattern to transform lists

%r{
 (?:
^[ ]{0,#{TabWidth - 1}}	# Indent < tab width
(\*|\d+\.)						# unordered or ordered ($1)
[ ]+							# At least one space
 )
 (?m:.+?)							# item content (include newlines)
 (?:
  \z							# Either EOF
|								#  or
  \n{2,}						# Blank line...
  (?=\S)						# ...followed by non-space
  (?![ ]* (\*|\d+\.) [ ]+)		# ...but not another item
 )
}x
ListItemRegexp =

Pattern for transforming list items

%r{
		(\n)?							# leading line = $1
		(^[ ]*)							# leading whitespace = $2
		(\*|\d+\.) [ ]+					# list marker = $3
		((?m:.+?)						# list item text   = $4
		(\n{1,2}))
		(?= \n* (\z | \2 (\*|\d+\.) [ ]+))
}x
CodeBlockRegexp =

Pattern for matching codeblocks

%r{
		(.?)								# $1 = preceding character
		:\n+								# colon + NL delimiter
		(									# $2 = the code block
 (?:
(?:[ ]{#{TabWidth}} | \t)		# a tab or tab-width of spaces
.*\n+
 )+
		)
		((?=^[ ]{0,#{TabWidth}}\S)|\Z)		# Lookahead for non-space at
								# line-start, or end of doc
}x
BlockQuoteRegexp =

Pattern for matching Markdown blockquote blocks

%r{
 (?:
^[ ]*>[ ]?		# '>' at the start of a line
  .+\n			# rest of the first line
(?:.+\n)*		# subsequent consecutive lines
\n*				# blanks
 )+
}x
AutoAnchorURLRegexp =
/<((https?|ftp):[^'">\s]+)>/
AutoAnchorEmailRegexp =
%r{
		<
		(
[-.\w]+
\@
[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+
		)
		>
}x
Encoders =

Encoder functions to turn characters of an email address into encoded entities.

[
	lambda {|char| "&#%03d;" % char},
	lambda {|char| "&#x%X;" % char},
	lambda {|char| char.chr },
]
SetextHeaderRegexp =

Regex for matching Setext-style headers

%r{
		(.+)			# The title text ($1)
		\n
		([\-=])+		# Match a line of = or -. Save only one in $2.
		[ ]*\n+
}x
AtxHeaderRegexp =

Regexp for matching ATX-style headers

%r{
		^(\#{1,6})	# $1 = string of #'s
		[ ]*
		(.+?)		# $2 = Header text
		[ ]*
		\#*			# optional closing #'s (not counted)
		\n+
}x
RefLinkIdRegex =

Pattern to match the linkid part of an anchor tag for reference-style links.

%r{
		[ ]?					# Optional leading space
		(?:\n[ ]*)?				# Optional newline + spaces
		\[
(.*?)				# Id = $1
		\]
}x
InlineLinkRegex =
%r{
		\(						# Literal paren
[ ]*				# Zero or more spaces
(.*?)				# URI = $1
[ ]*				# Zero or more spaces
(?:					# 
	([\"\'])		# Opening quote char = $2
	(.*?)			# Title = $3
	\2				# Matching quote char
)?					# Title is optional
		\)
}x
BoldRegexp =

Pattern to match strong emphasis in Markdown text

%r{ (\*\*|__) (?=\S) (.+?\S) \1 }x
ItalicRegexp =

Pattern to match normal emphasis in Markdown text

%r{ (\*|_) (?=\S) (.+?\S) \1 }x
InlineImageRegexp =

Next, handle inline images: ![alt text](url “optional title”) Don’t forget: encode * and _

%r{
		(					# Whole match = $1
!\[ (.*?) \]	# alt text = $2
 \([ ]* (\S+) [ ]*	# source url = $3
(				# title = $4
  (["'])		# quote char = $5
  .*?
  \5			# matching quote
  [ ]*
)?				# title is optional
 \)
		)
}xs
ReferenceImageRegexp =

Reference-style images

%r{
		(					# Whole match = $1
!\[ (.*?) \]	# Alt text = $2
[ ]?			# Optional space
(?:\n[ ]*)?		# One optional newline + spaces
\[ (.*?) \]		# id = $3
		)
}xs
CodeEscapeRegexp =

Regexp to match special characters in a code block

%r{( \* | _ | \{ | \} | \[ | \] )}x
HTMLCommentRegexp =

Matching constructs for tokenizing X/HTML

%r{ <! ( -- .*? -- \s* )+ > }mx
XMLProcInstRegexp =
%r{ <\? .*? \?> }mx
MetaTag =
Regexp::union( HTMLCommentRegexp, XMLProcInstRegexp )
HTMLTagOpenRegexp =
%r{ < [a-z/!$] [^<>]* }mx
HTMLTagCloseRegexp =
%r{ > }x
HTMLTagPart =
Regexp::union( HTMLTagOpenRegexp, HTMLTagCloseRegexp )

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from String

#texesc!

Constructor Details

#initialize(content = "", *restrictions) ⇒ BlueCloth

Create a new BlueCloth string.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/bluecloth_tweaked.rb', line 126

def initialize( content="", *restrictions )
	@log = Logger::new( $deferr )
	@log.level = $DEBUG ?
		Logger::DEBUG :
		($VERBOSE ? Logger::INFO : Logger::WARN)
	@scanner = nil

	# Add any restrictions, and set the line-folding attribute to reflect
	# what happens by default.
	restrictions.flatten.each {|r| __send__("#{r}=", true) }
	@fold_lines = true

	super( content )

	@log.debug "String is: %p" % self
end

Instance Attribute Details

#filter_htmlObject

Filters for controlling what gets output for untrusted input. (But really, you’re filtering bad stuff out of untrusted input at submission-time via untainting, aren’t you?)



151
152
153
# File 'lib/bluecloth_tweaked.rb', line 151

def filter_html
  @filter_html
end

#filter_stylesObject

Filters for controlling what gets output for untrusted input. (But really, you’re filtering bad stuff out of untrusted input at submission-time via untainting, aren’t you?)



151
152
153
# File 'lib/bluecloth_tweaked.rb', line 151

def filter_styles
  @filter_styles
end

#fold_linesObject

RedCloth-compatibility accessor. Line-folding is part of Markdown syntax, so this isn’t used by anything.



155
156
157
# File 'lib/bluecloth_tweaked.rb', line 155

def fold_lines
  @fold_lines
end

Instance Method Details

#apply_block_transforms(str, rs) ⇒ Object

Do block-level transforms on a copy of str using the specified render state rs and return the results.



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/bluecloth_tweaked.rb', line 238

def apply_block_transforms( str, rs )
	# Port: This was called '_runBlockGamut' in the original

	@log.debug "Applying block transforms to:\n  %p" % str
	text = transform_headers( str, rs )
	text = transform_hrules( text, rs )
	text = transform_lists( text, rs )
	text = transform_code_blocks( text, rs )
	text = transform_block_quotes( text, rs )
	text = transform_auto_links( text, rs )
	text = hide_html_blocks( text, rs )

	text = form_paragraphs( text, rs )

	@log.debug "Done with block transforms:\n  %p" % text
	return text
end

#apply_span_transforms(str, rs) ⇒ Object

Apply Markdown span transforms to a copy of the specified str with the given render state rs and return it.



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/bluecloth_tweaked.rb', line 259

def apply_span_transforms( str, rs )
	@log.debug "Applying span transforms to:\n  %p" % str

	str = transform_code_spans( str, rs )
	str = encode_html( str )
	str = transform_images( str, rs )
	str = transform_anchors( str, rs )
	str = transform_italic_and_bold( str, rs )

	# Hard breaks
	str.gsub!( / {2,}\n/, "<br#{EmptyElementSuffix}\n" )

	@log.debug "Done with span transforms:\n  %p" % str
	return str
end

#detab(tabwidth = TabWidth) ⇒ Object

Convert tabs in str to spaces.



214
215
216
217
218
# File 'lib/bluecloth_tweaked.rb', line 214

def detab( tabwidth=TabWidth )
	copy = self.dup
	copy.detab!( tabwidth )
	return copy
end

#detab!(tabwidth = TabWidth) ⇒ Object

Convert tabs to spaces in place and return self if any were converted.



222
223
224
225
226
227
228
229
# File 'lib/bluecloth_tweaked.rb', line 222

def detab!( tabwidth=TabWidth )
	newstr = self.split( /\n/ ).collect {|line|
		line.gsub( /(.*?)\t/ ) do
			$1 + ' ' * (tabwidth - $1.length % tabwidth)
		end
	}.join("\n")
	self.replace( newstr )
end

#encode_backslash_escapes(str) ⇒ Object

Return a copy of the given str with any backslashed special character in it replaced with MD5 placeholders.



432
433
434
435
436
437
438
439
440
441
442
# File 'lib/bluecloth_tweaked.rb', line 432

def encode_backslash_escapes( str )
	# Make a copy with any double-escaped backslashes encoded
	text = str.gsub( /\\\\/, EscapeTable['\\'][:md5] )
	
	EscapeTable.each_pair {|char, esc|
		next if char == '\\'
		text.gsub!( esc[:re], esc[:md5] )
	}

	return text
end

#encode_code(str, rs) ⇒ Object

Escape any characters special to HTML and encode any characters special to Markdown in a copy of the given str and return it.



1009
1010
1011
1012
1013
1014
# File 'lib/bluecloth_tweaked.rb', line 1009

def encode_code( str, rs )
	str.gsub( %r{&}, '&amp;' ).
		gsub( %r{<}, '&lt;' ).
		gsub( %r{>}, '&gt;' ).
		gsub( CodeEscapeRegexp ) {|match| EscapeTable[match][:md5]}
end

#encode_email_address(addr) ⇒ Object

Transform a copy of the given email addr into an escaped version safer for posting publicly.



616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
# File 'lib/bluecloth_tweaked.rb', line 616

def encode_email_address( addr )

	rval = ''
	("mailto:" + addr).each_byte {|b|
		case b
		when ?:
			rval += ":"
		when ?@
			rval += Encoders[ rand(2) ][ b ]
		else
			r = rand(100)
			rval += (
				r > 90 ? Encoders[2][ b ] :
				r < 45 ? Encoders[1][ b ] :
						 Encoders[0][ b ]
			)
		end
	}

	return %{<a href="%s">%s</a>} % [ rval, rval.sub(/.+?:/, '') ]
end

#encode_html(str) ⇒ Object

Return a copy of str with angle brackets and ampersands HTML-encoded.



1114
1115
1116
1117
# File 'lib/bluecloth_tweaked.rb', line 1114

def encode_html( str )
	str.gsub( /&(?!#?[x]?(?:[0-9a-f]+|\w{1,8});)/i, "&amp;" ).
		gsub( %r{<(?![a-z/?\$!])}i, "&lt;" )
end

#escape_md(str) ⇒ Object

Escape any markdown characters in a copy of the given str and return it.



1024
1025
1026
1027
1028
# File 'lib/bluecloth_tweaked.rb', line 1024

def escape_md( str )
	str.
		gsub( /\*/, '&#42;' ).
		gsub( /_/,  '&#95;' )
end

#escape_special_chars(str) ⇒ Object

Escape special characters in the given str



390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'lib/bluecloth_tweaked.rb', line 390

def escape_special_chars( str )
	@log.debug "  Escaping special characters"
	text = ''

	tokenize_html( str ) {|token, str|
		@log.debug "   Adding %p token %p" % [ token, str ]
		case token

		# Within tags, encode * and _
		when :tag
			text += str.
				gsub( /\*/, EscapeTable['*'][:md5] ).
				gsub( /_/, EscapeTable['_'][:md5] )

		# Encode backslashed stuff in regular text
		when :text
			text += encode_backslash_escapes( str )
		else
			raise TypeError, "Unknown token type %p" % token
		end
	}

	@log.debug "  Text with escapes is now: %p" % text
	return text
end

#form_paragraphs(str, rs) ⇒ Object

Wrap all remaining paragraph-looking text in a copy of str inside <p> tags and return it.



698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
# File 'lib/bluecloth_tweaked.rb', line 698

def form_paragraphs( str, rs )
	@log.debug " Forming paragraphs"
	grafs = str.
		sub( /\A\n+/, '' ).
		sub( /\n+\z/, '' ).
		split( /\n{2,}/ )

	rval = grafs.collect {|graf|

		# Unhashify HTML blocks if this is a placeholder
		if rs.html_blocks.key?( graf )
			rs.html_blocks[ graf ]

		# Otherwise, wrap in <p> tags
		else
			apply_span_transforms(graf, rs).
				sub( /^[ ]*/, '<p>' ) + '</p>'
		end
	}.join( "\n\n" )

	@log.debug " Formed paragraphs: %p" % rval
	return rval
end

#hide_html_blocks(str, rs) ⇒ Object

Replace all blocks of HTML in str that start in the left margin with tokens.



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/bluecloth_tweaked.rb', line 327

def hide_html_blocks( str, rs )
	@log.debug "Hiding HTML blocks in %p" % str
	
	# Tokenizer proc to pass to gsub
	tokenize = lambda {|match|
		key = Digest::MD5::hexdigest( match )
		rs.html_blocks[ key ] = match
		@log.debug "Replacing %p with %p" %
			[ match, key ]
		"\n\n#{key}\n\n"
	}

	rval = str.dup

	@log.debug "Finding blocks with the strict regex..."
	rval.gsub!( StrictBlockRegex, &tokenize )

	@log.debug "Finding blocks with the loose regex..."
	rval.gsub!( LooseBlockRegex, &tokenize )

	@log.debug "Finding hrules..."
	rval.gsub!( HruleBlockRegex ) {|match| $1 + tokenize[$2] }

	return rval
end

#outdent(str) ⇒ Object

Return one level of line-leading tabs or spaces from a copy of str and return it.



1122
1123
1124
# File 'lib/bluecloth_tweaked.rb', line 1122

def outdent( str )
	str.gsub( /^(\t|[ ]{1,#{TabWidth}})/, '')
end

Strip link definitions from str, storing them in the given RenderState rs.



376
377
378
379
380
381
382
383
384
385
386
# File 'lib/bluecloth_tweaked.rb', line 376

def strip_link_definitions( str, rs )
	str.gsub( LinkRegex ) {|match|
		id, url, title = $1, $2, $3

		rs.urls[ id.downcase ] = encode_html( url )
		unless title.nil?
			rs.titles[ id.downcase ] = title.gsub( /"/, "&quot;" )
		end
		""
	}
end

#to_html(lite = false) ⇒ Object

Render Markdown-formatted text in this string object as HTML and return it. The parameter is for compatibility with RedCloth, and is currently unused, though that may change in the future.



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
# File 'lib/bluecloth_tweaked.rb', line 161

def to_html( lite=false )

	# Create a StringScanner we can reuse for various lexing tasks
	@scanner = StringScanner::new( '' )

	# Make a structure to carry around stuff that gets placeholdered out of
	# the source.
	rs = RenderState::new( {}, {}, {} )

	# Make a copy of the string with normalized line endings, tabs turned to
	# spaces, and a couple of guaranteed newlines at the end
	text = self.gsub( /\r\n?/, "\n" ).detab
	text += "\n\n"
	@log.debug "Normalized line-endings: %p" % text

	# Filter HTML if we're asked to do so
	if self.filter_html
		text.gsub!( "<", "&lt;" )
		text.gsub!( ">", "&gt;" )
		@log.debug "Filtered HTML: %p" % text
	end

	# Simplify blank lines
	text.gsub!( /^ +$/, '' )
	@log.debug "Tabs -> spaces/blank lines stripped: %p" % text

	# Replace HTML blocks with placeholders
	text = hide_html_blocks( text, rs )
	@log.debug "Hid HTML blocks: %p" % text
	@log.debug "Render state: %p" % rs

	# Strip link definitions, store in render state
	text = strip_link_definitions( text, rs )
	@log.debug "Stripped link definitions: %p" % text
	@log.debug "Render state: %p" % rs

	# Escape meta-characters
	text = escape_special_chars( text )
	@log.debug "Escaped special characters: %p" % text

	# Transform block-level constructs
	text = apply_block_transforms( text, rs )
	@log.debug "After block-level transforms: %p" % text

	# Now swap back in all the escaped characters
	text = unescape_special_chars( text )
	@log.debug "After unescaping special characters: %p" % text

	return text
end

#tokenize_html(str) ⇒ Object

Break the HTML source in str into a series of tokens and return them. The tokens are just 2-element Array tuples with a type and the actual content. If this function is called with a block, the type and text parts of each token will be yielded to it one at a time as they are extracted.



1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
# File 'lib/bluecloth_tweaked.rb', line 1045

def tokenize_html( str )
	depth = 0
	tokens = []
	@scanner.string = str.dup
	type, token = nil, nil

	until @scanner.empty?
		@log.debug "Scanning from %p" % @scanner.rest

		# Match comments and PIs without nesting
		if (( token = @scanner.scan(MetaTag) ))
			type = :tag

		# Do nested matching for HTML tags
		elsif (( token = @scanner.scan(HTMLTagOpenRegexp) ))
			tagstart = @scanner.pos
			@log.debug " Found the start of a plain tag at %d" % tagstart

			# Start the token with the opening angle
			depth = 1
			type = :tag

			# Scan the rest of the tag, allowing unlimited nested <>s. If
			# the scanner runs out of text before the tag is closed, raise
			# an error.
			while depth.nonzero?

				# Scan either an opener or a closer
				chunk = @scanner.scan( HTMLTagPart ) or
					raise "Malformed tag at character %d: %p" % 
						[ tagstart, token + @scanner.rest ]
					
				@log.debug "  Found another part of the tag at depth %d: %p" %
					[ depth, chunk ]

				token += chunk

				# If the last character of the token so far is a closing
				# angle bracket, decrement the depth. Otherwise increment
				# it for a nested tag.
				depth += ( token[-1, 1] == '>' ? -1 : 1 )
				@log.debug "  Depth is now #{depth}"
			end

		# Match text segments
		else
			@log.debug " Looking for a chunk of text"
			type = :text

			# Scan forward, always matching at least one character to move
			# the pointer beyond any non-tag '<'.
			token = @scanner.scan_until( /[^<]+/m )
		end

		@log.debug " type: %p, token: %p" % [ type, token ]

		# If a block is given, feed it one token at a time. Add the token to
		# the token list to be returned regardless.
		if block_given?
			yield( type, token )
		end
		tokens << [ type, token ]
	end

	return tokens
end

#transform_anchors(str, rs) ⇒ Object

Apply Markdown anchor transforms to a copy of the specified str with the given render state rs and return it.



748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
# File 'lib/bluecloth_tweaked.rb', line 748

def transform_anchors( str, rs )
	@log.debug " Transforming anchors"
	@scanner.string = str.dup
	text = ''

	# Scan the whole string
	until @scanner.empty?
	
		if @scanner.scan( /\[/ )
			link = ''; linkid = ''
			depth = 1
			startpos = @scanner.pos
			@log.debug " Found a bracket-open at %d" % startpos

			# Scan the rest of the tag, allowing unlimited nested []s. If
			# the scanner runs out of text before the opening bracket is
			# closed, append the text and return (wasn't a valid anchor).
			while depth.nonzero?
				linktext = @scanner.scan_until( /\]|\[/ )

				if linktext
					@log.debug "  Found a bracket at depth %d: %p" %
						[ depth, linktext ]
					link += linktext

					# Decrement depth for each closing bracket
					depth += ( linktext[-1, 1] == ']' ? -1 : 1 )
					@log.debug "  Depth is now #{depth}"

				# If there's no more brackets, it must not be an anchor, so
				# just abort.
				else
					@log.debug "  Missing closing brace, assuming non-link."
					link += @scanner.rest
					@scanner.terminate
					return text + '[' + link
				end
			end
			link.slice!( -1 ) # Trim final ']'
			@log.debug " Found leading link %p" % link

			# Look for a reference-style second part
			if @scanner.scan( RefLinkIdRegex )
				linkid = @scanner[1]
				linkid = link.dup if linkid.empty?
				linkid.downcase!
				@log.debug "  Found a linkid: %p" % linkid

				# If there's a matching link in the link table, build an
				# anchor tag for it.
				if rs.urls.key?( linkid )
					@log.debug "   Found link key in the link table: %p" %
						rs.urls[linkid]
					url = escape_md( rs.urls[linkid] )

					text += %{<a href="#{url}"}
					if rs.titles.key?(linkid)
						text += %{ title="%s"} % escape_md( rs.titles[linkid] )
					end
					text += %{>#{link}</a>}

				# If the link referred to doesn't exist, just append the raw
				# source to the result
				else
					@log.debug "  Linkid %p not found in link table" % linkid
					@log.debug "  Appending original string instead: %p" %
						@scanner.string[ startpos-1 .. @scanner.pos ]
					text += @scanner.string[ startpos-1 .. @scanner.pos ]
				end

			# ...or for an inline style second part
			elsif @scanner.scan( InlineLinkRegex )
				url = @scanner[1]
				title = @scanner[3]
				@log.debug "  Found an inline link to %p" % url

				text += %{<a href="%s"} % escape_md( url )
				if title
					text += %{ title="%s"} % escape_md( title )
				end
				text += %{>#{link}</a>}

			# No linkid part: just append the first part as-is.
			else
				@log.debug "No linkid, so no anchor. Appending literal text."
				text += @scanner.string[ startpos-1 .. @scanner.pos-1 ]
			end # if linkid

		# Plain text
		else
			@log.debug " Scanning to the next link from %p" % @scanner.rest
			text += @scanner.scan( /[^\[]+/ )
		end

	end # until @scanner.empty?

	return text
end

Transform URLs in a copy of the specified str into links and return it.



597
598
599
600
601
602
603
# File 'lib/bluecloth_tweaked.rb', line 597

def transform_auto_links( str, rs )
	@log.debug " Transforming auto-links"
	str.gsub( AutoAnchorURLRegexp, %{<a href="\\1">\\1</a>}).
		gsub( AutoAnchorEmailRegexp ) {|addr|
		encode_email_address( unescape_special_chars($1) )
	}
end

#transform_block_quotes(str, rs) ⇒ Object

Transform Markdown-style blockquotes in a copy of the specified str and return it.



571
572
573
574
575
576
577
578
579
580
581
# File 'lib/bluecloth_tweaked.rb', line 571

def transform_block_quotes( str, rs )
	@log.debug " Transforming block quotes"

	str.gsub( BlockQuoteRegexp ) {|quote|
		@log.debug "Making blockquote from %p" % quote
		quote.gsub!( /^[ ]*>[ ]?/, '' )
		%{<blockquote>\n%s\n</blockquote>\n\n} %
			apply_block_transforms( quote, rs ).
			gsub( /^/, " " * TabWidth )
	}
end

#transform_code_blocks(str, rs) ⇒ Object

Transform Markdown-style codeblocks in a copy of the specified str and return it.



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/bluecloth_tweaked.rb', line 542

def transform_code_blocks( str, rs )
	@log.debug " Transforming code blocks"

	str.gsub( CodeBlockRegexp ) {|block|
		prevchar, codeblock = $1, $2

		@log.debug "  prevchar = %p" % prevchar

		# Generated the codeblock
		%{%s\n\n<pre><code>%s\n</code></pre>\n\n} % [
			(prevchar.empty? || /\s/ =~ prevchar) ? "" : "#{prevchar}:",
			encode_code( outdent(codeblock), rs ).rstrip,
		]
	}
end

#transform_code_spans(str, rs) ⇒ Object

Transform backticked spans into <code> spans.



865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
# File 'lib/bluecloth_tweaked.rb', line 865

def transform_code_spans( str, rs )
	@log.debug " Transforming code spans"

	# Set up the string scanner and just return the string unless there's at
	# least one backtick.
	@scanner.string = str.dup
	unless @scanner.exist?( /`/ )
		@scanner.terminate
		@log.debug "No backticks found for code span in %p" % str
		return str
	end

	@log.debug "Transforming code spans in %p" % str

	# Build the transformed text anew
	text = ''

	# Scan to the end of the string
	until @scanner.empty?

		# Scan up to an opening backtick
		if pre = @scanner.scan_until( /.?(?=`)/m )
			text += pre
			@log.debug "Found backtick at %d after '...%s'" %
				[ @scanner.pos, text[-10, 10] ]

			# Make a pattern to find the end of the span
			opener = @scanner.scan( /`+/ )
			len = opener.length
			closer = Regexp::new( opener )
			@log.debug "Scanning for end of code span with %p" % closer

			# Scan until the end of the closing backtick sequence. Chop the
			# backticks off the resultant string, strip leading and trailing
			# whitespace, and encode any enitites contained in it.
			codespan = @scanner.scan_until( closer ) or
				raise FormatError::new( @scanner.rest[0,20],
					"No %p found before end" % opener )

			@log.debug "Found close of code span at %d: %p" %
				[ @scanner.pos - len, codespan ]
			codespan.slice!( -len, len )
			text += "<code>%s</code>" %
				encode_code( codespan.strip, rs )

		# If there's no more backticks, just append the rest of the string
		# and move the scan pointer to the end
		else
			text += @scanner.rest
			@scanner.terminate
		end
	end

	return text
end

#transform_headers(str, rs) ⇒ Object

Apply Markdown header transforms to a copy of the given str amd render state rs and return the result.



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# File 'lib/bluecloth_tweaked.rb', line 659

def transform_headers( str, rs )
	@log.debug " Transforming headers"

	# Setext-style headers:
	#	  Header 1
	#	  ========
	#  
	#	  Header 2
	#	  --------
	#
	str.
		gsub( SetextHeaderRegexp ) {|m|
			@log.debug "Found setext-style header"
			title, hdrchar = $1, $2
			title = apply_span_transforms( title, rs )

			case hdrchar
			when '='
				%[<h1>#{title}</h1>\n\n]
			when '-'
				%[<h2>#{title}</h2>\n\n]
			else
				title
			end
		}.

		gsub( AtxHeaderRegexp ) {|m|
			@log.debug "Found ATX-style header"
			hdrchars, title = $1, $2
			title = apply_span_transforms( title, rs )

			level = hdrchars.length
			%{<h%d>%s</h%d>\n\n} % [ level, title, level ]
		}
end

#transform_hrules(str, rs) ⇒ Object

Transform any Markdown-style horizontal rules in a copy of the specified str and return it.



447
448
449
450
# File 'lib/bluecloth_tweaked.rb', line 447

def transform_hrules( str, rs )
	@log.debug " Transforming horizontal rules"
	str.gsub( /^( ?[\-\*] ?){3,}$/, "\n<hr#{EmptyElementSuffix}\n" )
end

#transform_images(str, rs) ⇒ Object

Turn image markup into image tags.



950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'lib/bluecloth_tweaked.rb', line 950

def transform_images( str, rs )
	@log.debug " Transforming images" % str

	# Handle reference-style labeled images: ![alt text][id]
	str.
		gsub( ReferenceImageRegexp ) {|match|
			whole, alt, linkid = $1, $2, $3.downcase
			@log.debug "Matched %p" % match
			res = nil

			# for shortcut links like ![this][].
			linkid = alt.downcase if linkid.empty?

			if rs.urls.key?( linkid )
				url = escape_md( rs.urls[linkid] )
				@log.debug "Found url '%s' for linkid '%s' " %
					[ url, linkid ]

				# Build the tag
				result = %{<img src="%s" alt="%s"} % [ url, alt ]
				if rs.titles.key?( linkid )
					result += %{ title="%s"} % escape_md( rs.titles[linkid] )
				end
				result += EmptyElementSuffix

			else
				result = whole
			end

			@log.debug "Replacing %p with %p" %
				[ match, result ]
			result
		}.

		# Inline image style
		gsub( InlineImageRegexp ) {|match|
			@log.debug "Found inline image %p" % match
			whole, alt, title = $1, $2, $4
			url = escape_md( $3 )

			# Build the tag
			result = %{<img src="%s" alt="%s"} % [ url, alt ]
			unless title.nil?
				result += %{ title="%s"} % escape_md( title.gsub(/^"|"$/, '') )
			end
			result += EmptyElementSuffix

			@log.debug "Replacing %p with %p" %
				[ match, result ]
			result
		}
end

#transform_italic_and_bold(str, rs) ⇒ Object

Transform italic- and bold-encoded text in a copy of the specified str and return it.



855
856
857
858
859
860
861
# File 'lib/bluecloth_tweaked.rb', line 855

def transform_italic_and_bold( str, rs )
	@log.debug " Transforming italic and bold"

	str.
		gsub( BoldRegexp, %{<strong>\\2</strong>} ).
		gsub( ItalicRegexp, %{<em>\\2</em>} )
end

#transform_list_items(str, rs) ⇒ Object

Transform list items in a copy of the given str and return it.



501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
# File 'lib/bluecloth_tweaked.rb', line 501

def transform_list_items( str, rs )
	@log.debug " Transforming list items"

	# Trim trailing blank lines
	str = str.sub( /\n{2,}\z/, "\n" )

	str.gsub( ListItemRegexp ) {|line|
		@log.debug "  Found item line %p" % line
		leading_line, item = $1, $4

		if leading_line or /\n{2,}/.match( item )
			@log.debug "   Found leading line or item has a blank"
			item = apply_block_transforms( outdent(item), rs )
		else
			# Recursion for sub-lists
			@log.debug "   Recursing for sublist"
			item = transform_lists( outdent(item), rs ).chomp
			item = apply_span_transforms( item, rs )
		end

		%{<li>%s</li>\n} % item
	}
end

#transform_lists(str, rs) ⇒ Object

Transform Markdown-style lists in a copy of the specified str and return it.



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/bluecloth_tweaked.rb', line 473

def transform_lists( str, rs )
	@log.debug " Transforming lists at %p" % (str[0,100] + '...')

	str.gsub( ListRegexp ) {|list|
		@log.debug "  Found list %p" % list
		list_type = ($1 == '*' ? "ul" : "ol")
		list.gsub!( /\n{2,}/, "\n\n\n" )

		%{<%s>\n%s</%s>\n} % [
			list_type,
			transform_list_items( list, rs ),
			list_type,
		]
	}
end

#unescape_special_chars(str) ⇒ Object

Swap escaped special characters in a copy of the given str and return it.



419
420
421
422
423
424
425
426
427
# File 'lib/bluecloth_tweaked.rb', line 419

def unescape_special_chars( str )
	EscapeTable.each {|char, hash|
		@log.debug "Unescaping escaped %p with %p" %
			[ char, hash[:md5re] ]
		str.gsub!( hash[:md5re], char )
	}

	return str
end