Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
new = self.text_split(new_text)
matcher = SequenceMatcher(None, old, new)
def wrap(tag, words):
return self.mark_text(pos, u''.join(words), tag)
for tag, i1, i2, j1, j2 in matcher.get_opcodes():
if tag == 'replace':
wrap('del', old[i1:i2])
wrap('ins', new[j1:j2])
elif tag == 'delete':
wrap('del', old[i1:i2])
elif tag == 'insert':
wrap('ins', new[j1:j2])
else:
self.append(TEXT, u''.join(old[i1:i2]), pos)
new = self.text_split(new_text)
matcher = SequenceMatcher(None, old, new)
def wrap(tag, words):
return self.mark_text(pos, u''.join(words), tag)
for tag, i1, i2, j1, j2 in matcher.get_opcodes():
if tag == 'replace':
wrap('del', old[i1:i2])
wrap('ins', new[j1:j2])
elif tag == 'delete':
wrap('del', old[i1:i2])
elif tag == 'insert':
wrap('ins', new[j1:j2])
else:
self.append(TEXT, u''.join(old[i1:i2]), pos)
def handle_data(self, text):
self._enqueue(TEXT, text)
def ET(element):
"""Convert a given ElementTree element to a markup stream.
:param element: an ElementTree element
:return: a markup stream
"""
tag_name = QName(element.tag.lstrip('{'))
attrs = Attrs([(QName(attr.lstrip('{')), value)
for attr, value in element.items()])
yield START, (tag_name, attrs), (None, -1, -1)
if element.text:
yield TEXT, element.text, (None, -1, -1)
for child in element.getchildren():
for item in ET(child):
yield item
yield END, tag_name, (None, -1, -1)
if element.tail:
yield TEXT, element.tail, (None, -1, -1)
def _generate():
attrs = lc = None
text = []
for ttype, value in tokens:
c = self._get_css_class(ttype)
if c == 'n':
c = ''
if c == lc:
text.append(value)
elif value: # if no value, leave old span open
if text:
yield TEXT, u''.join(text), pos
if attrs:
yield END, span, pos
attrs = None
text = [value]
lc = c
if c:
attrs = Attrs([(class_, c)])
yield START, (span, attrs), pos
if text:
yield TEXT, u''.join(text), pos
if attrs:
yield END, span, pos
return Stream(_generate())
not next or next[0] != END or
next[1] != tag):
yield token
else:
yield self.startTag(namespace, name, converted_attribs)
elif kind == END:
name = data.localname
namespace = data.namespace
if namespace != namespaces["html"] or name not in voidElements:
yield self.endTag(namespace, name)
elif kind == COMMENT:
yield self.comment(data)
elif kind == TEXT:
for token in self.text(data):
yield token
elif kind == DOCTYPE:
yield self.doctype(*data)
elif kind in (XML_NAMESPACE, DOCTYPE, START_NS, END_NS,
START_CDATA, END_CDATA, PI):
pass
else:
yield self.unknown(kind)
def _interpolate_attrs(self, stream):
for kind, data, pos in stream:
if kind is START:
# Record any directive attributes in start tags
tag, attrs = data
new_attrs = []
for name, value in attrs:
if value:
value = list(interpolate(value, self.filepath, pos[1],
pos[2], lookup=self.lookup))
if len(value) == 1 and value[0][0] is TEXT:
value = value[0][1]
new_attrs.append((name, value))
data = tag, Attrs(new_attrs)
yield kind, data, pos
start, end = mo.span(1)
if start > offset:
text = _escape_sub(_escape_repl, source[offset:start])
for kind, data, pos in interpolate(text, self.filepath, lineno,
lookup=self.lookup):
stream.append((kind, data, pos))
lineno += len(text.splitlines())
lineno += len(source[start:end].splitlines())
command, value = mo.group(2, 3)
if command == 'include':
pos = (self.filename, lineno, 0)
value = list(interpolate(value, self.filepath, lineno, 0,
lookup=self.lookup))
if len(value) == 1 and value[0][0] is TEXT:
value = value[0][1]
stream.append((INCLUDE, (value, None, []), pos))
elif command == 'python':
if not self.allow_exec:
raise TemplateSyntaxError('Python code blocks not allowed',
self.filepath, lineno)
try:
suite = Suite(value, self.filepath, lineno,
lookup=self.lookup)
except SyntaxError, err:
raise TemplateSyntaxError(err, self.filepath,
lineno + (err.lineno or 1) - 1)
pos = (self.filename, lineno, 0)
stream.append((EXEC, suite, pos))
_ensure(value), gettext_functions,
search_text=False):
yield lineno, funcname, text, comments
if msgbuf:
msgbuf.append(kind, data, pos)
else:
msg_params = attrs.get(i18n_msg)
if msg_params is not None:
if type(msg_params) is list: # event tuple
msg_params = msg_params[0][1]
msgbuf = MessageBuffer(
msg_params, attrs.get(i18n_comment), pos[1]
)
elif not skip and search_text and kind is TEXT:
if not msgbuf:
text = data.strip()
if text and filter(None, [ch.isalpha() for ch in text]):
yield pos[1], None, text, []
else:
msgbuf.append(kind, data, pos)
elif not skip and msgbuf and kind is END:
msgbuf.append(kind, data, pos)
if not msgbuf.depth:
yield msgbuf.lineno, None, msgbuf.format(), \
filter(None, [msgbuf.comment])
msgbuf = None
elif kind is EXPR or kind is EXEC:
if msgbuf:
if subkind is TEXT:
values.append(subdata)
value = [x for x in values if x is not None]
if not value:
continue
new_attrs.append((name, u''.join(value)))
yield kind, (tag, Attrs(new_attrs)), pos
elif kind is EXPR:
result = _eval_expr(data, ctxt, **vars)
if result is not None:
# First check for a string, otherwise the iterable test
# below succeeds, and the string will be chopped up into
# individual characters
if isinstance(result, basestring):
yield TEXT, result, pos
elif isinstance(result, (int, float, long)):
yield TEXT, number_conv(result), pos
elif hasattr(result, '__iter__'):
substream = _ensure(result)
for filter_ in filters:
substream = filter_(substream, ctxt, **vars)
for event in substream:
yield event
else:
yield TEXT, unicode(result), pos
else:
yield kind, data, pos