Search code examples
python-sphinxcode-documentation

.. literalinclude:: from markers


I am using for documenting a C code. I am not interested in features offered by , however I sometime would like to include some snippets from the code-base.

The .. literalinclude:: looks great, but the :lines: option is a bit weak especially if the code is edited afterwards.

I am thinking about a new option such as :marker-start: <<<HERE and :marker-end: <<<END.

What would be the easiest way to achieve this behavior?


Solution

  • You can use the start-after and end-before options. Below is an example.

    example.txt:

    # START
    first line
    second line
    # END
    third line
    

    Markup:

    .. literalinclude:: example.txt
       :start-after: # START
       :end-before: # END
    

    This will appear in the output:

    first line
    second line
    

    See https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-literalinclude.