Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -115,33 +115,31 @@ def normalise_hyphens(text):
|
|
| 115 |
# Replace hyphen variants with U+002D for internal consistency
|
| 116 |
return text.replace('\u2011', '-').replace('\u2013', '-').replace('\u2014', '-')
|
| 117 |
|
| 118 |
-
def
|
| 119 |
-
# Split text by various
|
| 120 |
-
|
| 121 |
-
segments =
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
if
|
| 125 |
-
|
| 126 |
-
# Return the longest segment
|
| 127 |
-
return max(segments, key=len)
|
| 128 |
|
| 129 |
def encode_text_fragment(text):
|
| 130 |
-
# Get the longest segment if text contains dashes
|
| 131 |
-
fragment_text = get_longest_segment(text)
|
| 132 |
# Encode text for W3C Text Fragments, preserving only regular hyphens (U+002D)
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
def
|
| 136 |
-
#
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
return hashlib.sha256(data.encode('utf-8')).hexdigest()
|
| 142 |
|
| 143 |
def format_citation_html(url, fragment_text, author, year, scc_hash):
|
| 144 |
-
# Use
|
| 145 |
encoded_fragment = encode_text_fragment(fragment_text)
|
| 146 |
full_url = f"{url}#:~:text={encoded_fragment}"
|
| 147 |
return f'<a href="{full_url}" data-hash="{scc_hash}">{author} ({year})</a>'
|
|
|
|
| 115 |
# Replace hyphen variants with U+002D for internal consistency
|
| 116 |
return text.replace('\u2011', '-').replace('\u2013', '-').replace('\u2014', '-')
|
| 117 |
|
| 118 |
+
def select_longest_segment(text):
|
| 119 |
+
# Split text by various dashes (hyphen, en dash, em dash)
|
| 120 |
+
dash_variants = ['\u002D', '\u2011', '\u2013', '\u2014']
|
| 121 |
+
segments = text
|
| 122 |
+
for dash in dash_variants:
|
| 123 |
+
segments = segments.split(dash)
|
| 124 |
+
# Return the longest segment, or original text if no dashes
|
| 125 |
+
return max(segments, key=len, default=text).strip()
|
|
|
|
|
|
|
| 126 |
|
| 127 |
def encode_text_fragment(text):
|
|
|
|
|
|
|
| 128 |
# Encode text for W3C Text Fragments, preserving only regular hyphens (U+002D)
|
| 129 |
+
# Non-breaking hyphens (U+2011) are encoded as %E2%80%91
|
| 130 |
+
# En dashes (U+2013) are encoded as %E2%80%93
|
| 131 |
+
# Em dashes (U+2014) are encoded as %E2%80%94
|
| 132 |
+
return urllib.parse.quote(text, safe='-')
|
| 133 |
|
| 134 |
+
def format_citation_html(url, fragment_text, author, year, scc_hash):
|
| 135 |
+
# Select the longest segment for the text fragment to avoid breaking the link
|
| 136 |
+
selected_fragment = select_longest_segment(fragment_text)
|
| 137 |
+
encoded_fragment = encode_text_fragment(selected_fragment)
|
| 138 |
+
full_url = f"{url}#:~:text={encoded_fragment}"
|
| 139 |
+
return f'<a href="{full_url}" data-hash="{scc_hash}">{author} ({year})</a>'
|
|
|
|
| 140 |
|
| 141 |
def format_citation_html(url, fragment_text, author, year, scc_hash):
|
| 142 |
+
# Use original fragment_text for text fragment URL to match external source
|
| 143 |
encoded_fragment = encode_text_fragment(fragment_text)
|
| 144 |
full_url = f"{url}#:~:text={encoded_fragment}"
|
| 145 |
return f'<a href="{full_url}" data-hash="{scc_hash}">{author} ({year})</a>'
|