[svn] implemented {% raw %} and improved jinja/django highlighter
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 2894f22 · Browse files · 6 files·+68 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+68 added−21 removed
- other:+68 added−21 removed
Provenance
- Commit
b9c8ae1- Tree
e5fcffd- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
2894f22
6 files changed+68 added−21 removed
modified:docs/src/designerdoc.txt+48 added-4 removed
117 unmodified lines118 These tests are especially useful when used in `if` conditions.119 120 [[list_of_tests]]121 122+Global Functions123+================124+125+Test functions and filter functions live in their own namespace. Global126+functions not. They behave like normal objects in the context. Beside the127+functions added by the application or framewhere there are two functions128+available per default:129+130+`range`131+ 132+ Works like the python `range function`_ just that it doesn't support133+ ranges greater than ``1000000``.134+135+`debug`136+137+ Function that outputs the contents of the context.138+139 Loops140 =====141 142 To iterate over a sequence, you can use the `for` loop. It basically looks like a9 unmodified lines152 <li><em>no users found</em></li>153 {% endfor %}154 </ul>155 139-*Important difference from Python:* the optional ``else`` block is only156+*Important* Contrary to Python is the optional ``else`` block only157 executed if there was no iteration because the sequence was empty.158 159 Inside of a `for` loop block you can access some special variables:160 435 unmodified lines596 597 {{ undefined.attribute().attribute_too[42] }}598 still returns `undefined`.599 600+Escaping601+========602+603+Sometimes you might want to add Jinja syntax elements into the template604+without executing them. In that case you have quite a few possibilities.605+606+For small parts this might be a good way:607+608+.. sourcecode:: jinja609+610+ {{ "{{ foo }} is variable syntax and {% foo %} is block syntax" }}611+612+When you have multiple elements you can use the ``raw`` block:613+614+.. sourcecode:: jinja615+616+ {% raw %}617+ Filtering blocks works like this in Jinja:618+ {% filter escape %}619+ <html>620+ <code>goes here</code>621+ </html>622+ {% endfilter %}623+ {% endraw %}624+625 Internationalization626 ====================627 628 If the application is configured for i18n, you can define translatable blocks31 unmodified lines660 number that is used to determine the correct singular or plural form. If you661 don't have the indicator variable on position 1 you have to tell the `pluralize`662 tag the correct variable name.663 622-Inside translatable blocks you cannot use blocks or expressions. The variable623-print syntax (``{{ variablename }}``) can just be used to insert the variables defined624-in the ``trans`` header.664+Inside translatable blocks you cannot use blocks or expressions (however you can665+still use the ``raw`` block which will work as expected). The variable666+print syntax (``{{ variablename }}``) is the only way to insert the variables667+defined in the ``trans`` header. Filters must be applied in the header.668 669 .. admonition:: note670 671 Please make sure that you always use pluralize blocks where required.9 unmodified lines681 one user found.682 {% endif %}683 684 .. _slicing chapter: http://diveintopython.org/native_data_types/lists.html#odbchelper.list.slice685+.. _range function: http://docs.python.org/tut/node6.html#SECTION006300000000000000000modified:jinja/translators/python.py+2 added
171 unmodified lines172 """173 Return a comment that helds the node informations or None174 if there is no need to add a debug comment.175 """176+ if node.filename is None:177+ return178 rv = '# DEBUG(filename=%s, lineno=%s)' % (179 node.filename,180 node.lineno181 )714 unmodified linesmodified:jinja/lexer.py+5 added-1 removed
88 unmodified lines89 90 # global parsing rules91 self.rules = {92 'root': [93+ (c('(%s\s*raw\s%s)(.*?)(%s\s*endraw\s*%s)' % (94+ (e(environment.block_start_string),95+ e(environment.block_end_string)) * 2)),96+ (None, 'data', None), None),97 (c('(.*?)(?:%s)' % '|'.join([98 '(?P<%s_begin>%s)' % (n, e(r)) for n, r in root_tag_rules99 ])), ('data', '#bygroup'), '#bygroup'),100 (c('.+'), 'data', None)52 unmodified lines153 if isinstance(tokens, tuple):154 for idx, token in enumerate(tokens):155 # hidden group156 if token is None:153- g += m.group(idx)157+ g = m.group(idx)158 if g:159 lineno += g.count('\n')160 continue161 # failure group72 unmodified linesmodified:jinja/parser.py+1 added-1 removed
478 unmodified lines479 480 # here the only token we should get is "data". all other481 # tokens just exist in block or variable sections. (if the482 # tokenizer is not brocken)483- elif token == 'data':483+ elif token in 'data':484 result.append(nodes.Text(lineno, data))485 486 # so this should be unreachable code487 else:21 unmodified linesmodified:jinja/utils.py+6 added-14 removed
201 unmodified lines202 m = _debug_info_re.search(sourcelines[startpos])203 if m is not None:204 filename, lineno = m.groups()205 if filename == 'None':206- filename = ''207- if lineno == 'None':208- lineno = 0209- else:206+ filename = None207+ if lineno != 'None':208 lineno = int(lineno)209 break210 startpos -= 1211 212 # no traceback information found, reraise unchanged215- if filename is None:213+ if not filename:214 return traceback215+216 return raise_template_exception(exc_value, filename,217 lineno, context)218 219 95 unmodified lines315 else:316 self._queue = []317 pop = self._queue.pop318 self._popleft = lambda: pop(0)319+320 # alias all queue methods for faster lookup321 self._pop = self._queue.pop322-323- # XXX: Is this good? Didn't find another sollution324- def remove_by_value(value):325- for i, v in enumerate(self._queue):326- if v == value:327- self._queue.__delitem__(i)328- break329-330- self._remove = remove_by_value322+ self._remove = self._queue.remove323 self._append = self._queue.append324 325 def copy(self):326 rv = CacheDict(self.capacity)63 unmodified linesmodified:jdebug.py+6 added-1 removed
8 unmodified lines9 :license: BSD, see LICENSE for more details.10 """11 from jinja import Environment12 from jinja.parser import Parser13+from jinja.lexer import Lexer14 from jinja.translators.python import PythonTranslator15 16 16-__all__ = ['e', 't', 'p']17+__all__ = ['e', 't', 'p', 'l']18 19 e = Environment()20 t = e.from_string21 22 def p(x):23 print PythonTranslator(e, Parser(e, x).parse()).translate()24+25+def l(x):26+ for item in e.lexer.tokenize(x):27+ print '%5s %-20s %r' % item