1
1
#! /bin/sh
2
2
if [ $# != 2 ]; then
3
- echo >&2 " usage: $0 BASE_DOC_COMMIT DOC_REPO"
4
- echo >&2 " creates CHANGES.html in the current directory"
5
- echo >&2 " for the diffs of DOC_REPO against BASE_DOC_COMMIT"
3
+ echo >&2 " Usage: $0 DIFF_TEXT DOC_REPO"
4
+ echo >&2 " This script generates a CHANGES.html file in the current directory"
5
+ echo >&2 " and adds anchor targets in the documents within DOC_REPO"
6
+ echo >&2 " based on the diff hunks in the DIFF_TEXT file."
6
7
exit 1
7
8
fi
8
- BASE_DOC_COMMIT =" $1 "
9
+ DIFF_TEXT =" $1 "
9
10
DOC_REPOSITORY=" $2 "
10
11
11
- # Wipe out chronic diffs between old doc and new doc
12
- (cd $DOC_REPOSITORY && find . -name " *.html" | xargs sed -i -e ' \;<script type="application/vnd\.jupyter\.widget-state+json">;,\;</script>; d' )
13
12
# Create CHANGES.html
14
13
echo ' <html>' > CHANGES.html
15
14
echo ' <head>' >> CHANGES.html
@@ -19,79 +18,102 @@ echo '<script>hljs.highlightAll();</script>' >> CHANGES.html
19
18
cat >> CHANGES.html << EOF
20
19
<script>
21
20
document.addEventListener('DOMContentLoaded', () => {
22
- const baseDocURL = 'https://sagemath.netlify.app'
21
+ // This URL is hardcoded in the file .github/workflows/doc-publish.yml.
22
+ // See NETLIFY_ALIAS of the "Deploy to Netlify" step.
23
+ const baseDocURL = 'https://doc-develop--sagemath.netlify.app'
23
24
const diffSite = 'https://pianomister.github.io/diffsite'
24
25
const diffParagraphs = document.querySelectorAll('p.diff');
25
26
diffParagraphs.forEach(paragraph => {
26
27
const rootURL = window.location.origin;
27
- const docAnchor = paragraph.querySelector('a'); // first "a" element
28
+ const docAnchor = paragraph.querySelector('a');
28
29
const url = new URL(docAnchor.href);
29
30
const path = url.pathname;
30
31
const anchor = document.createElement('a');
31
32
anchor.href = diffSite + '/?url1=' + rootURL + path + '&url2=' + baseDocURL + path;
32
33
anchor.textContent = 'compare with the base';
33
34
anchor.setAttribute('target', '_blank');
35
+ paragraph.innerHTML += ' ';
34
36
paragraph.appendChild(anchor);
35
- paragraph.innerHTML += ' ' ;
36
- const hunkAnchors = paragraph.querySelectorAll('a. hunk');
37
- hunkAnchors.forEach( hunkAnchor => {
37
+ const hunks = paragraph.parentNode.querySelectorAll('p.hunk') ;
38
+ hunks.forEach( hunk => {
39
+ const hunkAnchor = hunk.querySelector('a');
38
40
const url = new URL(hunkAnchor.href);
39
41
const path = url.pathname;
40
42
const pathHash = path + url.hash.replace('#', '%23');
41
43
const anchor = document.createElement('a');
42
44
anchor.href = diffSite + '/?url1=' + rootURL + pathHash + '&url2=' + baseDocURL + path;
43
- anchor.textContent = hunkAnchor.textContent ;
45
+ anchor.textContent = 'compare with the base' ;
44
46
anchor.setAttribute('target', '_blank');
45
- paragraph.appendChild(anchor) ;
46
- paragraph.innerHTML += ' ' ;
47
+ hunk.innerHTML += ' ' ;
48
+ hunk.appendChild(anchor) ;
47
49
});
48
50
});
49
51
});
50
52
</script>
51
53
EOF
52
54
echo ' </head>' >> CHANGES.html
53
55
echo ' <body>' >> CHANGES.html
54
- (cd $DOC_REPOSITORY && git diff $BASE_DOC_COMMIT -- " *.html" ) > diff.txt
55
56
python3 - << EOF
56
57
import os, re, html
57
- with open('diff.txt', 'r') as f:
58
+ from itertools import chain
59
+ with open('$DIFF_TEXT ', 'r') as f:
58
60
diff_text = f.read()
59
61
diff_blocks = re.split(r'^(?=diff --git)', diff_text, flags=re.MULTILINE)
60
62
out_blocks = []
61
63
for block in diff_blocks:
62
64
match = re.search(r'^diff --git a/(.*) b/\1', block, flags=re.MULTILINE)
63
65
if match:
64
- doc = match.group(1)
65
- file_path = os.path.join('$DOC_REPOSITORY ', doc )
66
+ path = match.group(1)
67
+ file_path = os.path.join('$DOC_REPOSITORY ', path )
66
68
try:
67
69
with open(file_path, 'r') as file:
68
70
content = file.readlines()
69
71
except FileNotFoundError:
70
72
content = []
71
73
count = 0
74
+ hunks = []
75
+ hunk_lines = []
76
+ in_hunk = False
72
77
for line in block.splitlines():
73
78
if line.startswith('@@ -'):
79
+ if hunk_lines:
80
+ hunks.append('<pre><code class="language-diff">'
81
+ + html.escape('\n'.join(hunk_lines)).strip()
82
+ + '</code></pre>')
83
+ hunk_lines = []
74
84
search_result = re.search(r'@@ -(\d+),(\d+) \+(\d+),(\d+)', line)
75
85
if search_result:
76
- line_number = int(search_result.group(3))
77
- for i in range(line_number - 1, -1, -1):
78
- if content[i].startswith('<'):
86
+ line_number = int(search_result.group(3)) - 1
87
+ span = int(search_result.group(4))
88
+ for i in chain(range(line_number, line_number + span), range(line_number - 1, -1, -1)):
89
+ try:
90
+ ln = content[i]
91
+ except IndexError:
92
+ continue
93
+ for idx, char in enumerate(ln):
94
+ if not char.isspace():
95
+ break
96
+ else:
97
+ idx = len(ln)
98
+ if ln.startswith('<', idx) and not ln.startswith('</', idx):
79
99
count += 1
80
- content[i] = f'<span id="hunk{count}" style="visibility: hidden;"></span>' + content[i]
100
+ content[i] = ln[:idx] + f'<span id="hunk{count}" style="visibility: hidden;"></span>' + ln[idx:]
101
+ hunks.append(f'<p class="hunk"><a href="{path}#hunk{count}" class="hunk" target="_blank">hunk #{count}</a></p>')
81
102
break
103
+ hunk_lines.append(line)
104
+ if hunk_lines:
105
+ hunks.append('<pre><code class="language-diff">'
106
+ + html.escape('\n'.join(hunk_lines)).strip()
107
+ + '</code></pre>')
82
108
if content:
83
109
with open(file_path, 'w') as file:
84
110
file.writelines(content)
85
- path = doc
86
- hunks = ' '.join(f'<a href="{path}#hunk{i+1}" class="hunk" target="_blank">#{i + 1}</a>' for i in range(count))
87
- out_blocks.append(f'<p class="diff"><a href="{path}">{doc}</a> ' + hunks + ' </p>'
88
- + '\n<pre><code class="language-diff">'
89
- + html.escape(block).strip() + '</code></pre>')
111
+ out_blocks.append(f'<div class="diff"><p class="diff"><a href="{path}">{path}</a></p>\n' + '\n'.join(hunks) + '\n</div>')
90
112
output_text = '\n'.join(out_blocks)
91
113
with open('diff.html', 'w') as f:
92
114
f.write(output_text)
93
115
EOF
94
116
cat diff.html >> CHANGES.html
95
117
echo ' </body>' >> CHANGES.html
96
118
echo ' </html>' >> CHANGES.html
97
- rm diff.txt diff. html
119
+ rm diff.html
0 commit comments