Skip to content

Commit 9e8a43d

Browse files
authored
Extract examples (#156)
1 parent 6d8681a commit 9e8a43d

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
examples/
12
src/datadog_api_client/version.py
23

34
# VSCode

check-examples.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
./extract-code-blocks.sh
4+
5+
ls examples/v*/*/*.pybeta | xargs -P $(($(nproc)*2)) -n 1 python -m py_compile
6+
if [ $? -ne 0 ]; then
7+
echo -e "Failed to build examples"
8+
exit 1
9+
fi

extract-code-blocks.awk

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env -S awk -f
2+
# SPDX-License-Identifier: Apache-2.0 OR MIT
3+
# Based on https://codereview.stackexchange.com/questions/194986/print-code-fenced-sections-of-a-markdown-document/195030#195030
4+
BEGIN {
5+
in_code_block = 0;
6+
tag = "";
7+
operation_id = "";
8+
}
9+
10+
function slug(value) {
11+
head = "";
12+
tail = value;
13+
while ( match(tail,/[[:upper:]][[:lower:]]/) ) {
14+
tgt = substr(tail,RSTART,1);
15+
if ( substr(tail,RSTART-1,1) ~ /[[:lower:]]/ || RSTART > 1 ) {
16+
tgt = "-" tolower(tgt);
17+
}
18+
head = head substr(tail,1,RSTART-1) tgt;
19+
tail = substr(tail,RSTART+1);
20+
}
21+
return tolower(head) tail;
22+
}
23+
24+
function camel(value) {
25+
gsub("_ip_", "_iP_", value);
26+
gsub("_id_", "_iD_", value);
27+
28+
head = toupper(substr(value,0,1)) substr(value,2);
29+
while ( match(head, /_([a-z])/) ) {
30+
head = substr(head,0,RSTART-1) toupper(substr(head,RSTART+1, 1)) substr(head,RSTART+2)
31+
}
32+
# NOTE special cases for all caps groups which we can't handle otherwise
33+
gsub("Aws", "AWS", head);
34+
gsub("Gcp", "GCP", head);
35+
return head;
36+
}
37+
38+
/^# datadog_api_client\.v[0-9]*\.(.+)Api/ {
39+
tag = slug(substr($2, 23, length($2)-25));
40+
}
41+
/^##? \*\*.+\*\*/ {
42+
operation_id = camel(substr($2, 3, length($2)-4));
43+
}
44+
/^```python/ {
45+
if (in_code_block == 0) {
46+
in_code_block = 1;
47+
if (out_file) {
48+
close(out_file);
49+
}
50+
system("mkdir -p " output "/" tag);
51+
out_file=output "/" tag "/" operation_id ".pybeta";
52+
print out_file;
53+
} else {
54+
print "Can't parse " FILENAME > "/dev/stderr"
55+
exit 1
56+
}
57+
next;
58+
}
59+
/^```/ {
60+
in_code_block = 0;
61+
}
62+
63+
in_code_block {
64+
# Make sure that the file is newly created
65+
if (in_code_block == 1) {
66+
in_code_block = 2;
67+
print > out_file;
68+
} else {
69+
print >> out_file;
70+
}
71+
}

extract-code-blocks.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
OUTPUT=${1:-examples}
4+
5+
cd ${0%/*}
6+
7+
ls docs/v1/*Api.md| xargs -n1 ./extract-code-blocks.awk -v output="${OUTPUT}/v1"
8+
ls docs/v2/*Api.md| xargs -n1 ./extract-code-blocks.awk -v output="${OUTPUT}/v2"

0 commit comments

Comments
 (0)