File indexing completed on 2024-05-12 17:18:07
0001 # -*- coding: UTF-8 -*- 0002 0003 """ 0004 Additions to resolving UI references. 0005 0006 @author: Chusslove Illich (Часлав Илић) <caslav.ilic@gmx.net> 0007 @license: GPLv3 0008 """ 0009 0010 import re 0011 0012 entity_tail_rxstr = r"([\w:][\w\d.:-]*);" 0013 _entity_tail_rx = re.compile(r"%s" % entity_tail_rxstr, re.U) 0014 _entity_rx = re.compile(r"&%s" % entity_tail_rxstr, re.U) 0015 0016 def mod_entities (unescape=False, basesuff=""): 0017 """ 0018 Modify entities in resolved UI text. 0019 0020 If the UI text has been XML-escaped, any XML-entities in it have been 0021 escaped to C{&<original>;} form. If C{unescents} is C{True}, such 0022 forms will be unescaped to original entity. 0023 0024 If the original entity is of the form C{&<basename>-<suffix>;}, a suffix 0025 may be inserted to the base name itself using the parameter C{basesuff}. 0026 The entity then becomes C{&<basename><basesuff>-<suffix>;}. 0027 If there are several hyphens in the original name, the suffix is taken 0028 as part after the last hyphen; if there are no hyphens, the suffix is 0029 taken as empty string. 0030 0031 @param unescape: whether to unescape escaped entities 0032 @type unescape: bool 0033 @param basesuff: suffix to append to base entity name 0034 @type basesuff: string 0035 0036 @return: type F1A hook 0037 @rtype: C{(text) -> text} 0038 """ 0039 0040 if unescape: 0041 ent_head = "&" 0042 else: 0043 ent_head = "&" 0044 0045 def hook (text): 0046 0047 p = 0 0048 tsegs = [] 0049 while True: 0050 pp = p 0051 p = text.find(ent_head, p) 0052 if p < 0: 0053 tsegs.append(text[pp:]) 0054 break 0055 0056 tsegs.append(text[pp:p]) 0057 0058 m = _entity_tail_rx.match(text, p + len(ent_head)) 0059 if m: 0060 ent = m.group(1) 0061 if basesuff: 0062 q = ent.rfind("-") 0063 if q < 0: 0064 q = len(ent) 0065 ent = ent[:q] + "_ot" + ent[q:] 0066 tsegs.append("&%s;" % ent) 0067 p = m.end() 0068 else: 0069 tsegs.append(ent_head) 0070 p += len(ent_head) 0071 0072 return "".join(tsegs) 0073 0074 return hook 0075