2

I was wondering whether it is possible to process a string in python, using sphinx. Basically, I would like to pass a string containing restructured text to sphinx, and then generate the corresponding HTML output. I meant something like this

import sphinx.something
s = 'some restructured text'
html_out = sphinx.something(s)

However, I could not find anything along these lines. So, is this possible, and if so, how would one do this?

mzjn
  • 48,958
  • 13
  • 128
  • 248
v923z
  • 1,237
  • 5
  • 15
  • 25
  • Maybe look at [docutils](http://docutils.sourceforge.net/), which Sphinx is based on. – Thomas K Jan 26 '12 at 19:40
  • Thanks, but I think I really need the sphinx services. – v923z Jan 26 '12 at 20:18
  • What are you trying to do? It doesn't really make sense to process one string in Sphinx, because Sphinx is all about linking between documents etc. – Thomas K Jan 27 '12 at 00:35
  • But even then, I should be able to process an rst string, shouldn't I? I am trying to write a simple editor for rst, so I would take the string from the text window, compile it, and then display the output in an HTML viewer. – v923z Jan 27 '12 at 08:22
  • Have a look here: http://pymolurus.blogspot.com/2012/01/documentation-viewer-for-sphinx.html – Thomas K Jan 27 '12 at 13:18
  • Oh, that really looks like it! Thanks for the pointer! – v923z Jan 27 '12 at 22:44
  • How about http://stackoverflow.com/a/6654576/407651? – mzjn Jan 28 '12 at 13:28
  • Thanks for the suggestion! I have used pygments before, but the problem with that and docutils is that you can't include snippets from external files. sphinx is really significantly better in that regard. – v923z Jan 28 '12 at 16:33
  • @v923z: Why do you bring up Pygments? It is not mentioned in the answer that I linked to. And in the question you don't say anything about including snippets from external files. – mzjn Jan 29 '12 at 09:06
  • Because I believe that if you want to have syntax highlighting, you will need pygments. I might be wrong, though... Sphinx automatically includes that. And I didn't say anything about including snippets, because I clearly stated that I wanted to process a sphinx string. That should include snippets, and many other things. – v923z Jan 31 '12 at 09:05

1 Answers1

1

The quickest solution I've seen is:

from docutils.examples import html_body

def rst(rstStr):
return html_body(rstStr, input_encoding='utf-8', 
                 output_encoding='utf-8').strip()

I'd be interested myself in better solutions..

Bud P. Bruegger
  • 1,021
  • 10
  • 15