Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 37 additions & 24 deletions content/glossary/_create_glossaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,45 @@ def process_references(references_text, apa_lookup, missing_refs_log=None):
if not references_text:
return []

citation_pattern = r'\\?\[@([^\\]+)\\?\]'
matches = re.findall(citation_pattern, references_text)

formatted_refs = []
for match in matches:
# Clean the key: remove markdown formatting, trailing punctuation, etc.
key = match.strip()
original_key = key # Keep for logging

# Remove markdown formatting
key = re.sub(r'^\*+|\*+$', '', key) # Remove leading/trailing asterisks
key = re.sub(r'^_+|_+$', '', key) # Remove leading/trailing underscores

# Remove trailing punctuation
key = re.sub(r'[,;:]+$', '', key)

# Remove trailing digits that look like typos (e.g., "Pownall20210" -> "Pownall2021")
key = re.sub(r'(\d{4})0+$', r'\1', key)

if key in apa_lookup:
formatted_refs.append(apa_lookup[key])
else:
# Log missing reference but don't include in output

# Match [@key] format (Pandoc citations)
# Pattern handles: \[@key\], [@key], \[@key], [@key\]
# The Google Sheets may have escaped brackets (\[@key\]) from Markdown,
# so we match both escaped and unescaped versions
citation_pattern = r'\\?\[@([^\]\\]+)\\?\]'
matches = re.findall(citation_pattern, references_text)

if matches:
# Process [@key] format citations
for match in matches:
# Clean the key: remove markdown formatting, trailing punctuation, etc.
key = match.strip()
original_key = key # Keep for logging

# Remove markdown formatting
key = re.sub(r'^\*+|\*+$', '', key) # Remove leading/trailing asterisks
key = re.sub(r'^_+|_+$', '', key) # Remove leading/trailing underscores

# Remove trailing punctuation
key = re.sub(r'[,;:]+$', '', key)

# Remove trailing digits that look like typos (e.g., "Pownall20210" -> "Pownall2021")
key = re.sub(r'(\d{4})0+$', r'\1', key)

if key in apa_lookup:
formatted_refs.append(apa_lookup[key])
else:
# Log missing reference but don't include in output
if missing_refs_log is not None:
missing_refs_log.add(original_key)
print(f"Warning: Missing reference key '{original_key}' (cleaned: '{key}') - skipping")
else:
# No citation keys found - log a warning
if references_text.strip():
print(f"Warning: No citation keys found in: '{references_text[:50]}...'")
if missing_refs_log is not None:
missing_refs_log.add(original_key)
print(f"Warning: Missing reference key '{original_key}' (cleaned: '{key}') - skipping")
missing_refs_log.add(f"[NO KEYS]: {references_text[:50]}")

return list(dict.fromkeys(formatted_refs))

Expand Down
18 changes: 15 additions & 3 deletions layouts/glossary/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,21 @@
{{ end }}

{{ if .Params.references }}
{{ $references := delimit .Params.references ", " | len }}
{{ if ne $references 0 }}
<p class="mb-1 pr-2"><b>{{ if gt (len .Params.references) 1 }}{{ index (index $dictionary $lang) "references" }}{{ else }}{{ index (index $dictionary $lang) "reference" }}{{ end }}: </b>{{ delimit .Params.references ", " ", & " | markdownify }}</p>
{{ $refs := .Params.references }}
{{ $refsIsString := eq (printf "%T" $refs) "string" }}
{{ if $refsIsString }}
{{ if ne $refs "" }}
<p class="mb-1 pr-2"><b>{{ index (index $dictionary $lang) "references" }}: </b>{{ $refs | markdownify }}</p>
{{ end }}
{{ else }}
{{ if gt (len $refs) 0 }}
{{ $firstRef := index $refs 0 }}
{{ if $firstRef }}
{{ $refLabel := index (index $dictionary $lang) "reference" }}
{{ if gt (len $refs) 1 }}{{ $refLabel = index (index $dictionary $lang) "references" }}{{ end }}
<p class="mb-1 pr-2"><b>{{ $refLabel }}: </b>{{ range $index, $ref := $refs }}{{ if $index }}, {{ end }}{{ $ref }}{{ end }}</p>
{{ end }}
{{ end }}
{{ end }}
{{ end }}

Expand Down