2 # $Id: Indenter.py,v 1.7 2006/01/08 01:09:30 tavis_rudd Exp $
4 @@TR: this code is unsupported and largely undocumented ...
6 This version is based directly on code by Robert Kuzelj
7 <robert_kuzelj@yahoo.com> and uses his directive syntax. Some classes and
8 attributes have been renamed. Indentation is output via
9 $self._CHEETAH__indenter.indent() to prevent '_indenter' being looked up on the
10 searchList and another one being found. The directive syntax will
11 soon be changed somewhat.
14 ================================================================================
15 Author: Mike Orr <iron@mso.oz.net>
16 License: This software is released for unlimited distribution under the
17 terms of the MIT license. See the LICENSE file.
18 Version: $Revision: 1.7 $
19 Start Date: 2001/11/07
20 Last Revision Date: $Date: 2006/01/08 01:09:30 $
22 __author__
= "Mike Orr <iron@mso.oz.net>"
23 __revision__
= "$Revision: 1.7 $"[11:-2]
28 def indentize(source
):
29 return IndentProcessor().process(source
)
31 class IndentProcessor
:
32 """Preprocess #indent tags."""
35 INDENT_DIR
= re
.compile(r
'[ \t]*#indent[ \t]*(?P<args>.*)')
36 DIRECTIVE
= re
.compile(r
"[ \t]*#")
38 WHITESPACES
= re
.compile(r
"(?P<ws>[ \t]*)")
52 def process(self
, _txt
):
55 for line
in _txt
.splitlines():
56 match
= self
.INDENT_DIR
.match(line
)
58 #is indention directive
59 args
= match
.group(self
.ARGS
).strip()
61 line
= "#silent $self._CHEETAH__indenter.on()"
62 elif args
== self
.OFF
:
63 line
= "#silent $self._CHEETAH__indenter.off()"
64 elif args
== self
.INC
:
65 line
= "#silent $self._CHEETAH__indenter.inc()"
66 elif args
== self
.DEC
:
67 line
= "#silent $self._CHEETAH__indenter.dec()"
68 elif args
.startswith(self
.SET
):
70 line
= "#silent $self._CHEETAH__indenter.setLevel(%(level)d)" % {"level":level
}
71 elif args
.startswith('chars'):
72 self
.indentChars
= eval(args
.split('=')[1])
73 line
= "#silent $self._CHEETAH__indenter.setChars(%(level)d)" % {"level":level
}
74 elif args
.startswith(self
.PUSH
):
75 line
= "#silent $self._CHEETAH__indenter.push()"
76 elif args
.startswith(self
.POP
):
77 line
= "#silent $self._CHEETAH__indenter.pop()"
79 match
= self
.DIRECTIVE
.match(line
)
81 #is not another directive
82 match
= self
.WHITESPACES
.match(line
)
84 size
= len(match
.group("ws").expandtabs(4))
85 line
= ("${self._CHEETAH__indenter.indent(%(size)d)}" % {"size":size
}) + line
.lstrip()
87 line
= "${self._CHEETAH__indenter.indent(0)}" + line
90 return self
.LINE_SEP
.join(result
)
93 """A class that keeps track of the current indentation level.
94 .indent() returns the appropriate amount of indentation.
108 """decrement can only be applied to values greater zero
109 values below zero don't make any sense at all!"""
113 self
.LevelStack
.append(self
.Level
)
115 """the levestack can not become -1. any attempt to do so
116 sets the level to 0!"""
117 if len(self
.LevelStack
) > 0:
118 self
.Level
= self
.LevelStack
.pop()
121 def setLevel(self
, _level
):
122 """the leve can't be less than zero. any attempt to do so
123 sets the level automatically to zero!"""
128 def setChar(self
, _chars
):
130 def indent(self
, _default
=0):
132 return self
.Chars
* self
.Level
134 return " " * _default