Changeset 129
- Timestamp:
- 01/16/2007 15:23:36 (18 months ago)
- Location:
- trunk
- Files:
-
- 4 modified
-
lib/redcloth/base.rb (modified) (7 diffs)
-
lib/redcloth/docbook.rb (modified) (7 diffs)
-
lib/redcloth/textile.rb (modified) (6 diffs)
-
run-tests.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/redcloth/base.rb
r128 r129 10 10 :block_markdown_bq, :block_markdown_lists, 11 11 :inline_markdown_reflink, :inline_markdown_link] 12 DOCBOOK_RULES = [:refs_docbook, :block_docbook_table, :block_docbook_lists, :block_docbook_simple_lists,13 :block_docbook_defs, :block_docbook_prefix, :inline_docbook_image, :inline_docbook_link,12 DOCBOOK_RULES = [:refs_docbook, :block_docbook_table, :block_docbook_lists, :block_docbook_simple_lists, 13 :block_docbook_defs, :block_docbook_prefix, :inline_docbook_image, :inline_docbook_link, 14 14 :inline_docbook_code, :inline_docbook_glyphs, :inline_docbook_span, 15 15 :inline_docbook_wiki_words, :inline_docbook_wiki_links, :inline_docbook_autolink_urls, … … 98 98 text = self.dup 99 99 100 return "" if text == ""100 return "" if text == "" 101 101 102 102 @urlrefs = {} … … 401 401 BLOCK_GROUP_SPLITTER = "XXX_BLOCK_GROUP_XXX\n\n" 402 402 def blocks( text, deep_code = false ) 403 @current_class ||= nil404 405 # Find all occurences of div(class). and process them as blocks406 text.gsub!( /^div\((.*?)\)\.\s*(.*?)(?=div\([^\)]+\)\.\s*)/m ) do |blk|407 block_class = (@current_class == $1) ? nil : %{ class=#{$1.inspect}}408 @current_class = $1409 BLOCK_GROUP_SPLITTER + ( ($2.strip.empty? || block_class.nil?) ? $2 : textile_p('div', block_class, nil, "\n\n#{$2.strip}\n\n") )410 end411 412 # Take care of the very last div413 text.sub!( /div\((.*?)\)\.\s*(.*)/m ) do |blk|414 block_class = (@current_class == $1) ? nil : %{ class=#{$1.inspect}}415 @current_class = $1416 BLOCK_GROUP_SPLITTER + ( ($2.strip.empty? || block_class.nil?) ? $2 : textile_p('div', block_class, nil, "\n\n#{$2.strip}\n\n") )417 end403 @current_class ||= nil 404 405 # Find all occurences of div(class). and process them as blocks 406 text.gsub!( /^div\((.*?)\)\.\s*(.*?)(?=div\([^\)]+\)\.\s*)/m ) do |blk| 407 block_class = (@current_class == $1) ? nil : %{ class=#{$1.inspect}} 408 @current_class = $1 409 BLOCK_GROUP_SPLITTER + ( ($2.strip.empty? || block_class.nil?) ? $2 : textile_p('div', block_class, nil, "\n\n#{$2.strip}\n\n") ) 410 end 411 412 # Take care of the very last div 413 text.sub!( /div\((.*?)\)\.\s*(.*)/m ) do |blk| 414 block_class = (@current_class == $1) ? nil : %{ class=#{$1.inspect}} 415 @current_class = $1 416 BLOCK_GROUP_SPLITTER + ( ($2.strip.empty? || block_class.nil?) ? $2 : textile_p('div', block_class, nil, "\n\n#{$2.strip}\n\n") ) 417 end 418 418 419 419 # Handle the text now that the placeholders for divs are set, splitting at BLOCK_GROUP_SPLITTER … … 422 422 end.join) 423 423 end 424 425 def block_groups( text, deep_code = false )426 text.replace text.split( BLOCKS_GROUP_RE ).collect { |blk| blk(blk, deep_code) }.join("\n")427 end428 429 # Surrounds blocks with paragraphs and shelves them when necessary430 def blk( text, deep_code = false )431 return text if text =~ /<[0-9]+>/432 433 plain = text !~ /\A[#*> ]/424 425 def block_groups( text, deep_code = false ) 426 text.replace text.split( BLOCKS_GROUP_RE ).collect { |blk| blk(blk, deep_code) }.join("\n") 427 end 428 429 # Surrounds blocks with paragraphs and shelves them when necessary 430 def blk( text, deep_code = false ) 431 return text if text =~ /<[0-9]+>/ 432 433 plain = text !~ /\A[#*> ]/ 434 434 435 435 # skip blocks that are complex HTML … … 467 467 text << "\n#{ code_blk }" 468 468 end 469 return text469 return text 470 470 end 471 471 472 472 end 473 473 474 474 def refs( text ) 475 475 @rules.each do |rule_name| … … 482 482 ret || [text, nil] 483 483 end 484 485 # Puts text in storage and returns is placeholder486 # e.g. shelve("some text") => <1>484 485 # Puts text in storage and returns is placeholder 486 # e.g. shelve("some text") => <1> 487 487 def shelve( val ) 488 488 @shelf << val … … 490 490 end 491 491 492 # Retrieves text from storage using its placeholder493 # e.g. retrieve("<1>") => "some text"492 # Retrieves text from storage using its placeholder 493 # e.g. retrieve("<1>") => "some text" 494 494 def retrieve( text ) 495 495 @shelf.each_with_index do |r, i| -
trunk/lib/redcloth/docbook.rb
r127 r129 193 193 text.gsub!( /(\w)(\^[0-9,]+\^)/, '\1 \2' ) 194 194 text.gsub!( /(\w)(\~[0-9,]+\~)/, '\1 \2' ) 195 196 {'w' => 'warning', 'n' => 'note', 'c' => 'comment', 'pro' => 'production', 'dt' => 'dt', 'dd' => 'dd'}.each do |char, word|197 parts = text.split(/^\s*#{char}\./)198 text.replace(parts.first + "\n" + parts[1..-1].map do |part|199 if part =~ /\.#{char}\s*$/200 "div(#{word}).\n" + part.sub(/\.#{char}\s*$/, "\ndiv(#{word}). \n")201 else202 "#{char}.#{part}"203 end+"\n"204 end.join("\n"))205 206 self.class.class_eval %!207 def docbook_#{char}(tag, atts, cite, content)195 196 {'w' => 'warning', 'n' => 'note', 'c' => 'comment', 'pro' => 'production', 'dt' => 'dt', 'dd' => 'dd'}.each do |char, word| 197 parts = text.split(/^\s*#{char}\./) 198 text.replace(parts.first + "\n" + parts[1..-1].map do |part| 199 if part =~ /\.#{char}\s*$/ 200 "div(#{word}).\n" + part.sub(/\.#{char}\s*$/, "\ndiv(#{word}). \n") 201 else 202 "#{char}.#{part}" 203 end+"\n" 204 end.join("\n")) 205 206 self.class.class_eval %! 207 def docbook_#{char}(tag, atts, cite, content) 208 208 docbook_p('p', #{word.inspect}, cite, content) 209 209 end 210 210 ! 211 end212 213 {'bq' => 'blockquote'}.each do |char, word|214 parts = text.split(/^\s*#{char}\./)215 text.replace(parts.first + "\n" + parts[1..-1].map do |part|216 if part =~ /\.#{char}\s*$/217 "div(#{word}).\n\n<para>" + part.sub(/\.#{char}\s*$/, "</para>\n\ndiv(#{word}). ")218 else219 "#{char}.#{part}"220 end221 end.join("\n"))211 end 212 213 {'bq' => 'blockquote'}.each do |char, word| 214 parts = text.split(/^\s*#{char}\./) 215 text.replace(parts.first + "\n" + parts[1..-1].map do |part| 216 if part =~ /\.#{char}\s*$/ 217 "div(#{word}).\n\n<para>" + part.sub(/\.#{char}\s*$/, "</para>\n\ndiv(#{word}). ") 218 else 219 "#{char}.#{part}" 220 end 221 end.join("\n")) 222 222 end 223 223 … … 240 240 text.gsub!( NB, "" ) 241 241 text << "</#{@div_atts}>" if @div_atts 242 text.gsub!(%r{<(#{DOCBOOK_PARAS.join("|")})([^>]*)>\s*<para>(.*?)</para>\s*</\1>}mi) { |m| t, c = $~[1..2]; "<#{t}#{c}>" << $3.gsub(/<para>/, "<#{t}#{c}>").gsub(/<\/para>/, "</#{t}>") << "</#{t}>" }243 text.gsub! %r{<para[^>]*>\s*<para([^>]*)>}i,'<para\1>' # clean multiple paragraphs in a row just in case244 text.gsub! %r{</para>\s*</para>}i,'</para>' # clean multiple paragraphs in a row just in case245 text.gsub! %r{<para[^>]*>\s*</para>\s*}i, '' # clean emtpy paras242 text.gsub!(%r{<(#{DOCBOOK_PARAS.join("|")})([^>]*)>\s*<para>(.*?)</para>\s*</\1>}mi) { |m| t, c = $~[1..2]; "<#{t}#{c}>" << $3.gsub(/<para>/, "<#{t}#{c}>").gsub(/<\/para>/, "</#{t}>") << "</#{t}>" } 243 text.gsub! %r{<para[^>]*>\s*<para([^>]*)>}i,'<para\1>' # clean multiple paragraphs in a row just in case 244 text.gsub! %r{</para>\s*</para>}i,'</para>' # clean multiple paragraphs in a row just in case 245 text.gsub! %r{<para[^>]*>\s*</para>\s*}i, '' # clean emtpy paras 246 246 text.gsub! %r{<(/?)sup>}i, '<\1superscript>' 247 247 text.gsub! %r{<(/?)sub>}i, '<\1subscript>' … … 448 448 # Parses docbook definition lists and generates HTML 449 449 def block_docbook_defs( text ) 450 text.gsub!(/^-\s+(.*?):=(.*?)=:\s*$/m) do |m|451 "- #{$1.strip} := <para>"+$2.split(/\n/).map{|w|w.strip}.delete_if{|w|w.empty?}.join("</para><para>")+"</para>"452 end453 450 text.gsub!(/^-\s+(.*?):=(.*?)=:\s*$/m) do |m| 451 "- #{$1.strip} := <para>"+$2.split(/\n/).map{|w|w.strip}.delete_if{|w|w.empty?}.join("</para><para>")+"</para>" 452 end 453 454 454 text.gsub!( DEFS_RE ) do |match| 455 455 lines = match.split( /\n/ ) … … 466 466 467 467 if line_id == lines.length - 1 468 lines[-1] << "\n</variablelist>"468 lines[-1] << "\n</variablelist>" 469 469 end 470 470 end … … 489 489 text.gsub!( /(.)\n(?! *[#*\s|]|$)/, "\\1<sbr />" ) if hard_breaks 490 490 end 491 491 492 492 def docbook_bq( tag, atts, cite, content ) 493 493 cite, cite_title = check_refs( cite ) … … 869 869 DOCBOOK_PARAS = ['para', 'remark', 'tip', 'important'] 870 870 def docbook_blocks( text, deep_code = false ) 871 @current_class ||= nil872 873 # Find all occurences of div(class). and process them as blocks874 text.gsub!( /^div\((.*?)\)\.\s*(.*?)(?=div\([^\)]+\)\.\s*)/m ) do |blk|875 block_class = (@current_class == $1) ? nil : %{ role=#{$1.inspect}}876 @current_class = $1877 BLOCK_GROUP_SPLITTER + ( ($2.strip.empty? || block_class.nil?) ? $2 : docbook_div('div', block_class, nil, "\n\n#{$2.strip}\n\n", false) )878 end879 880 # Take care of the very last div881 text.sub!( /div\((.*?)\)\.\s*(.*)/m ) do |blk|882 block_class = (@current_class == $1) ? nil : %{ role=#{$1.inspect}}883 @current_class = $1884 BLOCK_GROUP_SPLITTER + ( ($2.strip.empty? || block_class.nil?) ? $2 : docbook_div('div', block_class, nil, "\n\n#{$2.strip}\n\n", false) )885 end871 @current_class ||= nil 872 873 # Find all occurences of div(class). and process them as blocks 874 text.gsub!( /^div\((.*?)\)\.\s*(.*?)(?=div\([^\)]+\)\.\s*)/m ) do |blk| 875 block_class = (@current_class == $1) ? nil : %{ role=#{$1.inspect}} 876 @current_class = $1 877 BLOCK_GROUP_SPLITTER + ( ($2.strip.empty? || block_class.nil?) ? $2 : docbook_div('div', block_class, nil, "\n\n#{$2.strip}\n\n", false) ) 878 end 879 880 # Take care of the very last div 881 text.sub!( /div\((.*?)\)\.\s*(.*)/m ) do |blk| 882 block_class = (@current_class == $1) ? nil : %{ role=#{$1.inspect}} 883 @current_class = $1 884 BLOCK_GROUP_SPLITTER + ( ($2.strip.empty? || block_class.nil?) ? $2 : docbook_div('div', block_class, nil, "\n\n#{$2.strip}\n\n", false) ) 885 end 886 886 887 887 # Handle the text now that the placeholders for divs are set, splitting at BLOCK_GROUP_SPLITTER … … 903 903 end.join) 904 904 end 905 906 def docbook_block_groups( text, deep_code = false )907 text.replace text.split( BLOCKS_GROUP_RE ).collect { |blk| docbook_blk(blk, deep_code) }.join("\n")908 end909 910 def docbook_blk( text, deep_code = false )911 return text if text =~ /<[0-9]+>/912 913 plain = text !~ /\A[#*> ]/905 906 def docbook_block_groups( text, deep_code = false ) 907 text.replace text.split( BLOCKS_GROUP_RE ).collect { |blk| docbook_blk(blk, deep_code) }.join("\n") 908 end 909 910 def docbook_blk( text, deep_code = false ) 911 return text if text =~ /<[0-9]+>/ 912 913 plain = text !~ /\A[#*> ]/ 914 914 915 915 # skip blocks that are complex HTML -
trunk/lib/redcloth/textile.rb
r117 r129 46 46 47 47 def textile_pre_process(text) 48 {'w' => 'warning', 'n' => 'note', 'c' => 'comment', 'pro' => 'production', 'dt' => 'dt', 'dd' => 'dd'}.each do |char, word|49 parts = text.split(/^\s*#{char}\./)50 text.replace(parts.first + "\n" + parts[1..-1].map do |part|51 if part =~ /\.#{char}\s*$/52 "div(#{word})." + part.sub(/\.#{char}\s*$/, "div(#{word}). \n")53 else54 "#{char}.#{part}"55 end56 end.join("\n"))57 58 self.class.class_eval %!59 def textile_#{char}(tag, atts, cite, content)48 {'w' => 'warning', 'n' => 'note', 'c' => 'comment', 'pro' => 'production', 'dt' => 'dt', 'dd' => 'dd'}.each do |char, word| 49 parts = text.split(/^\s*#{char}\./) 50 text.replace(parts.first + "\n" + parts[1..-1].map do |part| 51 if part =~ /\.#{char}\s*$/ 52 "div(#{word})." + part.sub(/\.#{char}\s*$/, "div(#{word}). \n") 53 else 54 "#{char}.#{part}" 55 end 56 end.join("\n")) 57 58 self.class.class_eval %! 59 def textile_#{char}(tag, atts, cite, content) 60 60 textile_p('p', %{ class=#{word.inspect}}, cite, content) 61 61 end 62 62 ! 63 end64 {'bq' => 'blockquote'}.each do |char, word|65 parts = text.split(/^\s*#{char}\./)66 text.replace(parts.first + "\n" + parts[1..-1].map do |part|67 if part =~ /\.#{char}\s*$/68 "div(#{word})." + part.sub(/\.#{char}\s*$/, "div(#{word}). ")69 else70 "#{char}.#{part}"71 end72 end.join("\n"))73 end74 63 end 64 {'bq' => 'blockquote'}.each do |char, word| 65 parts = text.split(/^\s*#{char}\./) 66 text.replace(parts.first + "\n" + parts[1..-1].map do |part| 67 if part =~ /\.#{char}\s*$/ 68 "div(#{word})." + part.sub(/\.#{char}\s*$/, "div(#{word}). ") 69 else 70 "#{char}.#{part}" 71 end 72 end.join("\n")) 73 end 74 75 75 text.gsub!( BACKTICK_CODE_RE ) do |m| 76 76 before,lang,code,after = $~[1..4] … … 124 124 # Parses Textile lists and generates HTML 125 125 def block_textile_lists( text ) 126 orig_text = text.dup127 128 # Take care of _*'s and _#'s to turn them into paragraphs129 text.gsub!(/([\*#] )((.*?\n\s*_[\*#].*?)+)/) do |m|130 "#{$1}<p>"+$2.split(/_[\*#]/).map{|w|w.strip}.delete_if{|w|w.empty?}.join("</p><p>")+"</p>"131 end132 126 orig_text = text.dup 127 128 # Take care of _*'s and _#'s to turn them into paragraphs 129 text.gsub!(/([\*#] )((.*?\n\s*_[\*#].*?)+)/) do |m| 130 "#{$1}<p>"+$2.split(/_[\*#]/).map{|w|w.strip}.delete_if{|w|w.empty?}.join("</p><p>")+"</p>" 131 end 132 133 133 @last_line ||= -1 134 134 … … 155 155 lines[line_id - 1] << "</li>\n#{"\t"*(depth.size-1)}</#{ lT( depth[i] ) }l>" 156 156 depth.pop 157 tab_in = true157 tab_in = true 158 158 end 159 159 end 160 160 if depth.last && depth.last.length == tl.length 161 lines[line_id - 1] << "</li>"161 lines[line_id - 1] << "</li>" 162 162 end 163 163 end … … 200 200 201 201 if line_id == lines.length - 1 202 tabs = depth.size-1202 tabs = depth.size-1 203 203 depth.reverse.delete_if do |v| 204 lines[-1] << "</li>\n#{"\t"*tabs}</#{ lT( v ) }l>"205 tabs -= 1206 end204 lines[-1] << "</li>\n#{"\t"*tabs}</#{ lT( v ) }l>" 205 tabs -= 1 206 end 207 207 end 208 208 end … … 215 215 # Parses Textile definition lists and generates HTML 216 216 def block_textile_defs( text ) 217 text.gsub!(/^-\s+(.*?):=(.*?)=:\s*$/m) do |m|218 "- #{$1.strip} := <p>"+$2.split(/\n/).map{|w|w.strip}.delete_if{|w|w.empty?}.join("</p><p>")+"</p>"219 end220 217 text.gsub!(/^-\s+(.*?):=(.*?)=:\s*$/m) do |m| 218 "- #{$1.strip} := <p>"+$2.split(/\n/).map{|w|w.strip}.delete_if{|w|w.empty?}.join("</p><p>")+"</p>" 219 end 220 221 221 text.gsub!( DEFS_RE ) do |match| 222 222 lines = match.split( /\n/ ) … … 232 232 233 233 if line_id == lines.length - 1 234 lines[-1] << "\n</dl>"234 lines[-1] << "\n</dl>" 235 235 end 236 236 end -
trunk/run-tests.rb
r128 r129 21 21 red.to_html( :markdown ) 22 22 elsif testfile =~ /docbook/ 23 red.to_docbook23 red.to_docbook 24 24 elsif testfile =~ /textile/ 25 25 red.to_html( :textile ) … … 41 41 puts 42 42 puts "---" 43 puts "in: "; p input43 puts "in: "; p input 44 44 puts "out: "; p out 45 45 puts "expected: "; p expected