Skip to content

Commit

Permalink
Use a single custom_replace method
Browse files Browse the repository at this point in the history
This handles
- [ZoteroID]
- (ID:pk)
- <<link text [ZoteroID]>>
  • Loading branch information
gythaogg committed Apr 23, 2024
1 parent d30cbe7 commit dfa5cdc
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions apis_ontology/templatetags/parse_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,57 @@
logger = logging.getLogger(__name__)

from django import template
from django.contrib.contenttypes.models import ContentType

from apis_ontology.models import ZoteroEntry
from apis_core.apis_metainfo.models import RootObject

register = template.Library()


@register.filter
def parse_comment(value):
if not value:
return ""
print(f"Parsing {value}")

def replace_zotero_link(match):
def custom_replace(match):
if match.group("text"):
# Case: <<text [ID]>>
# Handle <<text [ZoteroID]>>
text = match.group("text")
zotero_id = match.group("zotero_id")
return f'<a target="_BLANK" href="https://www.zotero.org/groups/4394244/tibschol/items/{zotero_id}/item-details#">{text}</a>'
replacement = f'<a target="_BLANK" href="https://www.zotero.org/groups/4394244/tibschol/items/{zotero_id}/item-details#">{text}</a>'
elif match.group("zotero_id_only"):
# Case: [ID]
# Handle [ZoteroID]
zotero_id = match.group("zotero_id_only")
try:
zotero_obj = ZoteroEntry.objects.filter(zoteroId=zotero_id)[0]
return f'<a target="_BLANK" href="https://www.zotero.org/groups/4394244/tibschol/items/{zotero_id}/item-details#">{zotero_obj.shortTitle}</a>'
link_text = (
zotero_obj.shortTitle if zotero_obj.shortTitle else zotero_id
)
return f'<a target="_BLANK" href="https://www.zotero.org/groups/4394244/tibschol/items/{zotero_id}/item-details#">{link_text}</a>'
except Exception as e:
logger.error(e)
logger.error(f"Error finding cached Zotero entry with ID %s", zotero_id)
logger.error(repr(e))
return f'<a target="_BLANK" href="https://www.zotero.org/groups/4394244/tibschol/items/{zotero_id}/item-details#">{zotero_id}</a>'
elif match.group("entity_id"):
# Handle (ID: number)
entity_id = match.group("entity_id")
try:
root_obj = RootObject.objects_inheritance.get_subclass(pk=entity_id)
ct = ContentType.objects.get_for_model(root_obj)
return f'<a target="_BLANK" href="/apis/apis_ontology.{ct.name}/{root_obj.pk}">{root_obj}</a>'

except Exception as e:
logger.error("Error finding entity #%s", entity_id)
logger.error(repr(e))
return f'<a target="_BLANK" href="/entity/{entity_id}">{entity_id}</a>'
else:
# If no specific group is matched, return the original match
replacement = match.group(0)

def replace_entity_link(match):
entity_id = match.group("entity_id")
return f'<a target="_BLANK" href="/entity/{entity_id}">{entity_id}</a>'
return replacement

# Combined regex pattern to handle all cases
# Define the regex pattern to capture different groups
combined_pattern = r"<<(?P<text>.*?) \[(?P<zotero_id>[A-Z0-9]+)\]>>|\[(?P<zotero_id_only>[A-Z0-9]+)\]|\(ID:\s*(?P<entity_id>\d+)\)"

# Apply substitutions using the combined pattern
transformed_value = re.sub(
combined_pattern,
lambda match: replace_zotero_link(match) or replace_entity_link(match),
value,
)
# Apply substitutions using the combined pattern and custom replacement function
transformed_value = re.sub(combined_pattern, custom_replace, value)

return transformed_value

0 comments on commit dfa5cdc

Please sign in to comment.