def parse_highlight(self): """Highlights unit placeables.""" highlights = highlight_string(self.value, self.unit) for start, end, _content in highlights: self.tags[start].append( '<span class="hlcheck"><span class="highlight-number"></span>') self.tags[end].insert(0, "</span>")
def test_simple(self): self.assertEqual( highlight_string( "simple {format} string", MockUnit(flags="python-brace-format") ), [(7, 15, "{format}")], )
def test_overlap(self): self.assertEqual( highlight_string( 'nested <a href="{format}">string</a>', MockUnit(flags="python-brace-format"), ), [(7, 26, '<a href="{format}">'), (32, 36, "</a>")], )
def test_multi(self): self.assertEqual( highlight_string( "simple {format} %d string", MockUnit(flags="python-brace-format, python-format"), ), [(7, 15, "{format}"), (16, 18, "%d")], )
def test_overlap(self): unit = MockUnit( source='nested <a href="{format}">string</a>', flags="python-brace-format", ) self.assertEqual( highlight_string(unit.source, unit), [(7, 26, '<a href="{format}">'), (32, 36, "</a>")], )
def test_multi(self): unit = MockUnit( source="simple {format} %d string", flags="python-brace-format, python-format", ) self.assertEqual( highlight_string(unit.source, unit), [(7, 15, "{format}"), (16, 18, "%d")], )
def test_simple(self): unit = MockUnit( source="simple {format} string", flags="python-brace-format", ) self.assertEqual( highlight_string(unit.source, unit), [(7, 15, "{format}")], )
def parse_highlight(self): """Highlights unit placeables.""" highlights = highlight_string(self.value, self.unit) cleaned_value = list(self.value) for start, end, content in highlights: self.tags[start].append(format_html(HLCHECK, content)) self.tags[end].insert(0, "</span>") cleaned_value[start:end] = [" "] * (end - start) # Prepare cleaned up value for glossary terms (we do not want to extract those # from format strings) self.cleaned_value = "".join(cleaned_value)
def test_syntax(self): unit = MockUnit( source="Text with a `link <https://www.sphinx-doc.org>`_.", flags="rst-text", ) self.assertEqual( highlight_string(unit.source, unit, hightlight_syntax=True), [(12, 13, "`"), (18, 46, "<https://www.sphinx-doc.org>"), (46, 48, "`_")], ) self.assertEqual( highlight_string( "Hello `world <https://weblate.org>`_", unit, hightlight_syntax=True ), [(6, 7, "`"), (13, 34, "<https://weblate.org>"), (34, 36, "`_")], ) self.assertEqual( highlight_string("Hello **world**", unit, hightlight_syntax=True), [(6, 8, "**"), (13, 15, "**")], ) self.assertEqual( highlight_string("Hello *world*", unit, hightlight_syntax=True), [(6, 7, "*"), (12, 13, "*")], )
def fmt_highlights(raw_value, value, unit): """Format check highlights.""" if unit is None: return value highlights = highlight_string(raw_value, unit) start_search = 0 for highlight in highlights: htext = escape(highlight[2]) find_highlight = value.find(htext, start_search) if find_highlight >= 0: newpart = HL_CHECK.format(htext) next_part = value[(find_highlight + len(htext)):] value = value[:find_highlight] + newpart + next_part start_search = find_highlight + len(newpart) return value
def cleanup_text(self, unit): """Removes placeholder to avoid confusing the machine translation.""" text = unit.get_source_plurals()[0] replacements = {} if not self.do_cleanup: return text, replacements highlights = highlight_string(text, unit) parts = [] start = 0 for h_start, h_end, h_text in highlights: parts.append(text[start:h_start]) placeholder = f"[{h_start}]" replacements[placeholder] = h_text parts.append(placeholder) start = h_end parts.append(text[start:]) return "".join(parts), replacements
def cleanup_text(self, unit): """Removes placeholder to avoid confusing the machine translation.""" text = unit.source_string replacements = {} if not self.do_cleanup: return text, replacements highlights = highlight_string(text, unit, hightlight_syntax=self.hightlight_syntax) parts = [] start = 0 for h_start, h_end, h_text in highlights: parts.append(self.escape_text(text[start:h_start])) h_text = self.escape_text(h_text) placeholder = self.format_replacement(h_start, h_end, h_text) replacements[placeholder] = h_text parts.append(placeholder) start = h_end parts.append(self.escape_text(text[start:])) return "".join(parts), replacements