Sphinx doesn't highlight my code block :-(

1,418 views
Skip to first unread message

vitalije

unread,
Sep 17, 2012, 6:18:06 AM9/17/12
to sphin...@googlegroups.com
Hello,
I can't figure out why sphinx refuses to highlight my code snippets. I tried to update my installation of docutils, sphinx and pygments, but the problem is still present.

When I try to pygmentize code snippet it works as expected. But when I run sphinx build it makes just  'pre' block and inside it just a plain text. It doesn't have any span tag at all.

I have tried to put  'highlight' directive before this block.
I tried to put '::' on the end of previous text line and to indent code block.
I tried to put .. code-block:: actionscript.

All that with no success.
TIA Vitalije

Kevin Horn

unread,
Sep 17, 2012, 9:16:22 AM9/17/12
to sphin...@googlegroups.com
It will be easier to help you if you can show us the block you are trying to highlight.

Kevin Horn 

vitalije

unread,
Sep 17, 2012, 11:01:37 AM9/17/12
to sphin...@googlegroups.com
I think I found what is the cause of this problem. Sphinx is checking code snippet and if it finds syntax errors it refuses to highlight code. A Code snippets that I was trying to highlight have some elements that are not syntactically correct. Those are some directives that Leo editor understands (that is the editor I use). I don't know if it is possible to tell sphinx not to check syntax of code snippets or not, but I have found a solution.

I made script that pre-process my rst files and replaces those code snippets with the raw:html directive along with the output of pygments highlight. I am primarily interested in producing html, so I don't mind loosing readability of rst files.

If anyone is interested to see my script, please let me know, I would be glad to share it. Maybe it is too much tied to the tools I use and because of that less interesting.

I use Leo editor for editing code and writing documentation. When I want to include some part of source code in document I put 'LEOUNL' tag followed by path to the part I want to insert (path includes language for highlighting, path to Leo document and path inside Leo document to the code part). The script opens Leo document and replaces all tags with the proper parts of source code highlighted with pygments and write rst files and finally executes make html. I have made my own template to produce not whole html file but just part of body. The rest of html is served from the fossil repository that keeps also source code.
I find combination of those tools very powerful. If anyone wants to see final documentation I made here are links: kviziracija  and igmator

Vitalije

Bryan A. Jones

unread,
Sep 17, 2012, 11:17:24 AM9/17/12
to sphin...@googlegroups.com
If you make your highlight-language python3, then Sphinx won't perform syntax checks before highlighting.



Vitalije

--
You received this message because you are subscribed to the Google Groups "sphinx-dev" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sphinx-dev/-/FNq3ko-SwHYJ.
To post to this group, send email to sphin...@googlegroups.com.
To unsubscribe from this group, send email to sphinx-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sphinx-dev?hl=en.



--
Bryan A. Jones, Ph.D.
Associate Professor
Department of Electrical and Computer Engineering
231 Simrall / PO Box 9571
Mississippi State University
Mississippi state, MS 39762
http://www.ece.msstate.edu/~bjones
bjones AT ece DOT msstate DOT edu
voice 662-325-3149
fax 662-325-2298

Our Master, Jesus Christ, is on his way. He'll show up right on
time, his arrival guaranteed by the Blessed and Undisputed Ruler,
High King, High God.
- 1 Tim. 6:14b-15 (The Message)

vitalije

unread,
Sep 17, 2012, 11:50:22 AM9/17/12
to sphin...@googlegroups.com


On Monday, September 17, 2012 5:17:58 PM UTC+2, Bryan wrote:
If you make your highlight-language python3, then Sphinx won't perform syntax checks before highlighting.


That's what I found in sphinx documentation, but it is not true. I have looked in the sphinx.highlight module and there is in sphinx.highlight.PygmentsBridge.highlight_block method, somewhere in the middle check like this:
...

elif lang in ('python3', 'py3') and source.startswith('>>>'):

# for py3, recognize interactive sessions, but do not try parsing...

lexer = lexers['pycon3']

elif lang == 'guess':

...

So it is just that sphinx want check interactive session in python3. Where really comes check is at the end of this method:

# highlight via Pygments

try:

formatter = self.get_formatter(**kwargs)

hlsource = highlight(source, lexer, formatter)

if self.dest == 'html':

return hlsource

else:

return hlsource.translate(tex_hl_escape_map_new)

except ErrorToken:

# this is most probably not the selected language,

# so let it pass unhighlighted

return self.unhighlighted(source)


ie if there is any ErrorToken the whole block won't be highlighted. I am not sure but I think that it wouldn't check if the method add_filter('raiseonerror') of lexer was not called a few lines before this part of method.

Besides, I want my code block written (mostly) in actionscript3 to be highlighted as such not as python3 ;-)

Vitalije

vitalije

unread,
Sep 17, 2012, 11:54:02 AM9/17/12
to sphin...@googlegroups.com
well after I posted previous post, I realised that google removed leading white space in the above source code. :-(

But if you want to see original look at module highlight in  sphinx package.

pstephens

unread,
Sep 17, 2012, 2:48:33 PM9/17/12
to sphin...@googlegroups.com
which editor do you use? ;->  since you didn't provide us a snippet or source, but talked a lot about Leo, you have wasted the time I spent reading this.

Pete

unread,
Sep 18, 2012, 10:43:48 AM9/18/12
to sphin...@googlegroups.com
This problem might be resolved by changing the highlighting language to "guess"
Reply all
Reply to author
Forward
0 new messages