def go(d, out): d0 = os.path.dirname(out) artefacts = get_artefacts(d0, d) print "\n".join(map(str, artefacts)) links = get_links_from_artefacts(artefacts) body = Tag(name='body') body.append(links) html = Tag(name='html') head = Tag(name='head') meta = Tag(name='meta') meta.attrs['content'] = "text/html; charset=utf-8" meta.attrs['http-equiv'] = "Content-Type" style = Tag(name='style') style.append(""" body { column-count: 3; } """) head.append(meta) html.append(head) html.append(body) write_data_to_file(str(html), out)
def process_sections(session, package_name: str, filename: str, soup): for section_h2 in soup.select('h2'): section_h2_text = section_h2.text res = re.search(r'(Card|Namelist):\s+&?(\S+)', section_h2_text) if not res: continue section_name = res.group(2) name = '%s.%s' % (package_name, section_name) anchor_name = generate_anchor_name('Section', section_name) anchor = Tag(name='a', builder=soup.builder, attrs={ 'name': anchor_name, 'class': 'dashAnchor' }) uuid_anchor = Tag(name='a', builder=soup.builder, attrs={ 'name': str(uuid.uuid4()), 'class': 'dashUUIDAnchor' }) path = '%s#%s' % (filename, anchor_name) section_h2.insert_before(anchor) section_h2.insert_before(uuid_anchor)
def process_variables(session, package_name: str, filename: str, soup): for variable_th in soup.select('tr > th'): if not variable_th.has_attr('style'): continue if 'background: #ffff99; padding: 2 2 2 10;' not in variable_th[ 'style']: continue variable_name = variable_th.contents[0] if type(variable_name) != bs4.element.NavigableString: continue variable_name = str(variable_name).strip() variable_name = re.sub('\s+', ' ', variable_name) if variable_name == '': continue name = '%s.%s' % (package_name, variable_name) anchor_name = generate_anchor_name('Variable', variable_name) anchor = Tag(name='a', builder=soup.builder, attrs={ 'name': anchor_name, 'class': 'dashAnchor' }) uuid_anchor = Tag(name='a', builder=soup.builder, attrs={ 'name': str(uuid.uuid4()), 'class': 'dashUUIDAnchor' }) path = '%s#%s' % (filename, anchor_name) variable_th.insert_before(anchor) variable_th.insert_before(uuid_anchor)
def renames_to_html(self, table_renames): renamesdf = table_renames[0] columns = renamesdf.columns soup = BeautifulSoup("", "html.parser") table = Tag(soup, name="table") table["class"] = "blueTable" table["id"] = "divrenames" tr = Tag(soup, name="tr") table.append(tr) for col in columns: th = Tag(soup, name="th") tr.append(th) th.append(col) for index, row in renamesdf.iterrows(): tr = Tag(soup, name="tr") for col in columns: td = Tag(soup, name='td') td.insert(1, row[col]) tr.append(td) table.append(tr) soup.append(table) return soup
def add_mathjax_call(soup): head = soup.find('head') if not head: msg = 'Could not find <head>' raise_desc(ValueError, msg, s=str(soup)) src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-MML-AM_CHTML' config = r""" MathJax.Hub.Config({ extensions: ["tex2jax.js"], jax: ["input/TeX", "output/HTML-CSS"], tex2jax: { inlineMath: [ ['$','$'], ], displayMath: [ ['$$','$$'], ["\\[","\\]"] ], processEscapes: true }, "HTML-CSS": { availableFonts: ["TeX"] } }); """ script = Tag(name='script') script['type'] = 'text/x-mathjax-config' script.append(config) head.append(script) script = Tag(name='script') script.attrs['src'] = src head.append(script)
def create_tag(parent, tag_name, class_name=None, id_number=None): """ Create a new tag with tag_name and its attribute attrs_name and append it as the child of the parent tag. params: parent -- the parent of the created tag tag_name -- name of the tag. For example, <div> has name "div" attrs_name -- optional. Name of the tag's attribute. For example, <div class="answer"> has attribute_name "answer">. returns: The created tag. """ if (id_number): child = Tag(name=tag_name, attrs={ 'class': class_name, 'id': id_number }) if class_name: child = Tag(name=tag_name, attrs={'class': class_name}) else: child = Tag(name=tag_name) try: parent.append(child) except AttributeError: print("Object " + str(type(parent)) + " does not have attribute append.") except Exception as detail: print("Get exception when appending tag:", detail) return child
def generate_card(card: cards.Card): article = Tag(name="article", attrs={'class': 'playing-card', 'data-ability-requirement': str(card.ability_requirement)}) if card.full_image is not None: article['style'] = 'background-blend-mode: exclusion; background-image: url("data:image/{};base64,{}"); background-size: cover; background;'.format(card.full_image.mime_type, card.full_image.base64_data) header = generate_header(card) article.append(header) article.append(generate_card_figure(card)) section = Tag(name="section") if card.special_quote is not None: blockquote = Tag(name="blockquote") blockquote.append(NavigableString(card.special_quote)) section.append(blockquote) if card.flavour is not None: p_flavour = Tag(name="p") section.append(p_flavour) p_flavour['class'] = "flavour" p_flavour_string = NavigableString(card.flavour) p_flavour.append(p_flavour_string) [section.append(generate_description_line(description_line)) for description_line in card.description] [section.append(swv_section) for swv_section in generate_stats_with_values(card.stat_blocks)] article.append(section) for granted_action in card.grants_actions: action_section = Tag(name="section") article.append(generate_header(granted_action)) article.append(generate_sub_figure(granted_action)) [action_section.append(generate_description_line(description_line)) for description_line in granted_action.description] [action_section.append(swv_section) for swv_section in generate_stats_with_values(granted_action.stat_blocks)] article.append(action_section) article.append(generate_footer(card)) return article
def node_html_tag(node, line_no=None): "Should this be in scan⠶lever" pre_name = node.prefix._name_ pre = node.prefix.str if node.prefix else "" con = node.contents if node.contents else "" suf = node.suffix.str if node.suffix else "" inner = con.strip() if suf != "": suf_span = Tag(name="span") suf_span.append(suf) else: suf_span = "" if pre_name == "SectBreak": t = Tag(name="hr") # never has inner content else: # anything with inner content goes here if pre_name.startswith("Header"): # headers can't go in inline span elements html_tag_name = pre_name.replace("Header", "h") t = Tag(name=html_tag_name) else: t = Tag(name="span") t.append(inner) t.append(suf_span) # Then regardless of whether/not the node has inner content: cls_list = ["Node", f"pre_{pre_name}"] if node.suffix: cls_list.append(f"suf_{node.suffix._name_}") attrs = {"class": " ".join(cls_list), "prefix": pre, "suffix": suf} t.attrs.update(attrs) if line_no is not None: t.attrs.update({"id": f"L{line_no}"}) return t
def make_head(self, css=None, desc=None, og_img=None, tw_card=True): head = Tag(name="head") ct_a = { "http-equiv": "Content-Type", "content": "text/html; charset=utf-8" } head.append(MetaTag(ct_a)) head.append(MetaTag({"charset": "UTF-8"})) vp_a = { "name": "viewport", "content": "width=device-width, initial-scale=1" } head.append(MetaTag(vp_a)) if desc: head.append(MetaTag({"description": desc})) if og_img: head.append(MetaTag({"og:image": og_img})) if tw_card: tw_card_attrs = {"name": "twitter:card", "content": "summary"} head.append(MetaTag(tw_card_attrs)) if css is None: css = self._default_css_param for s in css: # all stylesheets live in the css directory under the site root css_href = str(Path(*[".."] * self.depth) / "css" / f"{s}.css") css_attrs = {"rel": "stylesheet", "href": css_href} style = Tag(name="link", attrs=css_attrs, can_be_empty_element=True) head.append(style) return head
def test__add_urls_if_give_2(self): um = UrlManager("111", "xxx") um.add_urls(1, (Tag(name="a", attrs={'href': '222'}), Tag(name="a", attrs={'href': '333'}))) self.assertEqual(um.get_url_node().url, "111") self.assertEqual(um.get_url_node().url, "xxx222") self.assertEqual(um.get_url_node().url, "xxx333")
def replace_chinese_tag(soup): tags = [] assert isinstance(soup, (Tag, NavigableString)) try: children = list(soup.children) except AttributeError: children = [] if children.__len__() > 1: for tag in children: if not isinstance(tag, (Tag, NavigableString)): continue tags.extend(replace_chinese_tag(tag)) else: match = re.search("[\u4e00-\u9fa5]", soup.text if isinstance(soup, Tag) else str(soup)) try: has_title_attr = 'title' in soup.attrs except AttributeError: has_title_attr = False if not match or has_title_attr: if has_title_attr: soup.string = soup['title'] if re.match("(\s+)?\d{1,2}\.(\s+)?", soup.string if soup.string else ""): p_tag = Tag(name="p") p_tag.insert(0, Tag(name="br")) tags.append(p_tag) tags.append(soup) else: if match: hanzi_pos = soup.string.find(match.group(0)) if hanzi_pos >= 5: soup = soup.string[:hanzi_pos] tags.append(soup) return tags
def addMetaInformation(html, type=None, path=None): """ read and return the information stored in the <meta> tag(s) """ # parse form if os.path.isfile(html): with open(html) as h: form = BeautifulSoup(h, 'lxml') else: form = BeautifulSoup(html, 'lxml') if not form.head: new_tag = Tag(builder=form.builder, name="head") form.find().insert(0, new_tag) # prepend meta information tag new_tag = Tag(builder=form.builder, name="meta", attrs={ "type": type, "path": path }) form.head.insert(0, new_tag) return str(form)
def make_tabs(timeseries): tabs = OrderedDict() import plotly.offline as offline i = 0 for name, tsp in timeseries.items(): assert isinstance(tsp, TimeseriesPlot) div = Tag(name='div') table = Tag(name='table') table.attrs['style'] = 'width: 100%' tr = Tag(name='tr') td = Tag(name='td') td.attrs['style'] = 'width: 15em; min-height: 20em; vertical-align: top;' td.append(get_markdown(tsp.long_description)) tr.append(td) td = Tag(name='td') td.attrs['style'] = 'width: calc(100%-16em); min-height: 20em; vertical-align: top;' import plotly.graph_objs as go import plotly.tools as tools scatters = [] for name_sequence, sequence in tsp.sequences.items(): assert isinstance(sequence, SampledSequence) trace = go.Scatter( x=sequence.timestamps, y=sequence.values, mode='lines+markers', name=name_sequence, ) scatters.append(trace) layout = {'font': dict(size=10), 'margin': dict(t=0)} n = len(scatters) fig = tools.make_subplots(rows=1, cols=n) fig.layout.update(layout) for j, scatter in enumerate(scatters): fig.append_trace(scatter, 1, j + 1) # include_plotlyjs = True if i == 0 else False include_plotlyjs = True res = offline.plot(fig, output_type='div', show_link=False, include_plotlyjs=include_plotlyjs) td.append(bs(res)) i += 1 tr.append(td) table.append(tr) div.append(table) tabs[name] = Tab(title=tsp.title, content=div) return render_tabs(tabs)
def tag(self): tt = Tag(name='table') for r in self.cells: rt = Tag(name='tr') for c in r: rt.append(c.tag()) tt.append(rt) return tt
def heading2table(soup, table, row): """add heading row to table""" tr = Tag(soup, name="tr") table.append(tr) for attr in row: th = Tag(soup, name="th") tr.append(th) th.append(attr)
def row2table(soup, table, row): """ad a row to the table""" tr = Tag(soup, name="tr") table.append(tr) for attr in row: td = Tag(soup, name="td") tr.append(td) td.append(attr)
def nest_tags(names): current = Tag(name=names[0]) root = current for i in range(1, len(names)): new_tag = Tag(name=names[i]) current.append(new_tag) current = new_tag return root
def generate_sub_figure(typed_thing: Union[cards.Action, cards.Card]) -> Tag: figure = Tag(name="figure") figcaption = Tag(name="figcaption") h2 = Tag(name="h2", attrs={'class': 'type'}) h2.append(NavigableString(typed_thing.sub_type.title())) figcaption.append(h2) figure.append(figcaption) return figure
def as_str(self): soup = BeautifulSoup(features="html5lib") html = Tag(soup, name="html") html.append(self.head) body = Tag(name="body") article = self.process_article() body.append(article) html.append(body) return self.preamble + str(html) #.prettify()
def _genPTag(self, text, has_b): tag = Tag(name=u'p', attrs={u'class': u'calibre1'}) if has_b: b_tag = Tag(name=u'b', attrs={u'class': u'calibre3'}) b_tag.append(text) tag.append(b_tag) else: tag.append(text) return tag
def f(html): tag = Tag(name='script', builder=BUILDER) tag.attrs = { 'type': 'text/javascript', 'src': URL, } if not html.head: html.html.insert(0, Tag(name='head')) html.head.append(tag) return html
def generate_footer(card: cards.Card) -> Tag: footer = Tag(name="footer") h3 = Tag(name="h3", attrs={'class': 'level-requirement'}) h3.append(NavigableString(str(card.level_requirement))) footer.append(h3) if card.ammo_type is not None: aside = Tag(name="aside", attrs={"class": "ammunition"}) aside.append(NavigableString(card.ammo_type)) footer.append(aside) return footer
def __call__(self, DOM): tag = Tag(name='script', builder=BUILDER) tag.attrs = { 'type': 'text/javascript', 'src': self.url, } if not DOM.body: DOM.html.insert(0, Tag(name='body')) DOM.body.append(tag) return DOM
def as_str(self): soup = BeautifulSoup(features="html5lib") html = Tag(soup, name="html", attrs={"lang": "en"}) html.append(self.head) body = Tag(name="body") c_tags = self.content if isinstance(self.content, Sequence) else [self.content] for c in c_tags: body.append(c) html.append(body) return self.preamble + str(html) #.prettify()
def generate_description_line(description_line: str) -> Tag: p_description = Tag(name="p") for text_segment in modifier_re.split(description_line): if modifier_re.match(text_segment) is not None: strong_modifier = Tag(name="strong") p_description.append(strong_modifier) strong_modifier.append(NavigableString(text_segment)) else: p_description_string = NavigableString(description_line) p_description.append(p_description_string) return p_description
def f(html): tag = Tag(name='link', builder=BUILDER) tag.attrs = { 'type': 'text/css', 'rel': 'stylesheet', 'href': URL, } if not html.head: html.html.insert(0, Tag(name='head')) html.head.append(tag) return html
def __call__(self, DOM): tag = Tag(name='link', builder=BUILDER) tag.attrs = { 'type': 'text/css', 'rel': 'stylesheet', 'href': self.url, } if not DOM.head: DOM.html.insert(0, Tag(name='head')) DOM.head.append(tag) return DOM
def postprocess(self, text): # Hack for unescape special chars for key, value in markdown2.g_escape_table.iteritems(): text = text.replace(value, key) urls = set(URL_RE.findall(text)) # Treat images as gallery post = BeautifulSoup(text, 'html.parser') imgs = post.find_all('img') gallery = Tag(name='div', attrs={ 'class': "gallery", 'style': 'display: none;', 'id': hashlib.md5(text.encode('utf-8')).hexdigest(), }) img_urls = [img['src'] for img in imgs] for img in imgs: img.extract() img.attrs.update({ 'data-image': img['src'], 'data-description': img['alt'], }) gallery.append(img) # Add url as web rich object wros = '' for url in urls: if HTMLParser().unescape(url) in img_urls: continue try: wro = WebRichObject.objects.create_or_update_from_url(url) if wro.type != 'image': wros += wro.get_widget(video_width="100%", video_height='320px')\ .decode('utf8') else: img = Tag(name='img', attrs={ 'alt': wro.description or '', 'src': wro.url, 'data-image': wro.url, 'data-description': wro.description or '' }) gallery.append(img) except IOError as err: print err post.append(gallery) text = urlize_html(unicode(post)) text += wros return text
def add_likebtn_(soup, likebtn_site_id): sections = 'h1[id],h2[id]' for h in list(soup.select(sections)): id_ = h.attrs['id'] div = Tag(name='div') div.attrs['class'] = 'like_buttons' div.append('Please provide your feedback: ') tag = Tag(name='span') tag.attrs['class'] = 'likebtn-wrapper' tag.attrs['data-identifier'] = 'btn-%s' % id_ tag.attrs['data-site_id'] = likebtn_site_id t = tag.attrs t['data-theme'] = "tick" t['data-white_label'] = "true" t['data-white_label'] = "true" t['data-identifier'] = "f1-%s" % id_ t['data-show_dislike_label'] = "true" t['data-icon_like_show'] = "false" t['data-icon_dislike_show'] = "false" t['data-counter_type'] = "percent" # t['data-popup_disabled'] = "true" t['data-popup_dislike'] = "true" t['data-popup_position'] = "bottom" t['data-popup_html'] = "Thanks for the feedback!" t['data-share_enabled'] = "false" t['data-share_size'] = "small" t['data-item_url'] = "item-url" t['data-item_title'] = 'title' t['data-item_description'] = "item - description" t['data-item_image'] = "item-image" t['data-lazy_load'] = "true" t['data-event_handler'] = "callback" t['data-i18n_like'] = "Great work!" t['data-i18n_dislike'] = "This needs more improvement" # t['data-i18n_after_like'] = "Glad you liked it!" # t['data-i18n_after_dislike'] = "Please help us improve!" t['data-i18n_like_tooltip'] = "This is great content" t['data-i18n_dislike_tooltip'] = "Something does not feel right" # t['data-i18n_unlike_tooltip'] = "dislike - tooltip - after" # t['data-i18n_undislike_tooltip'] = "dislike - tooltip - after" t['data-i18n_share_text'] = "Share this content" script = bs(likebtn_code).script div.append(tag) div.append(script) h.insert_after(div)
def generate_header(headable_thing: cards.Labelled) -> Tag: header = Tag(name="header", attrs={'data-type': headable_thing.label_type()}) h1 = Tag(name="h1") h1.append(NavigableString(headable_thing.name)) header.append(h1) if hasattr(headable_thing, 'action_type'): aside = Tag(name="aside", attrs={'class': 'action-type'}) aside.append(NavigableString(headable_thing.action_type.__str__())) header.append(aside) if hasattr(headable_thing, 'spell_duration'): aside = Tag(name="aside", attrs={'class': 'spell-duration'}) aside.append(NavigableString(headable_thing.spell_duration.__str__())) header.append(aside) return header