File indexing completed on 2024-05-05 17:52:55

0001 # -*- coding: UTF-8 -*-
0002 
0003 """
0004 Replace normal space by non-breaking space where needed.
0005 
0006 Documented in C{doc/user/sieving.docbook}.
0007 
0008 @author: Sébastien Renard <sebastien.renard@digitalfox.org>
0009 @license: GPLv3"""
0010 
0011 import re
0012 
0013 from pology import _, n_
0014 from pology.report import report
0015 
0016 
0017 def setup_sieve (p):
0018 
0019     p.set_desc(_("@info sieve description",
0020                  "Replace normal space by non-breaking space where needed."))
0021 
0022 
0023 class Sieve (object):
0024     """Replace normal space by unbreakable space when needed"""
0025 
0026     def __init__ (self, params):
0027 
0028         self.nmatch = 0
0029 
0030         self.percent=re.compile("( %)(?=$| |\.|,)")
0031 
0032     def process (self, msg, cat):
0033 
0034         oldcount=msg.modcount
0035 
0036         for i in range(len(msg.msgstr)):
0037             msg.msgstr[i]=self.setUbsp(msg.msgstr[i])
0038 
0039         if oldcount<msg.modcount:
0040             self.nmatch+=1
0041 
0042     def finalize (self):
0043 
0044         if self.nmatch > 0:
0045             report(n_("@info",
0046                       "Non-breaking spaces added in %(num)d message.",
0047                       "Non-breaking spaces added in %(num)d messages.",
0048                       num=self.nmatch))
0049 
0050     def setUbsp(self, text):
0051         """Set correctly unbreakable spaces"""
0052         text=text.replace("\xa0", " ")
0053         text=text.replace("&nbsp;:", "\xc2\xa0:")
0054         text=text.replace(" :", "\xa0:")
0055         text=text.replace(" ;", "\xa0;")
0056         text=text.replace(" ?", "\xa0?")
0057         text=text.replace(" !", "\xa0!")
0058         text=text.replace("« ", "«\xa0")
0059         text=text.replace(" »", "\xa0»")
0060         text=text.replace(" / ", "\xa0/ ")
0061         text=self.percent.sub("\xa0%", text)
0062         
0063         return text