@@ -24,26 +24,27 @@ const diffSite = 'https://pianomister.github.io/diffsite'
2424const diffParagraphs = document.querySelectorAll('p.diff');
2525diffParagraphs.forEach(paragraph => {
2626 const rootURL = window.location.origin;
27- const docAnchor = paragraph.querySelector('a'); // first "a" element
27+ const docAnchor = paragraph.querySelector('a');
2828 const url = new URL(docAnchor.href);
2929 const path = url.pathname;
3030 const anchor = document.createElement('a');
3131 anchor.href = diffSite + '/?url1=' + rootURL + path + '&url2=' + baseDocURL + path;
3232 anchor.textContent = 'compare with the base';
3333 anchor.setAttribute('target', '_blank');
34+ paragraph.innerHTML += ' ';
3435 paragraph.appendChild(anchor);
35- paragraph.innerHTML += ' ' ;
36- const hunkAnchors = paragraph.querySelectorAll('a. hunk');
37- hunkAnchors.forEach( hunkAnchor => {
36+ const hunks = paragraph.parentNode.querySelectorAll('p.hunk') ;
37+ hunks.forEach( hunk => {
38+ const hunkAnchor = hunk.querySelector('a');
3839 const url = new URL(hunkAnchor.href);
3940 const path = url.pathname;
4041 const pathHash = path + url.hash.replace('#', '%23');
4142 const anchor = document.createElement('a');
4243 anchor.href = diffSite + '/?url1=' + rootURL + pathHash + '&url2=' + baseDocURL + path;
43- anchor.textContent = hunkAnchor.textContent ;
44+ anchor.textContent = 'compare with the base' ;
4445 anchor.setAttribute('target', '_blank');
45- paragraph.appendChild(anchor) ;
46- paragraph.innerHTML += ' ' ;
46+ hunk.innerHTML += ' ' ;
47+ hunk.appendChild(anchor) ;
4748 });
4849});
4950});
@@ -54,39 +55,51 @@ echo '<body>' >> CHANGES.html
5455(cd $DOC_REPOSITORY && git diff $BASE_DOC_COMMIT -- " *.html" ) > diff.txt
5556python3 - << EOF
5657import os, re, html
58+ from itertools import chain
5759with open('diff.txt', 'r') as f:
5860 diff_text = f.read()
5961diff_blocks = re.split(r'^(?=diff --git)', diff_text, flags=re.MULTILINE)
6062out_blocks = []
6163for block in diff_blocks:
6264 match = re.search(r'^diff --git a/(.*) b/\1', block, flags=re.MULTILINE)
6365 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 )
6668 try:
6769 with open(file_path, 'r') as file:
6870 content = file.readlines()
6971 except FileNotFoundError:
7072 content = []
7173 count = 0
74+ hunks = []
75+ hunk_lines = []
76+ in_hunk = False
7277 for line in block.splitlines():
7378 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 = []
7484 search_result = re.search(r'@@ -(\d+),(\d+) \+(\d+),(\d+)', line)
7585 if search_result:
7686 line_number = int(search_result.group(3))
77- for i in range(line_number - 1, -1, -1):
78- if content[i].startswith('<'):
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+ if content[i].startswith('<') and not content[i].startswith('</'):
7990 count += 1
8091 content[i] = f'<span id="hunk{count}" style="visibility: hidden;"></span>' + content[i]
92+ hunks.append(f'<p class="hunk"><a href="{path}#hunk{count}" class="hunk" target="_blank">hunk #{count}</a></p>')
8193 break
94+ hunk_lines.append(line)
95+ if hunk_lines:
96+ hunks.append('<pre><code class="language-diff">'
97+ + html.escape('\n'.join(hunk_lines)).strip()
98+ + '</code></pre>')
8299 if content:
83100 with open(file_path, 'w') as file:
84101 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>')
102+ out_blocks.append(f'<div class="diff"><p class="diff"><a href="{path}">{path}</a></p>\n' + '\n'.join(hunks) + '\n</div>')
90103output_text = '\n'.join(out_blocks)
91104with open('diff.html', 'w') as f:
92105 f.write(output_text)
0 commit comments