def make_short_papers(p_list, count=None): authors_dict = json.load(open("authors.json")) def gen_author_link(a): if a in authors_dict: return "<a href=\"" + authors_dict[a] + "\">" + a + "</a>" else: return a tag_list = [] for paper in p_list[:count]: authors = gen_author_link(paper.authors[0]) for a in paper.authors[1:-1]: authors += ", " + gen_author_link(a) if len(paper.authors) > 1: authors += " and " + gen_author_link(paper.authors[-1]) links = [] for key in paper.links: links += [builder.A(key, href=paper.links[key])] tag_list.append( builder.DIV( builder.H1(paper.title, builder.CLASS("paper-title")), builder.DIV(html.fromstring(authors), builder.CLASS("paper-authors")), builder.DIV(paper.date.strftime("%d %b. %Y"), builder.CLASS("paper-date")), builder.DIV(*links, builder.CLASS("paper-links")), builder.CLASS("paper-container"))) return tag_list
def list_bans_menu(ban_list, purpose): """need to put bans and boards table creating to a joint function in future""" tablerows = [E.TR(E.TD(str(b.id)), E.TD(b.ip), E.TD(b.initiator), E.TD(time.strftime('%d/%m/%Y %H:%M', time.localtime(b.date))), E.TD(str(b.level)), E.TD(E.BUTTON('Снять', type = 'button', onclick = 'remove_ban(this);')) )for b in ban_list] #purpose will be applyed later html = E.HTML( E.HEAD( E.LINK(rel="stylesheet", href="/css/deeplight.css", type="text/css"), E.TITLE("Creating board"), E.SCRIPT(type = 'text/javascript', src = '/adminscript.js') #js ), E.BODY( E.DIV(E.CLASS('adminupdiv'), E.DIV(E.CLASS('logout'), E.A('Logout', href='/admin/logout')), E.H2(E.CLASS("heading"), "Listing bans"), ), E.TABLE( E.CLASS("boardstable"), E.TR(E.TH('ID'), E.TH('IP'), E.TH('Забанивший'), E.TH('Дата'), E.TH('Уровень'), E.TH('') ), *tablerows ) ) ) return lxml.html.tostring(html)
def body(self): """The BODY of the html document""" reports = E.OL(id='reports') code = self.code() for i, (state_html, state_problem) in enumerate(self.states(), 1): reports.append( E.LI( E.ATTR(id="state{0}".format(i)), E.E.header( E.DIV( E.CLASS('error'), state_problem, ), E.DIV( E.CLASS('report-count'), E.H3('Report'), str(i), ), ), E.DIV( E.CLASS('body'), E.DIV( E.CLASS('source'), deepcopy(code), ), state_html, ), ), ) return E.BODY( self.header(), reports, self.footer(), )
def html(self): from lxml.html import builder as E return E.TABLE( E.CLASS("register"), E.TR(E.TH("Register Table", colspan="3")), *[ E.TR(E.TD(k), E.TD(E.CLASS("fixed"), "%x" % id(v)), E.TD(str(v))) for k, v in self.__p.iteritems() ])
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 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 generate_navigation_board_list(boards): #would be redone categories = {} for b in boards: #if b.hidden = none #should add for hidden boards if b.category in categories: categories[b.category].append((b.address, b.name)) else: categories[b.category] = [((b.address, b.name))] spans = [] iter_list = list(categories.keys()) iter_list.sort() for category in iter_list: #probably another order would be better urls = [] for params in categories[category]: urls.append( E.A( params[0], #the name title = params[1],#bydlocode, better to find another way href = '/' + params[0] + '/', ) ) urls.append('/') urls.pop() if category is None: category = '' urls = [E.CLASS('boardcategory'), category+' ['] + urls + [']'] spans.append(E.SPAN(*urls)) code = EM('nav', E.CLASS('boardlist'), *spans ) return code
def gen_papers(p_list): index = builder.HTML( builder.HEAD(*make_head()), builder.BODY( make_menu(), builder.DIV(builder.H1("Papers", builder.CLASS("section-title")), *make_short_papers(p_list), builder.CLASS("section")))) print(html.etree.tostring(index, pretty_print=True, method='html').decode("utf-8"), file=open(os.path.join(base_path, "papers.html"), "w"))
def html(self): from lxml.html import builder as E return E.DIV( E.CLASS("treeObject tree"+self.__class__.__name__), self.__tag, E.DIV( E.CLASS("treeObject treeLeaf"+self.__class__.__name__), self.__name ) )
def make_short_posts(p_list): tag_list = [] for post in p_list: tag_list.append( builder.DIV( builder.H1( builder.A(post.title, href=base_url + "posts/" + post.id), builder.CLASS("post-title")), html.fromstring(post.summary), builder.DIV(post.date.strftime("%d %b. %Y, %H:%M"), builder.CLASS("post-date")), builder.CLASS("post-container"))) return tag_list
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 header(self): """Make the header bar of the webpage""" return E.E.header( E.ATTR(id='header'), E.DIV( E.ATTR(id='title'), E.H1( E.A( 'GCC Python Plugin', href='http://gcc-python-plugin.readthedocs.org/', ), ), E.DIV( E.ATTR(id='info'), E.SPAN( E.CLASS('label'), 'Filename: ', ), self.data['filename'], E.SPAN( E.CLASS('label'), 'Function: ', ), self.data['function']['name'], ), E.DIV( E.ATTR(id='report-pagination'), E.SPAN( E.CLASS('label'), 'Report: ', ), *( E.A(str(i + 1), href="#state{0}".format(i + 1)) for i in range(len(self.data['reports'])) ) ), E.DIV( E.ATTR(id='prev'), E.IMG( src=data_uri('image/png', 'images/arrow-180.png'), ), ), E.DIV( E.ATTR(id='next'), E.IMG( src=data_uri('image/png', 'images/arrow.png'), ), ), ), )
def board_creation_menu(): #here is the html board creation menu html = E.HTML( E.HEAD( E.LINK(rel="stylesheet", href="/css/deeplight.css", type="text/css"), E.TITLE("Creating board") ), E.BODY( E.DIV(E.CLASS('adminupdiv'), E.DIV(E.CLASS('logout'), E.A('Logout', href='/admin/logout')), E.H2(E.CLASS("heading"), "Create new board"), ), E.DIV(E.CLASS("boardcreateform"), E.FORM( E.INPUT(type = 'hidden', name = 'action', value = 'create'), E.INPUT(type = 'hidden', name = 'instance', value = 'board'), E.TABLE( E.TR(E.TD('Address'), E.TD(E.INPUT(type = 'text', name = 'address', value = '')) ), E.TR(E.TD('Tablename'), E.TD(E.INPUT(type = 'text', name = 'tablename', value = '')) ), E.TR(E.TD('Name'), E.TD(E.INPUT(type = 'text', name = 'name', value = '')) ), E.TR(E.TD('Fullname'), E.TD(E.INPUT(type = 'text', name = 'fullname', value = '')) ), E.TR(E.TD('Description'), E.TD(E.INPUT(type = 'text', name = 'description', value = '')) ), E.TR(E.TD('Pics number'), E.TD(E.INPUT(type = 'number', name = 'picsnum', value = '', min = '0', max = '10')) ), E.TR(E.TD('Bumplimit'), E.TD(E.INPUT(type = 'number', name = 'bumplimit', value = '', min = '0')) ), E.TR(E.TD('Max threads'), E.TD(E.INPUT(type = 'number', name = 'maxthreads', value = '', min = '-1')) ), E.TR(E.TD(E.INPUT(type='checkbox', name='delposts', value='1', checked='checked'), 'Удаление постов', colspan='2', style='text-align:center;')), E.TR(E.TD(E.INPUT(type='checkbox', name='delopposts', value='1', checked='checked'), 'Удаление тредов', colspan='2', style='text-align:center;')), ), E.INPUT(type = 'submit', value = 'Create'), method='POST', action='/admin/' ) ) ) ) return lxml.html.tostring(html)
def list_boards_menu(board_list, purpose): """need to put boards table creating to a separate function in future""" posts_num_cell = E.DIV(E.SPAN('????', style = 'display:inline-block; width:4em; text-align:center;'), E.INPUT(type='number', size='6', min='0', value='1', style = 'width: 6em;'), E.SELECT(E.OPTION('Секунды', value='1'), E.OPTION('Минуты', value='60'), E.OPTION('Часы', value='3600'), E.OPTION('Дни', value='86400', selected='') ), E.BUTTON('GET', onclick='get_posts_num_from_time(this)', type = 'button')) tablerows = [E.TR(E.TD(E.A(b.address, href = '/'+b.address)), E.TD(b.tablename), E.TD(str(b.name)), E.TD(str(b.fullname)), E.TD(str(b.description)), E.TD(str(b.category)), E.TD(str(b.pictures)), E.TD(str(b.bumplimit)), E.TD(str(b.maxthreads)), E.TD(copy.copy(posts_num_cell)) )for b in board_list] #purpose will be applyed later html = E.HTML( E.HEAD( E.LINK(rel="stylesheet", href="/css/deeplight.css", type="text/css"), E.TITLE("Creating board"), E.SCRIPT(type = 'text/javascript', src = '/adminscript.js') #js ), E.BODY( E.DIV(E.CLASS('adminupdiv'), E.DIV(E.CLASS('logout'), E.A('Logout', href='/admin/logout')), E.H2(E.CLASS("heading"), "Listing boards"), ), E.TABLE( E.CLASS("boardstable"), E.TR(E.TH('Адрес'), E.TH('Таблица'), E.TH('Название'), E.TH('Полное название'), E.TH('Описание'), E.TH('Категория'), E.TH('Максимум картинок'), E.TH('Бамплимит'), E.TH('Максимум тредов'), E.TH('Постов за последнее время') ), *tablerows ) ) ) return lxml.html.tostring(html)
def add_tag(self, tag, text="", _class=None, **kwargs): """ :param tag: the HTML container name :type tag: string :param text: :type text: string / list of strings :param _class: the value of the attribute 'class' in the container :type _class: string :param kwargs: :return: """ try: html_tag = getattr(builder, tag.upper()) except AttributeError: return "tag {} doesn't exist in html".format(tag) args = list() if _class is not None: args.append(builder.CLASS(_class)) if isinstance(text, str): e = self.__handle_string_to_html(html_tag, text, *args, **kwargs) return html.tostring(e, encoding="utf-8").decode("utf-8") else: iter_return = list() for t in text: e = self.__handle_string_to_html(html_tag, t, *args, **kwargs) iter_return.append( html.tostring(e, encoding="utf-8").decode("utf-8")) return list(iter_return)
def parse_wiki(tree, title): root = tree.getroot() parser_div = root.xpath("//div[@class='mw-parser-output']")[0] headers = ["h1","h2","h3","h4","h5","h6"] children = parser_div.getchildren() text = "" header = "" html = "" for child in children: if child.tag == "p": text += child.text_content().lstrip().rstrip() elif child.tag in headers: if len(text) > 0: summary = summarize(text, limit=2) html += "<h2>"+header+"</h2><p>"+summary+"</p>" text = "" header = child.text_content().split("[")[0] print(header) # TODO - add style sheet # TODO - format text html_out = E.HTML( E.HEAD( E.TITLE(title) ), E.BODY( E.H1(E.CLASS("heading"), title), lxml.html.fromstring(html) ) ) html_out.getroottree().write(file="summarized-roanoke.html", method="html")
def get_boards(lines, position, hierarchy): divs = [] pattern = re.compile('([^\s]+)\s(.+)') while position < len(lines): line = lines[position] line = line.replace('\n', '') if line == '': position += 1 continue match = pattern.search(line) if match is None: return divs, position hierarchy.append(match.group(1)) cats, pos = get_categories(lines, position + 1, hierarchy) hierarchy.pop() divs.append( E.DIV( E.CLASS('fib'), E.A(match.group(2), id='board', target='main', href='http://' + match.group(1)), E.A('[+]', id='plus', onclick="toggle(this, '" + match.group(2) + "');"))) divs.append(E.DIV(*cats, style="display: none;", id=match.group(2))) position = pos return divs, position
def renew_board_cache(renew_cache_dict=True, renew_thread_cache=True, renew_style_cache=True): global board_cache#should be done by the request global board_cache_navigation global board_cache_main_page global style_cache global stats_cache = "" boards = sess.query(Board).filter().all()#add 'order by' if renew_cache_dict: board_cache = {} for b in boards: board_cache[b.address] = board_cache_class(b) if cf.static_board_footer: board_cache_navigation = cf.board_cache_footer else: board_cache_navigation = generate_navigation_board_list(boards) #there must be html code generated if cf.static_board_main_page: board_cache_main_page = cf.board_cache_main_page else: board_cache_main_page = generate_main_page_board_list(boards)#there must be html code generated start_stats_thread() if renew_style_cache: style_cache = [] for f in os.listdir('css'): fname, ext = os.path.splitext(f) if ext == '.css': style_cache.append(E.LINK(E.CLASS('stylesheetlink'), rel="alternate stylesheet", title=fname, href='/css/'+f, type="text/css"))
def info_table(self): """Table with meta information about this definition.""" table = builder.TABLE(builder.CLASS("definition-info")) for row in self.rows: table.append(row) return table
def get_rendered_for_toc(self, path_override=None): """Returns an html 'ul' element, that represents the entire table of contents for this category. Parameters --------- path_override : str If not None, gets urls for this path. Intended for use by parent categories getting the toc of their children. """ root_elm = E.UL(E.CLASS("toc_category")) # Children categories. nav = self.navigation for child in self.children: if path_override != None: child_path = get_url(path_override, child.file_path) else: child_path = get_url(self.file_path, child.file_path) child_elm = E.LI( E.A(child.name, href=child_path), child.get_rendered_for_toc(path_override=self.path) ) root_elm.append(child_elm) # Assets. for asset in self.assets: asset_path = get_new_ext(asset.path, 'html') if path_override != None: asset_path = get_url(path_override, asset_path) else: asset_path = get_url(self.file_path, asset_path) asset_elm = E.LI( E.A(asset.title, href=asset_path) ) root_elm.append(asset_elm) return root_elm
def span(self): """Assemble the HTML cell object for the match""" section = B.B(self.section) section.tail = ' - "' + self.prefix.lstrip() term = B.B(self.text, B.CLASS("error")) term.tail = self.suffix.rstrip() + '"' return B.SPAN(section, term)
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 title(self): '''Generate html part with following structure <HX> ${Title} </HX> ''' hx = getattr(E, self.data.header_type)( E.CLASS('portlet-audiogallery-title'), self.data.header) return html.tostring(hx)
def generate_right_up_corner_menu(): html = E.SPAN( E.CLASS('rightupmenu'), E.SPAN('[', E.A('Стили', href='#', onclick='stylechanger(this);'), ']'), E.SPAN('[', E.A('Главная', href='/'), ']'), E.SPAN('[', E.A('A', href='/admin'), ']'), ) return html
def htm(self, data): aut = 'Czyzycki, W.; Filo, G.; Domagala, M.;' html = E.DIV( E.CLASS("iso-690"), E.SPAN('' + data[3] + '.', E.I(' ' + data[2] + '.'), ' ' + data[5] + '.', ' ' + str(data[4]) + '.')) fo = open('biblografia.html', 'a') fo.write(lxml.html.tostring(html) + '\n') fo.close()
def get_sections(lines, position, hierarchy): trs = [] pattern = re.compile('(?<=#)(.+)') while position < len(lines): line = lines[position] line = line.replace('\n', '') if line == '': position += 1 continue match = pattern.search(line) if match is None: return trs, position hierarchy.append(match.group(1)) divs, pos = get_boards(lines, position + 1, hierarchy) hierarchy.pop() trs.append(E.TR(E.TD(E.CLASS('header'), match.group(1)))) trs.append(E.TR(E.TD(E.CLASS('list'), *divs))) position = pos return trs, position
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 scene_breaks(root): # Empty <h3> tags appear as a rule on qntm.org for e in root.xpath('.//h3'): if len(e) == 0 and e.text_content().strip() == "": e.addprevious(E.HR()) e.drop_tree() # Other stories use a centered orange * as a scene break for e in root.xpath('.//h4'): if e.text_content().strip() == "*": e.addprevious(E.HR(E.CLASS('scene-break'))) e.drop_tree()
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 gen_posts(p_list): index = builder.HTML( builder.HEAD(*make_head()), builder.BODY(make_menu(), builder.DIV( builder.H1("Posts", builder.CLASS("section-title")), *make_short_posts(p_list), builder.CLASS("section")), style="background-color:#f7f7f7")) print(html.etree.tostring(index, pretty_print=True, method='html').decode("utf-8"), file=open(os.path.join(base_path, "posts.html"), "w")) for post in p_list: html_content = builder.DIV( builder.H1(post.title, builder.CLASS("full-post-title")), builder.DIV(post.date.strftime("%d %B %Y, %H:%M"), builder.CLASS("full-post-date")), builder.DIV(html.fromstring(post.content), builder.CLASS("full-post-content")), builder.CLASS("full-post-container")) page = builder.HTML( builder.HEAD( *make_head(), builder.SCRIPT("", src=base_url + "js/table.js"), ), builder.BODY(make_menu(), html_content)) print(html.etree.tostring(page, pretty_print=True, method='html').decode("utf-8"), file=open( os.path.join(base_path, "posts", post.id, "index.html"), "w"))