> Hi Makoto,
> Thank you for the updated code. I had an implmentation question about
> one part:
> > s = buf[-1]
> > if s and s.isspace() and s.find("\n") < 0:
> I usually try to avoid str.find() in algorithms whenever possible. I
> did some debug logging and it looked like buf[-1] was always some
> number of spaces or '\n'. Would there ever be a newline inside the
> middle of the string? If not, I'd like to use:
> if s and s.isspace() and s[-1] != '\n':
> > I think that it is very hard to control indents accurately
> > in Tenjin approach. To controll them, it is necessary to
> > parse Python statements and detect block correctly, like
> > Mako or Django does. But it makes Tenjin more complex and
> > heavy, so I wouldn't do that.
> > I'm sorry but I recommend to compromise at some level.
> I know it is a selfish perspective, but I would really like it if
> Tenjin would detect blocks correctly similar to Mako. Having moved
> from django to pytenjin for performance reasons, I can understand not
> wanting slow tenjin down too much. I agree that finding a compromise
> would be a good thing.
> May I suggest three aspects of what I would consider an ideal
> compromise:
> 1) Improve block detection so that blocks could be arbitrarily
> indented. So this is okay:
> <?py if ?>
> <?py if ?>
> <?py else ?>
> <?py #end ?>
> <?py if ?>
> <?py else ?>
> <?py #end ?>
> <?py end?>
> 2) Disallow inconsistent indenting of a single block level control
> structure. For instance, this wound NOT be okay:
> <?py if ?>
> <?py else ?>
> <?py #end ?>
> 3) Generate python code with normalized indentation instead of using
> "if True:" dummy blocks.
> I think these three principals could be accomplished relatively easily
> with the following sort of pseudo-code algorithm:
> py_parse = re.compile(r'(\s*)<\?py (\s*)(.*?)\?>')
> output = []
> new_blocks = set(('if', 'while', 'for', 'try', 'with', 'def',
> 'class'))
> cont_blocks = set(('elif', 'else', 'except', 'finally'))
> indent_spaces = 4
> indent = ' ' * indent_spaces
> depth = -1
> for line in py_lines:
> pre_space, in_space, statement = py_parse.match(line).groups()
> if statement in new_blocks:
> depth += 1
> output.append(''.join((indent * depth, statement)))
> elif statement in cont_blocks:
> output.append(''.join((indent * depth, statement)))
> elif statement.startswith('#end'):
> depth -= 1
> else:
> output.append(''.join((indent * (depth+1), statement)))
> output = '\n'.join(output)
> I admit that this only handles single-line <?py> statements with no
> trailing text. But, I think trailing text is easy and mutli-line <?py
> \n?> statements could be handled with an additional regex.
> I would personally like to see this functionality built-in to tenjin
> because I think it makes sense to better be able to combined html
> indentation levels with python statement indentation. However, if
> this is not feasible it's not a very big problem for me. I
> implemented a slightly simpler algorithm as a pre-process python
> script that strips out almost all leading whitespace. It uses a
> similar technique to preserve <?py > indentation levels but only
> outputs a single space per indentation depth. With this done, it is
> not hard to see and hand-edit inconsistent <?py> indentations.
> In a previous email I had mentioned that I used an HTML "compressor"
> to reduce file size by ~30%. With the leading-space elimination pre-
> process script I managed to reduce the file size by ~25%. That's
> enough that I don't feel bad about missing the last 5%.
> Thanks,
> Steve