You are not logged in Log in Join
You are here: Home » Members » jim » StructuredTextWiki » StructuredTextExampleSyntaxHilight

Log in
Name

Password

 
 

History for StructuredTextExampleSyntaxHilight

??changed:
-
It would be really useful if Zope would syntax-hilight to the best of its ability code that I include in my structured text documents.

There are plenty of syntax-hilighting programs out there, and there's probably alogrythms built into the open-source text editor that you're using. Here's an example of how this might work. Instead of this::

     def doc_xref(self, s,
        expr = re.compile('\[([%s0-9\-.:/;,\n\r\~]+)\]' % letters).search
        ):
        r = expr(s)
        if r:
            start, end = r.span(1)
            return (StructuredTextXref(s[start:end]), start-1, end+1)
        else:
            return None

You would, instead, see this:

<pre>
     <b><font COLOR="#A020F0">def</font></b> <b><font COLOR="#0000FF">doc_xref</font></b>(self, s,
        expr = re.compile(<font COLOR="#BC8F8F"><b>'\[([%s0-9\-.:/;,\n\r\~]+)\]'</font></b> % letters).search
        ):
        r = expr(s)
        <b><font COLOR="#A020F0">if</font></b> r:
            start, end = r.span(1)
            <b><font COLOR="#A020F0">return</font></b> (StructuredTextXref(s[start:end]), start-1, end+1)
        <b><font COLOR="#A020F0">else</font></b>:
            <b><font COLOR="#A020F0">return</font></b> None
</pre>

Pretty, no? This Syntax Hilight style comes directly from that used on 'cvs.zope.org' by 'viewcvs.cgi 0.7'.

Optimally, I'd like to add some sort of intelligence which would allow STX to determine what language it was hilighting automatically, possibly borrowing from the *nix 'file' command as well as the alogrythms embedded in most modern syntax-hilighting open-source text editors. Barring that, perhaps we could come up with some sort of machine-readable (yet not markup-like) identifier. Maybe 'Language: Python' on the first line of the 'example' paragraph?

Comments very welcome :)

<hr solid id=comments_below>


datagrok (Nov 2, 2001 10:24 am; Comment #1)  --
 I think the proper way to implement this would be to use a bunch of &lt;span&gt; tags within the preformatted text, then use CSS to handle the colorization. That way, the user is able to build a CSS stylesheet that colors, bolds, applies formatting based on the type of element it's working on.

 Here's a quick example. View source to check it out:
 
 <style>
 PRE .keyword { color: purple; font-weight: bold; }
 PRE .comment { color: green; font-family: helvetica; 
                font-style: italic; position: absolute;
                right: 1em; width: 200px;
                border-left: 1px dotted green;}
 PRE .string  { color: darkred; font-weight: bold; }
 PRE .number  { color: red; }
 PRE .class   { color: yellow; font-weight: bold; }
 PRE .method  { color: blue; font-weight: bold; }
 </style>
 
<pre>
     <span class="keyword">def</span> <span class="method">doc_xref</span>(self, s,
        expr = re.compile(<span class="string">'\[([%s0-9\-.:/;,\n\r\~]+)\]'</span> % letters).search
        ):
        r = expr(s)
        <span class="keyword">if</span> r:
            start, end = r.span(1) <span class="comment"># How bout a comment?</span>
            <span class="keyword">return</span> (StructuredTextXref(s[start:end]), start-1, end+1) <span class="comment"># And Another</span>
        <span class="keyword">else</span>:
            <span class="keyword">return</span> None
</pre>

 Cool, eh? My favorite bit is the cool stuff that you can do with the comments, blocking them off to the right, away from the code, in a proportional font, with borders... sweet!