Login  Register

Re: How to extract the raw content of a section

Posted by mojavelinux on Mar 08, 2020; 12:49am
URL: https://discuss.asciidoctor.org/How-to-extract-the-raw-content-of-a-section-tp7732p7735.html

The API does not make the raw source of a section available. The source is only stored at the block level.

What I've done in the past to extract the source of the section is to leverage the sourcemap. By enabling the sourcemap option, you get the file and line number for each section. Then, you take that information and go back to the original source of the document and use it to cut out the source for the second.

Here's some really rough code to show you what I'm talking about:

require 'asciidoctor'

source = <<~'EOS'
= Document Title

== First Section

content

== Second Section

content
EOS

doc = Asciidoctor.load source, sourcemap: true

section_source = doc.source_lines[(doc.sections[0].lineno - 1)..(doc.sections[1].lineno - 2)].join ?\n

Best Regards,

-Dan

On Sat, Mar 7, 2020 at 5:27 PM elmicka [via Asciidoctor :: Discussion] <[hidden email]> wrote:
David,

Thank you for your answer.

I tried to use 'level2_block.text' and 'level2_block.lines',
but it seems that there is no such method for this object (undefined method error)




If you reply to this email, your message will be added to the discussion below:
https://discuss.asciidoctor.org/How-to-extract-the-raw-content-of-a-section-tp7732p7734.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