forked from DataDog/datadog-api-client-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
extract-code-blocks.awk
executable file
·60 lines (58 loc) · 1.47 KB
/
extract-code-blocks.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env -S awk -f
# SPDX-License-Identifier: Apache-2.0 OR MIT
# Based on https://codereview.stackexchange.com/questions/194986/print-code-fenced-sections-of-a-markdown-document/195030#195030
BEGIN {
in_code_block = 0;
tag = "";
operation_id = "";
}
function slug(value) {
head = "";
tail = value;
while ( match(tail,/[[:upper:]][[:lower:]]/) ) {
tgt = substr(tail,RSTART,1);
if ( substr(tail,RSTART-1,1) ~ /[[:lower:]]/ || RSTART > 1 ) {
tgt = "-" tolower(tgt);
}
head = head substr(tail,1,RSTART-1) tgt;
tail = substr(tail,RSTART+1);
}
return tolower(head) tail;
}
/^# .+Api/ {
tag = slug(substr($2, 0, length($2)-3));
}
/^## / {
operation_id = toupper( substr( $2, 1, 1 ) ) substr( $2, 2 );
}
/^```java/ {
if (in_code_block == 0) {
in_code_block = 1;
if (out_file) {
close(out_file);
}
system("mkdir -p " output "/" tag);
out_file=output "/" tag "/" operation_id ".java";
if (system("[ -f " out_file " ]") == 0) {
printf "skipped: "
in_code_block = 0;
}
print out_file;
} else {
print "Can't parse " FILENAME > "/dev/stderr"
exit 1
}
next;
}
/^```/ {
in_code_block = 0;
}
in_code_block {
# Make sure that the file is newly created
if (in_code_block == 1) {
in_code_block = 2;
print > out_file;
} else {
print >> out_file;
}
}