Spaces:
Running
Running
Update app.py
Browse filesNow? we're cooking with gas? Forgot the copy button
111 unmodified lines1121131141151161171181191201211221231241251266 unmodified lines1331341351361371381391401411421431441451461471481491501511521531541551561571581591601611627 unmodified lines17017117217317417517617717817918018118218318418511 unmodified lines1971981992002012022032042052062072082092102 unmodified lines2132142152162172182192202212222232242252262272282298 unmodified lines238239240241242243244111 unmodified linesdef get_minimum_column_width_in(word_dict, is_reference_block=False, ref_text="", ms_text=""):"""Measures character counts to calculate the physical minimum widthin inches needed to hold a column's text without any word-wrapping."""# System assumptions: 1 character in Arial 14.6px is approx 0.075 inches wide.# Hebrew characters at 24px are much wider, approx 0.145 inches per character.char_width_in = 0.075hebrew_char_width_in = 0.145# Base cell padding safety allowance: 0.069 inches on both sides = 0.138 inches totalpadding_buffer_in = 0.069 * 2if is_reference_block:6 unmodified linesw_morph = len(word_dict.get("morphology", "")) * char_width_inmax_content_width = max(w_hebrew, w_translit, w_trans, w_morph)# Give it a baseline minimum size if it's mostly blankreturn max(max_content_width, 0.65) + padding_buffer_indef calculate_recursive_table_chunks(words_data, reference, manuscript):"""ESSENTIAL PROCEDURE: Groups words dynamically. If a 7-slot row's minimum un-wrappedwidths exceed 7.0 inches total, the last word drops down to the next row group recursively."""total_allowed_width_in = 7.0final_row_chunks = []# Work with a shallow copy of the word array list so we can shift items dynamicallyword_pool = list(words_data)while len(word_pool) > 0:is_first_row = (len(final_row_chunks) == 0)# First row has 1 slot reserved for reference, leaving up to 6 word positions.# Other rows take up to 7 word positions.max_word_limit = 6 if is_first_row else 7# Pull out a trial row slicecurrent_row_words = word_pool[:max_word_limit]# Recursive check loop to pull words out until the row fits into 7 incheswhile len(current_row_words) > 1:minimum_widths = []7 unmodified linestotal_required_row_width = sum(minimum_widths)# Scenario A: The columns fit safely within 7 inches without wrapping text!if total_required_row_width <= total_allowed_width_in:break# Scenario B: Over 7 inches! Move the last word down to the next table row.current_row_words.pop()# Commit the finalized word slice to this table blockfinal_row_chunks.append(current_row_words)# Delete the processed slice off the front of our remaining word poolword_pool = word_pool[len(current_row_words):]return final_row_chunks11 unmodified lineshtml_output = ['<div id="table-payload" class="white-canvas" style="background-color: #ffffff; padding: 20px; border-radius: 8px; display: inline-block; width: 100%; box-sizing: border-box; text-align: center;">']# RUN RECURSIVE LAYOUT SPLITTING PROCEDUREtable_chunks = calculate_recursive_table_chunks(words_data, reference, manuscript)for idx, current_chunk in enumerate(table_chunks):is_first = (idx == 0)is_last_table = (idx == len(table_chunks) - 1)# Step 1: Establish baseline minimum requirements to verify absolute protection boundariesslot_minimums = []if is_first:ref_min = get_minimum_column_width_in({}, is_reference_block=True, ref_text=reference, ms_text=manuscript)2 unmodified linesfor word in current_chunk:slot_minimums.append(get_minimum_column_width_in(word))# Symmetrical filler pads trailing tables up to exactly 7 columns wideif is_last_table:while (len(current_chunk) + (1 if is_first else 0)) < total_slots:current_chunk.append({"hebrew": " ", "transliteration": " ", "translation": " ", "morphology": " "})slot_minimums.append(0.75) # Standard padding size allocation score for blank boxes# Step 2 & 3: Run Proportional Balancing to scale the table footprint to exactly 7.0 inches widetotal_minimum_score = sum(slot_minimums)calculated_widths = []for min_w in slot_minimums:# Scale proportionally so the combined columns perfectly lock at 7 inchesallocated_width = (min_w / total_minimum_score) * total_table_width_incalculated_widths.append(round(allocated_width, 3))8 unmodified lineshtml_output.append(f' <col style="width: {c_width}in !important; min-width: {c_width}in !important; max-width: {c_width}in !important;">')html_output.append(' </colgroup>')# LOCKED REQUIREMENT: Exact 0.069in padding on all sides, text wrap disabled via nowrap, box size clampedbase_cell = "border: 1pt solid #b7b7b7; padding: 0.069in !important; height: auto; overflow: hidden; white-space: nowrap !important; box-sizing: border-box !important;"hebrew_cell_style = f"font-family: 'Times New Roman', serif !important; font-size: 24px !important; color: #000000 !important; text-align: right; vertical-align: middle; {base_cell}"english_cell_style = f"font-family: Arial, sans-serif !important; font-size: 14.6667px !important; color: #000000 !important; text-align: center; vertical-align: top; line-height: 1.0 !important; {base_cell}"111 unmodified lines1121131141151161171181191201211221231246 unmodified lines1311321331341351361371381391401411421431441451461471481491501511521537 unmodified lines16116216316416516616716816917017117217317411 unmodified lines1861871881891901911921931941951961972 unmodified lines2002012022032042052062072082092102112122132148 unmodified lines223224225226227228229111 unmodified linesdef get_minimum_column_width_in(word_dict, is_reference_block=False, ref_text="", ms_text=""):"""Measures character lengths to establish the minimum width in inchesneeded to hold content cleanly on a single line without wrapping."""char_width_in = 0.072hebrew_char_width_in = 0.140# Precise padding safety buffer: 0.069 inches on both sides = 0.138 inchespadding_buffer_in = 0.069 * 2if is_reference_block:6 unmodified linesw_morph = len(word_dict.get("morphology", "")) * char_width_inmax_content_width = max(w_hebrew, w_translit, w_trans, w_morph)return max(max_content_width, 0.65) + padding_buffer_indef calculate_recursive_table_chunks(words_data, reference, manuscript):"""ESSENTIAL PROCEDURE: Groups words. If a 7-column layout requires more than7.0 inches to prevent wrapping, the last word drops down recursively."""total_allowed_width_in = 7.0final_row_chunks = []word_pool = list(words_data)while len(word_pool) > 0:is_first_row = (len(final_row_chunks) == 0)max_word_limit = 6 if is_first_row else 7current_row_words = word_pool[:max_word_limit]while len(current_row_words) > 1:minimum_widths = []7 unmodified linestotal_required_row_width = sum(minimum_widths)# If the combined row width fits comfortably inside 7 inches, we break and lock itif total_required_row_width <= total_allowed_width_in:break# If it exceeds 7 inches, drop the last word to the next row poolcurrent_row_words.pop()final_row_chunks.append(current_row_words)word_pool = word_pool[len(current_row_words):]return final_row_chunks11 unmodified lineshtml_output = ['<div id="table-payload" class="white-canvas" style="background-color: #ffffff; padding: 20px; border-radius: 8px; display: inline-block; width: 100%; box-sizing: border-box; text-align: center;">']table_chunks = calculate_recursive_table_chunks(words_data, reference, manuscript)for idx, current_chunk in enumerate(table_chunks):is_first = (idx == 0)is_last_table = (idx == len(table_chunks) - 1)slot_minimums = []if is_first:ref_min = get_minimum_column_width_in({}, is_reference_block=True, ref_text=reference, ms_text=manuscript)2 unmodified linesfor word in current_chunk:slot_minimums.append(get_minimum_column_width_in(word))if is_last_table:while (len(current_chunk) + (1 if is_first else 0)) < total_slots:current_chunk.append({"hebrew": " ", "transliteration": " ", "translation": " ", "morphology": " "})slot_minimums.append(0.75)# Proportional balancing math ensures all columns total exactly 7.0 inches overalltotal_minimum_score = sum(slot_minimums)calculated_widths = []for min_w in slot_minimums:allocated_width = (min_w / total_minimum_score) * total_table_width_incalculated_widths.append(round(allocated_width, 3))8 unmodified lineshtml_output.append(f' <col style="width: {c_width}in !important; min-width: {c_width}in !important; max-width: {c_width}in !important;">')html_output.append(' </colgroup>')# Symmetrical cell padding exactly 0.069in on all 4 sides with wrap suppressed safelybase_cell = "border: 1pt solid #b7b7b7; padding: 0.069in !important; height: auto; overflow: hidden; white-space: nowrap !important; box-sizing: border-box !important;"hebrew_cell_style = f"font-family: 'Times New Roman', serif !important; font-size: 24px !important; color: #000000 !important; text-align: right; vertical-align: middle; {base_cell}"english_cell_style = f"font-family: Arial, sans-serif !important; font-size: 14.6667px !important; color: #000000 !important; text-align: center; vertical-align: top; line-height: 1.0 !important; {base_cell}"
app.py+12-27