docs/normalize-includes.sh
Elisamara Aoki Goncalves 003d5d4439 Improved Subcloud Deployment / Upgrading Error Reporting (GUI & CLI)
Add new items with example to sections.
Add new example figure.
Fix numbering.
Resize figures to fit the page.
Update log files name.
Add item 4 in include file.
RS> Conditionalize 'dcmanager subcloud list' states for
    various run scenarios
RS> Move step from .. only:: block to .. include::
RS> Pre-process included blocks in non-block context
RS> Remove debug message from script
EG> Added example
EG> Fixed caution box

Story: 2010271
Task: 46946

Signed-off-by: Elisamara Aoki Goncalves <elisamaraaoki.goncalves@windriver.com>
Change-Id: I3791a23fefba8d9d428ae208b3bfcb9274d49278
2022-12-15 13:31:33 -03:00

123 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
directive='pre-include::'
d_begin=':start-after:'
d_end=':end-before:'
inc_base='doc/source'
RED='\033[0;31m'
NC='\033[0m'
message () { echo -e "$@" 1>&2; }
error () { message $RED$@$NC; }
OIFS=$IFS; IFS=$'\n'
parents=( $(grep -Rl '.. pre-include:: ' --exclude-dir=docs/build --include="*.r*st" --exclude-dir='.*' doc/*) )
IFS=$OIFS
check_file_deps () {
for filereq in $@
do
if [ ! -f "${filereq}" ] && [ ! -L "${filereq}" ]; then error "${filereq} not found. Quiting."; exit 1; fi
done
}
check_util_deps () {
for dep in $@
do
if ! hash $dep 2>/dev/null; then
error >&2 "... $dep dependency not met. Please install."
exit 1
fi
done
}
get_substr () {
local _str=${1//\//\\/}
local _drop=$2
local _regex="$_drop\s+(.*)\s*$"
message "${_str} =~ $_regex"
if [[ "${str}" =~ "$_regex" ]]
then
message "Found ${BASH_REMATCH[1]}"
echo "${BASH_REMATCH[1]}"
else
echo ""
fi
}
trimspaces () {
local _s=$1
_s=${_s##*( )}
_s=${_s%%*( )}
echo $_s
}
get_inc_path () {
local _ppath=$1
local _inc=$2
if [[ ${_inc::1} == "/" ]]; then
echo "$inc_base$_inc"
else
echo "$(dirname $_ppath)/$_inc"
fi
}
get_include_content () {
local _inc_file=$1
local _inc_start=$2
local _inc_end=$3
local _content
check_file_deps $_inc_file
if [[ $_inc_start != "" ]] && [[ $_inc_end != "" ]]; then
_content=$(awk "/.. $_inc_start/{flag=1; next} /.. $_inc_end/{flag=0} flag" "$_inc_file" | sed -r "s/^\s*\.\. $_inc_end\s*$//g")
# _content=$(sed -n '/\.\. $_inc_start/,/\.\. $_inc_end/{p;/\.\. $_inc_end/q}' $_inc_file)
elif [[ $_inc_start == "" ]] && [[ $_inc_end == "" ]]; then
_content=$(<$_inc_file)
else
error "Something went horribly wrong"
fi
echo "$_content"
}
## Run
for _f in "${parents[@]}"; do
readarray -t _content < "$_f"
for i in "${!_content[@]}"; do
if [[ ${_content[i]} =~ $directive ]]; then
_inc_file=$(trimspaces $(echo ${_content[i]} | sed -r 's|\s*\.\. pre-include::\s+||g'))
message "found ${_content[i]}: $_l\nExtracted $_inc_file"
if [[ ${_content[i+1]} =~ $d_begin ]]; then
_inc_start=$(trimspaces $(echo ${_content[i+1]} | sed -r 's|\s*:start-after:\s+||g'))
_inc_end=$(trimspaces $(echo ${_content[i+2]} | sed -r 's|\s*:end-before:\s+||g'))
if [[ $_inc_end == "" ]]
then
error "start/end paramter mismatch in\n$_f\n Quiting"
exit 1
fi
message "Extracted $_inc_start"
message "Extracted $_inc_end"
_content[i+1]=""
_content[i+2]=""
fi
_inc_file=$(get_inc_path "$_f" "$_inc_file")
_includestring=$(get_include_content $_inc_file $_inc_start $_inc_end)
_content[i]="$_includestring"
fi
# ((_line=$_line+1))
done
for line in "${_content[@]}"; do out="$out\n$line"; done
echo -e "${out//\\n/$'\n'}" > $_f
done