summaryrefslogtreecommitdiff
path: root/poezio/xhtml.py
diff options
context:
space:
mode:
Diffstat (limited to 'poezio/xhtml.py')
-rw-r--r--poezio/xhtml.py31
1 files changed, 16 insertions, 15 deletions
diff --git a/poezio/xhtml.py b/poezio/xhtml.py
index 899985ef..2875f1a1 100644
--- a/poezio/xhtml.py
+++ b/poezio/xhtml.py
@@ -3,7 +3,7 @@
# This file is part of Poezio.
#
# Poezio is free software: you can redistribute it and/or modify
-# it under the terms of the zlib license. See the COPYING file.
+# it under the terms of the GPL-3.0+ license. See the COPYING file.
"""
Various methods to convert
shell colors to poezio colors,
@@ -21,6 +21,7 @@ from pathlib import Path
from io import BytesIO
from xml import sax
from xml.sax import saxutils
+from xml.sax.handler import ContentHandler
from typing import List, Dict, Optional, Union, Tuple
from slixmpp.xmlstream import ET
@@ -32,7 +33,7 @@ digits = '0123456789' # never trust the modules
XHTML_NS = 'http://www.w3.org/1999/xhtml'
# HTML named colors
-colors = {
+colors: Dict[str, int] = {
'aliceblue': 231,
'antiquewhite': 231,
'aqua': 51,
@@ -180,7 +181,7 @@ colors = {
'whitesmoke': 255,
'yellow': 226,
'yellowgreen': 149
-} # type: Dict[str, int]
+}
whitespace_re = re.compile(r'\s+')
@@ -299,21 +300,21 @@ def get_hash(data: bytes) -> str:
b'/', b'-').decode()
-class XHTMLHandler(sax.ContentHandler):
+class XHTMLHandler(ContentHandler):
def __init__(self, force_ns=False,
tmp_image_dir: Optional[Path] = None) -> None:
- self.builder = [] # type: List[str]
- self.formatting = [] # type: List[str]
- self.attrs = [] # type: List[Dict[str, str]]
- self.list_state = [] # type: List[Union[str, int]]
- self.cids = {} # type: Dict[str, Optional[str]]
+ self.builder: List[str] = []
+ self.formatting: List[str] = []
+ self.attrs: List[Dict[str, str]] = []
+ self.list_state: List[Union[str, int]] = []
+ self.cids: Dict[str, Optional[str]] = {}
self.is_pre = False
self.a_start = 0
# do not care about xhtml-in namespace
self.force_ns = force_ns
self.tmp_image_dir = Path(tmp_image_dir) if tmp_image_dir else None
- self.enable_css_parsing = config.get('enable_css_parsing')
+ self.enable_css_parsing = config.getbool('enable_css_parsing')
@property
def result(self) -> str:
@@ -430,7 +431,7 @@ class XHTMLHandler(sax.ContentHandler):
if 'href' in attrs and attrs['href'] != link_text:
builder.append(' (%s)' % _trim(attrs['href']))
elif name == 'blockquote':
- builder.append('”')
+ builder.append('”\n')
elif name in ('cite', 'em', 'strong'):
self.pop_formatting()
elif name in ('ol', 'p', 'ul'):
@@ -488,7 +489,7 @@ def convert_simple_to_full_colors(text: str) -> str:
a \x19n} formatted one.
"""
# TODO, have a single list of this. This is some sort of
- # duplicate from windows.format_chars
+ # duplicate from ui.consts.FORMAT_CHARS
mapping = str.maketrans({
'\x0E': '\x19b',
'\x0F': '\x19o',
@@ -512,7 +513,7 @@ def convert_simple_to_full_colors(text: str) -> str:
return re.sub(xhtml_simple_attr_re, add_curly_bracket, text)
-number_to_color_names = {
+number_to_color_names: Dict[int, str] = {
1: 'red',
2: 'green',
3: 'yellow',
@@ -520,7 +521,7 @@ number_to_color_names = {
5: 'violet',
6: 'turquoise',
7: 'white'
-} # type: Dict[int, str]
+}
def format_inline_css(_dict: Dict[str, str]) -> str:
@@ -535,7 +536,7 @@ def poezio_colors_to_html(string: str) -> str:
# Maintain a list of the current css attributes used
# And check if a tag is open (by design, we only open
# spans tag, and they cannot be nested.
- current_attrs = {} # type: Dict[str, str]
+ current_attrs: Dict[str, str] = {}
tag_open = False
next_attr_char = string.find('\x19')
build = ["<body xmlns='http://www.w3.org/1999/xhtml'><p>"]