|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# @license Apache-2.0 |
| 4 | +# |
| 5 | +# Copyright (c) 2024 The Stdlib Authors. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | + |
| 19 | +# Script to remove related packages sections from README.md files. |
| 20 | +# |
| 21 | +# Usage: remove_related_packages file1 [file2 file3 ...] |
| 22 | +# |
| 23 | +# Arguments: |
| 24 | +# |
| 25 | +# file1 File path. |
| 26 | +# file2 File path. |
| 27 | +# file3 File path. |
| 28 | + |
| 29 | +# shellcheck disable=SC2068 |
| 30 | + |
| 31 | +# Ensure that the exit status of pipelines is non-zero in the event that at least one of the commands in a pipeline fails: |
| 32 | +set -o pipefail |
| 33 | + |
| 34 | +for file in ${@}; do |
| 35 | + if [[ $file == *README.md ]]; then |
| 36 | + awk ' |
| 37 | + BEGIN { print_mode = 1; in_related = 0; in_related_links = 0 } |
| 38 | + /<section class="related">/ { |
| 39 | + print; print ""; print "</section>"; |
| 40 | + in_related = 1; print_mode = 0; next |
| 41 | + } |
| 42 | + /<\/section>/ { |
| 43 | + if (in_related) { in_related = 0; print_mode = 1 } |
| 44 | + else { print } |
| 45 | + next |
| 46 | + } |
| 47 | + /<!-- <related-links> -->/ { |
| 48 | + print; print ""; print "<!-- </related-links> -->"; |
| 49 | + in_related_links = 1; print_mode = 0; next |
| 50 | + } |
| 51 | + /<!-- <\/related-links> -->/ { |
| 52 | + if (in_related_links) { in_related_links = 0; print_mode = 1 } |
| 53 | + else { print } |
| 54 | + next |
| 55 | + } |
| 56 | + { if (print_mode == 1) print } |
| 57 | + ' "$file" > "${file}.tmp" && mv "${file}.tmp" "$file" |
| 58 | + fi |
| 59 | +done |
| 60 | + |
| 61 | +echo "All files processed successfully." |
0 commit comments