File indexing completed on 2024-03-24 17:21:47

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Unfuzzy messages fuzzied only due to some tags being closed in-place
0005 (like C{<br>} to C{<br/>}).
0006 
0007 Documented in C{doc/user/sieving.docbook}.
0008 
0009 @author: Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net>
0010 @license: GPLv3
0011 """
0012 
0013 import re
0014 
0015 from pology import _, n_
0016 from pology.report import report
0017 
0018 
0019 def setup_sieve (p):
0020 
0021     p.set_desc(_("@info sieve discription",
0022     "Unfuzzy messages fuzzied only due to some tags being closed in-place "
0023     "(like '%(tag1)s' to '%(tag2)s')."
0024     "\n\n"
0025     "Possible only if catalogs were merged with --previous option.",
0026     tag1="<br>", tag2="<br/>"))
0027 
0028 
0029 _tags_inpl = r"(br|hr|nl)"
0030 _open_inpl_rx = re.compile(r"<\s*" + _tags_inpl + r"\s*>", re.U)
0031 _close_inpl_rx = re.compile(r"<\s*/\s*" + _tags_inpl + r"\s*>", re.U)
0032 _openclose_inpl_rx = re.compile(r"<\s*" + _tags_inpl + r"\s*/\s*>", re.U)
0033 
0034 # Replace any needed <...> with <.../> in the text.
0035 def _norm_inpl (text):
0036     text = _open_inpl_rx.sub(r"<\1/>", text)
0037     text = _openclose_inpl_rx.sub(r"<\1/>", text) # to normalize <br />, etc.
0038     return text
0039 
0040 
0041 class Sieve (object):
0042 
0043     def __init__ (self, params):
0044 
0045         self.caller_monitored = True
0046 
0047         self.nunfuzz = 0
0048         self.nmodinpl = 0
0049 
0050 
0051     def process (self, msg, cat):
0052 
0053         # Skip checks if the msgid contains closing </...>, too odd.
0054         if _close_inpl_rx.search(msg.msgid):
0055             return
0056 
0057         # Unfuzzy message if closed <.../> are the only difference.
0058         if (    msg.fuzzy
0059             and msg.msgid_previous is not None
0060             and msg.msgctxt_previous == msg.msgctxt
0061             and _open_inpl_rx.search(msg.msgid_previous)
0062         ):
0063             # Normalize <...> tags for checking.
0064             msgid_previous_n = _norm_inpl(msg.msgid_previous)
0065             msgid_plural_previous_n = _norm_inpl(msg.msgid_plural_previous or "")
0066             msgid_n = _norm_inpl(msg.msgid)
0067             msgid_plural_n = _norm_inpl(msg.msgid_plural or "")
0068 
0069             if (    msgid_n == msgid_previous_n
0070                 and msgid_plural_n == msgid_plural_previous_n
0071             ):
0072                 msg.unfuzzy()
0073                 self.nunfuzz += 1
0074 
0075         # Replace any <...> with <.../> in the msgstr.
0076         for i in range(len(msg.msgstr)):
0077             if _open_inpl_rx.search(msg.msgstr[i]):
0078                 msg.msgstr[i] = _open_inpl_rx.sub(r"<\1/>", msg.msgstr[i])
0079                 self.nmodinpl += 1
0080 
0081 
0082     def finalize (self):
0083 
0084         if self.nunfuzz > 0:
0085             msg = n_("@info:progress",
0086                      "Unfuzzied %(num)d message due to "
0087                      "closing tags in-place.",
0088                      "Unfuzzied %(num)d messages due to "
0089                      "closing tags in-place.",
0090                      num=self.nunfuzz)
0091             report("===== " + msg)
0092         if self.nmodinpl > 0:
0093             msg = n_("@info:progress",
0094                      "Modified %(num)d translations by "
0095                      "closing tags in-place.",
0096                      "Modified %(num)d translations by "
0097                      "closing tags in-place.",
0098                      num=self.nmodinpl)
0099             report("===== " + msg)
0100