def get_sheet(self): if self.tag != 'style': raise TypeError('Cannot get CSS rules of non <style> element.') if self.sheet_cache_hash != hash(self.content[0]): self.sheet_cache = css.parse(self.content[0]) self.sheet_cache_hash = hash(self.content[0]) return self.sheet_cache
def load_external_stylesheet(node): if node.attr('rel') == 'stylesheet': type = node.attr('type') href = node.attr('href') if type and type != 'text/css': errors.append( Unknown_Stylesheet_Type_Error( node=node, description="Unknown stylesheet type", data=type)) elif href: loc = current_location.get().make_location(href) add_stylesheet(css.parse(loc), loc)
def render_to_image(html_source, css_source, height, width, renderer): tree = html.parse(html_source) rules = css.parse(css_source) styled_tree = style.style(tree, rules) layout_tree = [layout.build_layout_tree(node) for node in styled_tree if layout.get_display(node) is not layout.Display.NONE] root = layout.Dimensions.default() root.content.width = width for node in layout_tree: node.layout(root) renderer = painting.Renderer(width, height, renderer) image = renderer.render(layout_tree) return image
<span></span> </section> <span></span> </div> ''' css_text = ''' body {bg-color:white;} div {bg-color:yellow;padding:5;border:1;} section {padding:10;bg-color:cyan;height:20;border:1;} span {display:inline;height:20;border:1;} header {margin:10;color:black;height:30;border:1;} ''' dom_tree = dom.parse(html_text) css_tree = css.parse(css_text) style_tree = style.get_style_tree(dom_tree, css_tree) layout_tree = layout.build_layout_tree(style_tree) window = layout.Rect() window.width = 200 window.height = 0 layout_tree.layout_box(window) print("LAYOUT TREE") layout_tree.print_node() dl = draw.get_draw_list(layout_tree)
import string import domrenderer import css import fs auto_closing_tags = ('area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'nextid', 'param', 'source', 'track', 'wbr') mastercss = css.parse(fs.read('rs/master.css')) def parse(html): ''' Parses html into an Element ''' html += ' ' * 8 # So range checker on look-ahead isn't required cel = Element(None, '$document') # Current ELement i = 0 while i < len(html) - 8: if cel.tag == 'plaintext': cel.write(html[i:]) i = len(html) elif html[ i] not in '<&>' + string.whitespace: # or cel.tag in ('style', 'script'): s = ''