Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_qname(self, name):
"""
Return a `QName` for a name.
:return: qname
"""
if ':' in name:
ns, localname = name.split(':')
name = self.get_namespace(ns)
if name is None:
self.fail('The namespace alias %s is unknown' % ns)
else:
localname = name
name = self.get_namespace('')
return QName(u'%s}%s' % (name, localname))
def block_process(self, events):
for event in events:
type, data, pos = event
if type == START:
self.enter(pos, *data)
elif type == END:
self.leave(pos, data)
elif type == TEXT:
if self._context is not None and data.strip():
tag = QName(self._context)
self.append(START, (QName(tag), Attrs()), pos)
self.append(type, data, pos)
self.append(END, tag, pos)
else:
self.append(type, data, pos)
else:
self.append(type, data, pos)
else:
total = total.strip()
if total == '[':
level += 1
elif total == ']':
level -= 1
pos = match.end()
# look up directive attribute and create tag and attr
end_pos = len(line)
attr_name = self.standalone_directives[directive_name]
attr_value = ' '.join(directive_buffer[:-1]).strip()
if attr_name is None and attr_value:
self.fail('directive %r accepts no arguments' %
directive_name)
attr = Attrs(((QName(attr_name), attr_value),))
tag = QName('http://genshi.edgewall.org/}' + directive_name)
# yield the start tag
self.add((START, (tag, attr), directive_pos))
# enqueue the tag so that it's closed for us when the stack is left.
self.enqueue((END, tag, self.make_pos(pos)))
# is there data left on the line? go back to the line parser!
if pos < end_pos:
self.parse_line(pos)
while 1:
match = endprog.search(line, pos)
if match is not None:
pos = match.end()
break
line = self.read_line(False)
pos = self.offset
# we have a regular match here, just count the braces.
else:
total = total.strip()
if total == '[':
level += 1
elif total == ']':
level -= 1
pos = match.end()
attrs.append((QName('http://genshi.edgewall.org/}' +
directive_name),
' '.join(directive_buffer[:-1])))
continue
end_pos = len(line)
pos = end_pos - len(line[pos:].lstrip())
break
# add special attributes class and id and set the namespaces.
if classes:
attrs.append((self.get_qname('class'), u' '.join(classes)))
if ident is not None:
attrs.append((self.get_qname('id'), ident))
for name, uri in xmlns.iteritems():
self.set_namespace(name, uri)
def _handle_start(self, tag, attrib):
attrs = Attrs([(QName(name), value) for name, value in
zip(*[iter(attrib)] * 2)])
self._enqueue(START, (QName(tag), attrs))
data = source.read(bufsize)
if not data: # end of data
self.close()
done = True
else:
if not isinstance(data, str):
raise UnicodeError("source returned bytes, but no encoding specified")
self.feed(data)
for kind, data, pos in self._queue:
yield kind, data, pos
self._queue = []
if done:
open_tags = self._open_tags
open_tags.reverse()
for tag in open_tags:
yield END, QName(tag), pos
break
except html.HTMLParseError as e:
msg = '%s: line %d, column %d' % (e.msg, e.lineno, e.offset)
raise ParseError(msg, self.filename, e.lineno, e.offset)
return Stream(_generate()).filter(_coalesce)
def _handle_start(self, tag, attrib):
attrs = Attrs([(QName(name), value) for name, value in
zip(*[iter(attrib)] * 2)])
self._enqueue(START, (QName(tag), attrs))
def block_process(self, events):
for event in events:
type, data, pos = event
if type == START:
self.enter(pos, *data)
elif type == END:
self.leave(pos, data)
elif type == TEXT:
if self._context is not None and data.strip():
tag = QName(self._context)
self.append(START, (QName(tag), Attrs()), pos)
self.append(type, data, pos)
self.append(END, tag, pos)
else:
self.append(type, data, pos)
else:
self.append(type, data, pos)
<p>Hallo, Hans</p>
Note that elements defining ``xml:lang`` attributes that do not contain
variable expressions are ignored by this filter. That can be used to
exclude specific parts of a template from being extracted and translated.
"""
directives = [
('comment', CommentDirective),
('msg', MsgDirective)
]
IGNORE_TAGS = frozenset([
QName('script'), QName('http://www.w3.org/1999/xhtml}script'),
QName('style'), QName('http://www.w3.org/1999/xhtml}style')
])
INCLUDE_ATTRS = frozenset(['abbr', 'alt', 'label', 'prompt', 'standby',
'summary', 'title'])
NAMESPACE = I18N_NAMESPACE
def __init__(self, translate=NullTranslations(), ignore_tags=IGNORE_TAGS,
include_attrs=INCLUDE_ATTRS, extract_text=True):
"""Initialize the translator.
:param translate: the translation function, for example ``gettext`` or
``ugettext``.
:param ignore_tags: a set of tag names that should not be localized
:param include_attrs: a set of attribute names should be localized
:param extract_text: whether the content of text nodes should be
extracted, or only text in explicit ``gettext``