3

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.

agf
  • 171,228
  • 44
  • 289
  • 238
wonder
  • 521
  • 4
  • 11
  • What happens when you set `'stylesheet' : '/home/wonder/lab/css/note.css'` and also set `'stylesheet-path' : './' or something like that at the same time – agf Jul 23 '11 at 06:39
  • Thanks for your suggestion! I have tried。let `args={'stylesheet-path' : '/home/wonder/lab/css/note.css','stylesheet' : './',}` or `args={'stylesheet' : '/home/wonder/lab/css/note.css','stylesheet-path' : './',}`。 But in the two situations, I always get the same as before: `AssertionError: stylesheet and stylesheet_path are mutually exclusive.` – wonder Jul 23 '11 at 08:00
  • I have found a example use of publish_string [click to visit](http://www.google.com/codesearch#5nXISCkkwPs/transforms/rest.py&q=publish_string&type=cs). but it use no HTML-Spec options at all. – wonder Jul 23 '11 at 08:07
  • Take a look at my answer, it works for me. Also remember to accept an answer by clicking the check mark next to it if it solved your problem. – agf Jul 23 '11 at 08:24

1 Answers1

3
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.

agf
  • 171,228
  • 44
  • 289
  • 238
  • Thank you very much! Your code have solved my problem. It works as I expects. But even you have miss the point! `settings=None` is unnecessary. Don't You see the Symbol `stylesheet_path` in the code you pasted, It's `stylesheet_path` rather than `stylesheet-path`! – wonder Jul 23 '11 at 13:24
  • HA! Wow, I didn't notice that at all. Copy-Paste to the rescue! – agf Jul 23 '11 at 13:28
  • 1
    I have to complain that what a bad design it is, How can it use `stylesheet-path` in settings_spec and cmd line but use a different `stylesheet_path` in this situation! it has wasted me a great deal of time! And I can find nothing about it in the [documentation](http://docutils.sourceforge.net/docs/api/runtime-settings.html) – wonder Jul 23 '11 at 13:35
  • It's really about the worst documentation I've ever seen. Just reading the source is much easier. – agf Jul 23 '11 at 13:36
  • Thank you! could you please give me the link to the source code you refer to? – wonder Jul 23 '11 at 13:43
  • I looked at several files but I think http://svn.berlios.de/viewvc/docutils/trunk/docutils/docutils/writers/html4css1/__init__.py?revision=7061&content-type=text%2Fplain was the one that I copied from. – agf Jul 23 '11 at 13:45
  • Maybe also http://svn.berlios.de/viewvc/docutils/trunk/docutils/docutils/examples.py?view=markup – agf Jul 23 '11 at 13:46
  • Got it! Many thanks! `stackoverflow` is a wonderful place. I am Chinese. In China, few people use python, seldom people use rst. When I encounter difficulties,it is difficult to find a solution when `Google` cannot help. recently, I found that google often takes me to `stackoverflow`. So I join here. Sorry for my poor English, I have to talk with the assistant of Google translator. – wonder Jul 23 '11 at 14:02