From: Noah S. <no...@no...> - 2005-12-20 19:10:09
|
> http://docutils.sourceforge.net/docutils/examples.py > > some sample code for using docutils. Note the overrides dictionary. > > This is what I have been using for generating html from text. The > relevant pieces are traceback and halt_level. I'm doing my html > generation offline and fixing any errors before making the html live. > The default behavior simply outputs the broken html. Background: I'm building a yet another Wiki page editor sort of thing. I have one form submits text to a CGI that takes the text and compiles it into static HTML that is saved. The problem is that docutils directly prints all the warning to stderr, so my "compile and save" CGI output is getting corrupted. The compile step happens before the CGI prints the "Content-type: text/html" header, so my HTTP response is getting corrupted with warning output from docutils. My only option appears to be to stop compiling and raise an exception. That's bad because I'm just getting a trial warning. I want to make a best effort to compile the RST text. In my example, I was just getting this warning printed to stderr test.rst:2: (WARNING/2) Title overline too short. ------- rst wiki ------- That doesn't seem like a good enough reason to reject the user's RST, so I'm happy that Docutils will continue to make a best effort to compile the RST. I'm just not happy that it prints to stderr. I didn't see any example of traceback or halt-level in examples.py. I did find some documentation here: http://docutils.sourceforge.net/docs/user/config.html This appears to only give me the choice between halting or not halting on exceptions. It does not give me the choice to turn off or redirect warning output away from stderr. By default Docutils does not halt and prints messages to stderr (that is exception is caught and reported to stderr). What I need is an option to "Allow Exception to caught but NOT reported". Or at least I need the ability to set which stream warniong messages are printed to. I found the 'warning_steam' settings_overrides option. I tried something like this, but it didn't seem to have any effect. overrides = {'warning_stream':None} static_html = publish_string (rst_text, writer_name="html", settings_overrides=overrides) It runs fine, but I still get Docutil warnings printed to stderr. Maybe I did it wrong. Again, if I set "sys.stderr=None" then I fix the Docutils warning problem, but with the side effect of hiding any real errors in my script. Using sys.stderr to report errors and warnings in a library class does not seem kosher. Normally stderr should be reserved for the Application. Otherwise it becomes difficult for the application developer to predict and control output. Yours, Noah > > overrides = {'input_encoding': input_encoding, > 'doctitle_xform': doctitle, > 'initial_header_level': initial_header_level, > 'traceback': True, > 'halt_level': 2, > } > > Hopefully that gets you looking in the right direction for a fix that > fits your needs. > > |