Skip to content

Feature: rst: include directive: Add the ability to skip lines with a given prefix - #26673

Open
moigagoo wants to merge 4 commits into
nim-lang:develfrom
moigagoo:feature/rst_include_skip_prefix
Open

moigagoo wants to merge 4 commits into
nim-lang:develfrom
moigagoo:feature/rst_include_skip_prefix

Conversation

@moigagoo

@moigagoo moigagoo commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

This is useful when including slices of code listings using start-after and end-before:

  • code:
#anchorStartProcFoo
proc foo(x: int): int =
  x * 2
#anchorEndProcFoo

discard foo(123)
  • include:
.. include:: foo.nim
  :code:
  :hide-lines-starting-with: #anchor
  • result:
proc foo(x: int): int =
  x * 2

discard foo(123)

… a given prefix.

This is useful when including slices of code listings using start-after
and end-before.
@Araq

Araq commented Nov 9, 2026

Copy link
Copy Markdown
Member

Is this a real RST feature that other RST processors also support?

@moigagoo

moigagoo commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

Is this a real RST feature that other RST processors also support?

Not sure about other RST processors, probably not, but this feature brings Nim's include on par with shiftinclude, i.e. we'll be able to use comments as selective inclusion markers while not polluting the code listings.

Compare these:

Both include the full file first and then explain separate sections. But the first one shows the control comments to the user and the second one doesn't.

@Araq

Araq commented Nov 11, 2026

Copy link
Copy Markdown
Member

If it is non-standard, we might as well do it like this:

.. include:: foo.nim
  :code:
  :from: #anchor :until: #anchor2

@moigagoo

Copy link
Copy Markdown
Contributor Author

If it is non-standard, we might as well do it like this:

.. include:: foo.nim
  :code:
  :from: #anchor :until: #anchor2

start-after and end-before are standard though, so it feels strange replacing them with from and until: https://docutils.sourceforge.io/docs/ref/rst/directives.html#include

Also, I would argue that start-after better communicates the intention than from because it is clear that the anchor itself is not included. With from it is not clear IMO.

The only non-standard addition is skip-prefix. This doesn't exist in the rst spec.

@moigagoo
moigagoo marked this pull request as draft November 11, 2026 09:32
@moigagoo

Copy link
Copy Markdown
Contributor Author

I am not happy with the name skip-prefix... It just doesn't do a good job explaining what it does.

Also, I think it is reasonable to provide a default value for this option, e.g. #ANCHOR. This way it is immediately compatible with mdBook's syntax: https://rust-lang.github.io/mdBook/format/mdbook.html#including-files

I've moved the PR to draft.

@Araq

Araq commented Nov 11, 2026

Copy link
Copy Markdown
Member

start-after and end-before are standard though

Yeah, so implement that one then.

@moigagoo
moigagoo marked this pull request as ready for review November 11, 2026 10:38
@moigagoo

Copy link
Copy Markdown
Contributor Author

start-after and end-before are standard though

Yeah, so implement that one then.

I implemented only skip-prefix (now renamed to hide-lines-starting-with). I did not touch pre-existing :start-after: or :end-before:.

@Araq

Araq commented Nov 11, 2026

Copy link
Copy Markdown
Member

I did not touch pre-existing :start-after: or :end-before:.

I had no idea we already had these. So simply use them, why even add more of the same here?

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants