Changeset 129

Show
Ignore:
Timestamp:
01/16/2007 15:23:36 (18 months ago)
Author:
jgarber
Message:

Convert tabs to spaces

Location:
trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/redcloth/base.rb

    r128 r129  
    1010                      :block_markdown_bq, :block_markdown_lists,  
    1111                      :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, 
    1414                     :inline_docbook_code, :inline_docbook_glyphs, :inline_docbook_span, 
    1515                     :inline_docbook_wiki_words, :inline_docbook_wiki_links, :inline_docbook_autolink_urls, 
     
    9898        text = self.dup 
    9999         
    100         return "" if text == "" 
     100        return "" if text == "" 
    101101 
    102102        @urlrefs = {} 
     
    401401    BLOCK_GROUP_SPLITTER = "XXX_BLOCK_GROUP_XXX\n\n" 
    402402    def blocks( text, deep_code = false ) 
    403                         @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 
     403      @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 
    418418       
    419419      # Handle the text now that the placeholders for divs are set, splitting at BLOCK_GROUP_SPLITTER 
     
    422422      end.join) 
    423423    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                 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[#*> ]/ 
     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[#*> ]/ 
    434434 
    435435      # skip blocks that are complex HTML 
     
    467467              text << "\n#{ code_blk }" 
    468468          end 
    469                                         return text 
     469          return text 
    470470      end 
    471471       
    472472    end 
    473                  
     473     
    474474    def refs( text ) 
    475475        @rules.each do |rule_name| 
     
    482482        ret || [text, nil] 
    483483    end 
    484                  
    485                 # Puts text in storage and returns is placeholder 
    486                 #  e.g. shelve("some text") => <1> 
     484     
     485    # Puts text in storage and returns is placeholder 
     486    #  e.g. shelve("some text") => <1> 
    487487    def shelve( val )  
    488488        @shelf << val 
     
    490490    end 
    491491     
    492                 # Retrieves text from storage using its placeholder 
    493                 #  e.g. retrieve("<1>") => "some text" 
     492    # Retrieves text from storage using its placeholder 
     493    #  e.g. retrieve("<1>") => "some text" 
    494494    def retrieve( text )  
    495495        @shelf.each_with_index do |r, i| 
  • trunk/lib/redcloth/docbook.rb

    r127 r129  
    193193      text.gsub!( /(\w)(\^[0-9,]+\^)/, '\1 \2' ) 
    194194      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                             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) 
     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) 
    208208            docbook_p('p', #{word.inspect}, cite, content) 
    209209          end 
    210210        ! 
    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")) 
     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")) 
    222222      end 
    223223 
     
    240240      text.gsub!( NB, "" ) 
    241241      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 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 
     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 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 
    246246      text.gsub! %r{<(/?)sup>}i,            '<\1superscript>' 
    247247      text.gsub! %r{<(/?)sub>}i,            '<\1subscript>' 
     
    448448    # Parses docbook definition lists and generates HTML 
    449449    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                         end 
    453                                  
     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         
    454454        text.gsub!( DEFS_RE ) do |match| 
    455455            lines = match.split( /\n/ ) 
     
    466466 
    467467                if line_id == lines.length - 1 
    468                                                                                 lines[-1] << "\n</variablelist>" 
     468                    lines[-1] << "\n</variablelist>" 
    469469                end 
    470470            end 
     
    489489        text.gsub!( /(.)\n(?! *[#*\s|]|$)/, "\\1<sbr />" ) if hard_breaks 
    490490    end 
    491                  
     491     
    492492    def docbook_bq( tag, atts, cite, content ) 
    493493        cite, cite_title = check_refs( cite ) 
     
    869869    DOCBOOK_PARAS = ['para', 'remark', 'tip', 'important'] 
    870870    def docbook_blocks( text, deep_code = false ) 
    871                         @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 
     871      @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 
    886886       
    887887      # Handle the text now that the placeholders for divs are set, splitting at BLOCK_GROUP_SPLITTER 
     
    903903      end.join) 
    904904    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                 end 
    909  
    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[#*> ]/ 
    914914 
    915915      # skip blocks that are complex HTML 
  • trunk/lib/redcloth/textile.rb

    r117 r129  
    4646     
    4747    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                             else 
    54                               "#{char}.#{part}" 
    55                             end 
    56                                 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) 
    6060            textile_p('p', %{ class=#{word.inspect}}, cite, content) 
    6161          end 
    6262        ! 
    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                          
     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       
    7575      text.gsub!( BACKTICK_CODE_RE ) do |m| 
    7676          before,lang,code,after = $~[1..4] 
     
    124124    # Parses Textile lists and generates HTML 
    125125    def block_textile_lists( text ) 
    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                                  
     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         
    133133        @last_line ||= -1 
    134134         
     
    155155                                lines[line_id - 1] << "</li>\n#{"\t"*(depth.size-1)}</#{ lT( depth[i] ) }l>" 
    156156                                depth.pop 
    157                                                                                                                                 tab_in = true 
     157                                tab_in = true 
    158158                            end 
    159159                        end 
    160160                        if depth.last && depth.last.length == tl.length 
    161                                                                                                                 lines[line_id - 1] << "</li>" 
     161                            lines[line_id - 1] << "</li>" 
    162162                        end 
    163163                    end 
     
    200200                 
    201201                if line_id == lines.length - 1 
    202                                                                                 tabs = depth.size-1 
     202                    tabs = depth.size-1 
    203203                    depth.reverse.delete_if do |v| 
    204                                                                                                 lines[-1] << "</li>\n#{"\t"*tabs}</#{ lT( v ) }l>" 
    205                                                                                                 tabs -= 1 
    206                                                                                 end 
     204                        lines[-1] << "</li>\n#{"\t"*tabs}</#{ lT( v ) }l>" 
     205                        tabs -= 1 
     206                    end 
    207207                end 
    208208            end 
     
    215215    # Parses Textile definition lists and generates HTML 
    216216    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                         end 
    220                                  
     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         
    221221        text.gsub!( DEFS_RE ) do |match| 
    222222            lines = match.split( /\n/ ) 
     
    232232 
    233233                if line_id == lines.length - 1 
    234                                                                                 lines[-1] << "\n</dl>" 
     234                    lines[-1] << "\n</dl>" 
    235235                end 
    236236            end 
  • trunk/run-tests.rb

    r128 r129  
    2121                       red.to_html( :markdown ) 
    2222                   elsif testfile =~ /docbook/ 
    23                                                                                         red.to_docbook 
     23                      red.to_docbook 
    2424                   elsif testfile =~ /textile/ 
    2525                       red.to_html( :textile ) 
     
    4141      puts 
    4242      puts "---" 
    43                         puts "in: "; p input 
     43      puts "in: "; p input 
    4444      puts "out: "; p out 
    4545      puts "expected: "; p expected