Skip to content

Commit 9933bbb

Browse files
committed
Meson build: Add insert_example_code.py
* docs/manual/insert_example_code.py: New Python file, equivalent to the insert_example_code.pl Perl file. The Perl file is still used when building with Autotools.
1 parent f23e0ff commit 9933bbb

File tree

5 files changed

+81
-20
lines changed

5 files changed

+81
-20
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# wildcard patterns
22
*.[ao]
33
*.l[ao]
4-
*~
54
Makefile
65
Makefile.in
76
.deps/
@@ -34,6 +33,7 @@ stamp-h?
3433
/docs/doxygen.css
3534
/docs/doxygen-extra.css
3635
/docs/tagfile-to-devhelp2.xsl
36+
/docs/manual/__pycache__/
3737
/docs/manual/html/
3838
/docs/manual/libxml++.xml
3939
/docs/reference/Doxyfile

Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ EXTRA_DIST = \
3030
meson_options.txt \
3131
libxml++config.h.meson \
3232
MSVC_NMake/libxml++/meson.build \
33+
docs/manual/insert_example_code.py \
3334
docs/manual/meson.build \
3435
docs/reference/meson.build \
3536
examples/meson.build \

docs/manual/insert_example_code.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/usr/bin/env python3
2+
3+
# argv[1] argv[2:-1] argv[-1]
4+
# insert_example_code.py <examples_base_dir> <input_xml_files>... <output_xml_file>
5+
6+
import os
7+
import sys
8+
import re
9+
import glob
10+
import shutil
11+
12+
# Where to insert example code.
13+
source_include_pattern = re.compile(
14+
r'\s*<para><ulink url="&url_examples_base;([/\w]+)">Source Code</ulink></para>')
15+
16+
# First line not part of leading comment in a source code file.
17+
# The comment typically consists of copyright and license text.
18+
start_of_source_pattern = re.compile(r'[#\w]')
19+
20+
def process_source_file(source_directory, source_basename, outfile):
21+
source_filename = os.path.join(source_directory, source_basename)
22+
with open(source_filename, mode='r') as srcfile:
23+
outfile.write('<para>File: <filename>' + source_basename + '</filename></para>\n')
24+
outfile.write('<programlisting>\n<![CDATA[')
25+
26+
found_start = False
27+
for line in srcfile:
28+
if not found_start:
29+
# Skip leading comment.
30+
if not start_of_source_pattern.match(line):
31+
continue
32+
found_start = True
33+
outfile.write(line)
34+
35+
outfile.write(']]></programlisting>\n')
36+
37+
def insert_example_code(examples_base_dir, input_xml_files, output_xml_file):
38+
if not isinstance(input_xml_files, list):
39+
input_xml_files = [input_xml_files]
40+
41+
with open(output_xml_file, mode='w') as outfile:
42+
for input_xml_file in input_xml_files:
43+
with open(input_xml_file, mode='r') as infile:
44+
for line in infile:
45+
# Print the line.
46+
outfile.write(line)
47+
48+
# Look for
49+
# <para><ulink url="&url_examples_base;helloworld">Source Code</ulink></para>
50+
source_include_match = source_include_pattern.match(line)
51+
if source_include_match:
52+
# List all the source files in the examples directory.
53+
source_directory = os.path.join(examples_base_dir, source_include_match.group(1))
54+
outfile.write('<!-- start inserted example code -->\n')
55+
for source_filename in glob.glob(os.path.join(source_directory, '*.h')) + \
56+
glob.glob(os.path.join(source_directory, '*.cc')):
57+
source_basename = os.path.basename(source_filename)
58+
process_source_file(source_directory, source_basename, outfile)
59+
outfile.write('<!-- end inserted example code -->\n')
60+
return 0
61+
62+
# ----- Main -----
63+
if __name__ == '__main__':
64+
if len(sys.argv) < 4:
65+
print('Usage: ' + sys.argv[0] + ' <examples_base_dir> <input_xml_files>... <output_xml_file>')
66+
sys.exit(1)
67+
68+
sys.exit(insert_example_code(sys.argv[1], sys.argv[2:-1], sys.argv[-1]))

docs/manual/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ xml_manual_docbook = custom_target('libxml++.xml',
3535
output: 'libxml++.xml',
3636
command: [
3737
python3, tutorial_custom_cmd_py, 'insert_example_code',
38-
meson.current_source_dir() / 'insert_example_code.pl',
38+
meson.current_source_dir(),
3939
project_source_root / 'examples',
4040
'@INPUT@',
4141
'@OUTPUT@',

tools/build_scripts/tutorial-custom-cmd.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,19 @@
88
import os
99
import sys
1010
import subprocess
11-
from pathlib import Path
1211
import shutil
1312

1413
subcommand = sys.argv[1]
1514

16-
def insert_example_code():
17-
# argv[2] argv[3] argv[4] argv[5]
18-
# <perl_script_file> <examples_dir> <input_xml_file> <output_xml_file>
15+
def insert_ex_code():
16+
# argv[2] argv[3] argv[4] argv[5]
17+
# <py_script_file> <examples_dir> <input_xml_file> <output_xml_file>
1918

20-
perl_script_file = sys.argv[2]
21-
examples_dir = sys.argv[3]
22-
input_xml_file = sys.argv[4]
23-
output_xml_file = sys.argv[5]
19+
# Search for insert_example_code.py first in <py_script_dir>.
20+
sys.path.insert(0, sys.argv[2])
21+
from insert_example_code import insert_example_code
2422

25-
cmd = [
26-
'perl',
27-
'--',
28-
perl_script_file,
29-
examples_dir,
30-
input_xml_file,
31-
]
32-
with open(output_xml_file, mode='w') as xml_file:
33-
return subprocess.run(cmd, stdout=xml_file).returncode
23+
return insert_example_code(sys.argv[3], sys.argv[4], sys.argv[5])
3424

3525
def html():
3626
# argv[2] argv[3] argv[4]
@@ -65,6 +55,8 @@ def html():
6555
return result.returncode
6656

6757
def xmllint():
58+
from pathlib import Path
59+
6860
# argv[2] argv[3] argv[4]
6961
# <validate> <input_xml_file> <stamp_file_path>
7062

@@ -184,7 +176,7 @@ def dist_doc():
184176

185177
# ----- Main -----
186178
if subcommand == 'insert_example_code':
187-
sys.exit(insert_example_code())
179+
sys.exit(insert_ex_code())
188180
if subcommand == 'html':
189181
sys.exit(html())
190182
if subcommand == 'xmllint':

0 commit comments

Comments
 (0)