mabuseif commited on
Commit
665bf18
·
verified ·
1 Parent(s): 6b97522

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -12
app.py CHANGED
@@ -118,12 +118,12 @@ def check_for_fragment(url):
118
 
119
  def parse_citation(citation_html):
120
  # Parse citation HTML to extract author, year, URL, and fragment text
121
- match = re.match(r'<a href="([^"]+)#:~:text=([^"]+)" data-hash="[^"]+">([^<]+) \((\d{4})\)</a>', citation_html)
122
  if match:
123
- url, encoded_fragment, author, year = match.groups()
124
  fragment_text = urllib.parse.unquote(encoded_fragment)
125
- return author, year, url, fragment_text
126
- return None, None, None, None
127
 
128
  def parse_metadata_hash(metadata_html):
129
  # Parse metadata HTML to extract hash, username, task_name, date, time
@@ -136,6 +136,36 @@ def parse_metadata_hash(metadata_html):
136
  return scc_hash, username, task_name, date, time
137
  return None, None, None, None, None
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  # --- Live Clock JavaScript ---
140
  def live_clock():
141
  return """
@@ -308,27 +338,42 @@ with tabs[1]:
308
  st.header("Verify Citation")
309
  st.markdown("""
310
  <div class="info-card">
311
- Paste the generated citation and hash with their embedded links below to verify the citation's authenticity.
312
  </div>
313
  """, unsafe_allow_html=True)
314
 
315
  # Input fields for citation and hash
316
- citation_input = st.text_area("Paste Citation (with embedded link)", help="Paste the citation HTML, e.g., <a href='URL'>Author (Year)</a>", placeholder='<a href="https://example.com#:~:text=fragment" data-hash="hash">Author (Year)</a>')
317
- hash_input = st.text_area("Paste Hash (with embedded link)", help="Paste the hash HTML, e.g., <a href='URL'>Author (Year). Hash</a>", placeholder='<a href="https://example.com#:~:text=metadata" data-hash="hash">Author (Year). Hash</a>')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
 
319
  verify_button = st.button("Verify Citation", type="primary", use_container_width=True)
320
 
321
  if verify_button:
322
- if not (citation_input and hash_input):
323
- st.error("Please paste both the citation and hash before verifying.")
324
  else:
325
  # Parse citation
326
- author, year, url, fragment_text = parse_citation(citation_input)
327
  # Parse hash and metadata
328
- scc_hash, username, task_name, date, time = parse_metadata_hash(hash_input)
329
 
330
  if not all([author, year, url, fragment_text, scc_hash, username, task_name, date, time]):
331
- st.error("Invalid citation or hash format. Please ensure both inputs are correctly formatted HTML with embedded links.")
332
  else:
333
  # Recompute hash
334
  recomputed_hash = generate_citation_hash(
 
118
 
119
  def parse_citation(citation_html):
120
  # Parse citation HTML to extract author, year, URL, and fragment text
121
+ match = re.match(r'<a href="([^"]+)#:~:text=([^"]+)" data-hash="([^"]+)">([^<]+) \((\d{4})\)</a>', citation_html)
122
  if match:
123
+ url, encoded_fragment, scc_hash, author, year = match.groups()
124
  fragment_text = urllib.parse.unquote(encoded_fragment)
125
+ return author, year, url, fragment_text, scc_hash
126
+ return None, None, None, None, None
127
 
128
  def parse_metadata_hash(metadata_html):
129
  # Parse metadata HTML to extract hash, username, task_name, date, time
 
136
  return scc_hash, username, task_name, date, time
137
  return None, None, None, None, None
138
 
139
+ # --- JavaScript for extracting link attributes ---
140
+ def verification_js():
141
+ return """
142
+ <script>
143
+ function extractCitationData() {
144
+ const citationInput = document.getElementById('citation_input');
145
+ const hashInput = document.getElementById('hash_input');
146
+ const citationOutput = document.getElementById('citation_output');
147
+ const hashOutput = document.getElementById('hash_output');
148
+
149
+ if (citationInput && hashInput && citationOutput && hashOutput) {
150
+ const citationLink = citationInput.getElementsByTagName('a')[0];
151
+ const hashLink = hashInput.getElementsByTagName('a')[0];
152
+
153
+ if (citationLink) {
154
+ citationOutput.value = citationLink.outerHTML;
155
+ } else {
156
+ citationOutput.value = '';
157
+ }
158
+
159
+ if (hashLink) {
160
+ hashOutput.value = hashLink.outerHTML;
161
+ } else {
162
+ hashOutput.value = '';
163
+ }
164
+ }
165
+ }
166
+ </script>
167
+ """
168
+
169
  # --- Live Clock JavaScript ---
170
  def live_clock():
171
  return """
 
338
  st.header("Verify Citation")
339
  st.markdown("""
340
  <div class="info-card">
341
+ Paste the generated citation and hash with their embedded links below to verify the citation's authenticity. Copy the rendered links directly from the output.
342
  </div>
343
  """, unsafe_allow_html=True)
344
 
345
  # Input fields for citation and hash
346
+ st.markdown("""
347
+ <div>
348
+ <label>Paste Citation (with embedded link)</label>
349
+ <div id="citation_input" contenteditable="true" style="border: 1px solid #e0e0e0; border-radius: 4px; padding: 0.5rem; min-height: 50px; margin-bottom: 1rem;"></div>
350
+ <input type="hidden" id="citation_output" name="citation_output">
351
+ <label>Paste Hash (with embedded link)</label>
352
+ <div id="hash_input" contenteditable="true" style="border: 1px solid #e0e0e0; border-radius: 4px; padding: 0.5rem; min-height: 50px; margin-bottom: 1rem;"></div>
353
+ <input type="hidden" id="hash_output" name="hash_output">
354
+ </div>
355
+ """, unsafe_allow_html=True)
356
+
357
+ # Add JavaScript to capture pasted link data
358
+ components.html(verification_js(), height=0)
359
+
360
+ # Capture the extracted data
361
+ citation_html = st.text_input("Citation HTML (auto-filled)", key="citation_output", label_visibility="collapsed")
362
+ hash_html = st.text_input("Hash HTML (auto-filled)", key="hash_output", label_visibility="collapsed")
363
 
364
  verify_button = st.button("Verify Citation", type="primary", use_container_width=True)
365
 
366
  if verify_button:
367
+ if not (citation_html and hash_html):
368
+ st.error("Please paste both the citation and hash links before verifying.")
369
  else:
370
  # Parse citation
371
+ author, year, url, fragment_text, citation_hash = parse_citation(citation_html)
372
  # Parse hash and metadata
373
+ scc_hash, username, task_name, date, time = parse_metadata_hash(hash_html)
374
 
375
  if not all([author, year, url, fragment_text, scc_hash, username, task_name, date, time]):
376
+ st.error("Invalid citation or hash format. Please ensure both inputs are correctly pasted links from the generated output.")
377
  else:
378
  # Recompute hash
379
  recomputed_hash = generate_citation_hash(