def processPost(postpath): parser = etree.HTMLParser(encoding='UTF-8') f = open(postpath, 'r+b') tree = etree.parse(f, parser) for src in tree.xpath("//img/@src"): #print src srctxt = re.sub(r'(^\S+\.[A-Za-z]{3,4})\?w=\d{2,4}', r'\1', src) #print srctxt md = '\n![](%s)\n' % replaceUrl(srctxt) if srcUrlPrefix in srctxt: copyImg(srctxt) par = src.getparent() parpar = par.getparent() if parpar.tag == 'a': # if <img> is inside <a> parpar.getparent().replace(parpar, E.P(md + parpar.tail)) else: # if <img> is not inside <a> par.getparent().replace(par, E.P(md + par.tail)) # Note on replace(): http://comments.gmane.org/gmane.comp.python.lxml.devel/1971 etree.strip_tags(tree, 'p', 'html', 'body') output = etree.tostring(tree, encoding='UTF-8') leadinghtml = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">\n<html>' trailinghtml = '</html>' output = output.replace(leadinghtml, '').replace(trailinghtml, '') output = output.replace(' ', '') f.seek(0) f.write(output) f.truncate() f.close()
def write_t_rec(self, t, autoAnchor=None, align='left', parent=None, level=0): """ Recursively writes a <t> element If no parent is specified, a dummy div will be treated as the parent and any text will go in a <p> element. Otherwise text and child elements will be insert directly into the parent -- meaning we are in a list """ if parent is None: parent = self.temp_div # Dummy div current = E.P() parent.append(current) else: current = parent if t.text: if "anchor" in t.attrib: a = E.A(name=t.attrib["anchor"]) a.tail = t.text current.append(a) else: current.text = t.text if autoAnchor: current.attrib['id'] = autoAnchor for child in t: if child.tag in ['xref', 'eref', 'iref', 'cref', 'spanx']: for element in self._expand_ref(child): current.append(element) elif child.tag == 'u': for element in self._expand_u(child): current.append(element) elif child.tag == 'vspace': br = E.BR() current.append(br) blankLines = int( child.attrib.get('blankLines', self.defaults['vspace_blanklines'])) for i in range(blankLines): br = E.BR() current.append(br) if child.tail: br.tail = child.tail elif child.tag == 'list': self.write_list(child, parent, level=level) if child.tail: parent.append(E.P(child.tail)) elif child.tag == 'figure': # Callback to base writer method self.write_figure(child) elif child.tag == 'texttable': # Callback to base writer method self.write_table(child) # If we are back at top level, serialize the whole temporary structure # Add to body buffer if parent == self.temp_div: self.buf.append(self._flush_temp_div())
def visit_Component(self, node): inst = getattr(self, 'inst', None) if inst: title = 'Instance {} of {} Register Map'.format(inst, node.name) else: title = 'Base {} Register Map'.format(node.name) self.title = title # Create the main content by sweeping the tree. bc = E.DIV(id='breadcrumbs') try: if self.breadcrumbs is not None: bc.append(self.breadcrumbs) except AttributeError: pass ww = node.width // 8 an = ((node.size - 1).bit_length() + 3) // 4 with self.tempvars(wordwidth=ww, address_nibbles=an, hlev=2): nodes = ([E.H1(title, id='title'), bc] + [E.P(d) for d in node.description] + [c for c in self.visitchildren(node)] + [self.footer(node)]) contentnode = E.DIV(*nodes, id='content') # Add a table of contents sidebar. We'll assume that everything that # wants to be in the TOC is already a heading and just work from there. h2list = E.UL() for elem in contentnode.iter('h2', 'h3'): id = escape(elem.text) elem.attrib['id'] = id if elem.tag == 'h2': h2node = E.LI(E.A(elem.text, href='#' + id)) h2list.append(h2node) h3list = None else: if h3list is None: h3list = E.UL() h2list.append(h3list) h3list.append(E.LI(E.A(elem.text, href='#' + id))) # Put it all together. return E.HTML( E.HEAD( E.TITLE(title), E.LINK(rel='stylesheet', type='text/css', href=htmlpathjoin(self.styledir, 'reg.css'))), E.BODY( E.DIV(E.DIV(E.P(E.A(title, href='#title')), h2list, id='sidebar'), contentnode, id='wrapper')), )
def brs_to_paragraphs(tree, inline_tags=None): """ Return an lxml tree with all <br> elements stripped and paragraphs put in place where necessary. """ # add these tags to p's that we're currently building, any other tags will # close the current p inline_tags = inline_tags or ['a'] # if this tree doesn't have any child elements, just return it as is if len(tree) == 0: return tree # if this tree doesn't contain any <br> tags, we don't need to touch it if tree.find('.//br') is None: return tree # XXX: We're building a whole new tree here and leaving out any attributes. # A) That might be a little slower and more memory intensive than modifying # the tree in place, and B) we're dropping any attributes on block elements. # The latter is probably fine for current use, but certainly not ideal. new_tree = Element(tree.tag) # if this tree starts out with text, create a new paragraph for it, and # add it to the tree if tree.text: p = E.P() p.text = tree.text new_tree.append(p) for e in tree: if e.tag == 'br': # avoid adding empty p elements if e.tail is None: continue # start a new p p = E.P() p.text = e.tail new_tree.append(p) # if this is a block tag, and it has trailing text, that text needs to # go into a new paragraph... only if the tail has actual content and # not just whitespace though. elif e.tail and re.match('[^\s]', e.tail) and e.tag not in inline_tags: p = E.P() p.text = e.tail e.tail = '' new_tree.append(e) new_tree.append(p) # keep inline tags inside the current paragraph elif e.tag in inline_tags: p.append(e) else: new_tree.append(brs_to_paragraphs(e)) return new_tree
def _generate_project_report_in_html(self, project_name, project_bugs): report = E.BODY( E.H2(E.CLASS("heading"), "%s (%d)" % (project_name, len(project_bugs)))) for bug in project_bugs: bug_link = E.A(bug.title, href=bug.web_link, target='_blank') report.append( E.P("[%s:%s] " % (bug.importance, bug.status), bug_link)) if bug.assignee: report.append( E.P("Assigned to: %s" % (bug.assignee.display_name))) return report
def generateHTML(city_info): html = E.HTML( E.HEAD(E.TITLE("Weather for:{}".format(city_info['city']))), E.BODY(E.H1("Weather for:{}".format(city_info['city'])), E.P("{}".format(city_info['city_temp'])), E.P("{}".format(city_info['city_forecast'])), E.P("{}".format(city_info['city_min'])), E.P("{}".format(city_info['city_max'])), E.P("{}".format(city_info['city_time'])))) byteString = lxml.html.tostring(html) string = byteString.decode('utf-8') return string
def getCity(city): weatherStuff = weatherHandler(city) weatherStuff.get_weather() html = E.HTML( E.HEAD(E.TITLE("Weather for:{}".format(weatherStuff.city))), E.BODY(E.H1("Weather for:{}".format(weatherStuff.city)), E.P("{}".format(weatherStuff.formatted_temp)), E.P("{}".format(weatherStuff.formatted_weather)), E.P("{}".format(weatherStuff.formatted_minMax)), E.P("{}".format(weatherStuff.formatted_time)))) byteString = lxml.html.tostring(html) string = byteString.decode('utf-8') return string
def test(): for key, value in d.items(): yield E.H2(news_topic[key]) if any(value): for i in value: yield lxml.html.fromstring( '<a href="http://news.gdut.edu.cn/viewarticle.aspx?articleid={id}">{title}[{loc}]</a><br></br>' .format_map(i)) else: del news_topic[key] yield E.P('无') yield E.P('Sent at {}'.format( time.strftime("%y-%m-%d %H:%M", time.localtime())))
def html_page_return(board, thread, default_style): html = E.HTML( E.HEAD( E.META(**{'http-equiv':"Default-Style", 'content':default_style, 'id':'stylemetatag'}), E.TITLE("/"+board+"/ - №"+str(thread)), #title E.SCRIPT(type = 'text/javascript', src = '/mainscript.js'), #js *initiate.style_cache ), E.BODY( E.P(E.CLASS("board"), board, id = 'board'), E.P(E.CLASS("thread"), str(thread), id = 'thread'), E.TABLE( E.CLASS("maintable"), E.THEAD(E.TR(E.TD( E.TABLE(E.TR(E.TD(E.CLASS('left'), copy.copy(initiate.board_cache_navigation)), E.TD(E.CLASS('right'), utilfunctions.generate_right_up_corner_menu()), ), id='headblock'), E.HR(E.CLASS("delimeter")), )), id = 'header'), E.TBODY(E.TR(E.TD( E.H2(E.CLASS("boardname"), E.A('/' + board + '/ - '+ initiate.board_cache[board].name, href = '/' + board), ), E.HR(E.CLASS("delimeter")), initiate.board_cache[board].post_form, #need to make it depending on post_form_type E.SCRIPT('function open_form() {document.getElementById("postform").style.display = "block"; document.getElementById("closeform").style.display = "block"; document.getElementById("threadcreate").style.display = "none";}'), E.SCRIPT('function close_form() {document.getElementById("postform").style.display = "none"; document.getElementById("closeform").style.display = "none"; document.getElementById("threadcreate").style.display = "block";}'), E.H3(E.A('Ответить в тред', href = "javascript:open_form();"), id = 'threadcreate'), E.H4(E.A('Скрыть форму', href = "javascript:close_form();"), id = 'closeform'), E.HR(E.CLASS("delimeter")), EM('main', '', id = 'mainframe'), E.DIV('', id = 'optionsdiv'), )), id = 'mainpart'), E.TFOOT(E.TR(E.TD( E.DIV( E.HR(E.CLASS("delimeter"), id = 'end') ), initiate.board_cache_navigation, E.DIV('powered by ', E.A('Farlight Imageboard Engine', href='https://github.com/Alpherie/farlight_board_engine', target='_blank', ), id='credentials'), )), id = 'footer'),#we make it a footer ), onload = 'threadfunc()' ) ) return lxml.html.tostring(html)
def main(results_file): summary = {} with open(results_file, 'r') as f: summary = json.loads(f.read()) summary['directory'] = os.path.dirname(results_file) date = datetime.datetime.fromtimestamp( summary['timestamp']).strftime('%Y-%m-%d-%H:%M:%S') token_create_rps = summary['token_creation']['requests_per_second'] token_create_tps = summary['token_creation']['time_per_request'] token_validate_rps = summary['token_validation']['requests_per_second'] token_validate_tps = summary['token_validation']['time_per_request'] index = e.HTML( e.HEAD(e.LINK(rel='stylesheet', type='text/css', href='theme.css'), e.TITLE('OpenStack Keystone Performance')), e.BODY( e.DIV(e.H1('OpenStack Keystone Performance'), e.P('Published reports after each merged patch.', CLASS('subtitle')), id='header'), e.DIV( e.P('Last run date: ' + date, ), e.P( 'keystone SHA: ', e.A(summary['sha'], target='_blank', href=KEYSTONE_LINK + summary['sha'])), e.P( 'os_keystone SHA: ', e.A(summary['osa_sha'], target='_blank', href=OSA_LINK + summary['osa_sha'])), e.P(e.A('Performance Data', href=PERF_LINK, target='_blank')), e.DIV(CLASS('left'), e.H2('Create Token'), e.P(e.STRONG(token_create_rps), ' requests per second'), e.P(e.STRONG(token_create_tps), ' ms per request')), e.DIV( CLASS('right'), e.H2('Validate Token'), e.P(e.STRONG(token_validate_rps), ' requests per second'), e.P(e.STRONG(token_validate_tps), ' ms per request')), id='content'), e.DIV(e.P( 'Results provided by the ', e.A('OSIC Performance Bot', target='_blank', href=BOT_LINK)), id='footer'))) with open(os.path.join(summary['directory'], 'index.html'), 'w') as f: f.write(et.tostring(index))
def visit_Register(self, node): """Generate a Register DIV with heading, bitfield table, field listing, etc.""" ap = self.addressparagraph(node) ap.text += ' ' + register_format(node) root = E.DIV(ap, self.heading(node.name), CLASS('register'), *[E.P(d, CLASS('description')) for d in node.description], id="REG_" + node.name) if node.space: # We need bitfield tables. table = E.TABLE(CLASS('bitfield')) rows = [] for startbit in range(0, node.space.size, 16): endbit = startbit + 16 row = E.TR(CLASS('fields')) cells = [] for obj, start, size in node.space[startbit:endbit]: if obj: cell = E.TD(obj.name, CLASS('std_field')) else: cell = E.TD('.', CLASS('reserved_field')) cell.attrib['colspan'] = str(size) cells.append(cell) row.extend(reversed(cells)) rows.append(row) rows.append( E.TR( CLASS('bit_numbers'), *[ E.TD(str(n - 1)) for n in range(endbit, startbit, -1) ])) table.extend(reversed(rows)) root.append(table) fieldlist = E.UL(CLASS('fieldlist'), *self.visitchildren(node, reverse=True)) root.append(fieldlist) elif node.width != node.parent.width: # We have a truncated field, i.e. not all the bits that the # component allows. root.append(E.P('Bits {}:0 only.'.format(node.width - 1))) return root
def process_jsonfiles(zipdir): """Process all JSON files in the resulting directory :param zipdir: zipdir name :type zipdir: Path | str """ body = gen_html() for jfile in listjsonfiles(str(zipdir)): content = load_jsonfile(jfile) # Create title div = E.DIV(E.H1(content.get("date_journal"))) # Create date: div.append(E.H5(content.get("address"))) # Create photos: divimg = E.DIV() for image in content.get('photos'): img = E.IMG( src=image, width="600", ) divimg.append(img) div.append(divimg) # Create text: text = content["text"] = markdown.markdown(content["text"]) texthtml = fromstring(text) div.append(E.P(texthtml)) body.append(div) return body
def test_replace_arch_2(self): replacement = h.DIV(h.P("Wheee")) result = self.registry('ir.ui.view').replace_arch_section( self.cr, self.uid, self.view_id, None, replacement) self.eq(result, replacement)
def write_paragraph(self, text, align='left', autoAnchor=None): if text: p = E.P(text) if autoAnchor: p.attrib['id'] = autoAnchor # Add to body buffer self.buf.append(self._serialize(p))
def password_change_menu(): html = E.HTML( E.HEAD( E.LINK(rel="stylesheet", href="/css/deeplight.css", type="text/css"), E.TITLE("Administration and moderation") ), E.BODY( E.H1(E.CLASS("heading"), "Farlight Engine Imageboard"), E.P(E.CLASS("loginmessage"), "Change your password"), E.FORM(E.CLASS("loginform"), E.INPUT(type = 'hidden', name = 'action', value = 'change'), E.INPUT(type = 'hidden', name = 'instance', value = 'password'), E.TABLE( E.TR(E.TD('OLD PASSWORD'), E.TD(E.INPUT(type = 'password', name = 'old_passwd', value = '')) ), E.TR(E.TD('NEW PASSWORD'), E.TD(E.INPUT(type = 'password', name = 'new_passwd', value = '')) ), E.TR(E.TD('NEW PASSWORD AGAIN'), E.TD(E.INPUT(type = 'password', name = 'new_passwd_again', value = '')) ), ), E.INPUT(type = 'submit', value = 'LOGIN'), method = 'POST', action = '/admin' ) ) ) return lxml.html.tostring(html)
def write_label(self, text, type='figure', align='center', source_line=None): # Ignore labels for table, they are handled in draw_table if type == 'figure': p = E.P(text) p.attrib['class'] = 'figure' # Add to body buffer self.buf.append(self._serialize(p))
def login_page_gen(): html = E.HTML( E.HEAD( E.LINK(rel="stylesheet", href="/css/deeplight.css", type="text/css"), E.TITLE("Administration and moderation") ), E.BODY( E.H1(E.CLASS("heading"), "Farlight Engine Imageboard"), E.P(E.CLASS("loginmessage"), "You need to login"), E.FORM(E.CLASS("loginform"), E.TABLE( E.TR(E.TD('LOGIN'), E.TD(E.INPUT(type = 'text', name = 'login', value = '')) ), E.TR(E.TD('PASSWORD'), E.TD(E.INPUT(type = 'text', name = 'password', value = '')) ), ), E.INPUT(type = 'submit', value = 'LOGIN'), method = 'POST', action = '/admin/login' ) ) ) return lxml.html.tostring(html)
def fix_spoilers_in_html(content: str, language: str) -> str: with override_language(language): spoiler_title: str = _("Open Zulip to see the spoiler content") fragment = lxml.html.fromstring(content) spoilers = fragment.find_class("spoiler-block") for spoiler in spoilers: header = spoiler.find_class("spoiler-header")[0] spoiler_content = spoiler.find_class("spoiler-content")[0] header_content = header.find("p") if header_content is None: # Create a new element to append the spoiler to) header_content = E.P() header.append(header_content) else: # Add a space. rear = header_content[-1] if len( header_content) else header_content rear.tail = (rear.tail or "") + " " span_elem = E.SPAN(f"({spoiler_title})", **E.CLASS("spoiler-title"), title=spoiler_title) header_content.append(span_elem) header.drop_tag() spoiler_content.drop_tree() content = lxml.html.tostring(fragment, encoding="unicode") return content
def tag_errors(tag, root): for i in root.cssselect(tag): name = i.attrib.get('name', None) value = form_errors.get(name, None) if value is not None: p = E.P(value) p.set('class', 'help-block error') i.addnext(p)
def states(self): """Return an ordered-list of states, for each report.""" for report in self.data['reports']: annotations = E.OL({'class': 'states'}) prevline = None lineno_to_index = {} index = -1 for state in report['states']: if not state['location'] or not state['message']: continue line = state['location'][0]['line'] state = E.P(state['message']) # We try to combine with the previous state. if line != prevline: child = E.LI({'data-line': str(line)}) annotations.append(child) index += 1 child.append(state) lineno_to_index[line] = (index, child) prevline = line for note in report['notes']: line = note['location'][0]['line'] note = E.P({'class': 'note'}, note['message']) # Put this note on the last matching state, if possible for ann in reversed(tuple(annotations)): annline = int(ann.attrib['data-line']) if line == annline: ann.append(note) break elif line > annline: ann.addnext( E.LI({'data-line': str(line)}, note) ) break else: annotations.insert(0, E.LI({'data-line': str(line)}, note)) yield annotations, report['message']
def footer(): """make the footer""" return E.E.footer( E.ATTR(id='footer'), E.P(' | '.join(( 'Hackathon 7.0', 'Buck G, Alex M, Jason M', 'Yelp HQ 2012', ))))
def visit_RegisterArray(self, node): """Generate a RegisterArray DIV.""" framebytes = node.framesize * self.wordwidth root = E.DIV(CLASS('regarray'), id="ARRAY_" + node.name) root.append(self.addressparagraph(node)) root.append(self.heading(node.name)) root.append( E.P("Array of {} copies, repeats every {} bytes.".format( node.count, framebytes))) for d in node.description: root.append(E.P(d, CLASS('description'))) with self.tempvars(offset='N*{}+'.format(framebytes), hlev=self.hlev + 1): root.extend(self.visitchildren(node)) return root
def build_locale_content(): main = template_keys['mirror.locale.main'] content = E.DIV(E.P("Browse in ", _locale_link(main), ", or:")) ul = E.UL() for locale in locales: ul.append(E.LI(_locale_link(locale))) content.append(ul) template_keys['mirror.locale.content'] = html.tostring(content).decode( 'utf-8')
def visit_MemoryMap(self, node): """Create an HTML file for a MemoryMap.""" self.title = title = node.name + ' Peripheral Map' an = ((node.size - 1).bit_length() + 3) // 4 # Sweep the document tree to build up the main content with self.tempvars(wordwidth=1, address_nibbles=an, base=node.base, subdir=node.name + '_instances', hlev=2): children = list(self.visitchildren(node)) table = E.TABLE( E.TR(E.TH('Peripheral'), E.TH('Base Address'), E.TH('Size'), E.TH('Description'), *children), CLASS('component_list')) nodes = ([E.H1(title, id='title')] + [E.P(d) for d in node.description] + [E.HR(), table, self.footer(node)]) contentnode = E.DIV(*nodes, id='content') # Add a table of contents sidebar for each table row. instlist = E.UL() for elem in contentnode.xpath("//td[contains(@class, 'peripheral')]"): text = tostring(elem, method="text", encoding="unicode") id = escape(text) elem.attrib['id'] = id node = E.LI(E.A(text, href='#' + id)) instlist.append(node) # And put it all together. return E.HTML( E.HEAD( E.TITLE(title), E.LINK(rel='stylesheet', type='text/css', href=htmlpathjoin(self.styledir, 'reg.css'))), E.BODY( E.DIV(E.DIV(E.P(E.A(title, href='#title')), instlist, id='sidebar'), contentnode, id='wrapper')), )
def parse(self, block): """""" lines = [] for line in block.splitlines(): if line.endswith(" "): lines.append(line[:-2]) lines.append(E.BR()) else: lines.append(line + " ") return E.P(*lines)
def format_msg(msg, status): if msg.text is not None: if "password" in msg.text: """Strip out the test password, just in case the report gets sent around.""" return E.P("Entering password") if status == "FAIL": msg_html = html.fromstring(msg.text) if msg_html.xpath(".//a") != []: href = msg_html.xpath(".//a")[0].get("href") return E.UL( E.CLASS("thumbnails"), E.LI( E.CLASS("span4"), E.A(E.CLASS("thumbnail"), E.IMG(src=href), href=href), ), ) else: return E.P(msg.text) else: return E.P(msg.text)
def default_get(self, fields): result = super(Invite, self).default_get(fields) if 'message' not in fields: return result user_name = self.env.user.display_name model = result.get('res_model') res_id = result.get('res_id') if model and res_id: document = self.env['ir.model']._get(model).display_name title = self.env[model].browse(res_id).display_name msg_fmt = _( '%(user_name)s invited you to follow %(document)s document: %(title)s' ) else: msg_fmt = _('%(user_name)s invited you to follow a new document.') text = msg_fmt % locals() message = html.DIV(html.P(_('Hello,')), html.P(text)) result['message'] = etree.tostring(message) return result
def default_get(self, cr, uid, fields, context=None): result = super(invite_wizard, self).default_get(cr, uid, fields, context=context) user_name = self.pool.get('res.users').name_get(cr, uid, [uid], context=context)[0][1] model = result.get('res_model') res_id = result.get('res_id') message = "" if 'message' in fields and model and res_id: ir_model = self.pool.get('ir.model') model_ids = ir_model.search(cr, uid, [('model', '=', self.pool[model]._name)], context=context) model_name = ir_model.name_get(cr, uid, model_ids, context=context)[0][1] document_name = self.pool[model].name_get(cr, uid, [res_id], context=context)[0][1] message = _('%s invited you to follow %s document: %s.') % (user_name, model_name, document_name) elif 'message' in fields: message = _('%s invited you to follow a new document.') % user_name if message: message = html.DIV( html.P(_('Hello,')), html.P(message) ) result['message'] = etree.tostring(message) return result
def manifest_html(self): """ This is manifest.html the human useable form of the manifest.xml special object to list needed criteria or return a manifest given a set of criteria """ web_page = \ E.HTML( E.HEAD( E.TITLE(_("%s A/I Webserver -- " "Maninfest Criteria Test") % _DISTRIBUTION) ), E.BODY( E.H1(_("Welcome to the %s A/I " "webserver") % _DISTRIBUTION), E.H2(_("Manifest criteria tester")), E.P(_("To test a system's criteria, all " "criteria listed are necessary. The " "format used should be:"), E.BR(), E.TT("criteria1=value1;criteria2=value2"), E.BR(), _("For example:"), E.BR(), E.TT("arch=sun4u;mac=EEE0C0FFEE00;" "ipv4=172020025012;" "manufacturer=sun microsystems") ), E.H1(_("Criteria:")), E.P(str(list(AIdb.getCriteria( self.AISQL.getQueue(), strip=True)))), E.FORM(E.INPUT(type="text", name="postData"), E.INPUT(type="submit"), action="manifest.xml", method="POST" ) ) ) return lxml.etree.tostring(web_page, pretty_print=True)
def visit_Field(self, node): """Create a LI for this field.""" if node.size == 1: bitrange = '[{}]'.format(node.offset) else: bitrange = '[{}:{}]'.format(node.offset + node.size - 1, node.offset) fmt = register_format(node) deftext = bitrange + ' ' + fmt if fmt else bitrange item = E.LI(E.P(node.name + ' ', E.SPAN(deftext, CLASS('fielddef')))) for d in node.description: item.append(E.P(d, CLASS('description'))) if node.space: # We need a definitionlist for the enums dl = E.DL(CLASS('enumlist')) dl.extend(x for itemset in self.visitchildren(node) for x in itemset) item.append(dl) return item