From: Guillaume Marcais Date: 2005-03-18T13:04:16+09:00 Subject: Re: *cloth and ToC --=-TwU1Cv15+RbsltOkxvVu Content-Type: text/plain Content-Transfer-Encoding: 7bit Sorry to respond to myself. Same thing, minus the bugs. Guillaume. --=-TwU1Cv15+RbsltOkxvVu Content-Disposition: attachment; filename=redcloth_toc.rb Content-Type: text/plain; name=redcloth_toc.rb; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit require 'redcloth' class RedCloth DEFAULT_RULES = [:refs_textile_header, :block_toc, :textile, :markdown] HEADER_RE = /(^\s*)(h(\d*))([^.]*)\.(?::(\S+))? (.*)$/ def refs_textile_header(text) text.gsub!(HEADER_RE) do |m| s,tag,num,atts,cite,content = $~[1..6] header = content.gsub(/\s/, '+') (@toc ||= []) << [num.to_i, header, content] "#{ s }#{ tag }#{ atts }.#{ cite } #{ content }" end end TOC_RE = /^toc(\d)?\.$/m def block_toc(text) if text =~ TOC_RE unless @toc text.gsub!( $&, "") else max_depth = $1 ? $1.to_i : 6 counter = Array.new(max_depth, 0) toc_html = @toc.collect do |te| i = te[0] - 1 p counter, i if counter[i] num = "" counter[i] += 1 counter[i+1..-1] = Array.new(max_depth - i - 1, 0) num += counter[0..i].join(".") "\t#{ num } #{ te[2] }
" end end.join("\n") text.gsub!($&, toc_html) end end text.gsub!(TOC_RE, "Table of Content") end end --=-TwU1Cv15+RbsltOkxvVu--