asciidoctor / asciidoctor

:gem: A fast, open source text processor and publishing toolchain, written in Ruby, for converting AsciiDoc content to HTML 5, DocBook 5, and other formats.
https://asciidoctor.org
Other
4.79k stars 788 forks source link

Set section number manually something like this --- :section: 7 #1113

Open jxxcarlson opened 9 years ago

jxxcarlson commented 9 years ago

In Noteshare a "notebook" is consists of many separate chunks --- normally sections. When I compile these into a master document, I can turn numbering on and all is fine. However, I would like to turn numbering on within chunks and have the numbering be correct. Since chunks are processed separately and independently, I need to carry the "current section number" in a register and then set the section number before rendering with asciidoctor.

Is there a way to do this? Take a look at http://math.noteshare.io/notebook/37/?note=441 and imagine section numbers starting at 1 for each section. That is what happens if I turn numbering on given my current knowledge of how things work.

I t would be desirable to do this with subsections, sub subsections, etc., but this is less important.

jxxcarlson commented 9 years ago

Here is an example of what I want to avoid:

http://epsilon.my.noteshare.io/notebook/195/?note=782

Notice that "Elliptic Curves" appears as section1 in the main text but as section 2 in the index.

mojavelinux commented 9 years ago

What we need to do here is wire the section numbering logic into the counter system inside of Asciidoctor. That will enable you to control the "seed" attribute value that is used for getting the next number. It would be something like:

:section-number: 7

== Section 7

This parallels how figures and other blocks are numbered.

What might help here is if we have a Cucumber test case. For examples, see https://github.com/asciidoctor/asciidoctor/blob/master/features/xref.feature

jxxcarlson commented 9 years ago

That would be great! I will look into making a cucumber test case ... have never used it before.

On Sep 24, 2014, at 5:34 PM, Dan Allen notifications@github.com wrote:

What we need to do here is wire the section numbering logic into the counter system inside of Asciidoctor. That will enable you to control the "seed" attribute value that is used for getting the next number. It would be something like:

:section-number: 7

== Section 7 This parallels how figures and other blocks are numbered.

What might help here is if we have a Cucumber test case. For examples, see https://github.com/asciidoctor/asciidoctor/blob/master/features/xref.feature

— Reply to this email directly or view it on GitHub.

mojavelinux commented 9 years ago

Don't worry about making the test run. A rough sketch should do. Thanks!

jxxcarlson commented 9 years ago

Here is text that needs to be cut into piece and store in individual files to run the indicated test.

Test file for manual numbering of sections

Cut the file below into pieces
as indicated with the indicated
names.  Then run

  $ asciidoctor compendium.adoc

---------------------------------
////
File 1: compendium.adoc
////

= Compendium of Strange and Fearsome Animals
Bornum Dactor, Ph.D
bodac@foo.org
:numbered:
:toc2:

include::intro.adoc[]

:section-number: 5
include::aaa.adoc[]
include::aar.adoc[]

:section-number: 23
include::bbb.adoc[]

---------------------------------
////
File 2: intro.adoc
////

== Introduction

This is an extract, free of copyright, from
the full text of _A Compendium of Strange and Fearsome Animals_.
We present the sections in the their original numberings
so as to facilite references, cross-references, and 
other permitted bibliographic activities.

Please note that the numbering in the table of
contents is consistent with that in the body
of the text, even when some sectoin numbers
are set manually and others are not).

---------------------------------
////
File 3: aaa.adoc
////

== Aaieou

(This is section 5)

This is the story of the Aaieou, 
a small animal named for its
call of distress

=== Appearance

The Aaieou is one centimeter in length,
light pink, and furry.

=== Diet

Anything sweet, plus old shoes.

---------------------------------
////
File 4: aar.adoc
////

== Aardvark

(This is section 6)

This is the story of the Aardvark, 
an animal notable for its position 
in the alphabet.

=== Appearance

The Aardvark has long nose.

=== Diet

Insects?

---------------------------------
////
File 5: bbb.adoc
////

== Boreal Bandersnatch

(This is section 23)

Feared by all who know it, the boreal
bandersnatch has no enemies.

=== Appearance

So terrifying is its appearance that
one unprotected glance at this beast
is sufficient to induce a heart attack.

=== Diet

Anything that moves.
jxxcarlson commented 9 years ago

I put something in the comments to the issue that may be helpful.

Am thinking of making an asiicodoctor to LaTeX translator. This is because most mathematics publishers accept only LaeX. The resulting output my not exercise every LaTex feature, but it would be genuine LaTeX. In addition, this would give people peace mind. WDYT?

The LaTeX in Asciidoc will just get passed on, so crux of the matter is to map Asciidoc to LateX.

On Sep 24, 2014, at 8:22 PM, Dan Allen notifications@github.com wrote:

Don't worry about making the test run. A rough sketch should do. Thanks! — Reply to this email directly or view it on GitHub.

mojavelinux commented 9 years ago

I put something in the comments to the issue that may be helpful.

Perfect. Very helpful indeed.

Am thinking of making an asiicodoctor to LaTeX translator. ... WDYT?

I think it would be fantastic! In fact, there's an open request to implement this backend. See #681.

I recommend looking at the man page backend to get an idea of how you might approach it. Since LaTeX is not an SGML-based language, I think your best bet is to use either ERB or Ruby directly.

Here's an example of a simple Ruby-based converter for reference:

require 'asciidoctor'

class DemoConverter
  include Asciidoctor::Converter
  register_for 'demo'

  def convert node, transform = nil
    transform ||= node.node_name
    if respond_to? transform
      send transform, node
    else
      warn %(Write me! #{transform})
    end
  end

  def document node
    node.content
  end

  def section node
    [node.title, node.content] * "\n\n"
  end

  def paragraph node
    node.content.tr("\n", ' ') << "\n"
  end
end

Then call:

$ asciidoctor -r ./demo-converter.rb -b demo sample.adoc

There are a lot of edge cases around substitutions, but you should be able to get the high-level stuff pretty fast.

You can also refer to the LaTeX backend in AsciiDoc Python for reference: https://github.com/asciidoc/asciidoc/blob/master/latex.conf

jxxcarlson commented 9 years ago

I will study this .. thanks!

On Sep 25, 2014, at 7:47 PM, Dan Allen notifications@github.com wrote:

I put something in the comments to the issue that may be helpful.

Perfect. Very helpful indeed.

Am thinking of making an asiicodoctor to LaTeX translator. ... WDYT?

I think it would be fantastic! In fact, there's an open request to implement this backend. See #681.

I recommend looking at the man page backend to get an idea of how you might approach it. Since LaTeX is not an SGML-based language, I think your best bet is to use either ERB or Ruby directly.

Here's an example of a simple Ruby-based converter for reference:

require 'asciidoctor'

class DemoConverter include Asciidoctor::Converter register_for 'demo'

def convert node, transform = nil transform ||= node.node_name if respond_to? transform send transform, node else warn %(Write me! #{transform}) end end

def document node node.content end

def section node [node.title, node.content] * "\n\n" end

def paragraph node node.content.tr("\n", ' ') << "\n" end end Then call:

$ asciidoctor -r ./demo-converter.rb -b demo sample.adoc There are a lot of edge cases around substitutions, but you should be able to get the high-level stuff pretty fast.

You can also refer to the LaTeX backend in AsciiDoc Python for reference: https://github.com/asciidoc/asciidoc/blob/master/latex.conf

— Reply to this email directly or view it on GitHub.

mojavelinux commented 9 years ago

I created an extension that implements this feature. I decided to use the attribute sectnumoffset to control the offset of section numbering.

See https://github.com/asciidoctor/asciidoctor-extensions-lab/blob/master/lib/sectnumoffset-treeprocessor.rb

jxxcarlson commented 9 years ago

Wow!! Can't wait to try it out.

Sent from my iPhone

On Jul 3, 2015, at 11:40 PM, Dan Allen notifications@github.com wrote:

I created an extension that implements this feature. I decided to use the attribute sectnumoffset to control the offset of section numbering.

See https://github.com/asciidoctor/asciidoctor-extensions-lab/blob/master/lib/sectnumoffset-treeprocessor.rb

— Reply to this email directly or view it on GitHub.

rockyallen commented 8 years ago

I need something similar, but for the docbook backend: ie

:section-number: 2A
= Scrumping
TBD

or

[label="2A"]
= Scrumping
TBD

should produce

<section label="2A">
<title>Scrumping</title>

It isn't quite the same as your number-offset, because in docbook it is an override for that section only (ie it does not affect subsequent labels), but it covers this use case (and mine).

What is the easiest way to implement it in the short term, preferably with asciidoctor-ant?

rockyallen commented 8 years ago

Quick hack for docbook solved:

[role="A2"]
= Scrumping

Then apply this to the docbook

?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:d="http://docbook.org/ns/docbook">
  <xsl:template match="node() | @*">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="d:section/@role|d:chapter/@role">
      <xsl:attribute name="label"><xsl:value-of select="."/></xsl:attribute>
    <xsl:apply-templates/>
  </xsl:template>
</xsl:stylesheet>
rockyallen commented 8 years ago

Actually, I think @jxxcarlson 's requirement and mine are slightly different; I need arbitrary labels for which there is no sensible way to increment-my sequence for example is (2A,2B,3,4C,5). Would it be sensible to divorce them and raise a separate issue just to implement [label="x"] on headings?

tebeka commented 4 years ago

:+1: My use case is giving some free excerpt from a book I'm writing and would like to have the section number in the exceprt match the one in the book.

beatchristen commented 4 years ago

To add to the use case provided by @tebeka I want to present the currently 5 completed parts of a project management plan to the steering commitee and would like to keep the chapter numbering consistent with the final version (It's a standardized toc structure).

// 3. Project organisation
include::pmp/3_organisation.adoc[]

// -> 6. Quality Assurance
include::pmp/6_qualityassurance.adoc[]

// -> 13. Tools
include::pmp/13_tools.adoc[]

// -> 15. Risks
include::pmp/15_risks.adoc[]
beatchristen commented 4 years ago

Testing out the lib/sectnumoffset-treeprocessor.rb, I believe this is not possible yet.

:sectnumoffset: 3  
include::pmp/3_organisation.adoc[]
// Expected: 3. Project organisation
// Produced: 3. ....

:sectnumoffset: 6
include::pmp/6_qualityassurance.adoc[]
// Expected: 6. Quality assurance
// Produced: 4. Quality assurance

@mojavelinux I can add this issue to github/asciidoctor-extensions-lab and give it a try to fix it in the labs. Do you see a reason why the above would not be easy to fix (e.g. attributes don't work that way or something ;-)