def add_to_output(html_doc, para): global new_lines_in_raw # we shouldn't accept empty paragraph (?) assert len(para) > 0 with html_doc.body.children[-1]: assert html_doc.body.children[-1]['class'] == 'container-fluid' for (i, (type, text)) in enumerate(para): if 'heading' in type and text.strip(): # tags.p() # tags.p() heading = sizes.get_heading_type(size_kind) print type, text heading(text) elif type == "new_line": new_lines_in_raw += 1 if new_lines_in_raw == 1: tags.br() elif new_lines_in_raw == 2: tags.p() else: pass elif is_subject(para, i): if not is_prev_subject(para, i): # tags.p() #tags.br() pass subject(html_doc, type, text) else: regular(type, text) if type != "new_line": new_lines_in_raw = 0
def add_to_output(html_doc, para): global new_lines_in_raw # we shouldn't accept empty paragraph (?) assert len(para) > 0 with html_doc.body.children[-1]: assert html_doc.body.children[-1]['class'] == 'container-fluid'; for (i, (type, text)) in enumerate(para): if 'heading' in type and text.strip(): # tags.p() # tags.p() heading = sizes.get_heading_type(size_kind) print type, text heading(text) elif type == "new_line": new_lines_in_raw += 1 if new_lines_in_raw == 1: tags.br() elif new_lines_in_raw == 2: tags.p() else: pass elif is_subject(para, i): if not is_prev_subject(para, i): # tags.p() #tags.br() pass subject(html_doc, type, text) else: regular(type, text) if type != "new_line": new_lines_in_raw = 0
def add_images(self, ims, txts, links, width=400): """add images to the HTML file Parameters: ims (str list) -- a list of image paths txts (str list) -- a list of image names shown on the website links (str list) -- a list of hyperref links; when you click an image, it will redirect you to a new page """ self.t = table(border=1, style="table-layout: fixed;") # Insert a table self.doc.add(self.t) with self.t: with tr(): for im, txt, link in zip(ims, txts, links): with td(style="word-wrap: break-word;", halign="center", valign="top"): with p(): with a(href=os.path.join('images', link)): #img(style="width:%dpx" % width, src=os.path.join('images', im)) img(style="width:%dpx" % width, src=os.path.join(self.folder, im)) br() p(txt)
def test_other(self): c = Converter('Man', file='diff.man') c.translate() text = c.html.render() text = c.change_special_symbols(text) doc = tags.html(lang='en') doc = add_head(doc) doc_body = tags.body() row = tags.div(cls='row') row = add_row(row, 'GCC(1)', '', 'GCC(1)') doc_body.add(row) with doc_body: paragraph = tags.p() paragraph.add(tags.br()) paragraph.add('\n\\f(CW c c-header cpp-output\\fP') paragraph.add(tags.br()) paragraph.add(tags.br()) def_list = tags.dl() def_termin = tags.dt() def_termin.add('\n') def_termin.add('\\fB-x none\\fR') def_def = tags.dd(cls='indent') def_def.add('\nstandard_output.') def_list.add(def_termin) def_list.add(def_def) row = tags.div(cls='row') row = add_row(row, '', '2018-07-20', 'GCC(1)') doc_body.add(row) doc.add(doc_body) doc = c.change_special_symbols(doc.render()) self.assertEqual(doc, text)
def test_many_argument_strings(self): c = Converter('Man', file='links.man') c.translate() text = c.html.render() text = c.change_special_symbols(text) doc = tags.html(lang='en') doc = add_head(doc) doc_body = tags.body() row = tags.div(cls='row') row = add_row(row, 'GREP(1)', 'User Commands', 'GREP(1)') doc_body.add(row) with doc_body: paragraph = tags.p() paragraph += (tags.a('the bug-reporting address', href='mailto:[email protected]')) paragraph += tags.br() paragraph += (tags.a( 'email archive', href='http://lists.gnu.org/mailman/listinfo/bug-grep')) paragraph += tags.br() paragraph += tags.br() row = tags.div(cls='row') row = add_row(row, 'GNU grep 3.1', '2017-06-21', 'GREP(1)') doc_body.add(row) doc.add(doc_body) self.assertEqual(doc.render(), text)
def add_images(self, epoch, ims, txts, links, width=400): if epoch != -1: img_dir_epoch = os.path.join(self.img_dir, str(epoch)) util.mkdirs(img_dir_epoch) else: img_dir_epoch = self.img_dir path_parts = img_dir_epoch.split('/') if self.is_test: rel_path = path_parts[-1:] else: rel_path = path_parts[-2:] rel_path = '/'.join(rel_path) self.add_table() with self.t: with tr(): for im, txt, link in zip(ims, txts, links): with td(style="word-wrap: break-word;", halign="center", valign="top"): with p(): with a(href=os.path.join(rel_path, link)): img(style="width:%dpx" % width, src=os.path.join(rel_path, im)) br() p(txt)
def add_images(self, ims, txts, links, width=400): """add images to the HTML file Parameters: ims (str list) -- a list of image paths txts (str list) -- a list of image names shown on the website links (str list) -- a list of hyperref links; when you click an image, it will redirect you to a new page """ self.t = table(border=1, style="table-layout: fixed;") # Insert a table self.doc.add(self.t) with self.t: with tr(): for im, txt, link in zip(ims, txts, links): with td(style="word-wrap: break-word;", halign="center", valign="top"): with p(): with a(href=link): img(style="width:%dpx" % width, src=im) br() # attrs_path = os.path.join(self.web_dir,'attributes', txt) # with open(attrs_path, "r") as attr_log_file: # attrs_str =attr_log_file.read() # save the message # attrs_str.replace('\n','<br>') text(txt)
def add_image(self, im, title, txt='', width=None, height=None, font_pct=100): if width is None: width = self.default_image_width if height is None: height = self.default_image_height if self.t is None or self.row_image_count >= self.images_per_row: self._add_table() with self.t: # with tr(): # with td(style="word-wrap: break-word;", halign="center", valign="top"): with tags.td(halign="center", valign="top"): with tags.p(): if height is not None: img_style = "width:{}px; height:{}px".format( width, height) else: img_style = "width:{}px;".format(width) tags.img(style=img_style, src=r'data:image/png;base64,' + self._encode_image(im)) tags.br() tags.p( "{}\n{}".format(title, txt), style= 'width:{}px; word-wrap: break-word; white-space: pre-wrap; font-size: {}%;' .format(width, font_pct)) self.row_image_count += 1
def add_videos(self, vids, txts, links, width=400): """add images to the HTML file Parameters: ims (str list) -- a list of image paths txts (str list) -- a list of image names shown on the website links (str list) -- a list of hyperref links; when you click an image, it will redirect you to a new page """ self.t = table(border=1, style="table-layout: fixed;") # Insert a table self.doc.add(self.t) with self.t: with tr(): for vid, txt, link in zip(vids, txts, links): with td(style="word-wrap: break-word;", halign="center", valign="top"): with p(): with a(href=os.path.join('videos', link)): with video(style="width:%dpx" % width, controls=True): source(src=os.path.join('videos', vid), type="video/mp4") br() p(txt)
def index(): removed = list((REPORTS_PATH / "removed").iterdir()) added = list((REPORTS_PATH / "added").iterdir()) diff = list((REPORTS_PATH / "diff").iterdir()) title = "UI changes from master" doc = dominate.document(title=title) with doc: h1("UI changes from master") hr() h2("Removed:", style="color: red;") i("UI fixtures that have been removed:") html.report_links(removed, REPORTS_PATH) br() hr() h2("Added:", style="color: green;") i("UI fixtures that have been added:") html.report_links(added, REPORTS_PATH) br() hr() h2("Differs:", style="color: grey;") i("UI fixtures that have been modified:") html.report_links(diff, REPORTS_PATH) return html.write(REPORTS_PATH, doc, "index.html")
def add_br(self, *args): if not self.paragraph: if self.div_subheader: self.div_subheader.add(tags.br()) elif self.div_header: self.div_header.add(tags.br()) else: self.body.add(tags.br()) else: self.paragraph.add(tags.br())
def html(self, root_dir): self.save(root_dir) with p(): with a(href=self.relative_save_path): img_style = "image-rendering:pixelated;" if self.display_height is not None: img_style += 'height:{}px;'.format(self.display_height) img(style=img_style, src=self.relative_save_path) if self.desc is not None: br() pre(self.desc)
def add_images(self, ims, txts, links, width=400, height=300): self.add_table() with self.t: with tr(): for im, txt, link in zip(ims, txts, links): with td(style="word-wrap: break-word; height:{}px; width:{}px".format(height + 10,width + 10), halign="center", valign="top"): with p(): with a(href=os.path.join('/',link)): img(style="width:{}px;height:{}".format(width, height), src=os.path.join('/',im)) br() p(txt)
def add_images(self, ims, txts, links, width=400): self.add_table() with self.t: with tr(): for im, txt, link in zip(ims, txts, links): with td(style="word-wrap: break-word;", halign="center", valign="top"): with p(): with a(href=os.path.join('images', link)): img(style="width:%dpx" % width, src=os.path.join('images', im)) br() p(txt)
def add_images(self, ims, txts, links, width=400): self.t = table(border=1, style="table-layout: fixed;") # Insert a table self.doc.add(self.t) with self.t: with tr(): for im, txt, link in zip(ims, txts, links): with td(style="word-wrap: break-word;", halign="center", valign="top"): with p(): with a(href=os.path.join('images', link)): img(style="width:%dpx" % width, src=os.path.join('images', im)) br() p(txt)
def add(self, columns, no_tqdm=False, display=False): if display: no_tqdm = True for i in tqdm(range(0, len(columns), self.ncols), disable=no_tqdm): T = self.html.add_table(transpose=True) if self.header: T.set_header(**self.header) for column in columns[i : min(i + self.ncols, len(columns))]: T.add_col(**column) if display: display_html(T) with self.html.doc: br() return self
def add_footnote_to_output(paragraphs): with tags.li(): for (para) in paragraphs: for (run) in para.runs: style = footer.get_style(run) if style == "bolded": with tags.span(run.text): tags.attr(cls="sub-subject_small") else: with tags.span(run.text): tags.attr(cls="definition_small") tags.br()
def _add_shopping_links(container, record): s = t.div(t.a(t.img(src=_lurl(record['resource_source_logo'])), href=record['url'], target='_blank'), cls='shopping_links') if record['instance_note']: s += '(' + record['instance_note'] + ')' container += t.br() container += s container += t.br() else: container += s
def add_summary(self, dict_): """ Add a summary with key value pairs from the dictionary """ self.doc += br() self.doc += hr() self.doc += br() self.add_header("Test Summary") self.doc += br() for k, v in dict_.items(): self.doc += p(b(k), ' = ', str(v)) self.doc += br()
def add_verbatim_begin(self, *args): self.add_paragraph() first_time = True while True: if first_time: self.paragraph.add(tags.br()) first_time = False next_line = next(self.iterator) if next_line.startswith('.Ve'): break if not self.process_line(next_line): self.add_roman(next_line) self.paragraph.add(tags.br())
def build_html(parsed_tokens, css_style): with document(title="Code") as doc: style(css_style) for token in parsed_tokens: if token.type == line_break: br() else: lines = token.content.split('\n') for j in range(len(lines)): if j != 0: br() span(lines[j], cls=token.type) return doc.render(pretty=False)
def add_images(self, ims, txts, links, width=400): """Add images to table.""" table = self.add_table() tr = table.add(htags.tr()) with tr: for im, txt, link in zip(ims, txts, links): with htags.td(style="word-wrap: break-word;", halign="center", valign="top"): with htags.p(): with htags.a(href=os.path.join('images', link)): htags.img(style="width:%dpx" % width, src=os.path.join('images', im)) htags.br() htags.p(txt)
def add_images_filterchart(self, ims, txts, links, width=400): self.add_table() with self.t: with tr(): for im, txt, link in zip(ims, txts, links): with td(style="word-wrap: break-word;", halign="center", valign="top"): with p(): with a(href=link): img(style="width:%dpx" % width, src=link) br() splitted = txt.split('separator') if len(splitted) > 1: for h in splitted: p(h) else: p(txt)
def add_text(self, value): # FIXME(zpzhou): ugly! remove this hack when have time. re_full = r'(<span style="color:.*">.*</span>)' re_part = r'<span style="color:(.*)">(.*)</span>' value = str(value) with p(style="line-height: 1.0; font-family: monospace"): for line in value.split('\n'): for part in re.split(re_full, line): found = re.findall(re_part, part) if found: color, txt = found[0] if color == 'red': color = '#DC6A73' if color == 'green': color = '#88AE6D' if color == 'yellow': color = '#E4CA6B' span(txt, style=f'color:{color}') else: text(part) br()
def create_html_news_entry(news, doc_html, internet): """Create html for news entry""" images_links = news.links[1] all_links = news.create_list_of_links() with doc_html: tag.h1(news.title) with tag.p(news.date): tag.br() tag.a("Link to this article", href=news.link) if internet: for link in images_links: tag.img(src=link) else: for link in images_links: tag.a("Image link", href=link) tag.p(news.text) for num, link in enumerate(all_links): tag.a("Link №{}".format(num + 1), href=link) tag.br() return doc_html
def show_hide_div(divname: str, hide=False): """ Creates a show/hide button and matching div block :param divname: unique name of the div block :param hide: the div block is hidden by default if True :return: the div block """ if hide: hidden.append(divname) else: shown.append(divname) tags.button("Show / Hide", onclick="hideButton('" + divname + "')") tags.br() if hide: return tags.div(id=divname, style="display:none") return tags.div(id=divname)
def add_images(self, ims, txts, links, width=400): """add images to the HTML file Parameters: ims (str list) -- a list of image paths txts (str list) -- a list of image names shown on the website links (str list) -- a list of hyperref links; when you click an image, it will redirect you to a new page """ from dominate.tags import meta, h3, table, tr, td, p, a, img, br self.t = table(border=1, style="table-layout: fixed;") # Insert a table self.doc.add(self.t) with self.t: with tr(): for im, txt, link in zip(ims, txts, links): with td(style="word-wrap: break-word;", halign="center", valign="top"): with p(): with a(href=link): img(style="width:%dpx" % width, src=im) br() p(txt)
def deal_with_newlines(text): ''' Add a line break for newline characters. ''' if "\n" not in text: return text else: chunks = [dt.span(t) for t in text.split("\n")] tag_list = [] for i, chunk in enumerate(chunks): tag_list.append(chunk) if i != len(chunks) - 1: tag_list.append(dt.br()) return tag_list
def create_html_news_entry_for_epub(news, doc_html, internet, list_of_image_objects): """Create html for epub news entry""" images_links = news.links[1] all_links = news.create_list_of_links() with doc_html: tag.h1(news.title) with tag.p(news.date): tag.br() tag.a("Link to this article", href=news.link) if internet: for image_object in list_of_image_objects: tag.img(src=image_object.file_name) else: for link in images_links: if link: tag.a("Image link", href=link) tag.p(news.text) for num, link in enumerate(all_links): tag.a(f"Link №{num+1}", href=link) tag.br() return doc_html
def add_videos(self, vids, txts, links, width=400, hidden_tag="hidden"): """add images to the HTML file Parameters: vids (str list) -- a list of image paths txts (str list) -- a list of image names shown on the website links (str list) -- a list of hyperref links; when you click an image, it will redirect you to a new page """ self.t = table(border=1, style="table-layout: fixed;") # Insert a table self.doc.add(self.t) colors = ["red", "blue", "gold", "salman"] with self.t: with tr(): for vid, txt, link in zip(vids, txts, links): td_style = "word-wrap: break-word; width:{}px".format(width) with td(style=td_style, halign="center", valign="top"): with p(): vid_path = str(vid) if vid_path == hidden_tag: p_style = "font-weight: bold; width:{}px;" p_style = p_style.format(width * 3) p("hidden video", style=p_style) else: with a(href=str(link)): with video(): attr(controls="controls") source(src=vid_path, type="video/mp4") br() rows = txt.split("<br>") for idx, row in enumerate(rows): color = colors[idx % len(colors)] bold_tag = "<b>" if not row.startswith(bold_tag): s_style = "color:{};".format(color) else: s_style = "color:black; font-weight: bold;" row = row[len(bold_tag):] span(row, style=s_style)
def save_feed_to_html(self): """Creating an html file, using curent datetime as a filename""" logging.info('Started saving feed to html file') time_now = str(datetime.datetime.now()) time_for_path = time_now[:-16] + '_' + time_now[ -15:-13] + '-' + time_now[-12:-10] + '-' + time_now[-9:-7] html = dominate.document(title="HTML RSS feed") with html.head: dtags.meta(charset='utf-8') html += dtags.h1(self.feed_name) for article_number, article in enumerate(self.articles): html += dtags.br() date = article.published str_date = f'{date.tm_year}/{date.tm_mon}/{date.tm_mday} {date.tm_hour}:{date.tm_min}' html += dtags.h2(f'{article_number + 1}. {article.title}') html += dtags.h3(f' {str_date}') html += dtags.a(f'Link: {article.link}') html += dtags.br() with html: if check.internet_connection_check(): # if have internet access, downloading images and pasting in a html file for link in article.media: dtags.img(src=link['url']) else: # if no, paste links of these images dtags.a('Image links:') for link_number, link in enumerate(article.media): img_url = link['url'] dtags.a(f'{link_number + 1}. {img_url}', href=link) html += dtags.p(article.summary) html += dtags.br() with open('html_feeds/' + time_for_path + ' RSS_feeds.html', 'w') as html_file: html_file.write(str(html)) logging.info('Finished saving feed to html file')
def __init__(self, *args, field=None, **kwargs): self.kclass_dep = KClassDep('form-group') if field is not None: if field.errors: self.kclass_dep.append('has-error') if field.flags.required: self.kclass_dep.append('required') super().__init__(**self.update_kwargs(kwargs)) if field is not None: self.add( raw(str(field.label)), br(), raw(field(class_='form-control')), ) if field.errors: for error in field.errors: self.add(p(error, _class='help-block'))
def hocr(records, image_name=u'', image_size=(0, 0), line_bbox=True, split_words=True, word_bbox=True, char_cuts=True, confidence_vals=True): """ Merges a list of predictions and their corresponding character positions into an hOCR document. Args: records (iterable): List of kraken.rpred.ocr_record image_name (unicode): Name of the source image image_size (tuple): Dimensions of the source image line_bbox (bool): Enable writing of line bounding boxes split_words (bool): Split recognized line into words at non-alphanumeric characters word_bbox (bool): Enable writing of word bounding boxes (only with split_words) char_cuts (bool): Enable writing of character cuts (only with line_bbox) confidence_vals (bool): Enable writing of confidence values for recognition results """ doc = dominate.document() with doc.head: meta(name='ocr-system', content='kraken') meta(name='ocr-capabilities', content='ocr_page ocr_line ocrx_word') meta(charset='utf-8') with doc: hocr_title = micro_hocr() if image_size > (1, 1): hocr_title.add('bbox', 0, 0, *[str(s) for s in image_size]) if image_name: hocr_title.add(u'image', image_name) with div(cls='ocr_page', title=str(hocr_title)): for idx, record in enumerate(records): logger.debug('Adding record {} - {} to hocr'.format(idx, record.prediction)) with span(cls='ocr_line', id='line_' + str(idx)) as line_span: line_title = micro_hocr() if line_bbox: line_title.add('bbox', *max_bbox(record.cuts)) if char_cuts: line_title.add('cuts', *list(delta(max_bbox(record.cuts), record.cuts))) # only add if field contains text to avoid unseemly empty # titles if str(line_title): line_span['title'] = str(line_title) if split_words: logger.debug('Splitting record into words') splits = regex.split(u'(\w+)', record.prediction) line_offset = 0 # operate on pairs of non-word character strings and # words. The former are encoded in ocrx_cinfo classes # while the latter is adorned with ocrx_word classes. for w_idx, non_word, word in zip(count(), splits[0::2], splits[1::2]): # add non word blocks only if they contain actual # text if non_word: nw_span = span(non_word, cls='ocrx_block', id='block_' + str(idx) + '_' + str(w_idx)) nw = micro_hocr() if word_bbox: nw.add('bbox', *max_bbox(record.cuts[line_offset:line_offset + len(non_word)])) if confidence_vals: nw.add('x_conf', *[str(int(100*v)) for v in record.confidences[line_offset:line_offset + len(non_word)]]) if str(nw): nw_span['title'] = str(nw) line_offset += len(non_word) w_span = span(word, cls='ocrx_word', id='word_' + str(idx) + '_' + str(w_idx)) w = micro_hocr() if word_bbox: w.add('bbox', *max_bbox(record.cuts[line_offset:line_offset + len(word)])) if confidence_vals: w.add('x_conf', *[str(int(100*v)) for v in record.confidences[line_offset:line_offset + len(word)]]) if str(w): w_span['title'] = str(w) line_offset += len(word) else: line_span.add(record.prediction) br() return doc