Remove special sequence at line start before processing

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

Remove special sequence at line start before processing

cyberrob
I want to use Asciidoctor for documenting code source modules. But not by using a separate .adoc file and including parts of the source file but by using the source file itself to be processed by Asciidoctor.

All Asciidoc formatted content will be started by a line comment (of the programming language) which is followed by a special character, for example an exclamation mark like used by Doxygen. The code itself will be shown as a source code block in the processed output file.

Very small example in Python:

#! = Hello World module
#! John Doe
#! v1.0, 2017-01-01
#! 
#! This program just prints 'Hello World' and the current date and time.
#! 
#! == Import needed libraries
#!
#! [source,python]
#! ----

import datetime

#! ----
#! 
#! == Main program
#! 
#! [source,python]
#! ----

print("Hello World.")      # <1>
print "Current date and time:"
print str(datetime.datetime.now())      # <2>

#! ----
#! <1> Use 'print' function to output text
#! <2> This converts current date and time to a string and prints it
#!

All this would need is Asciidoctor to remove the sequence "#! " from the start of each line before processing the file. This would also be useful as an option for include::[]. The sequence should be configurable to support different language comments.

Is this already possible using an exisiting Asciidoctor option?
If not I would suggest it as a very useful feature.

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

Re: Remove special sequence at line start before processing

mojavelinux
Administrator
You are describing exactly the purpose of a Preprocessor. A preprocessor can visit all the lines before the parser has a chance to see them. It can either make changes to those lines or present a different set of lines.


Since you are including these files, you can accomplish this using an IncludeProcessor. An include processor is responsible for reading the lines from the included file and returning them to the parser. That gives you an opportunity to manipulate those lines.

You can find many examples of extensions in the extensions lab. See https://github.com/asciidoctor/asciidoctor-extensions-lab

Cheers,

-Dan

On Fri, Jan 20, 2017 at 2:34 PM, cyberrob [via Asciidoctor :: Discussion] <[hidden email]> wrote:
I want to use Asciidoctor for documenting code source modules. But not by using a separate .adoc file and including parts of the source file but by using the source file itself to be processed by Asciidoctor.

All Asciidoc formatted content will be started by a line comment (of the programming language) which is followed by a special character, for example an exclamation mark like used by Doxygen. The code itself will be shown as a source code block in the processed output file.

Very small example in Python:

#! = Hello World module
#! John Doe
#! v1.0, 2017-01-01
#! 
#! This program just prints 'Hello World' and the current date and time.
#! 
#! == Import needed libraries
#!
#! [source,python]
#! ----

import datetime

#! ----
#! 
#! == Main program
#! 
#! [source,python]
#! ----

print("Hello World.")      # <1>
print "Current date and time:"
print str(datetime.datetime.now())      # <2>

#! ----
#! <1> Use 'print' function to output text
#! <2> This converts current date and time to a string and prints it
#!

All this would need is Asciidoctor to remove the sequence "#! " from the start of each line before processing the file. This would also be useful as an option for include::[]. The sequence should be configurable to support different language comments.

Is this already possible using an exisiting Asciidoctor option?
If not I would suggest it as a very useful feature.

Thanks,
Robert



If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Remove-special-sequence-at-line-start-before-processing-tp5214.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