Skip to content

Commit

Permalink
fix citation shortcode
Browse files Browse the repository at this point in the history
  • Loading branch information
tavareshugo committed Aug 27, 2024
1 parent 817ce39 commit 975fe0d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 51 deletions.
2 changes: 1 addition & 1 deletion CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ message: >-
You may cite these materials using the metadata in this
file. Please cite our materials if you publish materials
derived from these, run a workshop using them, or use
the information in your own work.
the information in your own work.
type: dataset
authors:
- given-names: Hugo
Expand Down
2 changes: 1 addition & 1 deletion _extensions/courseformat/_extension.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
title: Course Page Format
author: Cambridge Informatics Training
version: 1.0.8
version: 1.1.0
contributes:
formats:
html:
Expand Down
144 changes: 95 additions & 49 deletions _extensions/courseformat/citation_cff.lua
Original file line number Diff line number Diff line change
@@ -1,57 +1,103 @@
return {
["citation"] = function(args, kwargs)
-- Read the CFF file into a table
local file = io.open("CITATION.cff", "r")
local lines = {}
for line in file:lines() do
table.insert(lines, line)
end
file:close()
-- Function to parse the CFF file
function parse_cff(file_path)
-- Initialize tables to store extracted information
local data = {
title = "",
year = "",
month = "",
url = "",
authors = {}
}

-- Initialize variables to store citation details
local title, year, month, url = "", "", "", "<the URL specified in citation CFF>"
local authors = {}
-- Function to trim whitespace from the beginning and end of a string
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end

-- Process each line of the CFF file
local i = 1
while i <= #lines do
local line = lines[i]
-- Extract title
if line:match("^title:") then
title = line:match("^title:%s*(.+)$")
-- Extract date (year and month)
elseif line:match("^date%-released:") then
local date = line:match("^date%-released:%s*'(%d+%-%d+%-%d+)'")
if date then
year = date:sub(1, 4)
month = tonumber(date:sub(6, 7))
end
-- Extract authors
elseif line:match("family%-names:") then
local family_name = line:match("family%-names:%s*(.+)$")
local given_name = lines[i - 1]:match("given%-names:%s*(.+)$")
local affiliation = lines[i + 1] and lines[i + 1]:match("affiliation:%s*(.+)$") or ""
local orcid = lines[i + 2] and lines[i + 2]:match("orcid:%s*'(.+)'") or ""
local alias = lines[i + 3] and lines[i + 3]:match("alias:%s*'(.+)'") or ""
-- Read the YAML content from the file
local file = io.open(file_path, "r")
local yaml_content = file:read("*all")
file:close()

table.insert(authors, {
family = family_name,
given = given_name,
affiliation = affiliation,
orcid = orcid,
roles = alias
})
-- Extract URL if available
elseif line:match("^url:") then
url = line:match("^url:%s*'(.+)'$")
-- Parse the YAML content line by line
local current_author = nil
local in_authors_section = false
for line in yaml_content:gmatch("[^\r\n]+") do
line = trim(line)

-- Detect the start of the authors section
if line:match("^authors:") then
in_authors_section = true
end

-- Parse the title
if line:match("^title:") then
data.title = trim(line:gsub("title:", ""))
end

-- Parse the URL
if line:match("^url:") then
data.url = trim(line:gsub("url:", ""):gsub("'", ""))
end

-- Parse the release date and extract year and month
if line:match("^date%-released:") then
local date = trim(line:gsub("date%-released:", ""):gsub("'", ""))
data.year, data.month = date:match("(%d%d%d%d)%-(%d%d)")
end

-- Start processing authors once in the authors section
if in_authors_section then
-- Detect a new author block by a line starting with "-"
if line:match("^%- ") then
-- Add the previous author to the authors list if it's not nil
if current_author then
table.insert(data.authors, current_author)
end
i = i + 1
-- Start a new author
current_author = {}
end

-- Populate author fields
if line:match("given%-names:") then
current_author["given-names"] = trim(line:gsub("%-?%s*given%-names:", ""))
elseif line:match("family%-names:") then
current_author["family-names"] = trim(line:gsub("%-?%s*family%-names:", ""))
elseif line:match("affiliation:") then
current_author["affiliation"] = trim(line:gsub("%-?%s*affiliation:", ""))
elseif line:match("orcid:") then
current_author["orcid"] = trim(line:gsub("%-?%s*orcid:", ""):gsub("'", ""))
elseif line:match("alias:") then
current_author["alias"] = trim(line:gsub("%-?%s*alias:", ""):gsub("'", ""))
end
end
end

-- Add the last author if it exists
if current_author then
table.insert(data.authors, current_author)
end

-- Return the extracted data
return data
end

return {
["citation"] = function(args, kwargs)
-- Read the CFF file into a table
local cff_data = parse_cff("CITATION.cff")

-- Extract relevant data from the parsed CFF
local title = cff_data.title
local year = cff_data.year
local month = cff_data.month
local url = cff_data.url
local authors = cff_data.authors

-- Generate APA-style author list
local apa_authors = {}
for _, author in ipairs(authors) do
local name = author.family .. ", " .. string.sub(author.given, 1, 1) .. "."
local name = author["family-names"] .. ", " .. string.sub(author["given-names"], 1, 1) .. "."
table.insert(apa_authors, name)
end
local apa_author_str = table.concat(apa_authors, ", ")
Expand All @@ -62,7 +108,7 @@ return {
-- Generate BibTeX citation
local bibtex_authors = {}
for _, author in ipairs(authors) do
local name = author.family .. ", " .. author.given
local name = author["family-names"] .. ", " .. author["given-names"]
table.insert(bibtex_authors, name)
end
local bibtex_author_str = table.concat(bibtex_authors, " and ")
Expand All @@ -74,15 +120,15 @@ return {
title = {%s},
url = {%s},
year = {%s}
}]], bibtex_author_str, month, title, url, year)
}]], bibtex_author_str, tonumber(month), title, url, year)

-- Generate the author information HTML
local author_info = "<ul>"
for _, author in ipairs(authors) do
local name = author.given .. " " .. author.family
local name = author["given-names"] .. " " .. author["family-names"]
local orcid_icon = author.orcid and '<a href="' .. author.orcid .. '" target="_blank"><i class="fa-brands fa-orcid" style="color:#a6ce39"></i></a>' or ""
local affiliation = author.affiliation and '<em>Affiliation</em>: ' .. author.affiliation .. '<br>' or ""
local roles = author.roles and '<em>Roles</em>: ' .. author.roles or ""
local roles = author.alias and '<em>Roles</em>: ' .. author.alias or ""

author_info = author_info .. string.format([[
<li><strong>%s</strong> %s<br>
Expand Down

0 comments on commit 975fe0d

Please sign in to comment.