diff options
| author | Antonin Bourguignon <abo@openerp.com> | 2012-10-02 14:06:34 +0200 |
|---|---|---|
| committer | Antonin Bourguignon <abo@openerp.com> | 2012-10-02 14:06:34 +0200 |
| commit | c28dfb19e19ea9dde6de2ae5fb20cfec3860744e (patch) | |
| tree | 4fdff0732f33426af59c7792b8e727458afe913c /addons/email_template/html2text.py | |
| parent | 79b2a0f6c6017d984b15160b8be806ecf7131b3e (diff) | |
| parent | 2b2288a3bd29c096cc5eb70c4d966cfd32bffc45 (diff) | |
[MERGE] trunk-unmutable-defaults-abo: fix the last few methods that still have mutable defaults
also improve code style (methods signatures and whitespaces)
contribution of Lionel Sausin from Numerigraphe
bzr revid: abo@openerp.com-20121002120634-ms2ins653tktqfnt
Diffstat (limited to 'addons/email_template/html2text.py')
| -rw-r--r--[-rwxr-xr-x] | addons/email_template/html2text.py | 104 |
1 files changed, 53 insertions, 51 deletions
diff --git a/addons/email_template/html2text.py b/addons/email_template/html2text.py index 745381850e3..880a8bb43a8 100755..100644 --- a/addons/email_template/html2text.py +++ b/addons/email_template/html2text.py @@ -41,13 +41,13 @@ def name2cp(k): if k.startswith("&#") and k.endswith(";"): return int(k[2:-1]) # not in latin-1 return ord(codecs.latin_1_decode(k)[0]) -unifiable = {'rsquo':"'", 'lsquo':"'", 'rdquo':'"', 'ldquo':'"', +unifiable = {'rsquo':"'", 'lsquo':"'", 'rdquo':'"', 'ldquo':'"', 'copy':'(C)', 'mdash':'--', 'nbsp':' ', 'rarr':'->', 'larr':'<-', 'middot':'*', 'ndash':'-', 'oelig':'oe', 'aelig':'ae', -'agrave':'a', 'aacute':'a', 'acirc':'a', 'atilde':'a', 'auml':'a', 'aring':'a', -'egrave':'e', 'eacute':'e', 'ecirc':'e', 'euml':'e', +'agrave':'a', 'aacute':'a', 'acirc':'a', 'atilde':'a', 'auml':'a', 'aring':'a', +'egrave':'e', 'eacute':'e', 'ecirc':'e', 'euml':'e', 'igrave':'i', 'iacute':'i', 'icirc':'i', 'iuml':'i', -'ograve':'o', 'oacute':'o', 'ocirc':'o', 'otilde':'o', 'ouml':'o', +'ograve':'o', 'oacute':'o', 'ocirc':'o', 'otilde':'o', 'ouml':'o', 'ugrave':'u', 'uacute':'u', 'ucirc':'u', 'uuml':'u'} unifiable_n = {} @@ -60,7 +60,7 @@ def charref(name): c = int(name[1:], 16) else: c = int(name) - + if not UNICODE_SNOB and c in unifiable_n.keys(): return unifiable_n[c] else: @@ -76,14 +76,14 @@ def entityref(c): def replaceEntities(s): s = s.group(1) - if s[0] == "#": + if s[0] == "#": return charref(s[1:]) else: return entityref(s) r_unescape = re.compile(r"&(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));") def unescape(s): return r_unescape.sub(replaceEntities, s) - + def fixattrs(attrs): # Fix bug in sgmllib.py if not attrs: return attrs @@ -105,7 +105,7 @@ def optwrap(text): """Wrap all paragraphs in the provided text.""" if not BODY_WIDTH: return text - + assert wrap, "Requires Python 2.3." result = '' newlines = 0 @@ -136,7 +136,7 @@ def hn(tag): class _html2text(sgmllib.SGMLParser): def __init__(self, out=sys.stdout.write, baseurl=''): sgmllib.SGMLParser.__init__(self) - + if out is None: self.out = self.outtextf else: self.out = out self.outtext = u'' @@ -157,43 +157,43 @@ class _html2text(sgmllib.SGMLParser): self.abbr_data = None # last inner HTML (for abbr being defined) self.abbr_list = {} # stack of abbreviations to write later self.baseurl = baseurl - - def outtextf(self, s): + + def outtextf(self, s): self.outtext += s - + def close(self): sgmllib.SGMLParser.close(self) - + self.pbr() self.o('', 0, 'end') - + return self.outtext - + def handle_charref(self, c): self.o(charref(c)) def handle_entityref(self, c): self.o(entityref(c)) - + def unknown_starttag(self, tag, attrs): self.handle_tag(tag, attrs, 1) - + def unknown_endtag(self, tag): self.handle_tag(tag, None, 0) - + def previousIndex(self, attrs): """ returns the index of certain set of attributes (of a link) in the self.a list - + If the set of attributes is not found, returns None """ if not attrs.has_key('href'): return None - + i = -1 for a in self.a: i += 1 match = 0 - + if a.has_key('href') and a['href'] == attrs['href']: if a.has_key('title') or attrs.has_key('title'): if (a.has_key('title') and attrs.has_key('title') and @@ -206,13 +206,13 @@ class _html2text(sgmllib.SGMLParser): def handle_tag(self, tag, attrs, start): attrs = fixattrs(attrs) - + if hn(tag): self.p() if start: self.o(hn(tag)*"#" + ' ') if tag in ['p', 'div']: self.p() - + if tag == "br" and start: self.o(" \n") if tag == "hr" and start: @@ -220,21 +220,21 @@ class _html2text(sgmllib.SGMLParser): self.o("* * *") self.p() - if tag in ["head", "style", 'script']: + if tag in ["head", "style", 'script']: if start: self.quiet += 1 else: self.quiet -= 1 if tag in ["body"]: self.quiet = 0 # sites like 9rules.com never close <head> - + if tag == "blockquote": - if start: + if start: self.p(); self.o('> ', 0, 1); self.start = 1 self.blockquote += 1 else: self.blockquote -= 1 self.p() - + if tag in ['em', 'i', 'u']: self.o("_") if tag in ['strong', 'b']: self.o("**") if tag == "code" and not self.pre: self.o('`') #TODO: `` `this` `` @@ -243,7 +243,7 @@ class _html2text(sgmllib.SGMLParser): attrsD = {} for (x, y) in attrs: attrsD[x] = y attrs = attrsD - + self.abbr_title = None self.abbr_data = '' if attrs.has_key('title'): @@ -253,13 +253,13 @@ class _html2text(sgmllib.SGMLParser): self.abbr_list[self.abbr_data] = self.abbr_title self.abbr_title = None self.abbr_data = '' - + if tag == "a": if start: attrsD = {} for (x, y) in attrs: attrsD[x] = y attrs = attrsD - if attrs.has_key('href') and not (SKIP_INTERNAL_LINKS and attrs['href'].startswith('#')): + if attrs.has_key('href') and not (SKIP_INTERNAL_LINKS and attrs['href'].startswith('#')): self.astack.append(attrs) self.o("[") else: @@ -277,7 +277,7 @@ class _html2text(sgmllib.SGMLParser): a['outcount'] = self.outcount self.a.append(a) self.o("][" + `a['count']` + "]") - + if tag == "img" and start: attrsD = {} for (x, y) in attrs: attrsD[x] = y @@ -296,20 +296,20 @@ class _html2text(sgmllib.SGMLParser): self.o("![") self.o(alt) self.o("]["+`attrs['count']`+"]") - + if tag == 'dl' and start: self.p() if tag == 'dt' and not start: self.pbr() if tag == 'dd' and start: self.o(' ') if tag == 'dd' and not start: self.pbr() - + if tag in ["ol", "ul"]: if start: self.list.append({'name':tag, 'num':0}) else: if self.list: self.list.pop() - + self.p() - + if tag == 'li': if start: self.pbr() @@ -323,10 +323,10 @@ class _html2text(sgmllib.SGMLParser): self.start = 1 else: self.pbr() - + if tag in ["table", "tr"] and start: self.p() if tag == 'td': self.pbr() - + if tag == "pre": if start: self.startpre = 1 @@ -334,34 +334,35 @@ class _html2text(sgmllib.SGMLParser): else: self.pre = 0 self.p() - + def pbr(self): if self.p_p == 0: self.p_p = 1 - def p(self): self.p_p = 2 - + def p(self): + self.p_p = 2 + def o(self, data, puredata=0, force=0): if self.abbr_data is not None: self.abbr_data += data - - if not self.quiet: + + if not self.quiet: if puredata and not self.pre: data = re.sub('\s+', ' ', data) if data and data[0] == ' ': self.space = 1 data = data[1:] if not data and not force: return - + if self.startpre: #self.out(" :") #TODO: not output when already one there self.startpre = 0 - + bq = (">" * self.blockquote) if not (force and data and data[0] == ">") and self.blockquote: bq += " " - + if self.pre: bq += " " data = data.replace("\n", "\n"+bq) - + if self.start: self.space = 0 self.p_p = 0 @@ -377,7 +378,7 @@ class _html2text(sgmllib.SGMLParser): if self.p_p: self.out(('\n'+bq)*self.p_p) self.space = 0 - + if self.space: if not self.lastWasNL: self.out(' ') self.space = 0 @@ -388,7 +389,7 @@ class _html2text(sgmllib.SGMLParser): newa = [] for link in self.a: if self.outcount > link['outcount']: - self.out(" ["+`link['count']`+"]: " + urlparse.urljoin(self.baseurl, link['href'])) + self.out(" ["+`link['count']`+"]: " + urlparse.urljoin(self.baseurl, link['href'])) if link.has_key('title'): self.out(" ("+link['title']+")") self.out("\n") else: @@ -397,7 +398,7 @@ class _html2text(sgmllib.SGMLParser): if self.a != newa: self.out("\n") # Don't need an extra line when nothing was done. self.a = newa - + if self.abbr_list and force == "end": for abbr, definition in self.abbr_list.items(): self.out(" *[" + abbr + "]: " + definition + "\n") @@ -410,8 +411,9 @@ class _html2text(sgmllib.SGMLParser): def handle_data(self, data): if r'\/script>' in data: self.quiet -= 1 self.o(data, 1) - - def unknown_decl(self, data): pass + + def unknown_decl(self, data): + pass def wrapwrite(text): sys.stdout.write(text.encode('utf8')) |
