mabuseif commited on
Commit
1003f90
·
verified ·
1 Parent(s): fcd7a3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -78
app.py CHANGED
@@ -5,7 +5,6 @@ import urllib.parse
5
  from datetime import datetime
6
  import pytz
7
  import pandas as pd
8
- import json
9
 
10
  # --- Constants ---
11
  MELBOURNE_TIMEZONE = 'Australia/Melbourne'
@@ -29,21 +28,6 @@ def load_css():
29
  font-family: 'Courier New', monospace;
30
  }
31
 
32
- .copy-button {
33
- background: #e0e0e0;
34
- color: black;
35
- border: none;
36
- padding: 0.5rem 1rem;
37
- border-radius: 4px;
38
- cursor: pointer;
39
- font-size: 0.9rem;
40
- margin-top: 0.5rem;
41
- }
42
-
43
- .copy-button:hover {
44
- background: #d0d0d0;
45
- }
46
-
47
  .warning-box {
48
  background: #f8f8f8;
49
  border: 1px solid #e0e0e0;
@@ -122,45 +106,15 @@ def format_citation_html(url, fragment_text, author, year, scc_hash):
122
  full_url = f"{url}#:~:text={encoded_fragment}"
123
  return f'<a href="{full_url}" data-hash="{scc_hash}">{author} ({year})</a>'
124
 
 
 
 
 
 
 
125
  def check_for_fragment(url):
126
  return '#:~:text=' in url
127
 
128
- def copy_to_clipboard_js(text, button_id):
129
- """Generate JavaScript for copying text to clipboard with proper escaping."""
130
- # Escape special characters for JavaScript string
131
- escaped_text = json.dumps(text).strip('"').replace('</', '<\\/') # Properly escape for JS
132
- return f"""
133
- <script>
134
- function copyToClipboard_{button_id}() {{
135
- const textToCopy = `{escaped_text}`;
136
- if (navigator.clipboard) {{
137
- navigator.clipboard.writeText(textToCopy).then(function() {{
138
- const statusElement = document.getElementById('copy_status_{button_id}');
139
- if (statusElement) {{
140
- statusElement.innerHTML = 'Copied!';
141
- setTimeout(function() {{
142
- statusElement.innerHTML = '';
143
- }}, 2000);
144
- }}
145
- }}, function(err) {{
146
- const statusElement = document.getElementById('copy_status_{button_id}');
147
- if (statusElement) {{
148
- statusElement.innerHTML = 'Copy failed';
149
- }}
150
- console.error('Could not copy text: ', err);
151
- }});
152
- }} else {{
153
- const statusElement = document.getElementById('copy_status_{button_id}');
154
- if (statusElement) {{
155
- statusElement.innerHTML = 'Clipboard API not supported';
156
- }}
157
- }}
158
- }}
159
- </script>
160
- <button onclick="copyToClipboard_{button_id}()" class="copy-button">Copy to Clipboard</button>
161
- <span id="copy_status_{button_id}" style="margin-left: 10px; font-weight: bold;"></span>
162
- """
163
-
164
  # --- Live Clock JavaScript ---
165
  def live_clock():
166
  return """
@@ -299,8 +253,9 @@ with tabs[0]:
299
  """, unsafe_allow_html=True)
300
  else:
301
  scc_hash = generate_citation_hash(author_name, publication_year, source_url, annotated_text, annotated_text, username, task_name, current_date, current_time)
302
- citation_link_start = f'<a href="{source_url}#:~:text={urllib.parse.quote(annotated_text)}" data-hash="{scc_hash}">{author_name} ({publication_year})</a>'
303
  citation_link_end = f'<a href="{source_url}#:~:text={urllib.parse.quote(annotated_text)}" data-hash="{scc_hash}">({author_name}, {publication_year})</a>'
 
304
 
305
  st.markdown("## Generated Citations")
306
  col_html1, col_html2 = st.columns(2)
@@ -308,40 +263,21 @@ with tabs[0]:
308
  # HTML Citation - Start of Text
309
  with col_html1:
310
  st.markdown("### Citation (Start of Text)")
311
- html_citation_start = citation_link_start
312
-
313
  st.markdown('<div class="rendered-citation">', unsafe_allow_html=True)
314
- st.markdown(html_citation_start, unsafe_allow_html=True)
315
- st.markdown(copy_to_clipboard_js(html_citation_start.replace('`', '\\`'), "html_start"), unsafe_allow_html=True)
316
  st.markdown('</div>', unsafe_allow_html=True)
317
 
318
  # HTML Citation - End of Text
319
  with col_html2:
320
  st.markdown("### Citation (End of Text)")
321
- html_citation_end = citation_link_end
322
-
323
  st.markdown('<div class="rendered-citation">', unsafe_allow_html=True)
324
- st.markdown(html_citation_end, unsafe_allow_html=True)
325
- st.markdown(copy_to_clipboard_js(html_citation_end.replace('`', '\\`'), "html_end"), unsafe_allow_html=True)
326
  st.markdown('</div>', unsafe_allow_html=True)
327
 
328
- # Citation Hash Details
329
- st.markdown("### Citation Hash Details (for Verification)")
330
- hash_details = {
331
- "author": author_name,
332
- "year": publication_year,
333
- "url": source_url,
334
- "fragment_text": annotated_text,
335
- "cited_text": annotated_text,
336
- "username": username,
337
- "task_name": task_name,
338
- "date": current_date,
339
- "time": current_time,
340
- "hash": scc_hash
341
- }
342
-
343
  st.markdown('<div class="hash-display">', unsafe_allow_html=True)
344
- st.json(hash_details)
345
  st.markdown('</div>', unsafe_allow_html=True)
346
 
347
  # Guidance on Verifying Citations
@@ -423,7 +359,7 @@ with tabs[1]:
423
  "Year": verify_publication_year,
424
  "URL": verify_source_url,
425
  "Fragment text": verify_annotated_text,
426
- "Outlined text": verify_annotated_text,
427
  "Username": verify_username,
428
  "Task name": verify_task_name,
429
  "Date": verify_date,
 
5
  from datetime import datetime
6
  import pytz
7
  import pandas as pd
 
8
 
9
  # --- Constants ---
10
  MELBOURNE_TIMEZONE = 'Australia/Melbourne'
 
28
  font-family: 'Courier New', monospace;
29
  }
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  .warning-box {
32
  background: #f8f8f8;
33
  border: 1px solid #e0e0e0;
 
106
  full_url = f"{url}#:~:text={encoded_fragment}"
107
  return f'<a href="{full_url}" data-hash="{scc_hash}">{author} ({year})</a>'
108
 
109
+ def format_metadata_html(url, author, year, scc_hash, username, task_name, current_date, current_time):
110
+ metadata = f"{username}—{task_name}—{current_date}—{current_time}"
111
+ encoded_metadata = urllib.parse.quote(metadata)
112
+ full_url = f"{url}#:~:text={encoded_metadata}"
113
+ return f'<a href="{full_url}" data-hash="{scc_hash}">{author} ({year}). {scc_hash}</a>'
114
+
115
  def check_for_fragment(url):
116
  return '#:~:text=' in url
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  # --- Live Clock JavaScript ---
119
  def live_clock():
120
  return """
 
253
  """, unsafe_allow_html=True)
254
  else:
255
  scc_hash = generate_citation_hash(author_name, publication_year, source_url, annotated_text, annotated_text, username, task_name, current_date, current_time)
256
+ citation_link_start = format_citation_html(source_url, annotated_text, author_name, publication_year, scc_hash)
257
  citation_link_end = f'<a href="{source_url}#:~:text={urllib.parse.quote(annotated_text)}" data-hash="{scc_hash}">({author_name}, {publication_year})</a>'
258
+ metadata_link = format_metadata_html(source_url, author_name, publication_year, scc_hash, username, task_name, current_date, current_time)
259
 
260
  st.markdown("## Generated Citations")
261
  col_html1, col_html2 = st.columns(2)
 
263
  # HTML Citation - Start of Text
264
  with col_html1:
265
  st.markdown("### Citation (Start of Text)")
 
 
266
  st.markdown('<div class="rendered-citation">', unsafe_allow_html=True)
267
+ st.markdown(citation_link_start, unsafe_allow_html=True)
 
268
  st.markdown('</div>', unsafe_allow_html=True)
269
 
270
  # HTML Citation - End of Text
271
  with col_html2:
272
  st.markdown("### Citation (End of Text)")
 
 
273
  st.markdown('<div class="rendered-citation">', unsafe_allow_html=True)
274
+ st.markdown(citation_link_end, unsafe_allow_html=True)
 
275
  st.markdown('</div>', unsafe_allow_html=True)
276
 
277
+ # SCC Ledger
278
+ st.markdown("### SCC Ledger")
 
 
 
 
 
 
 
 
 
 
 
 
 
279
  st.markdown('<div class="hash-display">', unsafe_allow_html=True)
280
+ st.markdown(metadata_link, unsafe_allow_html=True)
281
  st.markdown('</div>', unsafe_allow_html=True)
282
 
283
  # Guidance on Verifying Citations
 
359
  "Year": verify_publication_year,
360
  "URL": verify_source_url,
361
  "Fragment text": verify_annotated_text,
362
+ "Cited text": verify_annotated_text,
363
  "Username": verify_username,
364
  "Task name": verify_task_name,
365
  "Date": verify_date,