#!/bin/bash
set -e
source /etc/profile.d/*.sh
readonly SCRIPT_NAME=$(basename "$0")
show_help() {
cat << EOF
Usage: $SCRIPT_NAME -v VERSION [-P] [-h]
Configure and build openYuanrong documentation.
Options:
-v VERSION (Optional) Specify the version string. Defaults to "latest".
-P (Optional) Use the installed package instead of building the runtime from source.
-h Display this help message and exit.
Examples:
./$SCRIPT_NAME -v 1.0.0
./$SCRIPT_NAME -v 1.0.0 -P
EOF
}
BUILD_VERSION="latest"
BUILD_WITH_PACKAGE="false"
while getopts "hv:P" opt; do
case $opt in
h)
show_help
exit 0
;;
v)
BUILD_VERSION="$OPTARG"
;;
P)
BUILD_WITH_PACKAGE="true"
;;
\? | :)
echo "Try '$SCRIPT_NAME -h' for more information." >&2
exit 1
;;
esac
done
export BUILD_VERSION
export BUILD_WITH_PACKAGE
BASE_DIR=$(dirname "$(readlink -f "$0")")
OUTPUT_DIR=${BASE_DIR}/../output
function add_noindex() {
local DIR="$1"
find "$DIR" -name "*.html" -not -path "*/_static/*" -not -path "*/_modules/*" -not -path "*/_sources/*" | while read -r file; do
if ! grep -q 'name="robots"' "$file"; then
sed -i 's/<head>/<head>\n <meta name="robots" content="noindex, nofollow">/' "$file"
fi
done
}
function build_zh_cn() {
pushd "${BASE_DIR}"/source_zh_cn
make html
popd
sed -i '/||$/{N;s/||\n\s*queryTerm\.match(\/\^\\d+\$\/)//;}' "${BASE_DIR}"/source_zh_cn/_build/html/_static/searchtools.js
rm -rf "${OUTPUT_DIR}"/docs/zh-cn && mkdir -p "${OUTPUT_DIR}"/docs/zh-cn
cp -rf "${BASE_DIR}"/source_zh_cn/_build/html/* "${OUTPUT_DIR}"/docs/zh-cn
if [ "$BUILD_VERSION" = "latest" ]; then
SITEMAP="${OUTPUT_DIR}"/docs/zh-cn/sitemap.xml
if [ -f "$SITEMAP" ]; then
BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
sed -i "/<urlset.*>/a<url><loc>https://docs.openyuanrong.org/zh-cn/${BUILD_VERSION}/index.html</loc><lastmod>${BUILD_DATE}</lastmod></url>" "$SITEMAP"
fi
else
add_noindex "${OUTPUT_DIR}"/docs/zh-cn
rm -f "${OUTPUT_DIR}"/docs/zh-cn/sitemap.xml
fi
}
function build_en() {
pushd "${BASE_DIR}"/source_en
make html
popd
sed -i '/||$/{N;s/||\n\s*queryTerm\.match(\/\^\\d+\$\/)//;}' "${BASE_DIR}"/source_en/_build/html/_static/searchtools.js
rm -rf "${OUTPUT_DIR}"/docs/en && mkdir -p "${OUTPUT_DIR}"/docs/en
cp -rf "${BASE_DIR}"/source_en/_build/html/* "${OUTPUT_DIR}"/docs/en
if [ "$BUILD_VERSION" = "latest" ]; then
SITEMAP="${OUTPUT_DIR}"/docs/en/sitemap.xml
if [ -f "$SITEMAP" ]; then
BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
sed -i "/<urlset.*>/a<url><loc>https://docs.openyuanrong.org/en/${BUILD_VERSION}/index.html</loc><lastmod>${BUILD_DATE}</lastmod></url>" "$SITEMAP"
fi
else
add_noindex "${OUTPUT_DIR}"/docs/en
rm -f "${OUTPUT_DIR}"/docs/en/sitemap.xml
fi
}
function generate_sitemap_index() {
cat > "${OUTPUT_DIR}"/docs/sitemap.xml << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://docs.openyuanrong.org/zh-cn/latest/sitemap.xml</loc>
</sitemap>
<sitemap>
<loc>https://docs.openyuanrong.org/en/latest/sitemap.xml</loc>
</sitemap>
</sitemapindex>
EOF
cat > "${OUTPUT_DIR}"/docs/robots.txt << 'EOF'
User-agent: *
Allow: /zh-cn/latest/
Allow: /en/latest/
Disallow: /zh-cn/
Disallow: /en/
Disallow: */search.html
Sitemap: https://docs.openyuanrong.org/sitemap.xml
EOF
}
function doc_build() {
pip install -r "${BASE_DIR}"/requirements_dev.txt
build_zh_cn
build_en
generate_sitemap_index
}
doc_build