[Checkins] SVN: z3c.rml/trunk/src/z3c/rml/ * Implemented color highlighting in example.

Stephan Richter srichter at cosmos.phy.tufts.edu
Sun Apr 15 01:05:16 EDT 2007


Log message for revision 74142:
  * Implemented color highlighting in example.
  
  * Implemented additional file info for example.
  

Changed:
  U   z3c.rml/trunk/src/z3c/rml/rml-reference.pt
  U   z3c.rml/trunk/src/z3c/rml/rml-reference.py
  U   z3c.rml/trunk/src/z3c/rml/tests/input/tag-circle.rml

-=-
Modified: z3c.rml/trunk/src/z3c/rml/rml-reference.pt
===================================================================
--- z3c.rml/trunk/src/z3c/rml/rml-reference.pt	2007-04-15 04:39:02 UTC (rev 74141)
+++ z3c.rml/trunk/src/z3c/rml/rml-reference.pt	2007-04-15 05:05:15 UTC (rev 74142)
@@ -68,10 +68,16 @@
         leftIndent="0.3cm"
         />
     <paraStyle
+        name="example-info"
+        fontName="Times-Roman"
+        fontSize="10"
+        leftIndent="0.5cm"
+        />
+    <paraStyle
         name="code"
         fontName="Courier"
         fontSize="10"
-        leftIndent="0.5cm"
+        leftIndent="0.8cm"
         />
   </stylesheet>
 
@@ -219,9 +225,13 @@
           <i>Examples</i>
         </para>
         <tal:block repeat="example directive/examples">
-          <pre style="code" tal:content="structure example">
+          <para style="example-info">
+            File <i tal:content="example/filename" />,
+            line <tal:block replace="example/line" />:
+          </para>
+          <xpre style="code" tal:content="structure example/code">
             Example Code
-          </pre>
+          </xpre>
         </tal:block>
       </tal:block>
 

Modified: z3c.rml/trunk/src/z3c/rml/rml-reference.py
===================================================================
--- z3c.rml/trunk/src/z3c/rml/rml-reference.py	2007-04-15 04:39:02 UTC (rev 74141)
+++ z3c.rml/trunk/src/z3c/rml/rml-reference.py	2007-04-15 05:05:15 UTC (rev 74142)
@@ -20,14 +20,39 @@
 import zope.schema
 import zope.schema.interfaces
 from lxml import etree
+from xml.sax import saxutils
 from z3c.rml import attr, document, pagetemplate
 
+try:
+    import SilverCity
+except ImportError:
+    SilverCity = None
+
+
 EXAMPLES_DIRECTORY = os.path.join(os.path.dirname(__file__), 'tests', 'input')
 IGNORE_ATTRIBUTES = ('RMLAttribute', 'BaseChoice')
 CONTENT_FIELD_TYPES = (
     attr.TextNode, attr.TextNodeSequence, attr.TextNodeGrid,
     attr.RawXMLContent, attr.XMLContent)
+STYLES_FORMATTING = {
+     1 : ('<font textColor="red">', '</font>'),
+     #3 : ('<font textColor="blue">', '</font>'),
+     6 : ('<font textColor="blue">', '</font>'),
+    11 : ('<font textColor="red">', '</font>'),
+    }
 
+
+def highlightRML(rml):
+    if SilverCity is None:
+        return rml
+    lexer = SilverCity.XML.XMLLexer()
+    styledRml = ''
+    for piece in lexer.tokenize_by_style(rml):
+        start, end = STYLES_FORMATTING.get(piece['style'], ('', ''))
+        styledRml += start + saxutils.escape(piece['text']) + end
+    return styledRml
+
+
 def getAttributeTypes():
     types = []
     candidates = sorted(attr.__dict__.items(), key=lambda e: e[0])
@@ -107,21 +132,22 @@
 
 def extractExamples(directory):
     EXAMPLE_NS = 'http://namespaces.zope.org/rml/doc'
-    EXAMPLE_ATTR_NAME = '{%s}elementExample' %EXAMPLE_NS
+    EXAMPLE_ATTR_NAME = '{%s}exampleFor' %EXAMPLE_NS
     examples = {}
-    for fileName in os.listdir(directory):
-        if not fileName.endswith('.rml'):
+    for filename in os.listdir(directory):
+        if not filename.endswith('.rml'):
             continue
-        rmlFile = open(os.path.join(directory, fileName), 'r')
+        rmlFile = open(os.path.join(directory, filename), 'r')
         root = etree.parse(rmlFile).getroot()
-        elements = root.xpath('//@doc:elementExample/parent::*',
+        elements = root.xpath('//@doc:exampleFor/parent::*',
                               {'doc': EXAMPLE_NS})
         for elem in elements:
             demoTag = elem.get(EXAMPLE_ATTR_NAME)
             del elem.attrib[EXAMPLE_ATTR_NAME]
-            xml = etree.tounicode(elem, pretty_print=True).strip()
+            xml = highlightRML(etree.tounicode(elem).strip())
             elemExamples = examples.setdefault(demoTag, [])
-            elemExamples.append(xml)
+            elemExamples.append(
+                {'filename': filename, 'line': elem.sourceline, 'code': xml})
 
     return examples
 

Modified: z3c.rml/trunk/src/z3c/rml/tests/input/tag-circle.rml
===================================================================
--- z3c.rml/trunk/src/z3c/rml/tests/input/tag-circle.rml	2007-04-15 04:39:02 UTC (rev 74141)
+++ z3c.rml/trunk/src/z3c/rml/tests/input/tag-circle.rml	2007-04-15 05:05:15 UTC (rev 74142)
@@ -8,16 +8,16 @@
 
     <circle x="10cm" y="25cm" radius="2cm"
             fill="false" stroke="false"
-            doc:elementExample="circle" />
+            doc:exampleFor="circle" />
 
     <circle x="10cm" y="20cm" radius="2cm"
             fill="true" stroke="false" />
 
     <circle x="10cm" y="15cm" radius="2cm"
-            fill="false" stroke="true" doc:elementExample="bulkData"/>
+            fill="false" stroke="true"/>
 
     <circle x="10cm" y="10cm" radius="2cm"
-            fill="true" stroke="true" doc:elementExample="circle"/>
+            fill="true" stroke="true" />
 
   </pageDrawing>
 </document>



More information about the Checkins mailing list