Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plant anchors for hunks to create links in doc preview changes #36742

Merged
merged 16 commits into from
Dec 10, 2023
91 changes: 91 additions & 0 deletions .ci/create-changes-html.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/bin/sh
if [ $# != 2 ]; then
echo >&2 "usage: $0 BASE_DOC_COMMIT DOC_REPOSITORY"
echo >&2 "creates CHANGES.html in the current directory"
echo >&2 "for the diffs of DOC_REPOSITORY against BASE_DOC_COMMIT"
exit 1
fi
BASE_DOC_COMMIT="$1"
DOC_REPOSITORY="$2"

# Wipe out chronic diffs between old doc and new doc
(cd $DOC_REPOSITORY && find . -name "*.html" | xargs sed -i -e '\;<script type="application/vnd\.jupyter\.widget-state+json">;,\;</script>; d')
# Create CHANGES.html
echo '<html>' > CHANGES.html
echo '<head>' >> CHANGES.html
echo '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css">' >> CHANGES.html
echo '<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>' >> CHANGES.html
echo '<script>hljs.highlightAll();</script>' >> CHANGES.html
cat >> CHANGES.html << EOF
<script>
document.addEventListener('DOMContentLoaded', () => {
const diffSite = 'https://pianomister.github.io/diffsite'
const baseDocURL = 'https://sagemath-tobias.netlify.app'
const diffParagraphs = document.querySelectorAll('p.diff');
diffParagraphs.forEach(paragraph => {
const rootURL = window.location.origin;
const docAnchor = paragraph.querySelector('a'); // first "a" element
const url = new URL(docAnchor.href);
const path = url.pathname;
const anchor = document.createElement('a');
anchor.href = diffSite + '/?url1=' + rootURL + path + '&url2=' + baseDocURL + path;
anchor.textContent = 'compare with the base';
anchor.setAttribute('target', '_blank');
paragraph.appendChild(anchor);
paragraph.innerHTML += '&nbsp;';
const hunkAnchors = paragraph.querySelectorAll('a.hunk');
hunkAnchors.forEach(hunkAnchor => {
const url = new URL(hunkAnchor.href);
const path = url.pathname;
const pathHash = path + url.hash.replace('#', '%23');
const anchor = document.createElement('a');
anchor.href = diffSite + '/?url1=' + rootURL + pathHash + '&url2=' + baseDocURL + path;
anchor.textContent = hunkAnchor.textContent;
anchor.setAttribute('target', '_blank');
paragraph.appendChild(anchor);
paragraph.innerHTML += '&nbsp;';
});
});
});
</script>
EOF
echo '</head>' >> CHANGES.html
echo '<body>' >> CHANGES.html
(cd $DOC_REPOSITORY && git diff $BASE_DOC_COMMIT -- *.html) > diff.txt
/sage/sage -python - << EOF
kwankyu marked this conversation as resolved.
Show resolved Hide resolved
import os, re, html
with open('diff.txt', 'r') as f:
diff_text = f.read()
diff_blocks = re.split(r'^(?=diff --git)', diff_text, flags=re.MULTILINE)
out_blocks = []
for block in diff_blocks:
match = re.search(r'^diff --git a/(.*) b/\1', block, flags=re.MULTILINE)
if match:
doc = match.group(1)
path = 'html/' + doc
file_path = os.path.join('$DOC_REPOSITORY', doc)
with open(file_path, 'r') as file:
content = file.readlines()
count = 0
for line in block.splitlines():
if line.startswith('@@ -'):
line_number = int(re.search(r'@@ -(\d+)', line).group(1))
for i in range(line_number, -1, -1):
if content[i].startswith('<'):
count += 1
content[i] = f'<span id="hunk{count}" style="visibility: hidden;"></span>' + content[i]
break
with open(file_path, 'w') as file:
file.writelines(content)
hunks = '&nbsp;'.join(f'<a href="{path}#hunk{i+1}" class="hunk" target="_blank">#{i + 1}</a>' for i in range(count))
out_blocks.append(f'<p class="diff"><a href="{path}">{doc}</a>&nbsp;' + hunks + '&emsp;</p>'
+ '\n<pre><code class="language-diff">'
+ html.escape(block).strip() + '</code></pre>')
output_text = '\n'.join(out_blocks)
with open('diff.html', 'w') as f:
f.write(output_text)
EOF
cat diff.html >> CHANGES.html
echo '</body>' >> CHANGES.html
echo '</html>' >> CHANGES.html
rm diff.txt diff.html
76 changes: 15 additions & 61 deletions .github/workflows/doc-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ jobs:
-e 's;'"$mathjax_path_from"';'"$mathjax_path_to"';' \
-e '\;<script type="application/vnd\.jupyter\.widget-state+json">;,\;</script>; d')
# Create git repo from old doc
(cd /sage/local/share/doc/sage/html && \
git init && \
DOC_DIR=/sage/local/share/doc/sage/html
(cd $DOC_DIR && git init && \
(echo "*.svg binary"; echo "*.pdf binary") >> .gitattributes && \
(echo ".buildinfo"; echo '*.inv'; echo '.git*'; echo '*.svg'; echo '*.pdf'; echo '*.png'; echo 'searchindex.js') > .gitignore; \
git add -A && git commit --quiet -m "old")
Expand Down Expand Up @@ -115,10 +115,11 @@ jobs:
# incremental docbuild may introduce broken links (inter-file references) though build succeeds
run: |
set -ex
export SAGE_USE_CDNS=yes
mv /sage/local/share/doc/sage/html/.git /sage/.git-doc
DOC_DIR=/sage/local/share/doc/sage/html
mv $DOC_DIR/.git /sage/.git-doc
make doc-clean doc-uninstall
mkdir -p /sage/local/share/doc/sage/html/ && mv /sage/.git-doc /sage/local/share/doc/sage/html/.git
mkdir -p $DOC_DIR/ && mv /sage/.git-doc $DOC_DIR/.git
export SAGE_USE_CDNS=yes
./config.status && make sagemath_doc_html-no-deps
working-directory: ./worktree-image
env:
Expand All @@ -130,63 +131,16 @@ jobs:
if: (success() || failure()) && steps.docbuild.outcome == 'success'
run: |
set -ex
mkdir -p ./docs
(cd /sage/local/share/doc/sage/html && git commit -a -m 'new')
# Wipe out chronic diffs between old doc and new doc
(cd /sage/local/share/doc/sage/html && \
find . -name "*.html" | xargs sed -i -e '\;<script type="application/vnd\.jupyter\.widget-state+json">;,\;</script>; d')
# Create CHANGES.html
echo '<html>' > ./docs/CHANGES.html
echo '<head>' >> ./docs/CHANGES.html
echo '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/default.min.css">' >> ./docs/CHANGES.html
echo '<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>' >> ./docs/CHANGES.html
echo '<script>hljs.highlightAll();</script>' >> ./docs/CHANGES.html
cat >> ./docs/CHANGES.html << EOF
<script>
document.addEventListener('DOMContentLoaded', () => {
const diffSite = 'https://pianomister.github.io/diffsite/'
const baseDocURL = 'https://sagemath-tobias.netlify.app/'
const diffParagraphs = document.querySelectorAll('p.diff');
diffParagraphs.forEach(paragraph => {
const rootURL = window.location.origin + '/';
const docAnchor = paragraph.querySelector('a');
const path = docAnchor.textContent; // .href doesn't work
const anchor = document.createElement('a');
anchor.href = diffSite + '?url1=' + rootURL + path + '&url2=' + baseDocURL + path;
anchor.textContent = 'compare with the base';
anchor.setAttribute('target', '_blank');
paragraph.appendChild(anchor);
});
});
</script>
EOF
echo '</head>' >> ./docs/CHANGES.html
echo '<body>' >> ./docs/CHANGES.html
(cd /sage/local/share/doc/sage/html && git diff HEAD^ -- *.html; rm -rf .git) > ./docs/diff.txt
/sage/sage -python - << EOF
import re, html
with open('./docs/diff.txt', 'r') as f:
diff_text = f.read()
diff_blocks = re.split(r'^(?=diff --git)', diff_text, flags=re.MULTILINE)
out_blocks = []
for block in diff_blocks:
match = re.search(r'^diff --git a/(.*) b/\1', block, flags=re.MULTILINE)
if match:
path = 'html/' + match.group(1)
out_blocks.append(f'<p class="diff"><a href="{path}">{path}</a>&emsp;</p>\n<pre><code class="language-diff">' + html.escape(block).strip() + '</code></pre>')
output_text = '\n'.join(out_blocks)
with open('./docs/diff.html', 'w') as f:
f.write(output_text)
EOF
cat ./docs/diff.html >> ./docs/CHANGES.html
echo '</body>' >> ./docs/CHANGES.html
echo '</html>' >>./docs/CHANGES.html
rm ./docs/diff.txt ./docs/diff.html
# For some reason the deploy step below cannot find /sage/...
# So copy everything from there to local folder
DOC_DIR=/sage/local/share/doc/sage/html
(cd $DOC_DIR && git commit -a -m 'new')
.ci/create-changes-html.sh $(cd $DOC_DIR; git rev-parse HEAD^) $DOC_DIR
(cd $DOC_DIR && rm -rf .git)
# We copy everything to a local folder
# We also need to replace the symlinks because netlify is not following them
cp -r -L /sage/local/share/doc/sage/html ./docs
cp /sage/local/share/doc/sage/index.html ./docs
mkdir -p ./docs
mv CHANGES.html ./docs
cp -r -L $DOC_DIR ./docs
cp $DOC_DIR/../index.html ./docs
# Zip everything for increased performance
zip -r docs.zip docs

Expand Down
Loading