Getting document's ToC using the Ruby library

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting document's ToC using the Ruby library

htmfilho
Hello,

Is there a way to get the ToC of a document using the Ruby library?

I'm doing this to get the title and the content:

doc = Asciidoctor.load_file 'path_to_adoc'
doc.doctitle
doc.content

... but I didn't find something like:

doc.toc

I need to decompose the document so I can show/render the different parts as required.
Reply | Threaded
Open this post in threaded view
|

Re: Getting document's ToC using the Ruby library

mojavelinux
Administrator
I'm happy to report that this is exactly the use case the Asciidoctor API was designed to handle.

If you're just interested in the outline of sections, I refer you to the method in the HTML5 converter that generates the table of contents. That should give you a taste of what is possible. (In this method, "node" is the Document instance).


You might also find the find_by method on any block node to be useful. It allows you to do a depth search for nodes that match the parameters. For instance, to find all paragraphs, you can use:

paragraphs = doc.find_by context: :paragraph


Cheers,

-Dan

On Wed, Sep 7, 2016 at 9:21 AM, htmfilho [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Hello,

Is there a way to get the ToC of a document using the Ruby library?

I'm doing this to get the title and the content:

doc = Asciidoctor.load_file 'path_to_adoc'
doc.doctitle
doc.content

... but I didn't find something like:

doc.toc

I need to decompose the document so I can show/render the different parts as required.


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Getting-document-s-ToC-using-the-Ruby-library-tp4935.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux