Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

python - Detect output format in conf.py during sphinx-build documentation generation

I'm adding workarounds to some of the Sphinx's problems straight into conf.py file. And I want to write code like this:

if documentation_will_be_generated_as_HTML():
    my_workaround_for_html_docs()
else:
    my_workaround_for_the_rest_of_the_formats()

How can predicate documentation_will_be_generated_as_HTML() be written for Sphinx?

P.S. I know that it's not good to make workarounds like this, and it's better to make them as extension, but deadlines are strict and tight.

question from:https://stackoverflow.com/questions/65846365/detect-output-format-in-conf-py-during-sphinx-build-documentation-generation

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use Sphinx's application API and event handling features to determine which builder that is currently running. See https://www.sphinx-doc.org/en/master/extdev/appapi.html.

Here is what a handler for the source-read event could look like:

def workaround(app, docname, source):
    if app.builder.name == "html":
        my_workaround_for_html_docs()
    else:
        my_workaround_for_the_rest_of_the_formats()
 
def setup(app):
    app.connect("source-read", workaround)

Some other event might be more appropriate for your specific case.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...