From: Guillaume Marcais Date: 2005-03-18T08:39:51+09:00 Subject: Re: *cloth and ToC --=-OgnaKGM2BXRXTpxgUKQg Content-Type: text/plain Content-Transfer-Encoding: 7bit > [many insightful answers] Well, with all the answers I got I finally wrote my own version. Right now it support redcloth and textile (not markdown). The file is attached. To use it, with instiki for example, save it as redcloth_toc someplace where it can be found by ruby (or add a -I option), and simply do: $ ruby -r redcloth_toc /usr/bin/instiki --storage=/home/gus/tmp/storage In my wiki pages I add:
Table of Content toc2.
To get a beautiful ToC. The 2 in toc2. is optional. It is the maximum depth of the table. Hope this helps other people too. Guillaume. --=-OgnaKGM2BXRXTpxgUKQg 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 if counter[i] num = "" counter[i] += 1 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 --=-OgnaKGM2BXRXTpxgUKQg--