Skip to content

Commit cdbf8b0

Browse files
authored
Merge pull request #155 from lucasbalieiro/fix-cross-compatibility-script
fixes cross-compatibility issue of `generate-readme.sh`
2 parents c4b9d85 + b9e3dc0 commit cdbf8b0

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

scripts/generate-readme.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ for file in $(ls *.md | grep -v -i 'README.md' | sort); do
1919
number="${BASH_REMATCH[2]}"
2020
title="${BASH_REMATCH[5]}"
2121
level=$((${#hashes} - 1))
22-
indent=$(printf ' %.0s' $(seq 1 $((level * 2))))
22+
indent=$(printf '%*s' $((level * 2)) " ")
2323
anchor=$(echo "$number-$title" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | tr -cd '[:alnum:]-')
2424
echo "${indent}- [$number. $title](./$file#$anchor)" >> "$INDEX_TMP"
2525
fi
2626
done < "$file"
2727
done
2828

29-
# Generate the new README content
30-
awk -v table_of_contents="$(cat $INDEX_TMP)" '{gsub(/\[table_of_contents\]/, table_of_contents)}1' "$TEMPLATE" > "$README_TMP"
29+
# Replace [table_of_contents] with generated index
30+
sed "/\[table_of_contents\]/{
31+
r $INDEX_TMP
32+
d
33+
}" "$TEMPLATE" > "$README_TMP"
3134

3235
if [[ "$1" == "--check" ]]; then
3336
if ! diff --color=auto -u "$OUTFILE" "$README_TMP"; then
@@ -46,4 +49,5 @@ elif [[ "$1" == "--bless" ]]; then
4649
else
4750
rm "$INDEX_TMP" "$README_TMP"
4851
echo -e "${GREEN}No changes made. Use --check to verify or --bless to update README.md.${NC}"
49-
fi
52+
fi
53+

scripts/readme.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
# README Generator Script
3+
4+
This `generate-readme.sh` script updates `README.md` by generating a Table of Contents (ToC) from all Markdown files in the repository.
5+
It replaces the `[table_of_contents]` placeholder in `README.template.md` with the generated index.
6+
7+
## Usage
8+
9+
```bash
10+
./scripts/generate-readme.sh [option]
11+
```
12+
13+
Options:
14+
15+
* `--check` : verify if `README.md` is up to date (exit 1 if not).
16+
* `--bless` : regenerate and overwrite `README.md`.
17+
18+
## Notes
19+
20+
* `README.template.md` must contain `[table_of_contents]`.
21+
* Run `--check` before committing to ensure the ToC is current.
22+

0 commit comments

Comments
 (0)