| 1 | # -*- coding: iso8859-1 -*- |
|---|
| 2 | # |
|---|
| 3 | # Author: Juanma Barranquero |
|---|
| 4 | # |
|---|
| 5 | # Ideas and syntax heavily lifted from Jan Finell's LegendBox.py wikiprocessor |
|---|
| 6 | # |
|---|
| 7 | |
|---|
| 8 | ''' |
|---|
| 9 | A Trac wiki-processor to display text as a striped listing. |
|---|
| 10 | |
|---|
| 11 | The output is enclosed in a <pre> block of class "striped", |
|---|
| 12 | and each line in a <div> block of alternating "even-stripe" |
|---|
| 13 | and "odd-stripe" classes. You *must* set this classes up in |
|---|
| 14 | your templates/site_css.cs file, or alternatively use the |
|---|
| 15 | processor arguments "box-style", "even-style" and "odd-style". |
|---|
| 16 | |
|---|
| 17 | The "number" argument allows automatic numbering of lines; |
|---|
| 18 | it accepts three args: a Python-style format (quotes and all), |
|---|
| 19 | the initial number for the first line, and the step between |
|---|
| 20 | numbers. If no format is passed, there is no automatic |
|---|
| 21 | numbering; otherwise, the start and step arguments default to 1. |
|---|
| 22 | |
|---|
| 23 | Example: |
|---|
| 24 | |
|---|
| 25 | {{{ |
|---|
| 26 | #!Striped |
|---|
| 27 | #!number: " %3d ", 100, 10 |
|---|
| 28 | #!box-style: border: thick solid #C0C0C0; margin: 20px; width: 70%; |
|---|
| 29 | #!even-style: background-color: #DAC0DA; |
|---|
| 30 | ...text here... |
|---|
| 31 | }}} |
|---|
| 32 | ''' |
|---|
| 33 | |
|---|
| 34 | from trac.util import escape, lstrip, rstrip |
|---|
| 35 | from StringIO import StringIO |
|---|
| 36 | |
|---|
| 37 | def execute(hdf, text, env): |
|---|
| 38 | |
|---|
| 39 | def clean(text): |
|---|
| 40 | return lstrip(rstrip(text, ' \r'), ' ') |
|---|
| 41 | |
|---|
| 42 | def stylize(style): |
|---|
| 43 | return ' style="%s"' % escape(clean(style)) |
|---|
| 44 | |
|---|
| 45 | bstyle = '' |
|---|
| 46 | estyle = '' |
|---|
| 47 | ostyle = '' |
|---|
| 48 | format = None |
|---|
| 49 | start = 1 |
|---|
| 50 | step = 1 |
|---|
| 51 | line = '' |
|---|
| 52 | offset = 0 |
|---|
| 53 | |
|---|
| 54 | lines = text.split('\n') |
|---|
| 55 | |
|---|
| 56 | for l in lines[:4]: |
|---|
| 57 | if l.startswith('#!number:'): |
|---|
| 58 | args = l[9:].split(',') |
|---|
| 59 | format = escape(clean(args[0]).split('"',2)[1]) |
|---|
| 60 | if len(args) > 1: start = int(args[1]) |
|---|
| 61 | if len(args) > 2: step = int(args[2]) |
|---|
| 62 | elif l.startswith('#!box-style:'): |
|---|
| 63 | bstyle = stylize(l[12:]) |
|---|
| 64 | elif l.startswith('#!even-style:'): |
|---|
| 65 | estyle = stylize(l[13:]) |
|---|
| 66 | elif l.startswith('#!odd-style:'): |
|---|
| 67 | ostyle = stylize(l[12:]) |
|---|
| 68 | else: |
|---|
| 69 | break |
|---|
| 70 | offset += 1 |
|---|
| 71 | |
|---|
| 72 | buf = StringIO() |
|---|
| 73 | buf.write('<pre class="striped"%s>' % bstyle) |
|---|
| 74 | |
|---|
| 75 | for i in range(offset, len(lines)-1): |
|---|
| 76 | if (i - offset) % 2: |
|---|
| 77 | alt = 'odd' |
|---|
| 78 | style = ostyle |
|---|
| 79 | else: |
|---|
| 80 | alt = 'even' |
|---|
| 81 | style = estyle |
|---|
| 82 | if format: |
|---|
| 83 | line = format % start |
|---|
| 84 | start += step |
|---|
| 85 | buf.write('<div class="%s-stripe"%s>%s%s</div>' % (alt, style, line, escape(lines[i]))) |
|---|
| 86 | |
|---|
| 87 | buf.write('</pre>') |
|---|
| 88 | return buf.getvalue() |
|---|