Search code examples
pythonhtmlrestructuredtextdocutils

Using docutils.core.publish_string instead of publish_cmdline, `settings_overrides` argument carrying HTML-Specific Options takes no effect!


Below is my code

from docutils.core import publish_string
from docutils.writers.html4css1 import Writer as HisWriter

args = {
    'stylesheet' : '/home/wonder/lab/css/note.css',
    'stylesheet-path' : None,
}

src = 'ccav'
print publish_string(src, writer=HisWriter(), settings_overrides=args)

I got the following error:

AssertionError: stylesheet and stylesheet_path are mutually exclusive.

So, I change args to:

args = {
    'stylesheet-path' : '/home/wonder/lab/css/note.css',
    'stylesheet' : None,
}

Now, There is no errors. But, The stylesheet inserted into the HTML output is not the content of /home/wonder/lab/css/note.css. It is still /usr/local/lib/python2.7/dist-packages/docutils/writers/html4css1/html4css1.css.

That is to say, unlike specify options in command line when using publish_cmdline, the settings_overrides argument carrying HTML-Specific Options takes no effect when using publish_string.


Solution

  • from docutils.core import publish_string
    from docutils.writers.html4css1 import Writer as HisWriter
    
    src = 'ccav'
    args = {
        'stylesheet_path' : '/path/to/your/stylesheet'
    
    }
    print publish_string(src, writer=HisWriter(), settings=None, settings_overrides=args)
    

    You need to do settings = None and use stylesheet_path rather than stylesheet to get it to ignore the built in stylesheet.

    Edit: Note that I found this answer in the source of one of the example scripts that comes with distutils, so even though settings = None seems bad, it doesn't seem to be.