jekyll: create plugin to update api toc

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2022-05-18 18:00:02 +02:00
parent b65866f2a6
commit 2120bd105d
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
3 changed files with 26 additions and 24 deletions

View File

@ -49,11 +49,6 @@ ENV TARGET=/out
RUN --mount=type=bind,target=.,rw \
--mount=type=cache,target=/src/.jekyll-cache <<EOT
set -eu
# substitute the "{site.latest_engine_api_version}" in the title for the latest
# API docs, based on the latest_engine_api_version parameter in _config.yml
(set -x ; ./_scripts/update-api-toc.sh)
if [ "${JEKYLL_ENV}" = "production" ]; then
(
set -x
@ -68,7 +63,6 @@ else
echo '[]' > ${TARGET}/js/metadata.json
)
fi
find ${TARGET} -type f -name '*.html' | while read i; do
sed -i 's#\(<a[^>]* href="\)https://${DOMAIN}/#\1/#g' "$i"
done

View File

@ -0,0 +1,26 @@
require 'jekyll'
require 'octopress-hooks'
module Jekyll
class UpdateApiToc < Octopress::Hooks::Site
def pre_read(site)
beginning_time = Time.now
Jekyll.logger.info "Starting plugin update_api_toc.rb..."
if File.file?("_config.yml") && File.file?("_data/toc.yaml")
# substitute the "{site.latest_engine_api_version}" in the title for the latest
# API docs, based on the latest_engine_api_version parameter in _config.yml
engine_ver = site.config['latest_engine_api_version']
toc_file = File.read("_data/toc.yaml")
replace = toc_file.gsub!("{{ site.latest_engine_api_version }}", engine_ver)
Jekyll.logger.info " Replacing '{{ site.latest_engine_api_version }}' with #{engine_ver} in _data/toc.yaml"
File.open("_data/toc.yaml", "w") { |file| file.puts replace }
end
end_time = Time.now
Jekyll.logger.info "done in #{(end_time - beginning_time)} seconds"
end
end
end

View File

@ -1,18 +0,0 @@
#!/bin/sh
# Do some sanity-checking to make sure we are running this from the right place
if ! [ -f _config.yml ]; then
echo "Could not find _config.yml. We may not be in the right place. Bailing."
exit 1
fi
# Helper function to deal with sed differences between osx and Linux
# See https://stackoverflow.com/a/38595160
sedi () {
sed --version >/dev/null 2>&1 && sed -i -- "$@" || sed -i "" "$@"
}
# Parse latest_engine_api_version variables from _config.yml to replace the value
# in toc.yaml. This is brittle!
latest_engine_api_version="$(grep 'latest_engine_api_version:' ./_config.yml | grep -oh '"[0-9].*"$' | sed 's/"//g')"
sedi "s/{{ site.latest_engine_api_version }}/${latest_engine_api_version}/g" ./_data/toc.yaml