generated from mintlify/starter
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgenerate-py-doc.sh
executable file
·109 lines (85 loc) · 3.19 KB
/
generate-py-doc.sh
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
pip3 install pydoc-markdown --break-system-packages
# ./run-docs.sh ../literal-docs/python-client/
# https://github.com/NiklasRosenstein/pydoc-markdown/blob/develop/src/pydoc_markdown/contrib/renderers/markdown.py#L52
# https://github.com/NiklasRosenstein/pydoc-markdown/blob/develop/src/pydoc_markdown/contrib/processors/filter.py#L31
SOURCE_DIR="../literalai-python"
# change the DOCS_DIR to output the generated files to a different directory
DOCS_DIR="python-client/api-reference"
CONFIG_FILE="pydoc-markdown.yaml"
BEAUTIFY=true
# same as above, but make the arguments switchable and add a --beautify flag
for i in "$@"
do
case $i in
-s=*|--source-dir=*)
SOURCE_DIR="${i#*=}"
shift # past argument=value
;;
-b|--beautify)
BEAUTIFY=true
shift # past argument=value
;;
-o=*|--output-dir=*)
DOCS_DIR="${i#*=}"
shift # past argument=value
;;
-c=*|--config-file=*)
CONFIG_FILE="${i#*=}"
shift # past argument=value
;;
*)
# unknown option
;;
esac
done
apiFiles=(
"api.__init__"
"client"
# "observability.message"
"observability.step"
"observability.thread"
"observability.generation"
"evaluation.dataset"
)
mkdir -p $DOCS_DIR
# read all the files in the api directory and generate the docs (read from the source directory)
for i in "${apiFiles[@]}"; do
echo "Writing docs for $i in $DOCS_DIR/$i.mdx"
# uncomment at your own risk
# rm -f $DOCS_DIR/$i.*
pydoc-markdown -I $SOURCE_DIR -m literalai.$i --no-render-toc "$CONFIG_FILE" > $DOCS_DIR/$i.mdx
# remove the third line of the file
sed "3d" $DOCS_DIR/$i.mdx > $DOCS_DIR/$i.tmp && mv $DOCS_DIR/$i.tmp $DOCS_DIR/$i.mdx
if [ "$BEAUTIFY" = false ]; then
continue
fi
echo "Beautifying $i.mdx"
sed -r -E 's/- `([^`]+)` _([^_]+)_ - (.*)$/<ResponseField name="\1" type="\2">\3<\/ResponseField>/' $DOCS_DIR/$i.mdx > $DOCS_DIR/$i.tmp
sed -r -E 's/- `([^`]+)` - (.*)$/<ResponseField name="\1">\2<\/ResponseField>/' $DOCS_DIR/$i.tmp > $DOCS_DIR/$i.mdx
# delete the temporary files created for the sed beautification
rm $DOCS_DIR/$i.tmp
done
# If the file is api.__init__, rename it to api.mdx after the loop
if [ -f "$DOCS_DIR/api.__init__.mdx" ]; then
mv $DOCS_DIR/api.__init__.mdx $DOCS_DIR/api.mdx
# change the api.__init__ to api in the file
# sed -i 's/api.__init__/api/g' $DOCS_DIR/api.mdx
fi
# for each mdx file in the DOCS_DIR, keep only the last word (after the avant dernier point)
for file in $DOCS_DIR/*.mdx; do
# get the last word of the file
filename=$(basename $file)
# get the last word of the filename
last_word=$(echo $filename | awk -F. '{print $(NF-1)}')
# rename the file to the last word
mv $file $DOCS_DIR/$last_word.mdx
done
# for each file in the DOC_DIR, add its name without the extension to a files.txt file
for file in $DOCS_DIR/*.mdx; do
echo $(basename $file .mdx) >> $DOCS_DIR/files.txt
done
# call the python script to add the files to the mint.json
python3 scripts/update_api_reference.py -py $DOCS_DIR/files.txt
# remove the files.txt file
rm $DOCS_DIR/files.txt