本文整理汇总了Python中weblate.trans.simplediff.html_diff函数的典型用法代码示例。如果您正苦于以下问题:Python html_diff函数的具体用法?Python html_diff怎么用?Python html_diff使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了html_diff函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fmttranslation
def fmttranslation(value, language=None, diff=None):
'''
Formats translation to show whitespace, plural forms or diff.
'''
# Get language
if language is None:
language = Language.objects.get(code='en')
# Split plurals to separate strings
plurals = split_plural(value)
# Split diff plurals
if diff is not None:
diff = split_plural(diff)
# Previous message did not have to be a plural
while len(diff) < len(plurals):
diff.append(diff[0])
# We will collect part for each plural
parts = []
for idx, value in enumerate(plurals):
# HTML escape
value = escape(force_unicode(value))
# Format diff if there is any
if diff is not None:
diffvalue = escape(force_unicode(diff[idx]))
value = html_diff(diffvalue, value)
# Normalize newlines
value = NEWLINES_RE.sub('\n', value)
# Split string
paras = value.split('\n')
# Format whitespace in each paragraph
paras = [fmt_whitespace(p) for p in paras]
# Show label for plural (if there are any)
if len(plurals) > 1:
value = '<span class="pluraltxt">%s</span><br />' % language.get_plural_label(idx)
else:
value = ''
# Join paragraphs
newline = u'<span class="hlspace" title="%s">↵</span><br />' % _('New line')
value += newline.join(paras)
parts.append(value)
value = '<hr />'.join(parts)
return mark_safe(
'<span lang="%s" dir="%s" class="direction">%s</span>' %
(language.code, language.direction, value)
)
示例2: format_translation
def format_translation(value, language=None, diff=None, search_match=None,
simple=False):
"""
Nicely formats translation text possibly handling plurals or diff.
"""
# Get language
if language is None:
language = Language.objects.get_default()
# Split plurals to separate strings
plurals = split_plural(value)
# Newline concatenator
newline = u'<span class="hlspace" title="{0}">↵</span><br />'.format(
_('New line')
)
# Split diff plurals
if diff is not None:
diff = split_plural(diff)
# Previous message did not have to be a plural
while len(diff) < len(plurals):
diff.append(diff[0])
# We will collect part for each plural
parts = []
for idx, value in enumerate(plurals):
# HTML escape
value = escape(force_unicode(value))
# Format diff if there is any
if diff is not None:
diffvalue = escape(force_unicode(diff[idx]))
value = html_diff(diffvalue, value)
# Format search term
if search_match:
# Since the search ignored case, we need to highlight any
# combination of upper and lower case we find. This is too
# advanced for str.replace().
caseless = re.compile(re.escape(search_match), re.IGNORECASE)
for variation in re.findall(caseless, value):
value = re.sub(
caseless,
u'<span class="hlmatch">{0}</span>'.format(variation),
value,
)
# Normalize newlines
value = NEWLINES_RE.sub('\n', value)
# Split string
paras = value.split('\n')
# Format whitespace in each paragraph
paras = [fmt_whitespace(p) for p in paras]
# Show label for plural (if there are any)
title = ''
if len(plurals) > 1:
title = language.get_plural_label(idx)
# Join paragraphs
content = mark_safe(newline.join(paras))
parts.append({'title': title, 'content': content})
return {
'simple': simple,
'items': parts,
'language': language,
}
示例3: fmt_diff
def fmt_diff(value, diff, idx):
"""Format diff if there is any"""
if diff is None:
return value
diffvalue = escape(force_text(diff[idx]))
return html_diff(diffvalue, value)
示例4: test_replace
def test_replace(self):
self.assertEqual(
html_diff('first old text', 'first new text'),
'first <del>old</del><ins>new</ins> text'
)
示例5: test_remove
def test_remove(self):
self.assertEqual(
html_diff('first old text', 'first text'),
'first <del>old </del>text'
)
示例6: test_unicode
def test_unicode(self):
self.assertEqual(
html_diff('zkouška text', 'zkouška nový text'),
'zkouška <ins>nový </ins>text'
)
示例7: test_add
def test_add(self):
self.assertEqual(
html_diff('first text', 'first new text'),
'first <ins>new </ins>text'
)
示例8: test_same
def test_same(self):
self.assertEqual(
html_diff('first text', 'first text'),
'first text'
)
示例9: fmttranslation
def fmttranslation(value, language=None, diff=None, search_match=None):
'''
Formats translation to show whitespace, plural forms or diff.
'''
# Get language
if language is None:
language = Language.objects.get_default()
# Split plurals to separate strings
plurals = split_plural(value)
# Split diff plurals
if diff is not None:
diff = split_plural(diff)
# Previous message did not have to be a plural
while len(diff) < len(plurals):
diff.append(diff[0])
# We will collect part for each plural
parts = []
for idx, value in enumerate(plurals):
# HTML escape
value = escape(force_unicode(value))
# Format diff if there is any
if diff is not None:
diffvalue = escape(force_unicode(diff[idx]))
value = html_diff(diffvalue, value)
# Format search term
if search_match is not None:
# Since the search ignored case, we need to highlight any
# combination of upper and lower case we find. This is too
# advanced for str.replace().
caseless = re.compile(re.escape(search_match), re.IGNORECASE)
for variation in re.findall(caseless, value):
value = re.sub(
caseless,
'<span class="hlmatch">%s</span>' % (variation),
value,
)
# Normalize newlines
value = NEWLINES_RE.sub('\n', value)
# Split string
paras = value.split('\n')
# Format whitespace in each paragraph
paras = [fmt_whitespace(p) for p in paras]
# Show label for plural (if there are any)
if len(plurals) > 1:
value = '<span class="pluraltxt">%s</span><br />' % (
language.get_plural_label(idx)
)
else:
value = ''
# Join paragraphs
newline = u'<span class="hlspace" title="%s">↵</span><br />' % (
_('New line')
)
value += newline.join(paras)
parts.append(value)
value = '<hr />'.join(parts)
return mark_safe(
'<span lang="%s" dir="%s" class="direction">%s</span>' %
(language.code, language.direction, value)
)
示例10: format_translation
def format_translation(value, language, diff=None, search_match=None, simple=False, num_plurals=2, checks=None):
"""
Nicely formats translation text possibly handling plurals or diff.
"""
# Split plurals to separate strings
plurals = split_plural(value)
# Show plurals?
if num_plurals <= 1:
plurals = plurals[:1]
# Newline concatenator
newline = SPACE_NL.format(_("New line"))
# Split diff plurals
if diff is not None:
diff = split_plural(diff)
# Previous message did not have to be a plural
while len(diff) < len(plurals):
diff.append(diff[0])
# We will collect part for each plural
parts = []
for idx, value in enumerate(plurals):
highlights = None
# Find all checks highlight
if checks:
highlights = []
for c in checks:
highlights += c.check_highlight(value, None)
# Sort by order in string
if highlights:
highlights.sort(key=lambda tup: tup[0])
# remove probelmatics ones
for n in xrange(0, len(highlights)):
if n >= len(highlights):
break
elref = highlights[n]
for n2 in xrange(n, len(highlights)):
if n2 >= len(highlights):
break
eltest = highlights[n2]
if eltest[0] >= elref[0] and eltest[0] <= (elref[0] + len(elref[1])):
highlights.pop(n2)
elif eltest[0] > (elref[0] + len(elref[1])):
break
# then transform highlights to escaped html
highlights = [(h[0], escape(force_unicode(h[1]))) for h in highlights]
# HTML escape
value = escape(force_unicode(value))
# Format diff if there is any
if diff is not None:
diffvalue = escape(force_unicode(diff[idx]))
value = html_diff(diffvalue, value)
# Create span for checks highlights
if highlights:
n = 0
for (hidx, htext) in highlights:
p = value.find(htext, n)
if p >= 0:
newpart = u'<span class="hlcheck">{0}</span>'.format(htext)
value = value[:p] + newpart + value[(p + len(htext)) :]
n = p + len(newpart)
# Format search term
if search_match:
# Since the search ignored case, we need to highlight any
# combination of upper and lower case we find. This is too
# advanced for str.replace().
caseless = re.compile(re.escape(search_match), re.IGNORECASE)
for variation in re.findall(caseless, value):
value = re.sub(caseless, u'<span class="hlmatch">{0}</span>'.format(variation), value)
# Normalize newlines
value = NEWLINES_RE.sub("\n", value)
# Split string
paras = value.split("\n")
# Format whitespace in each paragraph
paras = [fmt_whitespace(p) for p in paras]
# Show label for plural (if there are any)
title = ""
if len(plurals) > 1:
title = language.get_plural_label(idx)
# Join paragraphs
content = mark_safe(newline.join(paras))
parts.append({"title": title, "content": content})
return {"simple": simple, "items": parts, "language": language}