diff --git a/.ci/create-changes-html.sh b/.ci/create-changes-html.sh index 80335ea1613..2cca91d81c8 100755 --- a/.ci/create-changes-html.sh +++ b/.ci/create-changes-html.sh @@ -24,26 +24,27 @@ const diffSite = 'https://pianomister.github.io/diffsite' const diffParagraphs = document.querySelectorAll('p.diff'); diffParagraphs.forEach(paragraph => { const rootURL = window.location.origin; - const docAnchor = paragraph.querySelector('a'); // first "a" element + const docAnchor = paragraph.querySelector('a'); 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.innerHTML += ' '; paragraph.appendChild(anchor); - paragraph.innerHTML += ' '; - const hunkAnchors = paragraph.querySelectorAll('a.hunk'); - hunkAnchors.forEach(hunkAnchor => { + const hunks = paragraph.parentNode.querySelectorAll('p.hunk'); + hunks.forEach(hunk => { + const hunkAnchor = hunk.querySelector('a'); 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.textContent = 'compare with the base'; anchor.setAttribute('target', '_blank'); - paragraph.appendChild(anchor); - paragraph.innerHTML += ' '; + hunk.innerHTML += ' '; + hunk.appendChild(anchor); }); }); }); @@ -54,6 +55,7 @@ echo '
' >> CHANGES.html (cd $DOC_REPOSITORY && git diff $BASE_DOC_COMMIT -- "*.html") > diff.txt python3 - << EOF import os, re, html +from itertools import chain with open('diff.txt', 'r') as f: diff_text = f.read() diff_blocks = re.split(r'^(?=diff --git)', diff_text, flags=re.MULTILINE) @@ -61,32 +63,43 @@ 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) - file_path = os.path.join('$DOC_REPOSITORY', doc) + path = match.group(1) + file_path = os.path.join('$DOC_REPOSITORY', path) try: with open(file_path, 'r') as file: content = file.readlines() except FileNotFoundError: content = [] count = 0 + hunks = [] + hunk_lines = [] + in_hunk = False for line in block.splitlines(): if line.startswith('@@ -'): + if hunk_lines: + hunks.append(''
+ + html.escape('\n'.join(hunk_lines)).strip()
+ + '
')
+ hunk_lines = []
search_result = re.search(r'@@ -(\d+),(\d+) \+(\d+),(\d+)', line)
if search_result:
line_number = int(search_result.group(3))
- for i in range(line_number - 1, -1, -1):
- if content[i].startswith('<'):
+ span = int(search_result.group(4))
+ for i in chain(range(line_number, line_number + span), range(line_number - 1, -1, -1)):
+ if content[i].startswith('<') and not content[i].startswith(''):
count += 1
content[i] = f'' + content[i]
+ hunks.append(f'')
break
+ hunk_lines.append(line)
+ if hunk_lines:
+ hunks.append(''
+ + html.escape('\n'.join(hunk_lines)).strip()
+ + '
')
if content:
with open(file_path, 'w') as file:
file.writelines(content)
- path = doc
- hunks = ' '.join(f'#{i + 1}' for i in range(count))
- out_blocks.append(f'{doc} ' + hunks + '
' - + '\n'
- + html.escape(block).strip() + '
')
+ out_blocks.append(f'