From ca5e04b7d9f0aecd89ee6602defe69377eede78f Mon Sep 17 00:00:00 2001 From: Charles Beauville Date: Fri, 12 Apr 2024 15:51:06 +0200 Subject: [PATCH] Refactor example docs script (#3220) --- dev/update-examples.sh | 117 +++++++++++++++++++++++++---------------- 1 file changed, 72 insertions(+), 45 deletions(-) diff --git a/dev/update-examples.sh b/dev/update-examples.sh index 07cb90932875..1076b4621984 100755 --- a/dev/update-examples.sh +++ b/dev/update-examples.sh @@ -3,10 +3,80 @@ set -e cd "$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"/../ ROOT=`pwd` -INDEX=$ROOT/examples/README.md +INDEX=$ROOT/examples/doc/source/index.md INSERT_LINE=6 +copy_markdown_files () { + for file in $1/*.md; do + # Copy the README into the source of the Example docs as the name of the example + if [[ $(basename "$file") = "README.md" ]]; then + cp $file $ROOT/examples/doc/source/$1.md 2>&1 >/dev/null + else + # If the example contains other markdown files, copy them to the source of the Example docs + cp $file $ROOT/examples/doc/source/$(basename "$file") 2>&1 >/dev/null + fi + done +} + +add_gh_button () { + gh_text="[\"View](https://github.com/adap/flower/blob/main/examples/$1)" + readme_file="$ROOT/examples/doc/source/$1.md" + + if ! grep -Fq "$gh_text" "$readme_file"; then + awk -v text="$gh_text" ' + /^# / && !found { + print $0 "\n" text; + found=1; + next; + } + { print } + ' "$readme_file" > tmpfile && mv tmpfile "$readme_file" + fi +} + +copy_images () { + if [ -d "$1/_static" ]; then + cp $1/_static/**.{jpg,png,jpeg} $ROOT/examples/doc/source/_static/ 2>/dev/null || true + fi +} + +add_to_index () { + (echo $INSERT_LINE; echo a; echo $1; echo .; echo wq) | ed $INDEX 2>&1 >/dev/null +} + +add_single_entry () { + # Copy markdown files to correct folder + copy_markdown_files $1 + + # Add button linked to GitHub + add_gh_button $1 + + # Copy all images of the _static folder into the examples + # docs static folder + copy_images $1 + + # Insert the name of the example into the index file + add_to_index $1 +} + +add_all_entries () { + cd $ROOT/examples + # Iterate through each folder in examples/ + for d in $(printf '%s\n' */ | sort -V); do + # Add entry based on the name of the folder + example=${d%/} + + if [[ $example != doc ]]; then + add_single_entry $example + fi + done +} + +# Clean up before starting +rm -f $ROOT/examples/doc/source/*.md rm -f $INDEX + +# Create empty index file touch $INDEX echo "# Flower Examples Documentation" >> $INDEX @@ -16,49 +86,6 @@ echo "---" >> $INDEX echo "maxdepth: 1" >> $INDEX echo "---" >> $INDEX -rm -f examples/doc/source/*.md - -cd examples/ -for d in $(printf '%s\n' */ | sort -V); do - example=${d%/} - - if [[ $example != doc ]]; then - - for file in $example/*.md; do - # For each example, copy the README into the source of the Example docs - if [[ $(basename "$file") = "README.md" ]]; then - cp $file $ROOT/examples/doc/source/$example.md 2>&1 >/dev/null - else - # If the example contains other markdown files, copy them to the source of the Example docs - cp $file $ROOT/examples/doc/source/$(basename "$file") 2>&1 >/dev/null - fi - done - - gh_text="[\"View](https://github.com/adap/flower/blob/main/examples/$example)" - readme_file="$ROOT/examples/doc/source/$example.md" - - if ! grep -Fq "$gh_text" "$readme_file"; then - awk -v text="$gh_text" ' - /^# / && !found { - print $0 "\n" text; - found=1; - next; - } - { print } - ' "$readme_file" > tmpfile && mv tmpfile "$readme_file" - fi - - # For each example, copy all images of the _static folder into the examples - # docs static folder - [ -d "$example/_static" ] && { - cp $example/_static/**.{jpg,png,jpeg} $ROOT/examples/doc/source/_static/ 2>/dev/null || true - } - # For each example, insert the name of the example into the index file - (echo $INSERT_LINE; echo a; echo $example; echo .; echo wq) | ed $INDEX 2>&1 >/dev/null - - fi -done +add_all_entries echo "\`\`\`" >> $INDEX - -cp $INDEX $ROOT/examples/doc/source/index.md