Skip to main content

A literate programming extension for Sphinx

Project description

Literate Sphinx

Literate Sphinx is a literate programming extension for Sphinx. Literate programming is a method for writing code interleaved with text. With literate programming, code is intended to be written in an order that makes sense to a human reader, rather than a computer.

Producing the human-readable document from the document source is called "weaving", while producing the computer-readable code is called "tangling". In this extension, the weaving process is the normal Sphinx rendering process. For tangling, this extension provides a tangle builder — running make tangle will output the computer-readable files in _build/tangle.

As is customary with literate programming tools, the extension is also written in a literate programming style.

Usage

Install the extension by running pip install literate-sphinx, and add 'literate_sphinx' to the extensions list in your conf.py.

Code chunks are written using the literate-code directive, which takes the name of the chunk as its argument. It takes the following options:

  • lang: the language of the chunk. Defaults to highlight_language specified in conf.py
  • file: (takes no value) present if the chunk is a file. If the chunk is a file, then the code chunk name
  • class: a list of class names separated by spaces to add to the HTML output
  • name: a target name that can be referenced by ref or numrf. This should not be confused with the code chunk name.

e.g in ReST

.. literate-code:: code chunk name
   :lang: python

   def hello():
       print("Hello world")

or in Markdown using MyST parser

```{literate-code} code chunk name
:lang: python

def hello():
    print("Hello world")
```

To include another code chunk, enclose it between {{ and }} delimiters. Only one code chunk is allowed per line. The code chunk will be prefixed with everything before the delimiters on the line, and suffixed by everything after the delimiters.

For example,

.. literate-code:: file.py
   :file:
   # before
   {{code chunk name}}
   # after

will produce a file called file.py with the contents

# before
def hello():
    print("Hello world")
# after

and

.. literate-code:: file.py
   :file:
   # before
   class Hello:
       {{code chunk name}} # suffix
   # after

will produce

# before
class Hello:
    def hello(): # suffix
        print("Hello world") # suffix
# after

The delimiters can be changed by setting the literate_delimiters option in conf.py, which takes a tuple, where the first element is the left delimiter and the second element is the right delimiter. For example:

literate_delimiters = ('<<', '>>')

The same code chunk name can be used for multiple chunks; they will be included in the same order that they appear in the document. If the document is split across multiple files, they will be processed in the same order as they appear in the table of contents as defined in the toctree directive.

---
maxdepth: 1
caption: "More:"
---
code

License

This software may be redistributed under the same license as Sphinx.

:lang: text

Copyright Hubert Chathi <hubert@uhoreg.ca>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

SPDX-License-Identifier: BSD-2-Clause

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

literate-sphinx-0.1.1.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

literate_sphinx-0.1.1-py2.py3-none-any.whl (6.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file literate-sphinx-0.1.1.tar.gz.

File metadata

  • Download URL: literate-sphinx-0.1.1.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.2

File hashes

Hashes for literate-sphinx-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7fbe51d7c001d3494e37f9b01d74da41727b54a5975105049895bb002b000fe8
MD5 0b57fdd090e8261902743cc0710e56df
BLAKE2b-256 f0d18b2fb4c1ff8768abf4a8dd8c3f3f16bfb6b9518728e14c46b5dc9e3ed9a3

See more details on using hashes here.

Provenance

File details

Details for the file literate_sphinx-0.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for literate_sphinx-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3b8fe30f490a570b7fd03c6c4db37f0290b7c39c2c6d2d757f72a61b731c1bd9
MD5 34f551c7badf73c294ff76dc042f008b
BLAKE2b-256 c6707480ff15cdeb02a4d13cacd58e205a8014dc6f1a2ba8191ce746bc11334f

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page