Skip to content

Commit 5a616c9

Browse files
authored
Add a script to easily replace strings in rst or po files (#9677)
* Add a script to easily replace strings in rst or po files * Add default and callable arguments * Fix include
1 parent be81e9f commit 5a616c9

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

scripts/strings_fixer.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env bash
2+
3+
# This script is used to batch replace strings in the repo, either rst or po files
4+
# It can be used to fix e.g. broken or redirected URLs (spotted using linkcheck),
5+
# malformed Sphinx roles.
6+
# With po files, the idea is to replace the strings in the repo at once
7+
# and avoid pushing strings translators would have to fix in their languages.
8+
9+
# Usage: Run from the repository main folder
10+
# scripts/strings_fixer.sh -- will replace strings in po files in the "locale" folder
11+
# scripts/strings_fixer.sh docs/about rst -- will replace strings in rst files in the "docs/about" folder
12+
13+
# Harrissou Sant-anna, february 2025
14+
15+
# Folder to browse for text replacement, defaults to locale
16+
SOURCEFILES=${1:-"locale"}
17+
# target file format to update, defaults to po
18+
TARGETFORMAT=${2:-"po"}
19+
20+
declare -A MATCHING_STRINGS
21+
22+
MATCHING_STRINGS=(
23+
[">\` _"]=">\`_"
24+
["> \`_"]=">\`_"
25+
[":ref: "]=":ref:"
26+
[": ref: "]=":ref:"
27+
[":ref : "]=":ref:"
28+
[": ref :"]=":ref:"
29+
[":guilabel: "]=":guilabel:"
30+
[": guilabel: "]=":guilabel:"
31+
[":guilabel : "]=":guilabel:"
32+
[": guilabel :"]=":guilabel:"
33+
[":sup: "]=":sup:"
34+
[": sup: "]=":sup:"
35+
[":sup : "]=":sup:"
36+
[": sup :"]=":sup:"
37+
[":menuselection: "]=":menuselection:"
38+
[": menuselection: "]=":menuselection:"
39+
[":menuselection : "]=":menuselection:"
40+
[": menuselection :"]=":menuselection:"
41+
# complete specific strings to update, e.g., urls from make linkcheck output
42+
# ["oldstringtoreplace"]="newstringforreplacement"
43+
)
44+
45+
for FILE in `find ${SOURCEFILES} -type f -name "*.${TARGETFORMAT}"`
46+
do
47+
echo "${FILE}";
48+
49+
for string in "${!MATCHING_STRINGS[@]}"
50+
do
51+
#echo "$string - ${MATCHING_STRINGS[$string]}"
52+
sed -i -e "s@${string}@${MATCHING_STRINGS[${string}]}@g" "${FILE}"
53+
done
54+
done

0 commit comments

Comments
 (0)