Skip to content

Commit a520f4d

Browse files
authored
Consolidate common PPcomp diff (#1714)
* Consolidate common PPcomp diff Where a line is italic, frequently get solitary underscore in diffs, then the closing underscore on the end of the next line. Particularly happens in poetry. Consolidate those two diffs into one. Fixes #1711 * Further improvement Increment line number of underscore diff from italic verse
1 parent 57de5e8 commit a520f4d

1 file changed

Lines changed: 37 additions & 1 deletion

File tree

src/guiguts/tools/ppcomp.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,46 @@ def flush_changes():
553553
if cur_line and line_has_change:
554554
lines.append((join_tokens(cur_line), cur_linenum))
555555

556+
underscore_reg = (
557+
f"[{FLAG_CH_HTML_L}{FLAG_CH_TEXT_L}]_[{FLAG_CH_HTML_R}{FLAG_CH_TEXT_R}]"
558+
)
556559
# ---- Send to the dialog ----
557-
for text, line_pair in lines:
560+
for lnum, (text, line_pair) in enumerate(lines):
558561
if line_pair is None:
559562
continue
563+
# If there is another line after this one, look for the following circumstances:
564+
# 1. Next line is also the next line in the text file
565+
# 2. This line is just an underscore (plus flags)
566+
# 3. Next line has an underscore at the end
567+
if lnum < len(lines) - 1:
568+
next_text, next_line_pair = lines[lnum + 1]
569+
if (
570+
next_line_pair is not None
571+
and line_pair[1] + 1 == next_line_pair[1] # Next line in text file?
572+
and re.fullmatch(
573+
underscore_reg, # Just an underscore
574+
text,
575+
)
576+
and re.search(f"{underscore_reg}$", next_text) # Ends in underscore
577+
):
578+
# Bring info up from next line onto this one
579+
text = f"{text} {next_text}"
580+
line_pair = next_line_pair
581+
lines[lnum] = (text, line_pair)
582+
# Nullify next line so it is ignore next time round loop
583+
lines[lnum + 1] = (next_text, None)
584+
# If there is a line before this one, look for the following circumstances:
585+
# 1. This line is just an underscore (plus flags)
586+
# 2. Next line has an underscore at the end
587+
if lnum > 0:
588+
prev_text = lines[lnum - 1][0]
589+
if re.fullmatch(underscore_reg, text) and re.search(
590+
f"{underscore_reg}$", prev_text
591+
):
592+
# Increment text line number
593+
line_pair = (line_pair[0], line_pair[1] + 1)
594+
lines[lnum] = (text, line_pair)
595+
560596
a_line, b_line = line_pair
561597
if html_ln:
562598
if a_line is not None:

0 commit comments

Comments
 (0)