|
| 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