[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

*cloth and ToC

Guillaume Marcais

3/17/2005 3:45:00 PM

Is there a way to extract a table of content of a text with redcloth of
bluecloth?

What I have in mind is the ability to extract all the headers of a text
(h1., h2., etc. in textile) and generate: anchors within the html page
on these headers and a table of content with links to the headers.

So at the end of a wiki page I could add something like:

<div class="ToC">
ToC. <-- Replaced by *cloth bye the table of content -->
</div>

and get a beautiful and well linked table of content for free.

Is there already such a feature? Is it a reasonable feature request?

Guillaume.



5 Answers

Aria Stewart

3/17/2005 4:24:00 PM

0

On Fri, 2005-03-18 at 00:44 +0900, Guillaume Marcais wrote:
> Is there a way to extract a table of content of a text with redcloth of
> bluecloth?
>
> What I have in mind is the ability to extract all the headers of a text
> (h1., h2., etc. in textile) and generate: anchors within the html page
> on these headers and a table of content with links to the headers.
>
> So at the end of a wiki page I could add something like:
>
> <div class="ToC">
> ToC. <-- Replaced by *cloth bye the table of content -->
> </div>
>
> and get a beautiful and well linked table of content for free.
>
> Is there already such a feature? Is it a reasonable feature request?

Sounds like it would be better implemented as an output filter -- a
method that could take formatted HTML text as input, and add the table
of contents to the top, or return an array of the table of contents and
the HTML.

I'd be willing to take a stab at that if you'd like. It would be useful
to me, too, and useful to me to be independent of the markup engine.



Thomas Counsell

3/17/2005 4:30:00 PM

0

Hello

On 17 Mar 2005, at 15:44, Guillaume Marcais wrote:
> Is there a way to extract a table of content of a text with redcloth of
> bluecloth?
>
> What I have in mind is the ability to extract all the headers of a text
> (h1., h2., etc. in textile) and generate: anchors within the html page
> on these headers and a table of content with links to the headers.
>
> So at the end of a wiki page I could add something like:
>
> <div class="ToC">
> ToC. <-- Replaced by *cloth bye the table of content -->
> </div>
>
> and get a beautiful and well linked table of content for free.
>
> Is there already such a feature?

Not in Redcloth at the moment AFAIK.

> Is it a reasonable feature request?

It wouldn't be hard to write. Maybe an hours work? I think it could
be more or less added to the Redcloth class without changing any
existing code.

I would go about it by adding two extra methods:
# refs_toc(text) that would text.gsub! for h1,h2 etc, and put the
anchor next to each
# block_toc(text) that would text.gsub! for ToC and replace it with the
table html.

It could then be activated by passing :refs_toc, :block_toc,
:textile_rules to the to_html method or changing the DEFAULT_RULES
constant.

It is one of the things I've been considering writing for my Soks wiki,
so if you can wait I'll get to it, or someone else could step into the
breach?

Tom



Barry Dmytro

3/17/2005 6:01:00 PM

0


I did this exact thing in a wiki I'm writting. Here is the code for what I
did:

$toc = ""
headreg = /^\s*h([1-6])\.\s+(.*)/
$document.gsub!(headreg) do |m|
number = $1
name = $2
header = name.gsub(/\s/,"+")
$toc << '#'*num.to_i + ' "' + name + '":#' + header + "\n"
'h' + num + '. ==<a href="#TopOfPage">^</a><a name="' + header + '">'
+ name + '</a>=='
end

This will go through a textile document ($document) and add a link to the
top of the page every time it finds a header and build a table of contents
as it goes along. The table of contents it builds is a textile document
itself so you'd have to run it through redcloth as well, but it will link
to each header found throughout the page. As far as nameing the header it
takes the contents of the header and strips out the spaces so that it is a
valid url.

Guillaume Marcais wrote:

> Is there a way to extract a table of content of a text with redcloth of
> bluecloth?
>
> What I have in mind is the ability to extract all the headers of a text
> (h1., h2., etc. in textile) and generate: anchors within the html page
> on these headers and a table of content with links to the headers.
>
> So at the end of a wiki page I could add something like:
>
> <div class="ToC">
> ToC. <-- Replaced by *cloth bye the table of content -->
> </div>
>
> and get a beautiful and well linked table of content for free.
>
> Is there already such a feature? Is it a reasonable feature request?
>
> Guillaume.

--
Barry Dmytro
badcherry@mailc.net
http://badc...

Guillaume Marcais

3/17/2005 11:40:00 PM

0

> [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:

<div class="rightHandSide">
Table of Content

toc2.

</div>

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.

Guillaume Marcais

3/18/2005 4:04:00 AM

0

Sorry to respond to myself. Same thing, minus the bugs.

Guillaume.