Preserving original line numbers in source code

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

Preserving original line numbers in source code

Alex
Hi,

I'm using the Gradle plug in to convert my Asciidoc documents. So my (currently only) adoc file resides under:
{project-dir}/src/docs/asciidoc/sample.adoc

I've also created a directory for code samples where i currently have this file:
{project-dir}/src/docs/asciidoc/code/code-sample.html

Then in my asciidoc file (sample.adoc from above) i have the following Asciidoc markup:

== External code

[source,html,linenums]
----
include::code/code-sample.html[lines=2]
----

All works well and the second line (and only the second line) from my code example is being pulled into the result document. I was wondering if there was a way to preserve the original line numbers from the source code listing (either by declaring an offset similar to ordered lists as Mr. Haki had described here:
http://mrhaki.blogspot.de/2015/02/awesome-asciidoctor-change-start-number.html
or by providing some kind of macro option that tells Asciidoctor to use the original line numbers from the actual source file.

I'm using Coderay. The top of my document looks like this:

= Sample document with gradle
Alexander Klein
2015-03-03
:icons: font
:numbered:
:doctype: book

The Gradle configuration is this:

asciidoctor {
  backends = ['html5']
  attributes toc: 'left',
  'source-highlighter': 'coderay'
}

I googled a lot and couldn't find how to do this.
Is there already a way to achieve this? Or is it planned for a future release? I think that this would be very useful to document / annotate existing code. I recently had to demonstrate an issue with their code to an external company and quickly created a short Asciidoc document. It worked well but it would have been even better if i could have referrered to their original line numbers.

Many thanks in advance.

Best, Alex
Reply | Threaded
Open this post in threaded view
|

Re: Preserving original line numbers in source code

mojavelinux
Administrator
Alex,

On Tue, Mar 3, 2015 at 6:08 AM, Alex [via Asciidoctor :: Discussion] <[hidden email]> wrote:
I was wondering if there was a way to preserve the original line numbers from the source code listing.
Is there already a way to achieve this? Or is it planned for a future release? I think that this would be very useful to document / annote existing code.

I think this is a great idea!

We could support it in one of two ways (maybe both). We could allow for an explicit attribute (perhaps just `start` or `linenum_start`) that would set the numbering offset, similar to the `start` attribute for numbered lists. We'd simply pass that value to the syntax highlighter so that it set the line numbers accordingly. For instance, CodeRay has a `line_number_start` option.

We could also auto-detect the line offset from the include file and pass that to the source highlighter. That would be a bit tougher since includes are processed before parsing occurs, but I'm sure we could find a way.


Thanks!