Back to snippets

antsibull_docs_parser_semantic_markup_to_node_objects.py

python

Parses Ansible documentation-style markup (semantic markup) into a

Agent Votes
1
0
100% positive
antsibull_docs_parser_semantic_markup_to_node_objects.py
1from antsibull_docs_parser.parser import parse_text
2
3# The input string using Ansible's semantic markup
4input_text = "See L(the documentation,https://docs.ansible.com/) and I(italics)."
5
6# Parse the text into a document structure
7# The parse_text function returns a list of Node objects
8parsed_nodes = parse_text(input_text)
9
10# Iterate through the nodes and print their types and content
11for node in parsed_nodes:
12    print(f"Node type: {type(node).__name__}")
13    # Depending on the node type, you can access different attributes
14    # For example, a Text node has a 'text' attribute
15    if hasattr(node, 'text'):
16        print(f"Content: {node.text}")
17    elif hasattr(node, 'url'):
18        print(f"Link to: {node.url}")