html.py (3570B) - raw
1 """ 2 Some helper functions for converting box attributes to HTML 3 """ 4 import base64 5 import json 6 import urllib.parse 7 from collections import namedtuple 8 9 HTMLTag = namedtuple("HTMLTag", ["tag", "attributes"]) 10 11 12 def get_table_info(box_attribute): 13 attribute_type = box_attribute[0] 14 if attribute_type.startswith("struct-table"): 15 table_id = attribute_type.partition("_")[0].replace("struct-table", "") 16 if "_row" in attribute_type: # col and row backwards 17 row_id = attribute_type.partition("_row")[2] 18 return table_id, row_id, None 19 elif "_col" in attribute_type: 20 col_id = attribute_type.partition("_col")[2] 21 return table_id, None, col_id 22 return None, None, None 23 24 25 def get_list_attribute(box_attribute): # UNUSED 26 # ordered vs unordered 27 attribute_type = box_attribute[1][:-1] 28 if attribute_type.startswith("number"): 29 return HTMLTag("ol", {}) 30 else: 31 return HTMLTag("ul", {}) # set defautl value 32 33 34 def get_list_info(box_attribute): 35 if "list" in box_attribute[0]: 36 if box_attribute[1].startswith("number"): 37 kind = "ordered" 38 elif box_attribute[1].startswith("uncheck"): 39 kind = "unchecked" 40 elif box_attribute[1].startswith("check"): 41 kind = "checked" 42 else: 43 kind = "unordered" 44 # TODO: regex. cant do more than 9 list levels 45 return kind, int(box_attribute[1][-1]) 46 47 48 def convert_simple_element_to_html_tag(box_attribute): 49 tag = None 50 html_attrib = {} 51 attribute_type = box_attribute[0] 52 attribute_value = box_attribute[1] 53 assert attribute_value # WIP 54 if attribute_type == "bold": 55 tag = "b" 56 elif attribute_type == "underline": 57 tag = "u" 58 elif attribute_type == "italic": 59 tag = "i" 60 elif attribute_type == "strikethrough": 61 tag = "s" 62 elif "font-color" in attribute_type: 63 tag = "font" 64 html_attrib = {"color": attribute_type.split("-")[-1]} 65 elif "font-size" in attribute_type: 66 tag = "font" 67 size = attribute_type.split("-")[-1] 68 sizemap = { 69 "medium": "3", 70 "large": "+2", 71 "verylarge": "+3", 72 "small": "-1", # TODO: consider 73 } 74 html_attrib["size"] = sizemap[size] 75 elif attribute_type == "align": 76 tag = "div" 77 html_attrib["style"] = "text-align: {}".format(attribute_value) 78 elif attribute_type.startswith("link-"): 79 urlstring = _decode_link(attribute_type) 80 tag = "a" 81 html_attrib["href"] = urlstring 82 elif attribute_type.startswith("image"): 83 tag = "img" 84 html_attrib["src"] = _decode_image(attribute_type).get("boxSharedLink") 85 # elif attribute_type.startswith("struct-table"): 86 # tag = "td" 87 elif attribute_type == "list": 88 if "checked" in attribute_value: # "checked" or "unckecked" 89 tag = "input" 90 html_attrib["type"] = "checkbox" 91 if attribute_value.startswith("checked"): 92 html_attrib["checked"] = "checked" 93 else: 94 tag = "li" 95 return HTMLTag(tag, html_attrib) # NamedTuple 96 97 98 def _decode_link(urlstring): # move to separate module 99 return base64.b64decode(urlstring.split("-")[-1]).decode("utf-8").partition("-")[2] 100 101 102 def _decode_image(imagestring): 103 # distinguish image flags..." 104 # this one is fun 105 return json.loads( 106 urllib.parse.unquote( 107 base64.b64decode(imagestring.split("-")[-1]).decode("utf-8") 108 ) 109 )