Sphinx extension to arbitrarily convert text in pre-processing of document generation

Premise

Recently, I am writing a document with Sphinx.

So I solved it once, but when using PlantUML, it became stressful that I could not preview on VS Code.

Thing you want to do

When making html

```plantuml

Enclose

.. uml::

I want to automatically convert to.

#policy

Official documentationThere is an event implementation method inside.

#Extension plugin implementation

I wrote it like this.

uml_change.py


def uml_change(app, docname, source):  
    umlFlg = False
    lines = []
    for line in source[0].split("\n"):
        if umlFlg:
            if line.strip() == "```":
                #Delete when the end of the box comes
                line = ""
                umlFlg = False
            else:
                #Indent inside the box
                line = "    " + line

        ###Convert markdown notation to reST notation
        if line.strip() == "```plantuml":
            line = ".. uml::"
            umlFlg = True
           
        lines.append(line)

    source[0] = "\n".join(lines)
  
def setup(app):  
    app.connect("source-read", uml_change)  

conf.py settings

conf.Add the following description to py.

conf.py


import sys
import os
 sys.path.append (os.path.abspath ("./ bin")) # Folder name is an example

Store the above script in the specified folder (under bin in the above example).

Add the created plugin name to extensions.

conf.py


extensions = ['uml_change']

Try make html and if the uml image is displayed, it succeeds.

#Supplement

You should be able to write various preprocessing by applying this method. Convert markdown link format to restructuredText format, etc.

Recommended Posts

Sphinx extension to arbitrarily convert text in pre-processing of document generation
How to output a document in pdf format with Sphinx
Convert Excel file to text in Python for diff purposes
Convert HTML to text file
Speech to speech in python [text to speech]
Convert a large number of PDF files to text files using pdfminer
Summary of sharpening and smoothing of probabilities + Applied to simple document generation
Convert markdown to PDF in Python
How to convert 0.5 to 1056964608 in one shot
Convert PDF of available stores of Go To EAT in Kagoshima prefecture to CSV
Convert XML document stored in XML database (BaseX) to CSV format (using Python)
I want to batch convert the result of "string" .split () in Python
Convert PDF of Go To EAT member stores in Ishikawa prefecture to CSV
Convert PDF of new corona outbreak case in Aichi prefecture to CSV