Created
October 3, 2016 19:02
-
-
Save mdamien/fa3e53e43da6bf7f39dcbaba015c9d13 to your computer and use it in GitHub Desktop.
HTML to Lys
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from bs4 import BeautifulSoup | |
| from lys import render, L | |
| html_doc = """ | |
| <ul class="breadcrumb"> | |
| <li class="active">Home</li> | |
| </ul> | |
| <ul class="breadcrumb"> | |
| <li><a href="#">Home</a></li> | |
| <li class="active">Library</li> | |
| </ul> | |
| <ul class="breadcrumb"> | |
| <li><a href="#">Home</a></li> | |
| <li><a href="#">Library</a></li> | |
| <li class="active">Data</li> | |
| </ul> | |
| """ | |
| soup = BeautifulSoup(html_doc, 'html.parser') | |
| def render_to_lys(node): | |
| def render_children(node): | |
| ', '.join() | |
| if hasattr(node, 'attrs'): | |
| name = str(node.name) | |
| out = 'L.' + name | |
| if name == '[document]': | |
| out = '' | |
| if len(list(node.children)) > 0: | |
| if name != '[document]': | |
| out += ' / ' | |
| children = list(x for x in (render_to_lys(node) for node in node.children) if x) | |
| children = list(x for x in (render_to_lys(node) for node in node.children) if x) | |
| out += '( ' + ', '.join(children) + ' )' | |
| return out | |
| else: | |
| s = node.strip() | |
| if len(s) > 0: | |
| return repr(s) | |
| result = render_to_lys(soup) | |
| print(result) | |
| html = eval('render(' + result + ')') | |
| soup_result = BeautifulSoup(html, 'html.parser') | |
| print(soup_result.prettify()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment