def _write_anchor(self, container): """ Write HTML anchor for linking within page :param container: Container element. """ if self._anchor is not None: append_to(container, "a", name=self._anchor)
def test_render(): el = h.root() h.append_to(el, 'tag1') output = h.render(el, pretty=False) # Non-pretty output should not have line breaks assert output.find('\n') < 0 output = h.render(el, pretty=True) assert output.find('\n') > 0
def render_html(self, pretty=True, static_output=False, header_block=None, footer_block=None, pdf_page_size="A4"): """Returns html output of the block :param pretty: Toggles pretty printing of the resulting HTML. Not applicable for non-HTML output. :return html-code of the block """ # Render the contents html = root("html", doctype="html") head = append_to(html, "head") head = append_to(head, "meta", charset='utf-8') body = append_to(html, "body") # Make sure that the main style sheet is always included resource_deps = DependencyTracker(default_css_main) if header_block is not None: header_block._write_block(body, Cfg(), id_generator(), resource_deps=resource_deps, static_output=static_output) self._write_block(body, Cfg(), id_generator(), resource_deps=resource_deps, static_output=static_output) if footer_block is not None: footer_block._write_block(body, Cfg(), id_generator(), resource_deps=resource_deps, static_output=static_output) script_inflate.write(head) script_block_core.write(head) if static_output: # Add the load wait poller if there are any JS resources js_elem(body, "var loadWaitPoller=runWaitPoller();") # Write out resources for res in resource_deps: res.write(head) # Render the whole document (the parent of the html tag) content = render(html.parent, pretty=pretty) return content
def _write_block(self, parent, parent_cfg, id_gen, resource_deps=None, static_output=False): """ Writes out the block into the supplied stream, inheriting the parent_parameters. :param parent: Parent element :param parent_cfg: Parent parameters to inherit. :param id_gen: Unique ID generator. :param resource_deps: Object used to register resource dependencies. :param static_output: A value of True signals to blocks that the final output will be a static format. Certain dynamic content will render with alternate options. """ if resource_deps is not None: for res in self.resource_deps: resource_deps.add(res) actual_cfg = self._combine_parent_cfg(parent_cfg) if self.container_tag is not None: container = append_to(parent, self.container_tag) self._write_container_attrs(container, actual_cfg) else: container = parent self._write_anchor(container) self._write_title(container) self._write_contents(container, actual_cfg, id_gen, resource_deps=resource_deps, static_output=static_output)
def _write_contents(self, container, actual_cfg, id_gen, static_output=False, **kwargs): plot_container = append_to(container, "div") plot_container["id"] = plot_container_id = next(id_gen) # Write the config to the plot target as well self._write_container_attrs(plot_container, actual_cfg) self._write_plot(plot_container, plot_container_id, id_gen, static_output)
def _write_contents(self, container, *args, **kwargs): src = StringIO() src.write("data:image/{};base64,".format(self._mime_type)) src.write(self._img_data) img = append_to(container, "img", src=src.getvalue()) if len(self._img_styles) > 0: img["style"] = cfg_to_css_string(self._img_styles)
def _write_title(self, container): """ Write out the title (if there is any). :param container: Container element. """ if self._settings.title is not None and (self._settings.title != ""): title = append_to(container, "H%s" % self._settings.title_level, style="white-space: %s" % ("normal" if self._settings.title_wrap else "nowrap")) title.string = self._settings.title
def write(self, parent=None): if parent is None: el = root("style") else: el = append_to(parent, "style") el["type"] = "text/css" if self._tag_id is not None: el["id"] = self._tag_id with open(self._local_path, "r") as f: el.string = f.read() return el
def _write_contents(self, container, actual_cfg, *args, **kwargs): # The width of one column in percentage content_count = len(self._contents) # Skip layout if there is no content. if content_count > 0: cell_width = 100. / min(self._cols, content_count) row_count = int(math.ceil(content_count / float(self._cols))) for row_i in range(row_count): row_el = append_to(container, "div") row_el["class"] = ["pybloqs-grid-row"] if row_i > 0: row_el["style"] = "clear:both" written_row_item_count = row_i * self._cols for col_i in range(self._cols): item_count = written_row_item_count + col_i if item_count >= content_count: break cell_el = append_to(row_el, "div", style="width:%f%%;float:left;" % cell_width) cell_el["class"] = ["pybloqs-grid-cell"] self._contents[item_count]._write_block( cell_el, actual_cfg if self._cascade_cfg else Cfg(), *args, **kwargs) # Clear the floating, Yarr! append_to(container, "div", style="clear:both")
def _write_block(self, parent, *args, **kwargs): # Add a `hr` element to the parent append_to(parent, "hr")
def render_html(self, pretty=True, static_output=False, header_block=None, footer_block=None): """Returns html output of the block :param pretty: Toggles pretty printing of the resulting HTML. Not applicable for non-HTML output. :param static_output: Passed down to _write_block. Will render static version of blocks which support this. :param header_block: If not None, header is inlined into a HTML body as table. :param footer_block: If not None, header is inlined into a HTML body as table. :return html-code of the block """ # Render the contents html = root("html", doctype="html") head = append_to(html, "head") append_to(head, "meta", charset='utf-8') body = append_to(html, "body") # Make sure that the main style sheet is always included resource_deps = DependencyTracker(default_css_main) # If header or footer are passed into this function, inline them in the following structure: # # <body> # <table> # <thead><tr><td>Header html</td></tr></thead> # <tfoot><tr><td>Footer html</td></tr></tfoot> # <tbody><tr><td>Body html</td></tr></tbody> # </table> # </body> if header_block is not None or footer_block is not None: content_table = append_to(body, "table") if header_block is not None: header_thead = append_to(content_table, "thead") header_tr = append_to(header_thead, "tr") header_td = append_to(header_tr, "th") header_block._write_block(header_td, Cfg(), id_generator(), resource_deps=resource_deps, static_output=static_output) if footer_block is not None: footer_tfoot = append_to(content_table, "tfoot", id='footer') footer_tr = append_to(footer_tfoot, "tr") footer_td = append_to(footer_tr, "td") footer_block._write_block(footer_td, Cfg(), id_generator(), resource_deps=resource_deps, static_output=static_output) body_tbody = append_to(content_table, "tbody") body_tr = append_to(body_tbody, "tr") body_td = append_to(body_tr, "td") self._write_block(body_td, Cfg(), id_generator(), resource_deps=resource_deps, static_output=static_output) else: self._write_block(body, Cfg(), id_generator(), resource_deps=resource_deps, static_output=static_output) script_inflate.write(head) script_block_core.write(head) if static_output: # Add the load wait poller if there are any JS resources js_elem(body, "var loadWaitPoller=runWaitPoller();") # Write out resources for res in resource_deps: res.write(head) # Render the whole document (the parent of the html tag) content = render(html.parent, pretty=pretty) return content
def _write_contents(self, container, actual_cfg, *args, **kwargs): for content in self._contents: cell = append_to(container, "div") content._write_block(cell, actual_cfg if self._cascade_cfg else Cfg(), *args, **kwargs)
def test_append_to(): p = h.root() h.append_to(p, 'script') assert str(p) == '<html><script></script></html>'