Spaces:
Sleeping
Sleeping
Update app.py
Browse files114 unmodified lines11511611711811912010 unmodified lines131132133134135136137138246 unmodified lines385386387388389390391114 unmodified lines# Replace hyphen variants with U+002D for internal consistencyreturn text.replace('\u2011', '-').replace('\u2013', '-').replace('\u2014', '-')def encode_text_fragment(text):# Encode text for W3C Text Fragments, preserving only regular hyphens (U+002D)# Non-breaking hyphens (U+2011) are encoded as %E2%80%9110 unmodified linesreturn hashlib.sha256(data.encode('utf-8')).hexdigest()def format_citation_html(url, fragment_text, author, year, scc_hash):# Use original fragment_text for text fragment URL to match external sourceencoded_fragment = encode_text_fragment(fragment_text)full_url = f"{url}#:~:text={encoded_fragment}"return f'<a href="{full_url}" data-hash="{scc_hash}">{author} ({year})</a>'246 unmodified linesnormalised_task_name = normalise_hyphens(task_name)scc_hash = generate_citation_hash(author_name, publication_year, source_url, normalised_annotated_text, normalised_annotated_text, username, normalised_task_name, current_date, current_time)citation_link_start = format_citation_html(source_url, annotated_text, author_name, publication_year, scc_hash)citation_link_end = f'<a href="{source_url}#:~:text={encode_text_fragment(annotated_text)}" data-hash="{scc_hash}">({author_name}, {publication_year})</a>'metadata_link = format_metadata_html(source_url, author_name, publication_year, scc_hash, username, task_name, current_date, current_time)col_html1, col_html2 = st.columns(2)114 unmodified lines11511611711811912012112212312412512612712812913013113210 unmodified lines143144145146147148149150151246 unmodified lines398399400401402403404405406114 unmodified lines# Replace hyphen variants with U+002D for internal consistencyreturn text.replace('\u2011', '-').replace('\u2013', '-').replace('\u2014', '-')def select_longest_segment(text):# Split text by various dashes (hyphen, non-breaking hyphen, en dash, em dash)dash_variants = ['\u002D', '\u2011', '\u2013', '\u2014']segments = [text]for dash in dash_variants:new_segments = []for segment in segments:new_segments.extend(segment.split(dash))segments = new_segments# Return the longest segment, or original text if no dashesreturn max(segments, key=len, default=text).strip()def encode_text_fragment(text):# Encode text for W3C Text Fragments, preserving only regular hyphens (U+002D)# Non-breaking hyphens (U+2011) are encoded as %E2%80%9110 unmodified linesreturn hashlib.sha256(data.encode('utf-8')).hexdigest()def format_citation_html(url, fragment_text, author, year, scc_hash):# Select the longest segment for the text fragment to avoid breaking the linkselected_fragment = select_longest_segment(fragment_text)encoded_fragment = encode_text_fragment(selected_fragment)full_url = f"{url}#:~:text={encoded_fragment}"return f'<a href="{full_url}" data-hash="{scc_hash}">{author} ({year})</a>'246 unmodified linesnormalised_task_name = normalise_hyphens(task_name)scc_hash = generate_citation_hash(author_name, publication_year, source_url, normalised_annotated_text, normalised_annotated_text, username, normalised_task_name, current_date, current_time)citation_link_start = format_citation_html(source_url, annotated_text, author_name, publication_year, scc_hash)# Use the longest segment for the end-of-text citation linkselected_fragment = select_longest_segment(annotated_text)citation_link_end = f'<a href="{source_url}#:~:text={encode_text_fragment(selected_fragment)}" data-hash="{scc_hash}">({author_name}, {publication_year})</a>'metadata_link = format_metadata_html(source_url, author_name, publication_year, scc_hash, username, task_name, current_date, current_time)col_html1, col_html2 = st.columns(2)
app.py+18-3