| 1 |
""" |
| 2 |
Indentation maker. |
| 3 |
@@TR: this code is unsupported and largely undocumented ... |
| 4 |
|
| 5 |
This version is based directly on code by Robert Kuzelj |
| 6 |
<[email protected]> and uses his directive syntax. Some classes and |
| 7 |
attributes have been renamed. Indentation is output via |
| 8 |
$self._CHEETAH__indenter.indent() to prevent '_indenter' being looked up on the |
| 9 |
searchList and another one being found. The directive syntax will |
| 10 |
soon be changed somewhat. |
| 11 |
""" |
| 12 |
|
| 13 |
import re |
| 14 |
|
| 15 |
|
| 16 |
def indentize(source): |
| 17 |
return IndentProcessor().process(source) |
| 18 |
|
| 19 |
|
| 20 |
class IndentProcessor(object): |
| 21 |
"""Preprocess #indent tags.""" |
| 22 |
LINE_SEP = '\n' |
| 23 |
ARGS = "args" |
| 24 |
INDENT_DIR = re.compile(r'[ \t]*#indent[ \t]*(?P<args>.*)') |
| 25 |
DIRECTIVE = re.compile(r"[ \t]*#") |
| 26 |
WS = "ws" |
| 27 |
WHITESPACES = re.compile(r"(?P<ws>[ \t]*)") |
| 28 |
|
| 29 |
INC = "++" |
| 30 |
DEC = "--" |
| 31 |
|
| 32 |
SET = "=" |
| 33 |
CHAR = "char" |
| 34 |
|
| 35 |
ON = "on" |
| 36 |
OFF = "off" |
| 37 |
|
| 38 |
PUSH = "push" |
| 39 |
POP = "pop" |
| 40 |
|
| 41 |
def process(self, _txt): |
| 42 |
result = [] |
| 43 |
|
| 44 |
for line in _txt.splitlines(): |
| 45 |
match = self.INDENT_DIR.match(line) |
| 46 |
if match: |
| 47 |
|
| 48 |
args = match.group(self.ARGS).strip() |
| 49 |
if args == self.ON: |
| 50 |
line = "#silent $self._CHEETAH__indenter.on()" |
| 51 |
elif args == self.OFF: |
| 52 |
line = "#silent $self._CHEETAH__indenter.off()" |
| 53 |
elif args == self.INC: |
| 54 |
line = "#silent $self._CHEETAH__indenter.inc()" |
| 55 |
elif args == self.DEC: |
| 56 |
line = "#silent $self._CHEETAH__indenter.dec()" |
| 57 |
elif args.startswith(self.SET): |
| 58 |
level = int(args[1:]) |
| 59 |
line = "#silent " \ |
| 60 |
"$self._CHEETAH__indenter.setLevel(%(level)d)" \ |
| 61 |
% {"level": level} |
| 62 |
elif args.startswith('chars'): |
| 63 |
self.indentChars = eval(args.split('=')[1]) |
| 64 |
line = "#silent " \ |
| 65 |
"$self._CHEETAH__indenter.setChars(%(level)d)" \ |
| 66 |
% {"level": level} |
| 67 |
elif args.startswith(self.PUSH): |
| 68 |
line = "#silent $self._CHEETAH__indenter.push()" |
| 69 |
elif args.startswith(self.POP): |
| 70 |
line = "#silent $self._CHEETAH__indenter.pop()" |
| 71 |
else: |
| 72 |
match = self.DIRECTIVE.match(line) |
| 73 |
if not match: |
| 74 |
|
| 75 |
match = self.WHITESPACES.match(line) |
| 76 |
if match: |
| 77 |
size = len(match.group("ws").expandtabs(4)) |
| 78 |
line = ( |
| 79 |
"${self._CHEETAH__indenter.indent(%(size)d)}" |
| 80 |
% {"size": size}) \ |
| 81 |
+ line.lstrip() |
| 82 |
else: |
| 83 |
line = "${self._CHEETAH__indenter.indent(0)}" + line |
| 84 |
result.append(line) |
| 85 |
|
| 86 |
return self.LINE_SEP.join(result) |
| 87 |
|
| 88 |
|
| 89 |
class Indenter(object): |
| 90 |
""" |
| 91 |
A class that keeps track of the current indentation level. |
| 92 |
.indent() returns the appropriate amount of indentation. |
| 93 |
""" |
| 94 |
On = 1 |
| 95 |
Level = 0 |
| 96 |
Chars = ' ' |
| 97 |
LevelStack = [] |
| 98 |
|
| 99 |
def on(self): |
| 100 |
self.On = 1 |
| 101 |
|
| 102 |
def off(self): |
| 103 |
self.On = 0 |
| 104 |
|
| 105 |
def inc(self): |
| 106 |
self.Level += 1 |
| 107 |
|
| 108 |
def dec(self): |
| 109 |
"""decrement can only be applied to values greater zero |
| 110 |
values below zero don't make any sense at all!""" |
| 111 |
if self.Level > 0: |
| 112 |
self.Level -= 1 |
| 113 |
|
| 114 |
def push(self): |
| 115 |
self.LevelStack.append(self.Level) |
| 116 |
|
| 117 |
def pop(self): |
| 118 |
"""the levestack can not become -1. any attempt to do so |
| 119 |
sets the level to 0!""" |
| 120 |
if len(self.LevelStack) > 0: |
| 121 |
self.Level = self.LevelStack.pop() |
| 122 |
else: |
| 123 |
self.Level = 0 |
| 124 |
|
| 125 |
def setLevel(self, _level): |
| 126 |
"""the leve can't be less than zero. any attempt to do so |
| 127 |
sets the level automatically to zero!""" |
| 128 |
if _level < 0: |
| 129 |
self.Level = 0 |
| 130 |
else: |
| 131 |
self.Level = _level |
| 132 |
|
| 133 |
def setChar(self, _chars): |
| 134 |
self.Chars = _chars |
| 135 |
|
| 136 |
def indent(self, _default=0): |
| 137 |
if self.On: |
| 138 |
return self.Chars * self.Level |
| 139 |
return " " * _default |