SilverCity Examples SourceForge Logo

SilverCity Links

Examples

This is an example of how to use SilverCity from Python. I'll probably write a C usage example later.

Also note that there are some example scripts in the source distribution, so you might want to download the source just for that reason.

>>> import SilverCity
>>> p = SilverCity.PropertySet()
>>> from SilverCity import Keywords
>>> w = SilverCity.PropertySet(Keywords.python_keywords)
>>> l = SilverCity.find_lexer_module_by_name('python')
>>> l.tokenize_by_style("  \t  import string", w, p)
[{'end_index': 4, 'start_index': 0, 'start_column': 0, 'start_line': 0, 'text':
'  \t  ', 'end_column': 4, 'end_line': 0, 'style': 0}, {'end_index': 10, 'start_
index': 5, 'start_column': 5, 'start_line': 0, 'text': 'import', 'end_column': 1
0, 'end_line': 0, 'style': 5}, {'end_index': 11, 'start_index': 11, 'start_colum
n': 11, 'start_line': 0, 'text': ' ', 'end_column': 11, 'end_line': 0, 'style':
0}, {'end_index': 17, 'start_index': 12, 'start_column': 12, 'start_line': 0, 't
ext': 'string', 'end_column': 17, 'end_line': 0, 'style': 11}]
>>> p['tab.timmy.whinge.level'] = 2
>>> l.tokenize_by_style("  \t  import string", w, p)
[{'end_index': 4, 'start_index': 0, 'start_column': 0, 'start_line': 0, 'text':
'  \t  ', 'end_column': 4, 'end_line': 0, 'style': 64}, {'end_index': 10, 'start
_index': 5, 'start_column': 5, 'start_line': 0, 'text': 'import', 'end_column':
10, 'end_line': 0, 'style': 5}, {'end_index': 11, 'start_index': 11, 'start_colu
mn': 11, 'start_line': 0, 'text': ' ', 'end_column': 11, 'end_line': 0, 'style':
 0}, {'end_index': 17, 'start_index': 12, 'start_column': 12, 'start_line': 0, '
text': 'string', 'end_column': 17, 'end_line': 0, 'style': 11}]
>>> def callback(start_line, start_column, text, **other):
...     print "(%04d, %04d): %r" % (start_line, start_column, text)
...
>>> l.tokenize_by_style("  \t  import string\n\nprint 'Hello'\n", w, p, callback
)
(0000, 0000): '  \t  '
(0000, 0005): 'import'
(0000, 0011): ' '
(0000, 0012): 'string'
(0000, 0018): '\n\n'
(0002, 0000): 'print'
(0002, 0005): ' '
(0002, 0006): "'Hello'"
(0002, 0013): '\n'
>>> from SilverCity import Python
>>> class MyHandler(Python.PythonHandler):
...     def __init__(self):
...             self.numbers = []
...             Python.PythonHandler.__init__(self)
...     def handle_p_number(self, text, **other):
...             self.numbers.append(text)
...     def handle_other(self, **other): pass
...
>>> h = MyHandler()
>>> Python.PythonLexer().tokenize_by_style("assert 5+5 == 10", h.event_handler)
>>> h.numbers
['5', '5', '10']
>>> import StringIO
>>> file = StringIO.StringIO()
>>> g = Python.PythonHTMLGenerator()
>>> g.generate_html(file, 'import test\nprint 5+5')
>>> file.getvalue()
'<span class="p_word">import</span><span class="p_default"> </span><span cl
ass="p_identifier">test</span><span class="p_default"><br/>\n</span><span class=
"p_word">print</span><span class="p_default"> </span><span class="p_number"
>5</span><span class="p_operator">+</span><span class="p_number">5</span>'