Code

Ticket #2594: template_whitespace_ticket_2594.diff

File template_whitespace_ticket_2594.diff, 3.3 KB (added by jshedd, 4 years ago)

Causes template tag blocks to collapse the whitespace around them when they are on their own line.

Line 
1Index: template/__init__.py
2===================================================================
3--- template/__init__.py        (revision 12122)
4+++ template/__init__.py        (working copy)
5@@ -90,9 +90,13 @@
6 UNKNOWN_SOURCE="<unknown source>"
7 
8 # match a variable or block tag and capture the entire tag, including start/end delimiters
9-tag_re = re.compile('(%s.*?%s|%s.*?%s|%s.*?%s)' % (re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END),
10-                                          re.escape(VARIABLE_TAG_START), re.escape(VARIABLE_TAG_END),
11-                                          re.escape(COMMENT_TAG_START), re.escape(COMMENT_TAG_END)))
12+tag_re = re.compile('([\n\r\v\f]+[ \t]*?%s[^\n\r\v\f%s%s]*?%s(?=[ \t]*?[\n\r\v\f]+)|%s.*?%s|%s.*?%s|%s.*?%s)' % (
13+                        re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END), re.escape(BLOCK_TAG_END),
14+                        re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END),                       
15+                        re.escape(VARIABLE_TAG_START), re.escape(VARIABLE_TAG_END),
16+                        re.escape(COMMENT_TAG_START), re.escape(COMMENT_TAG_END)))
17+# find the command within a block tag
18+block_command_re = re.compile('%s(?P<token_string>.*?)%s' % (re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END)))
19 
20 # global dictionary of libraries that have been loaded using get_library
21 libraries = {}
22@@ -207,7 +211,7 @@
23             self.contents[:20].replace('\n', ''))
24 
25     def split_contents(self):
26-        split = []
27+        split = []             
28         bits = iter(smart_split(self.contents))
29         for bit in bits:
30             # Handle translation-marked template pieces
31@@ -230,7 +234,7 @@
32         "Return a list of tokens from a given template_string."
33         in_tag = False
34         result = []
35-        for bit in tag_re.split(self.template_string):
36+        for bit in tag_re.split(self.template_string):
37             if bit:
38                 result.append(self.create_token(bit, in_tag))
39             in_tag = not in_tag
40@@ -241,12 +245,14 @@
41         Convert the given token string into a new Token object and return it.
42         If in_tag is True, we are processing something that matched a tag,
43         otherwise it should be treated as a literal string.
44-        """
45+        """               
46         if in_tag:
47             if token_string.startswith(VARIABLE_TAG_START):
48                 token = Token(TOKEN_VAR, token_string[len(VARIABLE_TAG_START):-len(VARIABLE_TAG_END)].strip())
49-            elif token_string.startswith(BLOCK_TAG_START):
50-                token = Token(TOKEN_BLOCK, token_string[len(BLOCK_TAG_START):-len(BLOCK_TAG_END)].strip())
51+            elif token_string.lstrip().startswith(BLOCK_TAG_START):
52+                token_struct = block_command_re.search(token_string)
53+                token_string = token_struct.group('token_string')
54+                token = Token(TOKEN_BLOCK, token_string.strip())
55             elif token_string.startswith(COMMENT_TAG_START):
56                 token = Token(TOKEN_COMMENT, '')
57         else:
58@@ -984,4 +990,4 @@
59     builtins.append(get_library(module_name))
60 
61 add_to_builtins('django.template.defaulttags')
62-add_to_builtins('django.template.defaultfilters')
63+add_to_builtins('django.template.defaultfilters')
64\ No newline at end of file