def mailgen(): d = DataCmp().cmp_result() news_topic = {'hot_news': '热点新闻', 'hot_inform': '最新公告', 'hot_msg': '最新简讯'} 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()))) sub = [i for i in test()] if not any(news_topic): sub = [E.H2('今日无事可做')] html = E.HTML( E.HEAD( E.META(charset='UTF-8'), E.META(name='viewport', content='width=device-width, initial-scale=1.0'), E.TITLE("GDUTNews Stream")), E.BODY(*sub)) return lxml.html.tostring(html)
def _generate_meadata_elements(self): yield E.META(charset="UTF-8") for name, content in self.metadata.items(): if name == 'title': yield E.TITLE(content) else: yield E.META(name=name, content=content)
def process_file(input_file, output_file=None, encoding='utf-8'): if output_file is None: output_file = input_file + '.html' root = etree.parse(input_file).getroot() item = root.xpath("/items/item")[0] (method,) = item.xpath("method/text()") if method.lower() != "post": raise ValueError("Only POST requests are supported") # TODO (url,) = item.xpath("url/text()") (request,) = item.xpath("request") contents = request.text if request.get("base64"): contents = b64decode(contents) _, body = contents.split("\r\n\r\n", 1) output = E.HTML( E.HEAD(E.META(**{'http-equiv': 'Content-type', 'content': 'text/html; charset=' + encoding})), E.BODY( E.FORM( E.INPUT(type="submit"), *(E.INPUT(type="hidden", name=name, value=value) for name, value in decode_form_urlencoded_values(body, encoding)), action=url, method=method ) ) ) with codecs.open(output_file, 'wb', encoding) as html_output: html_output.write(html.tostring(output, encoding=unicode)) return output_file
def __init__(self, datas, lang, freeze, title, css_path): super().__init__(datas, lang, freeze) self._html = builder.HTML( builder.HEAD( builder.META(charset="utf-8"), builder.TITLE(title), builder.LINK(rel="stylesheet", href=css_path), ), builder.BODY(builder.DIV(id="text_area")), )
def make_head(): head = [ #builder.BASE(href="https://dakovalev1.github.io/my_site/"), #builder.BASE(href="http://localhost/my_site/docs/"), # ONLY FOR DEBUG!!! #builder.BASE(href=sys.argv[1]), builder.META(charset="utf-8"), builder.TITLE("Dmitry Kovalev"), builder.META(name="viewport", content="width=device-width, initial-scale=1"), builder.SCRIPT( "", type="text/javascript", src= "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.4/MathJax.js", async="async"), builder.SCRIPT(open("src/js/mathjax.js").read(), type="text/x-mathjax-config"), builder.SCRIPT( "", src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" ), #builder.SCRIPT("", src="js/jquery.waypoints.min.js"), #builder.SCRIPT("", src="js/jquery.scrollTo.min.js"), builder.LINK( rel="stylesheet", href="https://use.fontawesome.com/releases/v5.8.1/css/all.css", integrity= "sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf", crossorigin="anonymous"), builder.LINK( rel="stylesheet", href= "https://cdn.rawgit.com/jpswalsh/academicons/master/css/academicons.min.css" ), builder.LINK(rel="stylesheet", href=base_url + "css/menu.css"), builder.LINK(rel="stylesheet", href=base_url + "css/common.css"), builder.SCRIPT("", src=base_url + "js/menu.js"), builder.SCRIPT("", src=base_url + "js/scroll.js"), ] return head
def gen_html(encoding=ENCODING): """Create the HTML structure :return: Return the body structure from lxml.html """ body = E.BODY() html = E.HTML( E.HEAD( E.LINK(rel="stylesheet", href=CSSFILE, type="text/css"), E.META(charset=encoding), ), body) return body
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 head(self): """The HEAD of the html document""" head = E.HEAD( E.META({ 'http-equiv': 'Content-Type', 'content': 'text/html; charset=utf-8' }), E.TITLE('%s -- GCC Python Plugin' % self.data['filename']), ) head.extend( E.STYLE( file_contents(css + '.css'), media='screen', type='text/css') for css in ('extlib/reset-20110126.min', 'pygments_c', 'style')) return head
def head(self): """The HEAD of the html document""" head = E.HEAD( E.META({ 'http-equiv': 'Content-Type', 'content': 'text/html; charset=utf-8' }), E.TITLE('%s -- GCC Python Plugin' % self.data['filename']), ) head.extend( E.LINK(rel='stylesheet', href=css + '.css', type='text/css') for css in ('extlib/reset-20110126', 'pygments_c', 'style')) head.extend( E.SCRIPT(src=js + '.js') for js in ('extlib/prefixfree-1.0.4.min', 'extlib/jquery-1.7.1.min', 'script')) return head
def main_page_gen(default_style): html = E.HTML( E.HEAD( E.META(**{'http-equiv':"Default-Style", 'content':default_style, 'id':'stylemetatag'}), E.TITLE("U2ch - Main Page"), E.SCRIPT(type = 'text/javascript', src = '/mainscript.js'), #js *initiate.style_cache ), E.BODY( E.UL(initiate.stats_cache, style = "display: none;", id = "mblstatscache"), E.TABLE( E.CLASS("maintable"), E.THEAD(E.TR(E.TD(E.DIV(E.CLASS("mainslogandiv"), E.SPAN("U2CH"), E.SPAN("", style="display: inline-block; width: 5em;"), E.SPAN("Viewing above imageboards"), ), E.DIV(E.CLASS("mainimagediv"), E.IMG(src="u-2.jpg", style="width:496px;height:334px;"), ), )), id = 'header'), E.TBODY(E.TR(E.TD( E.HR(E.CLASS("delimeter")), E.DIV(E.CLASS("mblcontainer"), E.DIV(E.CLASS("mblcentering"), initiate.board_cache_main_page, ), ), E.HR(E.CLASS("delimeter")), )), id = 'mainpart'), E.TFOOT(E.TR(E.TD( E.DIV('powered by ', E.A('Farlight Imageboard Engine', href='https://github.com/Alpherie/farlight_board_engine', target='_blank', ), id='credentials'), )), id = 'footer'), ), onload = 'mainpagefunc()' ) ) return lxml.html.tostring(html)
async def feeds_html(redis, lid): timeline = int(datetime.now().timestamp()) - fv.FEED_NEWS_TIMELINE df = await get_latest_news(redis, lid, top=fv.FEED_NEWS_TOP, timeline=timeline) html = E.HTML( E.HEAD( E.META(content='text/html', charset='utf-8'), E.LINK(rel='stylesheet', href='../css/style.css', type='text/css'), E.TITLE(E.CLASS('title'), f'{ct.GLOBAL_CHANNELS[lid]}实时新闻摘要'))) body = etree.SubElement(html, 'body') logger.debug( f'html: {lxml.html.tostring(html, pretty_print=True, encoding="utf-8").decode("utf-8")}' ) news_count = 0 for row in df.iterrows(): row = row[1] div = etree.SubElement(body, 'div') h1 = etree.SubElement(div, 'h1', attrib={'class': 'heading'}) a = etree.SubElement(h1, 'a', attrib={'href': row['url']}) a.text = row['title'] p1 = etree.SubElement(div, 'p', attrib={'class': 'time'}) p1.text = row['time'] p2 = etree.SubElement(div, 'p', attrib={'class': 'summary'}) p2.text = row['summary'] logger.debug( f'Append one news to html body, news: {etree.tostring(div, pretty_print=True, encoding="utf-8").decode("utf-8")}' ) news_count = news_count + 1 html_file = os.path.join(ct.DAT_DIR, f'{ct.GLOBAL_CHANNELS[lid]}.html') logger.info(f'Writing html to file: {html_file}, news count: {news_count}') with open(html_file, 'w', encoding='utf-8') as f: f.write( lxml.html.tostring(html, pretty_print=True, encoding='utf-8').decode('utf-8'))
def feeds_html(): for lid in ct.GLOBAL_CHANNELS: df = get_latest_news(lid) html = E.HTML( E.HEAD( E.META(content='text/html', charset='utf-8'), E.LINK(rel='stylesheet', href='../css/style.css', type='text/css'), E.TITLE(E.CLASS('title'), f'{ct.GLOBAL_CHANNELS[lid]}实时新闻摘要') ) ) body = etree.SubElement(html, 'body') for row in df.iterrows(): row = row[1] div = etree.SubElement(body, 'div') h1 = etree.SubElement(div, 'h1', attrib={'class': 'heading'}) a = etree.SubElement(h1, 'a', attrib={'href': row['url']}) a.text = row['title'] p1 = etree.SubElement(div, 'p', attrib={'class': 'time'}) p1.text = row['time'] p2 = etree.SubElement(div, 'p', attrib={'class': 'summary'}) p2.text = row['summary'] with open(os.path.join(ct.DAT_DIR, f'{ct.GLOBAL_CHANNELS[lid]}.html'), 'w', encoding='utf-8') as f: f.write(lxml.html.tostring(html, pretty_print=True, encoding='utf-8').decode('utf-8'))
def __init__(self, datas, title="", log_level="ERROR", css_path="css/styles.css", lang="fr", freeze=False): """ Initializing thanks to a JSON (datas) HTML document head and body created """ self.__lang = lang self.__freeze = freeze self.datas = datas self.log_level = log_level self.__sections = [] self._html = builder.HTML( builder.HEAD( builder.META(charset="utf-8"), builder.TITLE(title), builder.LINK(rel="stylesheet", href=css_path), ), builder.BODY(builder.DIV(id="text_area")), )
def save_html_mail(msg): import os import tempfile basedir = tempfile.mkdtemp() def save_file(fname, content): fname = os.path.join(basedir, fname) if isinstance(content, str): f = open(fname, 'w') else: f = open(fname, 'wb') f.write(content) def name_gen(): i = 1 while True: yield str(i) i += 1 name_it = name_gen() m = msg title = decode_header(m['Subject']) mailtype = m.get_content_type() if mailtype == 'multipart/alternative': mainMail = [m for m in m.get_payload() if m.get_content_type() == 'text/html'][0] mailbody = decode_payload(mainMail) elif mailtype in ('multipart/related', 'multipart/mixed'): mails = m.get_payload() cidMapping = {} for mail in mails: if mail.get_content_type() == 'multipart/alternative': mainMail = [m for m in mail.get_payload() if m.get_content_type() == 'text/html'][0] mailbody = decode_payload(mainMail) elif mail.get_content_type().startswith('text/html'): mailbody = decode_payload(mail) else: try: cid = mail['Content-ID'][1:-1] except TypeError: if mail['Content-Disposition'] and \ mail['Content-Disposition'].find('attachment') != -1: continue raise fname = decode_header(mail.get_filename() or next(name_it)) cidMapping[cid] = fname body = decode_payload(mail, binary=True) save_file(fname, body) elif mailtype == 'text/html': mailbody = decode_payload(m) else: raise NotImplementedError('type %s not recognized' % mailtype) from lxml.html import fromstring, tostring # type: ignore from lxml.html import builder as E div = fromstring(mailbody) for cidLink in div.xpath('//*[starts-with(@src, "cid:")]'): cid = cidLink.get('src')[4:] cidLink.set('src', cidMapping[cid]) div.insert(0, E.TITLE(title)) div.insert(0, E.META(charset='utf-8')) mailbody_b = tostring(div, encoding='utf-8') save_file('index.html', mailbody_b) return os.path.join(basedir, 'index.html')
FIXED = builder.CLASS("fixed") OK = builder.CLASS("ok") CHECK = "\u2713" COLORS = {"-": "LightGoldenrodYellow", "+": "Khakhi", "?": "LightSkyBlue"} RULES = { "*": "font-family: Arial, sans-serif;", "h1": "color: maroon; font-size: 22pt;", "h2": "font-size: 20pt; color: green;", "h3": "background-color: green; color: white; padding: 5px;", "p.ok": "font-size: 16pt; padding-left: 30px;", "pre.fixed, pre.fixed span": "font-family: monospace; font-size: 9pt;", "input.path": "width: 500px;", } RULES = "\n".join([f"{sel} {{ {rules} }}" for (sel, rules) in RULES.items()]) HEAD = builder.HEAD( builder.META(charset="utf-8"), builder.TITLE(TITLE), builder.STYLE(RULES), ) def compare_table(body, name, old, new): items = [] ot = old.tables[name] nt = new.tables[name] if set(ot.cols) != set(nt.cols): ul = builder.UL() item = builder.LI("TABLE STRUCTURE MISMATCH", ul) ul.append(builder.LI(f"old: {ot.cols:!r}")) ul.append(builder.LI(f"new: {nt.cols:!r}")) items.append(item)
""" from lxml import etree from lxml import html from lxml.html import builder as E # A plain template with Bootstrap report = E.HTML( E.HEAD( E.LINK( href=("http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/" "bootstrap-combined.min.css"), rel="stylesheet", ), E.TITLE("OiRA Testing Report"), E.META(name="viewport", content="width=device-width, initial-scale=1.0"), ), E.BODY( E.DIV( E.CLASS("container"), E.DIV(E.CLASS("page-header"), E.H1("OiRA Testing Report")), id="content-area", )), ) 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."""
def create_table(): """HTML generation by lxml.html tree.""" divisions = ['All', 'E', 'CMS', 'T', 'A', 'AE', 'PPD', 'AD/APC', 'TD', 'CD', 'ND', 'LBN', 'Other'] pubtypes = ['All', 'PUB', 'THESIS', 'CONF', 'TM', 'FN', 'SLIDES', 'POSTER'] dates = [YEAR_2, YEAR_1, YEAR, MONTH_2, MONTH_1, MONTH] years = [YEAR_2, YEAR_1, YEAR] months = [MONTH_2, MONTH_1, MONTH] # This is a doctype work around for a lxml.etree bug doctype_wa = etree.parse(StringIO('''<!DOCTYPE html>\n<html>\n</html>''')) head_tag = E.HEAD( E.META({'charset': 'utf-8'}), E.TITLE("FERMILAB RESEARCH AT A GLANCE"), E.STYLE( {'type': 'text/css'}, "td {text-align: right;}", " td.l {text-align: left;padding: 7px;}", " a.t {display: block;}" ) ) body = E.BODY(E.P( E.A("Fermilab Technical Publications", href="http://ccd.fnal.gov/techpubs/fermilab_spires.html") )) tag_h3 = E.H3("FERMILAB RESEARCH AT A GLANCE") tag_p = E.P("Glossary at end.") tag_p_and_i = E.P(E.I("Updated: " + DATE_TIME_STAMP)) body.append(tag_h3) body.append(tag_p) body.append(tag_p_and_i) table = E.TABLE() tag_tr_td = E.TR(E.TD, E.TD("Date")) for division in divisions: if division == 'A': division = 'AT' tag_tr_td.append(E.TD(division)) table.append(tag_tr_td) pub_table_row = E.TR() for pubtype in pubtypes: pub_table_row.append(E.TD(pubtype)) pub_type_datelist = E.TD() year_list = E.UL() month_list = E.UL() for year in years: year_list.append(E.LI(year)) for month in months: month_list.append(E.LI(month)) pub_type_datelist.append(year_list) pub_type_datelist.append(month_list) pub_type_datelist.append(E.UL()) pub_table_row.append(pub_type_datelist) pub_type_datelist = E.TD() for division in divisions: tdg = E.TD() list_of_searches = E.UL() for date in dates: if division == 'All': division = '' if pubtype == 'All': pubtype = '' search = 'find r fermilab ' + pubtype if division == 'Other': for good_division in divisions[1:len(divisions)-1]: if good_division == 'AD/APC': search += ' not (r AD or APC)' else: search += ' not r ' + good_division elif division == 'AD/APC': search = 'find r fermilab ' + pubtype + \ ' and (r AD or APC)' else: search += ' ' + division search += ' and de ' + date search = re.sub(r'\s+', ' ', search) result = perform_request_search(p=search, cc="Fermilab") result = len(result) if result == 0: hit_number = E.LI() else: link = search.replace(' ', '+') link = 'https://inspirehep.net/search?cc=Fermilab&p=' + link link += '&rg=100&sf=earliestdate' hit_number = E.LI(E.A({'class': 't'}, str(result), href=link)) list_of_searches.append(hit_number) if date == YEAR or date == MONTH: tdg.append(list_of_searches) list_of_searches = E.UL() pub_table_row.append(tdg) table.append(pub_table_row) pub_table_row = E.TR() glos = E.H4('Glossary') table2 = E.TABLE() glos_tr_td = E.TR( E.TD({'class': 'l'}, "E: Experimental papers"), E.TD({'class': 'l'}, "PPD: Particle Physics Division papers") ) table2.append(glos_tr_td) glos_tr_td = E.TR(E.TD({'class': 'l'}, "T: Particle Physics Division \ Theoretical Physics Department papers"), E.TD({'class': 'l'}, "AD/APC: Accelerator Division \ and Accelerator Physics Center papers")) table2.append(glos_tr_td) glos_tr_td = E.TR(E.TD({'class': 'l'}, "AT: Fermilab Center for Particle \ Astrophysics theoretical papers"), E.TD({'class': 'l'}, "TD: Technical Division papers")) table2.append(glos_tr_td) glos_tr_td = E.TR(E.TD({'class': 'l'}, "AE: Fermilab Center for Particle \ Astrophysics experimental papers"), E.TD({'class': 'l'}, "CD: Computing Sector papers")) table2.append(glos_tr_td) glos_tr_td = E.TR(E.TD({'class': 'l'}, "ND: Neutrino Division papers"), E.TD({'class': 'l'}, "LBN: Long Baseline Neutrino \ Sector papers")) table2.append(glos_tr_td) glos_tr_td = E.TR(E.TD({'class': 'l'}, " "), E.TD({'class': 'l'}, " ")) table2.append(glos_tr_td) glos_tr_td = E.TR(E.TD({'class': 'l'}, " "), E.TD({'class': 'l'}, " ")) table2.append(glos_tr_td) glos_tr_td = E.TR(E.TD({'class': 'l'}, " "), E.TD({'class': 'l'}, " ")) table2.append(glos_tr_td) glos_tr_td = E.TR(E.TD({'class': 'l'}, "PUB: Paper intended for \ publication in a journal"), E.TD({'class': 'l'}, "FN: Physics note - short paper not \ fitting the other categories")) table2.append(glos_tr_td) glos_tr_td = E.TR(E.TD({'class': 'l'}, "CONF: Paper written as part of a \ conference"), E.TD({'class': 'l'}, "SLIDES: Slides presented at a \ conference or lecture")) table2.append(glos_tr_td) glos_tr_td = E.TR(E.TD({'class': 'l'}, "THESIS: Ph.D. thesis based on \ work done at Fermilab"), E.TD({'class': 'l'}, "POSTER: Poster presented at a \ conference")) table2.append(glos_tr_td) glos_tr_td = E.TR(E.TD({'class': 'l'}, "TM: Technical memo"), E.TD({'class': 'l'}, "")) table2.append(glos_tr_td) body.append(table) body.append(glos) body.append(table2) doctype_wa.getroot().append(head_tag) doctype_wa.getroot().append(body) out = lxml.html.tostring(doctype_wa, encoding='UTF-8', pretty_print=True, method='html').rstrip('\n') return out
termin["modul"] = modulname termin["studiengang"] = studiengaenge[counter] termin["id"] = id id += 1 tabelle.append(termin) #if "s_termin_typ" in termin: # print(" " + termin["s_termin_typ"]) # print(" " + termin["s_termin_von"] + " - " + termin["s_termin_bis"]) counter += 1 # Alle Module eingeladen, hoffe ich htmlfile = E.HTML( E.HEAD(E.LINK(rel="stylesheet", href="plan.css", type="text/css"), E.SCRIPT(src="plan.js", type="text/javascript"), E.META(charset="utf-8"), E.TITLE("Test")), E.BODY(E.H1("Stundenplan"))) document = htmlfile.find("body") for studiengang in studiengaenge: print(studiengang) document.append(E.H2(studiengang, name=studiengang)) container = E.DIV(E.CLASS("plancontainer")) # E.DIV(E.CLASS("plancontainer")) for stunde in range(59): style = "top: " + str(2 + (100 / 60) * stunde) + "%; " mnt = str(int((stunde + 2) % 4 * 15)) if mnt == "0": mnt = "00"