Skip to content

Commit 50c2f57

Browse files
add example content to xml
1 parent 4e3d630 commit 50c2f57

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

scripts/postprocess_doxygen_xml.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import re
55
import os
6+
import html
67
from bs4 import BeautifulSoup
78

89
# walk the combined output.
@@ -17,6 +18,31 @@ def compile_id_list(xml_content):
1718
id_list = [x["id"] for x in els]
1819
return id_list
1920

21+
def insert_example_code_from_file(combined_content):
22+
els = combined_content.doxygen.find_all("programlisting")
23+
all_examples = {}
24+
# get the examples path
25+
examples_path = re.sub(r"/scripts/.*+$", "/lib/pico-examples", os.path.realpath(__file__))
26+
# get a recursive list of all files in examples
27+
for f in os.walk(examples_path):
28+
for filename in f[2]:
29+
if filename in all_examples:
30+
all_examples[filename].append(os.path.join(f[0], filename))
31+
else:
32+
all_examples[filename] = [os.path.join(f[0], filename)]
33+
for el in els:
34+
if el.get("filename") is not None:
35+
filename = el.get("filename")
36+
# find the file here or in examples
37+
if filename in all_examples:
38+
with open(all_examples[filename][0]) as f:
39+
example_content = f.read()
40+
example_lines = example_content.split("\n")
41+
for line in example_lines:
42+
codeline = BeautifulSoup("<codeline>"+html.escape(line)+"</codeline>", 'xml')
43+
el.append(codeline)
44+
return combined_content
45+
2046
def walk_and_tag_xml_tree(el, output_contexts, all_contexts):
2147
"""
2248
Process an individual xml file, adding context-specific tags as needed.
@@ -97,6 +123,7 @@ def postprocess_doxygen_xml_file(combined_xmlfile, xmlfiles, output_context_path
97123
els = combined_content.doxygen.find_all(True, recursive=False)
98124
for el in els:
99125
walk_and_tag_xml_tree(el, output_contexts, list(output_context_paths.keys()))
126+
combined_content = insert_example_code_from_file(combined_content)
100127
return str(combined_content)
101128

102129
def postprocess_doxygen_xml(xml_path):

0 commit comments

Comments
 (0)