Skip to content

Commit f889a47

Browse files
committed
Add canonical header for old and new docs
1 parent 297754c commit f889a47

File tree

8 files changed

+129
-39
lines changed

8 files changed

+129
-39
lines changed

.github/workflows/update-gh-cache.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
- name: Build documentation
5454
env:
5555
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
56-
run: make docs
56+
run: make build-docs
5757

5858
- name: Deploy 🚀
5959
uses: JamesIves/github-pages-deploy-action@v4

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ _eeps
1717
eeps
1818
faq
1919
assets/js/*.bundle.js*
20+
LATEST_MAJOR_VSN
21+
otp-headers

Makefile

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.PHONY: setup clean build update serve test algolia setup_gems setup_npm format-eeps patches assets/img/favicon.ico
1+
.PHONY: setup clean build update serve test algolia setup_gems setup_npm format-eeps patches assets/img/favicon.ico build-docs otp-headers
22

33
## For netlify the BUNDLE_PATH is different so we need to check it
44
BUNDLE_PATH?=vendor/bundle
@@ -11,7 +11,7 @@ netlify: clean
1111
$(MAKE) -j $(shell nproc) --debug=basic BUNDLE_PATH=/opt/build/cache/bundle JEKYLL_ENV=production
1212

1313
clean:
14-
rm -rf _patches docs _eeps faq _clones eeps assets/js
14+
rm -rf _patches docs _eeps faq _clones eeps assets/js LATEST_MAJOR_VSN
1515

1616
$(BUNDLE_PATH):
1717
bundler install --jobs 4 --retry 3 --path $(BUNDLE_PATH)
@@ -81,7 +81,17 @@ format-eeps: _scripts/_build/default/bin/erlang-org _clones/eep
8181
$< format-eeps _eeps _clones/eep/eeps/eep-0000.html _clones/eep/eeps/*.md
8282
touch _eeps/$(shell cd _clones/eep && git rev-parse --short HEAD)-$(EEPS_HASH)
8383

84-
docs: otp_versions.table _scripts/download-docs.sh _scripts/otp_flatten_docs _scripts/otp_flatten_ex_docs _scripts/otp_doc_sitemap.sh
84+
LATEST_MAJOR_VSN: otp_versions.table
85+
@set -e ;\
86+
MAJOR_VSNs=$$(grep "OTP-[0-9]\+\.0 " $< \
87+
| awk '{print $1}' \
88+
| sed 's/OTP-\(.*\)/\1/g' \
89+
| sed 's/^\([0-9]\+\).*/\1/g') ;\
90+
LATEST_MAJOR_VSN=$$(echo "$$MAJOR_VSNs" | tr ' ' '\n' | sort -n | tail -1) ;\
91+
echo $$LATEST_MAJOR_VSN > $@
92+
93+
94+
docs: otp_versions.table _scripts/download-docs.sh _scripts/otp_flatten_docs _scripts/otp_flatten_ex_docs _scripts/otp_doc_sitemap.sh LATEST_MAJOR_VSN _scripts/otp_add_headers.sh
8595
if [ ! -d $@ ]; then git clone --single-branch -b $@ https://github.com/erlang/erlang-org $@; fi
8696
if [ "$(JEKYLL_ENV)" != "production" ]; then _scripts/download-docs.sh $<; fi
8797
@touch docs
@@ -108,11 +118,19 @@ patches: _scripts/_build/default/bin/erlang-org otp_versions.table
108118
update:
109119
npm update
110120

121+
otp-headers: docs _redirects _scripts/otp_add_headers.sh LATEST_MAJOR_VSN
122+
if [ ! -f "$@" ] || [ ! $$(cat "$@") = $$(tar c "$<" | md5sum | awk '{print $$1}') ]; then \
123+
_scripts/otp_add_headers.sh "$<"; \
124+
fi;
125+
tar c $< | md5sum | awk '{print $$1}' > $@
126+
127+
build-docs: docs otp-headers
128+
111129
_redirects: _redirects.in _scripts/redirects.sh docs
112130
cp _redirects.in "$@"
113131
_scripts/redirects.sh >> "$@"
114132

115-
setup: setup_gems setup_npm _patches docs _eeps eeps faq _redirects
133+
setup: setup_gems setup_npm _patches docs _eeps eeps faq _redirects otp-headers
116134

117135
serve: setup
118136
bundle exec jekyll serve --future --incremental --trace --livereload --host 0.0.0.0

_redirects.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
/rss/news/* /news.xml
1616
/erlang-enhancement-proposals/home.html /eeps/
1717
/erlang-enhancement-proposals/* /eeps/:splat
18+
/doc/index.html /doc/readme.html
1819

1920
## Redirects from old sites
2021
https://blog.erlang.org/feed.xml https://www.erlang.org/blog.xml 301!

_scripts/download-docs.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ TOKEN=${2:-"token ${GITHUB_TOKEN}"}
1313
HDR=(--silent --location --fail --show-error -H "Authorization: ${TOKEN}" -H "X-GitHub-Api-Version: 2022-11-28")
1414

1515
# The files that are involved when generating docs
16-
SCRIPT_FILES="${OTP_VERSIONS_TABLE} _scripts/download-docs.sh _scripts/otp_flatten_docs _scripts/otp_flatten_ex_docs _scripts/otp_doc_sitemap.sh"
16+
SCRIPT_FILES="${OTP_VERSIONS_TABLE} _scripts/download-docs.sh _scripts/otp_flatten_docs _scripts/otp_flatten_ex_docs _scripts/otp_doc_sitemap.sh LATEST_MAJOR_VSN _scripts/otp_add_headers.sh"
1717

1818
_get_vsns() {
1919
grep "${1}" "${OTP_VERSIONS_TABLE}" | awk '{print $1}' | sed 's/OTP-\(.*\)/\1/g'
@@ -37,7 +37,7 @@ _flatten_docs() {
3737
}
3838

3939
MAJOR_VSNs=$(_get_vsns "OTP-[0-9]\+\.0 " | sed 's/^\([0-9]\+\).*/\1/g')
40-
LATEST_MAJOR_VSN=$(echo "$MAJOR_VSNs" | tr ' ' '\n' | sort -n | tail -1)
40+
LATEST_MAJOR_VSN=$(cat "LATEST_MAJOR_VSN")
4141
RINCLUDE=()
4242

4343
for VSN in ${MAJOR_VSNs}; do

_scripts/otp_add_headers.sh

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/bin/bash
2+
3+
# This script takes all the html files and adds
4+
# necessary html head tags before the deployment
5+
#
6+
# List of html tags added:
7+
#
8+
# - <meta name="exdoc:full-text-search-url" \\
9+
# content="/doc/search.html?v=${MAJOR_VSN}&q=">
10+
#
11+
# - <meta name="major-vsn" content="${MAJOR_VSN}">
12+
#
13+
# - <meta name="exdoc:autocomplete" content="off">
14+
#
15+
# - <link rel="canonical" href="https://www.erlang.org/doc/(...)"/>
16+
#
17+
18+
set -e
19+
set -o pipefail
20+
21+
DOCS_DIR="$1"
22+
shift
23+
LATEST_MAJOR_VSN="$(cat "LATEST_MAJOR_VSN")"
24+
MAJOR_VSNS="$(ls -I "*\.*" "${DOCS_DIR}")"
25+
CANONICAL_URL="https://www.erlang.org/doc/"
26+
27+
28+
_fixup_search_link() {
29+
META_FULL_TEXT_SEARCH="<meta name=\"exdoc:full-text-search-url\""
30+
EXDOC_SEARCH=$(grep "${META_FULL_TEXT_SEARCH}" "$1" || echo "")
31+
if [ ! "$(echo "$EXDOC_SEARCH" | wc -w)" -gt "0" ]; then
32+
sed 's@\s*<title>@'"${META_FULL_TEXT_SEARCH}"' content="/doc/search.html?v='"${MAJOR_VSN}"'\&q=">\n&@g' -i -- "$1"
33+
fi
34+
}
35+
36+
_fixup_major_version() {
37+
MAJOR_VSN_SEARCH=$(grep -P "<meta name=\"major-vsn\" content=\"\d+\"" "$1" || echo "")
38+
if [ ! "$(echo "$MAJOR_VSN_SEARCH" | wc -w)" -gt "0" ]; then
39+
sed 's@\s*<title>@<meta name="major-vsn" content="'"${MAJOR_VSN}"'">\n&@g' -i -- "$1"
40+
fi
41+
}
42+
43+
_disable_autocomplete() {
44+
AUTOCOMPLETE_SEARCH=$(grep -P "<meta name=\"exdoc:autocomplete\" content=\"off\">" "$1" || echo "")
45+
if [ ! "$(echo "$AUTOCOMPLETE_SEARCH" | wc -w)" -gt "0" ]; then
46+
sed 's@\s*<title>@<meta name="exdoc:autocomplete" content="off">\n&@g' -i -- "$1"
47+
fi
48+
}
49+
50+
_add_canonical() {
51+
canonical=${1/#"${TARGET_DIR}/"}
52+
ADD_CANONICAL=true
53+
if [ ! "$MAJOR_VSN" -gt "26" ]; then
54+
REDIRECT=$(grep -P "^/doc/.*?/?${canonical#apps\/}" "_redirects" || echo "")
55+
if [ "$(echo "${REDIRECT}" | wc -l)" -gt "0" ] && [ "$(echo "${REDIRECT}" | wc -w)" -gt "0" ]; then
56+
canonical=$(echo "${REDIRECT}" | tr " " "\n" | head -1 | sed 's/^\/doc\///')
57+
canonical=${canonical#/}
58+
else
59+
ADD_CANONICAL=false
60+
fi
61+
fi
62+
CANONICAL_GREP=$(grep -P "<link rel=\"canonical\"" "$1" || echo "")
63+
if [ "$(echo "${CANONICAL_GREP}" | wc -w)" -gt "0" ]; then
64+
sed 's@<link rel="canonical" href="[^"]*" />@<link rel="canonical" href="'"${CANONICAL_URL}${canonical}"'" />@g' -i -- "$1"
65+
ADD_CANONICAL=false
66+
fi
67+
if [ "$ADD_CANONICAL" = true ]; then
68+
sed 's@\s*<title>@<link rel="canonical" href="'"${CANONICAL_URL}""${canonical}"'" />\n&@g' -i -- "$1"
69+
fi
70+
}
71+
72+
_add_head_tags() {
73+
find "${TARGET_DIR}" -type f -name "*.html" -print0 \
74+
| while IFS= read -r -d $'\0' file; do
75+
_fixup_major_version "${file}"
76+
if [ "$MAJOR_VSN" -gt "26" ]; then
77+
_fixup_search_link "${file}"
78+
fi
79+
_add_canonical "${file}"
80+
done
81+
}
82+
83+
for MAJOR_VSN in ${MAJOR_VSNS}; do
84+
85+
TARGET_DIR="${DOCS_DIR}/${MAJOR_VSN}"
86+
if [ "$MAJOR_VSN" = "doc" ]; then
87+
MAJOR_VSN="${LATEST_MAJOR_VSN}"
88+
elif [ "$MAJOR_VSN" -eq "1" ]; then # MAJOR_VSN = 1 means master-branch vsn
89+
MAJOR_VSN="${LATEST_MAJOR_VSN}"
90+
fi
91+
echo "Adding head tags for OTP ${MAJOR_VSN} in ${TARGET_DIR}"
92+
# shellcheck disable=SC2046
93+
_add_head_tags "${TARGET_DIR}"
94+
if [ "${MAJOR_VSN}" -gt "26" ]; then
95+
_disable_autocomplete "${TARGET_DIR}/"*.html
96+
_disable_autocomplete "${TARGET_DIR}/system/"*.html
97+
fi
98+
done
99+
100+
exit 0

_scripts/otp_flatten_docs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,6 @@ sub copy_html {
263263
close DEST;
264264
return copy_doc($src, $dest);
265265
}
266-
if ($include_docsearch) {
267-
s@(\s*)<title>@"$1<meta name=\"major-vsn\" content=\"" . $docsearch_version . "\">\n$&"@e;
268-
}
269266
s@src="[^\"]*erlang-logo.png"@"src=\"" . path_to("erlang-logo.png", $rel) . '"' @e;
270267
s@src="[^\"]*flipmenu[.]js"@"src=\"" . path_to("js/flipmenu/flipmenu.js", $rel) . '"' @e;
271268
s@image[Pp]ath="[^\"]*flipmenu"@"imagePath=\"" . path_to("js/flipmenu", $rel) . '"' @e;

_scripts/otp_flatten_ex_docs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ MASTER_MAJOR_VSN="$6"
2828

2929
APP_VSNS=$(cd "${SOURCE_DIR}/lib/" && ls -d -1 * && cd .. && ls -d -1 erts-*)
3030

31-
if [ "${MAJOR_VSN}" = "${LATEST_MAJOR_VSN}" ]; then
32-
CANONICAL_URL="https://www.erlang.org/doc/";
33-
else
34-
CANONICAL_URL="https://www.erlang.org/docs/${MAJOR_VSN}/";
35-
fi
3631

3732

3833
_app_name() {
@@ -67,24 +62,6 @@ _fixup_doc_links() {
6762
sed $FIXUP -i -- "$@"
6863
}
6964

70-
_fixup_search_link() {
71-
sed 's@\s*<title>@<meta name="exdoc:full-text-search-url" content="/doc/search.html?v='"${MAJOR_VSN}"'\&q=">\n&@g' -i -- "$@"
72-
}
73-
74-
_fixup_major_version() {
75-
sed 's@\s*<title>@<meta name="major-vsn" content="'"${MAJOR_VSN}"'">\n&@g' -i -- "$@"
76-
}
77-
78-
_disable_autocomplete() {
79-
sed 's@\s*<title>@<meta name="exdoc:autocomplete" content="off">\n&@g' -i -- "$@"
80-
}
81-
82-
_add_canonical() {
83-
for file in "$@"; do
84-
canonical=${file/#"${TARGET_DIR}/"}
85-
sed 's@\s*<title>@<link rel="canonical" href="'"${CANONICAL_URL}""${canonical}"'" />\n&@g' -i -- "${file}"
86-
done
87-
}
8865

8966
_get_vsns() {
9067
grep "${1}" "${OTP_VERSIONS_TABLE}" | awk '{print $1}' | sed 's/OTP-\(.*\)/\1/g'
@@ -140,21 +117,16 @@ _add_versions() {
140117

141118
_flatten_and_update_docs() {
142119
_fixup_doc_links "$@"
143-
_fixup_search_link "$@"
144-
_fixup_major_version "$@"
145-
_add_canonical "$@"
146120
}
147121

148122
mkdir "${TARGET_DIR}"
149123

150124
( GLOBIGNORE="${SOURCE_DIR}/doc/system"; cp -r "${SOURCE_DIR}/doc/"* "${TARGET_DIR}/" )
151125
_flatten_and_update_docs "${TARGET_DIR}/"*.html
152-
_disable_autocomplete "${TARGET_DIR}/"*.html
153126
_add_versions "${TARGET_DIR}" "" "OTP"
154127
cp -r "${SOURCE_DIR}/doc/system" "${TARGET_DIR}/system"
155-
_flatten_and_update_docs "${TARGET_DIR}/system/"*.html
156-
_disable_autocomplete "${TARGET_DIR}/system/"*.html
157128
_add_versions "${TARGET_DIR}/system" "/system" "OTP"
129+
_flatten_and_update_docs "${TARGET_DIR}/system/"*.html
158130

159131
mkdir "${TARGET_DIR}/apps"
160132

0 commit comments

Comments
 (0)