def _read_comment_from(file): import locale from zim.formats import ElementTreeModule as et # Etree fills in the namespaces which obfuscates the names mylang, enc = locale.getdefaultlocale() xmlns = "{http://www.w3.org/XML/1998/namespace}" xml = et.parse(file.path) fallback = [] #~ print "FIND COMMENT", file, mylang for elt in xml.getroot(): if elt.tag.endswith('comment'): lang = elt.attrib.get(xmlns+'lang', '') if lang == mylang: return elt.text elif not lang or mylang.startswith(lang+'_'): fallback.append((lang, elt.text)) else: pass else: #~ print "FALLBACK", fallback if fallback: fallback.sort() return fallback[-1][1] # longest match else: return None
def parse(self, content): if isinstance(content, list): content = ''.join(content) target = MemoryStoreTreeBuilder(self) builder = ElementTreeModule.XMLTreeBuilder(target=target) builder.feed(content) builder.close()
def create_table(self, attrib, text): ''' Automatic way for displaying the table-object as a table within the wiki, :param attrib: {type: 'table', wraps:'1,0,1' , aligns:'left,right,center' } :param text: XML - formated as a zim-tree table-object OR tuple of [header], [row1], [row2] :return: a TableViewObject ''' assert ElementTree.iselement(text) (header, rows) = self._tabledom_to_list(text) return TableViewObject(attrib, header, rows, self.preferences)
def _tabledom_to_list(self, tabledata): ''' Extracts necessary data out of a xml-table into a list structure :param tabledata: XML - formated as a zim-tree table-object :return: tuple of header-list and list of row lists - ([h1,h2],[[r11,r12],[r21,r22]) ''' headers = [head.text for head in tabledata.findall('thead/th')] headers = list(map(CellFormatReplacer.zim_to_cell, headers)) rows = [] for trow in tabledata.findall('trow'): row = trow.findall('td') row = [ElementTree.tostring(r, 'unicode').replace('<td>', '').replace('</td>', '') for r in row] row = list(map(CellFormatReplacer.zim_to_cell, row)) rows.append(row) return headers, rows
def model_from_element(self, attrib, element): assert ElementTree.iselement(element) attrib = self.parse_attrib(attrib) headers, rows = self._tabledom_to_list(element) return TableModel(attrib, headers, rows)
def model_from_element(self, attrib, element): assert ElementTree.iselement(element) attrib = self.parse_attrib(attrib) return TableModel(attrib)