forked from sagemath/sage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sagemathgh-36742: Plant anchors for hunks to create links in doc prev…
…iew changes <!-- ^^^^^ Please provide a concise, informative and self-explanatory title. Don't put issue numbers in there, do this in the PR body below. For example, instead of "Fixes sagemath#1234" use "Introduce new method to calculate 1+1" --> <!-- Describe your changes here in detail --> The "changes" page of the doc preview of a PR lacks links to the actual changed parts of the doc preview. We plant invisible anchors for each hunk of the diffs and use them to create links to the changed parts. A sample page is https://deploy-preview-36742--sagemath-tobias.netlify.app/changes Wishlist item: In the diffsite comparison view, the left pane showing the doc preview is scrolled to the changed part while the right pane showing the base doc does not. It would be nice if the right pane shows the corresponding part. But this cannot be implemented now because the base doc cannot be altered for specific doc preview for a PR. <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes sagemath#12345". --> <!-- If your change requires a documentation PR, please link it appropriately. --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> <!-- If your change requires a documentation PR, please link it appropriately --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> <!-- Feel free to remove irrelevant items. --> - [x] The title is concise, informative, and self-explanatory. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation accordingly. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on - sagemath#12345: short description why this is a dependency - sagemath#34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: sagemath#36742 Reported by: Kwankyu Lee Reviewer(s): Kwankyu Lee, Matthias Köppe
- Loading branch information
Showing
2 changed files
with
107 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_REPO" | ||
echo >&2 "creates CHANGES.html in the current directory" | ||
echo >&2 "for the diffs of DOC_REPO 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 += ' '; | ||
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 += ' '; | ||
}); | ||
}); | ||
}); | ||
</script> | ||
EOF | ||
echo '</head>' >> CHANGES.html | ||
echo '<body>' >> CHANGES.html | ||
(cd $DOC_REPOSITORY && git diff $BASE_DOC_COMMIT -- *.html) > diff.txt | ||
python3 - << EOF | ||
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 = ' '.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> ' + hunks + ' </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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters