mabuseif commited on
Commit
a4cf2a8
·
verified ·
1 Parent(s): dfd2cfc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -39
app.py CHANGED
@@ -4,7 +4,6 @@ import hashlib
4
  import urllib.parse
5
  from datetime import datetime
6
  import pytz
7
- import pandas as pd
8
  import re
9
 
10
  # --- Constants ---
@@ -84,12 +83,6 @@ def load_css():
84
  border-left: 1px solid #e0e0e0;
85
  }
86
 
87
- .verification-table {
88
- margin: 1rem 0;
89
- border-radius: 4px;
90
- overflow: hidden;
91
- }
92
-
93
  .rendered-citation {
94
  margin: 1rem 0;
95
  font-size: 1rem;
@@ -117,7 +110,6 @@ def check_for_fragment(url):
117
  return '#:~:text=' in 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, scc_hash, author, year = match.groups()
@@ -126,7 +118,6 @@ def parse_citation(citation_html):
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
130
  match = re.match(r'<a href="([^"]+)#:~:text=([^"]+)" data-hash="([^"]+)">[^<]+\(\d{4}\)\. ([^<]+)</a>', metadata_html)
131
  if match:
132
  url, encoded_metadata, scc_hash, same_hash = match.groups()
@@ -140,7 +131,7 @@ def parse_metadata_hash(metadata_html):
140
  def verification_js():
141
  return """
142
  <script>
143
- function updateOutputs() {
144
  const citationInput = document.getElementById('citation_input');
145
  const hashInput = document.getElementById('hash_input');
146
  const citationOutput = document.getElementById('citation_output');
@@ -155,23 +146,12 @@ def verification_js():
155
  }
156
  }
157
 
158
- document.addEventListener('DOMContentLoaded', function() {
159
- const citationInput = document.getElementById('citation_input');
160
- const hashInput = document.getElementById('hash_input');
161
-
162
- if (citationInput) {
163
- citationInput.addEventListener('paste', function() {
164
- setTimeout(updateOutputs, 100); // Delay to ensure paste is complete
165
- });
166
- citationInput.addEventListener('input', updateOutputs);
167
- }
168
-
169
- if (hashInput) {
170
- hashInput.addEventListener('paste', function() {
171
- setTimeout(updateOutputs, 100); // Delay to ensure paste is complete
172
- });
173
- hashInput.addEventListener('input', updateOutputs);
174
- }
175
  });
176
  </script>
177
  """
@@ -352,12 +332,6 @@ with tabs[1]:
352
  </div>
353
  """, unsafe_allow_html=True)
354
 
355
- # Initialize session state for storing extracted data
356
- if 'citation_html' not in st.session_state:
357
- st.session_state.citation_html = ''
358
- if 'hash_html' not in st.session_state:
359
- st.session_state.hash_html = ''
360
-
361
  # Input fields for citation and hash
362
  st.markdown("""
363
  <div>
@@ -373,20 +347,49 @@ with tabs[1]:
373
  # Add JavaScript to capture pasted link data
374
  components.html(verification_js(), height=0)
375
 
376
- # Use session state to capture JavaScript output
377
- citation_html = st.session_state.get('citation_output', '')
378
- hash_html = st.session_state.get('hash_output', '')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
379
 
380
  verify_button = st.button("Verify Citation", type="primary", use_container_width=True)
381
 
382
  if verify_button:
383
- if not (citation_html and hash_html):
384
  st.error("Please paste both the citation and hash links before verifying.")
385
  else:
386
  # Parse citation
387
- author, year, url, fragment_text, citation_hash = parse_citation(citation_html)
388
  # Parse hash and metadata
389
- scc_hash, username, task_name, date, time = parse_metadata_hash(hash_html)
390
 
391
  if not all([author, year, url, fragment_text, scc_hash, username, task_name, date, time]):
392
  st.error("Invalid citation or hash format. Please ensure both inputs are correctly pasted links from the generated output.")
 
4
  import urllib.parse
5
  from datetime import datetime
6
  import pytz
 
7
  import re
8
 
9
  # --- Constants ---
 
83
  border-left: 1px solid #e0e0e0;
84
  }
85
 
 
 
 
 
 
 
86
  .rendered-citation {
87
  margin: 1rem 0;
88
  font-size: 1rem;
 
110
  return '#:~:text=' in url
111
 
112
  def parse_citation(citation_html):
 
113
  match = re.match(r'<a href="([^"]+)#:~:text=([^"]+)" data-hash="([^"]+)">([^<]+) \((\d{4})\)</a>', citation_html)
114
  if match:
115
  url, encoded_fragment, scc_hash, author, year = match.groups()
 
118
  return None, None, None, None, None
119
 
120
  def parse_metadata_hash(metadata_html):
 
121
  match = re.match(r'<a href="([^"]+)#:~:text=([^"]+)" data-hash="([^"]+)">[^<]+\(\d{4}\)\. ([^<]+)</a>', metadata_html)
122
  if match:
123
  url, encoded_metadata, scc_hash, same_hash = match.groups()
 
131
  def verification_js():
132
  return """
133
  <script>
134
+ function updateCitationData() {
135
  const citationInput = document.getElementById('citation_input');
136
  const hashInput = document.getElementById('hash_input');
137
  const citationOutput = document.getElementById('citation_output');
 
146
  }
147
  }
148
 
149
+ document.getElementById('citation_input').addEventListener('paste', function(e) {
150
+ setTimeout(updateCitationData, 0);
151
+ });
152
+
153
+ document.getElementById('hash_input').addEventListener('paste', function(e) {
154
+ setTimeout(updateCitationData, 0);
 
 
 
 
 
 
 
 
 
 
 
155
  });
156
  </script>
157
  """
 
332
  </div>
333
  """, unsafe_allow_html=True)
334
 
 
 
 
 
 
 
335
  # Input fields for citation and hash
336
  st.markdown("""
337
  <div>
 
347
  # Add JavaScript to capture pasted link data
348
  components.html(verification_js(), height=0)
349
 
350
+ # Use session state to store the extracted data
351
+ if 'citation_html' not in st.session_state:
352
+ st.session_state.citation_html = ''
353
+ if 'hash_html' not in st.session_state:
354
+ st.session_state.hash_html = ''
355
+
356
+ # JavaScript to update session state via URL parameters
357
+ st.markdown("""
358
+ <script>
359
+ function updateSessionState() {
360
+ const citationOutput = document.getElementById('citation_output').value;
361
+ const hashOutput = document.getElementById('hash_output').value;
362
+ const params = new URLSearchParams(window.location.search);
363
+ params.set('citation_html', encodeURIComponent(citationOutput));
364
+ params.set('hash_html', encodeURIComponent(hashOutput));
365
+ window.history.replaceState({}, '', `${window.location.pathname}?${params}`);
366
+ }
367
+ document.getElementById('citation_input').addEventListener('paste', function() {
368
+ setTimeout(updateCitationData, 0);
369
+ setTimeout(updateSessionState, 100);
370
+ });
371
+ document.getElementById('hash_input').addEventListener('paste', function() {
372
+ setTimeout(updateCitationData, 0);
373
+ setTimeout(updateSessionState, 100);
374
+ });
375
+ </script>
376
+ """, unsafe_allow_html=True)
377
+
378
+ # Retrieve data from URL parameters
379
+ query_params = st.experimental_get_query_params()
380
+ st.session_state.citation_html = query_params.get('citation_html', [''])[0]
381
+ st.session_state.hash_html = query_params.get('hash_html', [''])[0]
382
 
383
  verify_button = st.button("Verify Citation", type="primary", use_container_width=True)
384
 
385
  if verify_button:
386
+ if not (st.session_state.citation_html and st.session_state.hash_html):
387
  st.error("Please paste both the citation and hash links before verifying.")
388
  else:
389
  # Parse citation
390
+ author, year, url, fragment_text, citation_hash = parse_citation(st.session_state.citation_html)
391
  # Parse hash and metadata
392
+ scc_hash, username, task_name, date, time = parse_metadata_hash(st.session_state.hash_html)
393
 
394
  if not all([author, year, url, fragment_text, scc_hash, username, task_name, date, time]):
395
  st.error("Invalid citation or hash format. Please ensure both inputs are correctly pasted links from the generated output.")