write syntax highlighting block content to file

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

write syntax highlighting block content to file

ttmetro
I'd like to output the code (raw, not formatted) to a file. E.g.

```python
def abc():
    pass
```

should get rendered in the output as usual with syntax highlighting, but the code also should be passed to an extension so it can be written to a file.

Alternatively, I know how to create an extension that gets the code, but then I do not know how to also have it appear formatted in the asciidoctor output.

Thanks,
Bernhard
Reply | Threaded
Open this post in threaded view
|

Re: write syntax highlighting block content to file

mojavelinux
Administrator
Bernhard,

It seems like a treeprocessor is what you want. With a treeprocessor, you can query for all the source blocks like so:

doc.find_by context: :listing, style: 'source'

Then you can either get the raw source of the block using .source or the highlighted source using .content. Invoking these methods will not prevent the processor from outputting the blocks as normal.

Here's an example that shows how to use a treeprocessor to find blocks and process them. https://github.com/asciidoctor/asciidoctor-extensions-lab/blob/master/lib/shell-session-treeprocessor/extension.rb

Cheers,

-Dan

On Tue, Nov 6, 2018 at 2:12 PM ttmetro [via Asciidoctor :: Discussion] <[hidden email]> wrote:
I'd like to output the code (raw, not formatted) to a file. E.g.

```python
def abc():
    pass
```

should get rendered in the output as usual with syntax highlighting, but the code also should be passed to an extension so it can be written to a file.

Alternatively, I know how to create an extension that gets the code, but then I do not know how to also have it appear formatted in the asciidoctor output.

Thanks,
Bernhard


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/write-syntax-highlighting-block-content-to-file-tp6582.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