template.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. #
  2. # Copyright 2009 Facebook
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. """A simple template system that compiles templates to Python code.
  16. Basic usage looks like::
  17. t = template.Template("<html>{{ myvalue }}</html>")
  18. print(t.generate(myvalue="XXX"))
  19. `Loader` is a class that loads templates from a root directory and caches
  20. the compiled templates::
  21. loader = template.Loader("/home/btaylor")
  22. print(loader.load("test.html").generate(myvalue="XXX"))
  23. We compile all templates to raw Python. Error-reporting is currently... uh,
  24. interesting. Syntax for the templates::
  25. ### base.html
  26. <html>
  27. <head>
  28. <title>{% block title %}Default title{% end %}</title>
  29. </head>
  30. <body>
  31. <ul>
  32. {% for student in students %}
  33. {% block student %}
  34. <li>{{ escape(student.name) }}</li>
  35. {% end %}
  36. {% end %}
  37. </ul>
  38. </body>
  39. </html>
  40. ### bold.html
  41. {% extends "base.html" %}
  42. {% block title %}A bolder title{% end %}
  43. {% block student %}
  44. <li><span style="bold">{{ escape(student.name) }}</span></li>
  45. {% end %}
  46. Unlike most other template systems, we do not put any restrictions on the
  47. expressions you can include in your statements. ``if`` and ``for`` blocks get
  48. translated exactly into Python, so you can do complex expressions like::
  49. {% for student in [p for p in people if p.student and p.age > 23] %}
  50. <li>{{ escape(student.name) }}</li>
  51. {% end %}
  52. Translating directly to Python means you can apply functions to expressions
  53. easily, like the ``escape()`` function in the examples above. You can pass
  54. functions in to your template just like any other variable
  55. (In a `.RequestHandler`, override `.RequestHandler.get_template_namespace`)::
  56. ### Python code
  57. def add(x, y):
  58. return x + y
  59. template.execute(add=add)
  60. ### The template
  61. {{ add(1, 2) }}
  62. We provide the functions `escape() <.xhtml_escape>`, `.url_escape()`,
  63. `.json_encode()`, and `.squeeze()` to all templates by default.
  64. Typical applications do not create `Template` or `Loader` instances by
  65. hand, but instead use the `~.RequestHandler.render` and
  66. `~.RequestHandler.render_string` methods of
  67. `tornado.web.RequestHandler`, which load templates automatically based
  68. on the ``template_path`` `.Application` setting.
  69. Variable names beginning with ``_tt_`` are reserved by the template
  70. system and should not be used by application code.
  71. Syntax Reference
  72. ----------------
  73. Template expressions are surrounded by double curly braces: ``{{ ... }}``.
  74. The contents may be any python expression, which will be escaped according
  75. to the current autoescape setting and inserted into the output. Other
  76. template directives use ``{% %}``.
  77. To comment out a section so that it is omitted from the output, surround it
  78. with ``{# ... #}``.
  79. These tags may be escaped as ``{{!``, ``{%!``, and ``{#!``
  80. if you need to include a literal ``{{``, ``{%``, or ``{#`` in the output.
  81. ``{% apply *function* %}...{% end %}``
  82. Applies a function to the output of all template code between ``apply``
  83. and ``end``::
  84. {% apply linkify %}{{name}} said: {{message}}{% end %}
  85. Note that as an implementation detail apply blocks are implemented
  86. as nested functions and thus may interact strangely with variables
  87. set via ``{% set %}``, or the use of ``{% break %}`` or ``{% continue %}``
  88. within loops.
  89. ``{% autoescape *function* %}``
  90. Sets the autoescape mode for the current file. This does not affect
  91. other files, even those referenced by ``{% include %}``. Note that
  92. autoescaping can also be configured globally, at the `.Application`
  93. or `Loader`.::
  94. {% autoescape xhtml_escape %}
  95. {% autoescape None %}
  96. ``{% block *name* %}...{% end %}``
  97. Indicates a named, replaceable block for use with ``{% extends %}``.
  98. Blocks in the parent template will be replaced with the contents of
  99. the same-named block in a child template.::
  100. <!-- base.html -->
  101. <title>{% block title %}Default title{% end %}</title>
  102. <!-- mypage.html -->
  103. {% extends "base.html" %}
  104. {% block title %}My page title{% end %}
  105. ``{% comment ... %}``
  106. A comment which will be removed from the template output. Note that
  107. there is no ``{% end %}`` tag; the comment goes from the word ``comment``
  108. to the closing ``%}`` tag.
  109. ``{% extends *filename* %}``
  110. Inherit from another template. Templates that use ``extends`` should
  111. contain one or more ``block`` tags to replace content from the parent
  112. template. Anything in the child template not contained in a ``block``
  113. tag will be ignored. For an example, see the ``{% block %}`` tag.
  114. ``{% for *var* in *expr* %}...{% end %}``
  115. Same as the python ``for`` statement. ``{% break %}`` and
  116. ``{% continue %}`` may be used inside the loop.
  117. ``{% from *x* import *y* %}``
  118. Same as the python ``import`` statement.
  119. ``{% if *condition* %}...{% elif *condition* %}...{% else %}...{% end %}``
  120. Conditional statement - outputs the first section whose condition is
  121. true. (The ``elif`` and ``else`` sections are optional)
  122. ``{% import *module* %}``
  123. Same as the python ``import`` statement.
  124. ``{% include *filename* %}``
  125. Includes another template file. The included file can see all the local
  126. variables as if it were copied directly to the point of the ``include``
  127. directive (the ``{% autoescape %}`` directive is an exception).
  128. Alternately, ``{% module Template(filename, **kwargs) %}`` may be used
  129. to include another template with an isolated namespace.
  130. ``{% module *expr* %}``
  131. Renders a `~tornado.web.UIModule`. The output of the ``UIModule`` is
  132. not escaped::
  133. {% module Template("foo.html", arg=42) %}
  134. ``UIModules`` are a feature of the `tornado.web.RequestHandler`
  135. class (and specifically its ``render`` method) and will not work
  136. when the template system is used on its own in other contexts.
  137. ``{% raw *expr* %}``
  138. Outputs the result of the given expression without autoescaping.
  139. ``{% set *x* = *y* %}``
  140. Sets a local variable.
  141. ``{% try %}...{% except %}...{% else %}...{% finally %}...{% end %}``
  142. Same as the python ``try`` statement.
  143. ``{% while *condition* %}... {% end %}``
  144. Same as the python ``while`` statement. ``{% break %}`` and
  145. ``{% continue %}`` may be used inside the loop.
  146. ``{% whitespace *mode* %}``
  147. Sets the whitespace mode for the remainder of the current file
  148. (or until the next ``{% whitespace %}`` directive). See
  149. `filter_whitespace` for available options. New in Tornado 4.3.
  150. """
  151. from __future__ import absolute_import, division, print_function
  152. import datetime
  153. import linecache
  154. import os.path
  155. import posixpath
  156. import re
  157. import threading
  158. from tornado import escape
  159. from tornado.log import app_log
  160. from tornado.util import ObjectDict, exec_in, unicode_type, PY3
  161. if PY3:
  162. from io import StringIO
  163. else:
  164. from cStringIO import StringIO
  165. _DEFAULT_AUTOESCAPE = "xhtml_escape"
  166. _UNSET = object()
  167. def filter_whitespace(mode, text):
  168. """Transform whitespace in ``text`` according to ``mode``.
  169. Available modes are:
  170. * ``all``: Return all whitespace unmodified.
  171. * ``single``: Collapse consecutive whitespace with a single whitespace
  172. character, preserving newlines.
  173. * ``oneline``: Collapse all runs of whitespace into a single space
  174. character, removing all newlines in the process.
  175. .. versionadded:: 4.3
  176. """
  177. if mode == 'all':
  178. return text
  179. elif mode == 'single':
  180. text = re.sub(r"([\t ]+)", " ", text)
  181. text = re.sub(r"(\s*\n\s*)", "\n", text)
  182. return text
  183. elif mode == 'oneline':
  184. return re.sub(r"(\s+)", " ", text)
  185. else:
  186. raise Exception("invalid whitespace mode %s" % mode)
  187. class Template(object):
  188. """A compiled template.
  189. We compile into Python from the given template_string. You can generate
  190. the template from variables with generate().
  191. """
  192. # note that the constructor's signature is not extracted with
  193. # autodoc because _UNSET looks like garbage. When changing
  194. # this signature update website/sphinx/template.rst too.
  195. def __init__(self, template_string, name="<string>", loader=None,
  196. compress_whitespace=_UNSET, autoescape=_UNSET,
  197. whitespace=None):
  198. """Construct a Template.
  199. :arg str template_string: the contents of the template file.
  200. :arg str name: the filename from which the template was loaded
  201. (used for error message).
  202. :arg tornado.template.BaseLoader loader: the `~tornado.template.BaseLoader` responsible
  203. for this template, used to resolve ``{% include %}`` and ``{% extend %}`` directives.
  204. :arg bool compress_whitespace: Deprecated since Tornado 4.3.
  205. Equivalent to ``whitespace="single"`` if true and
  206. ``whitespace="all"`` if false.
  207. :arg str autoescape: The name of a function in the template
  208. namespace, or ``None`` to disable escaping by default.
  209. :arg str whitespace: A string specifying treatment of whitespace;
  210. see `filter_whitespace` for options.
  211. .. versionchanged:: 4.3
  212. Added ``whitespace`` parameter; deprecated ``compress_whitespace``.
  213. """
  214. self.name = escape.native_str(name)
  215. if compress_whitespace is not _UNSET:
  216. # Convert deprecated compress_whitespace (bool) to whitespace (str).
  217. if whitespace is not None:
  218. raise Exception("cannot set both whitespace and compress_whitespace")
  219. whitespace = "single" if compress_whitespace else "all"
  220. if whitespace is None:
  221. if loader and loader.whitespace:
  222. whitespace = loader.whitespace
  223. else:
  224. # Whitespace defaults by filename.
  225. if name.endswith(".html") or name.endswith(".js"):
  226. whitespace = "single"
  227. else:
  228. whitespace = "all"
  229. # Validate the whitespace setting.
  230. filter_whitespace(whitespace, '')
  231. if autoescape is not _UNSET:
  232. self.autoescape = autoescape
  233. elif loader:
  234. self.autoescape = loader.autoescape
  235. else:
  236. self.autoescape = _DEFAULT_AUTOESCAPE
  237. self.namespace = loader.namespace if loader else {}
  238. reader = _TemplateReader(name, escape.native_str(template_string),
  239. whitespace)
  240. self.file = _File(self, _parse(reader, self))
  241. self.code = self._generate_python(loader)
  242. self.loader = loader
  243. try:
  244. # Under python2.5, the fake filename used here must match
  245. # the module name used in __name__ below.
  246. # The dont_inherit flag prevents template.py's future imports
  247. # from being applied to the generated code.
  248. self.compiled = compile(
  249. escape.to_unicode(self.code),
  250. "%s.generated.py" % self.name.replace('.', '_'),
  251. "exec", dont_inherit=True)
  252. except Exception:
  253. formatted_code = _format_code(self.code).rstrip()
  254. app_log.error("%s code:\n%s", self.name, formatted_code)
  255. raise
  256. def generate(self, **kwargs):
  257. """Generate this template with the given arguments."""
  258. namespace = {
  259. "escape": escape.xhtml_escape,
  260. "xhtml_escape": escape.xhtml_escape,
  261. "url_escape": escape.url_escape,
  262. "json_encode": escape.json_encode,
  263. "squeeze": escape.squeeze,
  264. "linkify": escape.linkify,
  265. "datetime": datetime,
  266. "_tt_utf8": escape.utf8, # for internal use
  267. "_tt_string_types": (unicode_type, bytes),
  268. # __name__ and __loader__ allow the traceback mechanism to find
  269. # the generated source code.
  270. "__name__": self.name.replace('.', '_'),
  271. "__loader__": ObjectDict(get_source=lambda name: self.code),
  272. }
  273. namespace.update(self.namespace)
  274. namespace.update(kwargs)
  275. exec_in(self.compiled, namespace)
  276. execute = namespace["_tt_execute"]
  277. # Clear the traceback module's cache of source data now that
  278. # we've generated a new template (mainly for this module's
  279. # unittests, where different tests reuse the same name).
  280. linecache.clearcache()
  281. return execute()
  282. def _generate_python(self, loader):
  283. buffer = StringIO()
  284. try:
  285. # named_blocks maps from names to _NamedBlock objects
  286. named_blocks = {}
  287. ancestors = self._get_ancestors(loader)
  288. ancestors.reverse()
  289. for ancestor in ancestors:
  290. ancestor.find_named_blocks(loader, named_blocks)
  291. writer = _CodeWriter(buffer, named_blocks, loader,
  292. ancestors[0].template)
  293. ancestors[0].generate(writer)
  294. return buffer.getvalue()
  295. finally:
  296. buffer.close()
  297. def _get_ancestors(self, loader):
  298. ancestors = [self.file]
  299. for chunk in self.file.body.chunks:
  300. if isinstance(chunk, _ExtendsBlock):
  301. if not loader:
  302. raise ParseError("{% extends %} block found, but no "
  303. "template loader")
  304. template = loader.load(chunk.name, self.name)
  305. ancestors.extend(template._get_ancestors(loader))
  306. return ancestors
  307. class BaseLoader(object):
  308. """Base class for template loaders.
  309. You must use a template loader to use template constructs like
  310. ``{% extends %}`` and ``{% include %}``. The loader caches all
  311. templates after they are loaded the first time.
  312. """
  313. def __init__(self, autoescape=_DEFAULT_AUTOESCAPE, namespace=None,
  314. whitespace=None):
  315. """Construct a template loader.
  316. :arg str autoescape: The name of a function in the template
  317. namespace, such as "xhtml_escape", or ``None`` to disable
  318. autoescaping by default.
  319. :arg dict namespace: A dictionary to be added to the default template
  320. namespace, or ``None``.
  321. :arg str whitespace: A string specifying default behavior for
  322. whitespace in templates; see `filter_whitespace` for options.
  323. Default is "single" for files ending in ".html" and ".js" and
  324. "all" for other files.
  325. .. versionchanged:: 4.3
  326. Added ``whitespace`` parameter.
  327. """
  328. self.autoescape = autoescape
  329. self.namespace = namespace or {}
  330. self.whitespace = whitespace
  331. self.templates = {}
  332. # self.lock protects self.templates. It's a reentrant lock
  333. # because templates may load other templates via `include` or
  334. # `extends`. Note that thanks to the GIL this code would be safe
  335. # even without the lock, but could lead to wasted work as multiple
  336. # threads tried to compile the same template simultaneously.
  337. self.lock = threading.RLock()
  338. def reset(self):
  339. """Resets the cache of compiled templates."""
  340. with self.lock:
  341. self.templates = {}
  342. def resolve_path(self, name, parent_path=None):
  343. """Converts a possibly-relative path to absolute (used internally)."""
  344. raise NotImplementedError()
  345. def load(self, name, parent_path=None):
  346. """Loads a template."""
  347. name = self.resolve_path(name, parent_path=parent_path)
  348. with self.lock:
  349. if name not in self.templates:
  350. self.templates[name] = self._create_template(name)
  351. return self.templates[name]
  352. def _create_template(self, name):
  353. raise NotImplementedError()
  354. class Loader(BaseLoader):
  355. """A template loader that loads from a single root directory.
  356. """
  357. def __init__(self, root_directory, **kwargs):
  358. super(Loader, self).__init__(**kwargs)
  359. self.root = os.path.abspath(root_directory)
  360. def resolve_path(self, name, parent_path=None):
  361. if parent_path and not parent_path.startswith("<") and \
  362. not parent_path.startswith("/") and \
  363. not name.startswith("/"):
  364. current_path = os.path.join(self.root, parent_path)
  365. file_dir = os.path.dirname(os.path.abspath(current_path))
  366. relative_path = os.path.abspath(os.path.join(file_dir, name))
  367. if relative_path.startswith(self.root):
  368. name = relative_path[len(self.root) + 1:]
  369. return name
  370. def _create_template(self, name):
  371. path = os.path.join(self.root, name)
  372. with open(path, "rb") as f:
  373. template = Template(f.read(), name=name, loader=self)
  374. return template
  375. class DictLoader(BaseLoader):
  376. """A template loader that loads from a dictionary."""
  377. def __init__(self, dict, **kwargs):
  378. super(DictLoader, self).__init__(**kwargs)
  379. self.dict = dict
  380. def resolve_path(self, name, parent_path=None):
  381. if parent_path and not parent_path.startswith("<") and \
  382. not parent_path.startswith("/") and \
  383. not name.startswith("/"):
  384. file_dir = posixpath.dirname(parent_path)
  385. name = posixpath.normpath(posixpath.join(file_dir, name))
  386. return name
  387. def _create_template(self, name):
  388. return Template(self.dict[name], name=name, loader=self)
  389. class _Node(object):
  390. def each_child(self):
  391. return ()
  392. def generate(self, writer):
  393. raise NotImplementedError()
  394. def find_named_blocks(self, loader, named_blocks):
  395. for child in self.each_child():
  396. child.find_named_blocks(loader, named_blocks)
  397. class _File(_Node):
  398. def __init__(self, template, body):
  399. self.template = template
  400. self.body = body
  401. self.line = 0
  402. def generate(self, writer):
  403. writer.write_line("def _tt_execute():", self.line)
  404. with writer.indent():
  405. writer.write_line("_tt_buffer = []", self.line)
  406. writer.write_line("_tt_append = _tt_buffer.append", self.line)
  407. self.body.generate(writer)
  408. writer.write_line("return _tt_utf8('').join(_tt_buffer)", self.line)
  409. def each_child(self):
  410. return (self.body,)
  411. class _ChunkList(_Node):
  412. def __init__(self, chunks):
  413. self.chunks = chunks
  414. def generate(self, writer):
  415. for chunk in self.chunks:
  416. chunk.generate(writer)
  417. def each_child(self):
  418. return self.chunks
  419. class _NamedBlock(_Node):
  420. def __init__(self, name, body, template, line):
  421. self.name = name
  422. self.body = body
  423. self.template = template
  424. self.line = line
  425. def each_child(self):
  426. return (self.body,)
  427. def generate(self, writer):
  428. block = writer.named_blocks[self.name]
  429. with writer.include(block.template, self.line):
  430. block.body.generate(writer)
  431. def find_named_blocks(self, loader, named_blocks):
  432. named_blocks[self.name] = self
  433. _Node.find_named_blocks(self, loader, named_blocks)
  434. class _ExtendsBlock(_Node):
  435. def __init__(self, name):
  436. self.name = name
  437. class _IncludeBlock(_Node):
  438. def __init__(self, name, reader, line):
  439. self.name = name
  440. self.template_name = reader.name
  441. self.line = line
  442. def find_named_blocks(self, loader, named_blocks):
  443. included = loader.load(self.name, self.template_name)
  444. included.file.find_named_blocks(loader, named_blocks)
  445. def generate(self, writer):
  446. included = writer.loader.load(self.name, self.template_name)
  447. with writer.include(included, self.line):
  448. included.file.body.generate(writer)
  449. class _ApplyBlock(_Node):
  450. def __init__(self, method, line, body=None):
  451. self.method = method
  452. self.line = line
  453. self.body = body
  454. def each_child(self):
  455. return (self.body,)
  456. def generate(self, writer):
  457. method_name = "_tt_apply%d" % writer.apply_counter
  458. writer.apply_counter += 1
  459. writer.write_line("def %s():" % method_name, self.line)
  460. with writer.indent():
  461. writer.write_line("_tt_buffer = []", self.line)
  462. writer.write_line("_tt_append = _tt_buffer.append", self.line)
  463. self.body.generate(writer)
  464. writer.write_line("return _tt_utf8('').join(_tt_buffer)", self.line)
  465. writer.write_line("_tt_append(_tt_utf8(%s(%s())))" % (
  466. self.method, method_name), self.line)
  467. class _ControlBlock(_Node):
  468. def __init__(self, statement, line, body=None):
  469. self.statement = statement
  470. self.line = line
  471. self.body = body
  472. def each_child(self):
  473. return (self.body,)
  474. def generate(self, writer):
  475. writer.write_line("%s:" % self.statement, self.line)
  476. with writer.indent():
  477. self.body.generate(writer)
  478. # Just in case the body was empty
  479. writer.write_line("pass", self.line)
  480. class _IntermediateControlBlock(_Node):
  481. def __init__(self, statement, line):
  482. self.statement = statement
  483. self.line = line
  484. def generate(self, writer):
  485. # In case the previous block was empty
  486. writer.write_line("pass", self.line)
  487. writer.write_line("%s:" % self.statement, self.line, writer.indent_size() - 1)
  488. class _Statement(_Node):
  489. def __init__(self, statement, line):
  490. self.statement = statement
  491. self.line = line
  492. def generate(self, writer):
  493. writer.write_line(self.statement, self.line)
  494. class _Expression(_Node):
  495. def __init__(self, expression, line, raw=False):
  496. self.expression = expression
  497. self.line = line
  498. self.raw = raw
  499. def generate(self, writer):
  500. writer.write_line("_tt_tmp = %s" % self.expression, self.line)
  501. writer.write_line("if isinstance(_tt_tmp, _tt_string_types):"
  502. " _tt_tmp = _tt_utf8(_tt_tmp)", self.line)
  503. writer.write_line("else: _tt_tmp = _tt_utf8(str(_tt_tmp))", self.line)
  504. if not self.raw and writer.current_template.autoescape is not None:
  505. # In python3 functions like xhtml_escape return unicode,
  506. # so we have to convert to utf8 again.
  507. writer.write_line("_tt_tmp = _tt_utf8(%s(_tt_tmp))" %
  508. writer.current_template.autoescape, self.line)
  509. writer.write_line("_tt_append(_tt_tmp)", self.line)
  510. class _Module(_Expression):
  511. def __init__(self, expression, line):
  512. super(_Module, self).__init__("_tt_modules." + expression, line,
  513. raw=True)
  514. class _Text(_Node):
  515. def __init__(self, value, line, whitespace):
  516. self.value = value
  517. self.line = line
  518. self.whitespace = whitespace
  519. def generate(self, writer):
  520. value = self.value
  521. # Compress whitespace if requested, with a crude heuristic to avoid
  522. # altering preformatted whitespace.
  523. if "<pre>" not in value:
  524. value = filter_whitespace(self.whitespace, value)
  525. if value:
  526. writer.write_line('_tt_append(%r)' % escape.utf8(value), self.line)
  527. class ParseError(Exception):
  528. """Raised for template syntax errors.
  529. ``ParseError`` instances have ``filename`` and ``lineno`` attributes
  530. indicating the position of the error.
  531. .. versionchanged:: 4.3
  532. Added ``filename`` and ``lineno`` attributes.
  533. """
  534. def __init__(self, message, filename=None, lineno=0):
  535. self.message = message
  536. # The names "filename" and "lineno" are chosen for consistency
  537. # with python SyntaxError.
  538. self.filename = filename
  539. self.lineno = lineno
  540. def __str__(self):
  541. return '%s at %s:%d' % (self.message, self.filename, self.lineno)
  542. class _CodeWriter(object):
  543. def __init__(self, file, named_blocks, loader, current_template):
  544. self.file = file
  545. self.named_blocks = named_blocks
  546. self.loader = loader
  547. self.current_template = current_template
  548. self.apply_counter = 0
  549. self.include_stack = []
  550. self._indent = 0
  551. def indent_size(self):
  552. return self._indent
  553. def indent(self):
  554. class Indenter(object):
  555. def __enter__(_):
  556. self._indent += 1
  557. return self
  558. def __exit__(_, *args):
  559. assert self._indent > 0
  560. self._indent -= 1
  561. return Indenter()
  562. def include(self, template, line):
  563. self.include_stack.append((self.current_template, line))
  564. self.current_template = template
  565. class IncludeTemplate(object):
  566. def __enter__(_):
  567. return self
  568. def __exit__(_, *args):
  569. self.current_template = self.include_stack.pop()[0]
  570. return IncludeTemplate()
  571. def write_line(self, line, line_number, indent=None):
  572. if indent is None:
  573. indent = self._indent
  574. line_comment = ' # %s:%d' % (self.current_template.name, line_number)
  575. if self.include_stack:
  576. ancestors = ["%s:%d" % (tmpl.name, lineno)
  577. for (tmpl, lineno) in self.include_stack]
  578. line_comment += ' (via %s)' % ', '.join(reversed(ancestors))
  579. print(" " * indent + line + line_comment, file=self.file)
  580. class _TemplateReader(object):
  581. def __init__(self, name, text, whitespace):
  582. self.name = name
  583. self.text = text
  584. self.whitespace = whitespace
  585. self.line = 1
  586. self.pos = 0
  587. def find(self, needle, start=0, end=None):
  588. assert start >= 0, start
  589. pos = self.pos
  590. start += pos
  591. if end is None:
  592. index = self.text.find(needle, start)
  593. else:
  594. end += pos
  595. assert end >= start
  596. index = self.text.find(needle, start, end)
  597. if index != -1:
  598. index -= pos
  599. return index
  600. def consume(self, count=None):
  601. if count is None:
  602. count = len(self.text) - self.pos
  603. newpos = self.pos + count
  604. self.line += self.text.count("\n", self.pos, newpos)
  605. s = self.text[self.pos:newpos]
  606. self.pos = newpos
  607. return s
  608. def remaining(self):
  609. return len(self.text) - self.pos
  610. def __len__(self):
  611. return self.remaining()
  612. def __getitem__(self, key):
  613. if type(key) is slice:
  614. size = len(self)
  615. start, stop, step = key.indices(size)
  616. if start is None:
  617. start = self.pos
  618. else:
  619. start += self.pos
  620. if stop is not None:
  621. stop += self.pos
  622. return self.text[slice(start, stop, step)]
  623. elif key < 0:
  624. return self.text[key]
  625. else:
  626. return self.text[self.pos + key]
  627. def __str__(self):
  628. return self.text[self.pos:]
  629. def raise_parse_error(self, msg):
  630. raise ParseError(msg, self.name, self.line)
  631. def _format_code(code):
  632. lines = code.splitlines()
  633. format = "%%%dd %%s\n" % len(repr(len(lines) + 1))
  634. return "".join([format % (i + 1, line) for (i, line) in enumerate(lines)])
  635. def _parse(reader, template, in_block=None, in_loop=None):
  636. body = _ChunkList([])
  637. while True:
  638. # Find next template directive
  639. curly = 0
  640. while True:
  641. curly = reader.find("{", curly)
  642. if curly == -1 or curly + 1 == reader.remaining():
  643. # EOF
  644. if in_block:
  645. reader.raise_parse_error(
  646. "Missing {%% end %%} block for %s" % in_block)
  647. body.chunks.append(_Text(reader.consume(), reader.line,
  648. reader.whitespace))
  649. return body
  650. # If the first curly brace is not the start of a special token,
  651. # start searching from the character after it
  652. if reader[curly + 1] not in ("{", "%", "#"):
  653. curly += 1
  654. continue
  655. # When there are more than 2 curlies in a row, use the
  656. # innermost ones. This is useful when generating languages
  657. # like latex where curlies are also meaningful
  658. if (curly + 2 < reader.remaining() and
  659. reader[curly + 1] == '{' and reader[curly + 2] == '{'):
  660. curly += 1
  661. continue
  662. break
  663. # Append any text before the special token
  664. if curly > 0:
  665. cons = reader.consume(curly)
  666. body.chunks.append(_Text(cons, reader.line,
  667. reader.whitespace))
  668. start_brace = reader.consume(2)
  669. line = reader.line
  670. # Template directives may be escaped as "{{!" or "{%!".
  671. # In this case output the braces and consume the "!".
  672. # This is especially useful in conjunction with jquery templates,
  673. # which also use double braces.
  674. if reader.remaining() and reader[0] == "!":
  675. reader.consume(1)
  676. body.chunks.append(_Text(start_brace, line,
  677. reader.whitespace))
  678. continue
  679. # Comment
  680. if start_brace == "{#":
  681. end = reader.find("#}")
  682. if end == -1:
  683. reader.raise_parse_error("Missing end comment #}")
  684. contents = reader.consume(end).strip()
  685. reader.consume(2)
  686. continue
  687. # Expression
  688. if start_brace == "{{":
  689. end = reader.find("}}")
  690. if end == -1:
  691. reader.raise_parse_error("Missing end expression }}")
  692. contents = reader.consume(end).strip()
  693. reader.consume(2)
  694. if not contents:
  695. reader.raise_parse_error("Empty expression")
  696. body.chunks.append(_Expression(contents, line))
  697. continue
  698. # Block
  699. assert start_brace == "{%", start_brace
  700. end = reader.find("%}")
  701. if end == -1:
  702. reader.raise_parse_error("Missing end block %}")
  703. contents = reader.consume(end).strip()
  704. reader.consume(2)
  705. if not contents:
  706. reader.raise_parse_error("Empty block tag ({% %})")
  707. operator, space, suffix = contents.partition(" ")
  708. suffix = suffix.strip()
  709. # Intermediate ("else", "elif", etc) blocks
  710. intermediate_blocks = {
  711. "else": set(["if", "for", "while", "try"]),
  712. "elif": set(["if"]),
  713. "except": set(["try"]),
  714. "finally": set(["try"]),
  715. }
  716. allowed_parents = intermediate_blocks.get(operator)
  717. if allowed_parents is not None:
  718. if not in_block:
  719. reader.raise_parse_error("%s outside %s block" %
  720. (operator, allowed_parents))
  721. if in_block not in allowed_parents:
  722. reader.raise_parse_error(
  723. "%s block cannot be attached to %s block" %
  724. (operator, in_block))
  725. body.chunks.append(_IntermediateControlBlock(contents, line))
  726. continue
  727. # End tag
  728. elif operator == "end":
  729. if not in_block:
  730. reader.raise_parse_error("Extra {% end %} block")
  731. return body
  732. elif operator in ("extends", "include", "set", "import", "from",
  733. "comment", "autoescape", "whitespace", "raw",
  734. "module"):
  735. if operator == "comment":
  736. continue
  737. if operator == "extends":
  738. suffix = suffix.strip('"').strip("'")
  739. if not suffix:
  740. reader.raise_parse_error("extends missing file path")
  741. block = _ExtendsBlock(suffix)
  742. elif operator in ("import", "from"):
  743. if not suffix:
  744. reader.raise_parse_error("import missing statement")
  745. block = _Statement(contents, line)
  746. elif operator == "include":
  747. suffix = suffix.strip('"').strip("'")
  748. if not suffix:
  749. reader.raise_parse_error("include missing file path")
  750. block = _IncludeBlock(suffix, reader, line)
  751. elif operator == "set":
  752. if not suffix:
  753. reader.raise_parse_error("set missing statement")
  754. block = _Statement(suffix, line)
  755. elif operator == "autoescape":
  756. fn = suffix.strip()
  757. if fn == "None":
  758. fn = None
  759. template.autoescape = fn
  760. continue
  761. elif operator == "whitespace":
  762. mode = suffix.strip()
  763. # Validate the selected mode
  764. filter_whitespace(mode, '')
  765. reader.whitespace = mode
  766. continue
  767. elif operator == "raw":
  768. block = _Expression(suffix, line, raw=True)
  769. elif operator == "module":
  770. block = _Module(suffix, line)
  771. body.chunks.append(block)
  772. continue
  773. elif operator in ("apply", "block", "try", "if", "for", "while"):
  774. # parse inner body recursively
  775. if operator in ("for", "while"):
  776. block_body = _parse(reader, template, operator, operator)
  777. elif operator == "apply":
  778. # apply creates a nested function so syntactically it's not
  779. # in the loop.
  780. block_body = _parse(reader, template, operator, None)
  781. else:
  782. block_body = _parse(reader, template, operator, in_loop)
  783. if operator == "apply":
  784. if not suffix:
  785. reader.raise_parse_error("apply missing method name")
  786. block = _ApplyBlock(suffix, line, block_body)
  787. elif operator == "block":
  788. if not suffix:
  789. reader.raise_parse_error("block missing name")
  790. block = _NamedBlock(suffix, block_body, template, line)
  791. else:
  792. block = _ControlBlock(contents, line, block_body)
  793. body.chunks.append(block)
  794. continue
  795. elif operator in ("break", "continue"):
  796. if not in_loop:
  797. reader.raise_parse_error("%s outside %s block" %
  798. (operator, set(["for", "while"])))
  799. body.chunks.append(_Statement(contents, line))
  800. continue
  801. else:
  802. reader.raise_parse_error("unknown operator: %r" % operator)