| Conditions | 8 |
| Total Lines | 32 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import base64 |
||
| 19 | def html_dom_graph_model(html_content): |
||
| 20 | graph_model = graphinate.model(name="HTML DOM Graph") |
||
| 21 | soup = BeautifulSoup(html_content, 'html.parser') |
||
| 22 | |||
| 23 | def node_type(tag: Tag): |
||
| 24 | return tag.name.strip('[]') |
||
| 25 | |||
| 26 | def node_key(tag: Tag): |
||
| 27 | return str((tag.sourceline, tag.sourcepos)) if isinstance(tag, Tag) else base64.b64encode( |
||
| 28 | tag.encode()).decode() |
||
| 29 | |||
| 30 | def node_label(tag: Tag): |
||
| 31 | return str(tag) |
||
| 32 | |||
| 33 | @graph_model.node(node_type, key=node_key, label=node_label) |
||
| 34 | def html_node(): |
||
| 35 | for tag in soup.descendants: |
||
| 36 | if tag.name is not None: |
||
| 37 | yield tag |
||
| 38 | |||
| 39 | @graph_model.edge() |
||
| 40 | def contains(): |
||
| 41 | for tag in soup.descendants: |
||
| 42 | if tag.name is not None: |
||
| 43 | for child in tag.children: |
||
| 44 | if child.name is not None: |
||
| 45 | yield { |
||
| 46 | 'source': node_key(tag), |
||
| 47 | 'target': node_key(child) |
||
| 48 | } |
||
| 49 | |||
| 50 | return graph_model |
||
| 51 | |||
| 62 |