On Sat, Apr 19, 2014 at 3:57 PM, Peter Mills <[email protected]>wrote:

>
>>     getConfig(gc.getString, 'syntax_highlight', 'long')
>>     getConfig(gc.getBool,   'no_compact_lists', False)
>>     getConfig(gc.getBool,   'no_compact_field_lists', False)
>>
>> and also specifying in your @settings at least:
>>
>>     @string vr-syntax-highlight = short
>>
>
> These look like really important additions to the default configuration in
> the plugin.  Given that Leo uses Pygments for syntax colouring, would we
> not make the default value in the GetConfig 'short' so we wouldn't need the
> nuisance requirement to have the 'short' option set in the @settings tree?
>
> Only after reading this thread have I realised that my leo_vr.css that I
> use and included in an earlier post was in fact a customised css
> specifically to get around this short/long problem.  I'd completely
> forgotten about that kludge.
>

I only used "long" because that is the docutils default. IMO
viewrendered2.py should be changed to allow the use of
"vr-syntax-highlight" **without** actually setting the syntax-highlight
docutils option. Currently specifying a @settings name and setting that
option to the default are combined in the getConfig() method.

I wondered why docutils sets syntax_highlight to long in the first place?
Presumably because the longer class names are easier to understand. But
then it should really have an pygmentize stylesheet class name converter or
something.

After completing the following investigation (which most people can skip),
I agree that the default for syntax-highlight should be "short". Docutils
goes through all sorts of complications to generate "long" class names by
essentially circumventing the normal pygments code.

I'm a reST user via Sphinx, I never used to use docutils directly until I
started playing around with Leo. And before this I never really had to
understand highlighting code, it just works in Sphinx (not surprising since
the creators of Sphinx also wrote Pygments). So I just took a look at why
that is, and the reason is in
sphinx.builders.html.StandaloneHTMLBuilder#copy_static_files() which does
this:

    # first, create pygments style file
    f = open(path.join(self.outdir, '_static', 'pygments.css'), 'w')
    f.write(self.highlighter.get_stylesheet())
    f.close()

Sphinx is automatically creating pyments.css for you based on what pygments
style you specify elsewhere. Looking at the generated HTML for some test
python code it looks like this:

   <div class="highlight-python"><div class="highlight"><pre><span
class="k">def</span> <span class="nf">myFunction</span><span
class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span
class="n">arg2</span><span class="p">):</span>
      <span class="sd">&quot;&quot;&quot;Do this not
that&quot;&quot;&quot;</span>

      <span class="kn">import</span> <span class="nn">sys</span>
      <span class="n">i</span> <span class="o">=</span> <span
class="n">arg1</span> <span class="o">+</span> <span class="n">arg2</span>
      <span class="k">print</span><span class="p">(</span><span
class="n">i</span><span class="p">)</span>
      <span class="k">return</span> <span class="n">i</span>
   </pre></div>
   </div>

Taking a look at sphinx.highlighting.PygmentsBridge#highlight_block(), you
can see that all it does is:

    hlsource = highlight(source, lexer, formatter)

OTOH docutils jumps through hoops to support its annoying "long" format in
docutils.utils.code_analyzer.Lexer#__iter__:

    def __iter__(self):
        """Parse self.code and yield "classified" tokens.
        """
        if self.lexer is None:
            yield ([], self.code)
            return
        tokens = pygments.lex(self.code, self.lexer)
        for tokentype, value in self.merge(tokens):
            if self.tokennames == 'long': # long CSS class args
                classes = str(tokentype).lower().split('.')
            else: # short CSS class args
                classes = [_get_ttype_class(tokentype)]
            classes = [cls for cls in classes if cls not in unstyled_tokens]
            yield (classes, value)

It eventually uses this info in
docutils.parsers.rst.directives.body.CodeBlock#run() to build up its
CodeBlock node. So it bypasses pygments when handling code highlighting.
This also explains the mysterious lack of the surrounding <div
class="highlight"> that the Pygments documentations says should be there.

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to