Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hacks to demo possible CiTO support #33

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion data/defaults/pdf.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ filters:
- type: lua
path: draft.lua
- type: lua
path: self-citation.lua
path: insert-cito-in-ref.lua
variables:
# styling options
colorlinks: true
Expand Down
246 changes: 246 additions & 0 deletions data/filters/extract-cito.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
-- Copyright © 2017–2021 Albert Krewinkel, Robert Winkler
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the MIT license. See LICENSE for details.

local _version = '1.0.0'
local properties_and_aliases = {
agreesWith = {
'agreeWith',
'agree_with',
'agrees_with',
},
citation = {
},
cites = {
},
citesAsAuthority = {
'asAuthority',
'cites_as_authority',
'as_authority',
'authority'
},
citesAsDataSource = {
"asDataSource",
"dataSource",
'cites_as_data_source',
"as_data_source",
"data_source"
},
citesAsEvidence = {
'asEvidence',
'cites_as_evidence',
'as_evidence',
'evidence'
},
citesAsMetadataDocument = {
'asMetadataDocument',
'metadataDocument',
'cites_as_metadata_document',
'as_metadata_document',
'metadata_document',
'metadata'
},
citesAsPotentialSolution = {
'cites_as_potential_solution',
'potentialSolution',
'potential_solution',
'solution'
},
citesAsRecommendedReading = {
'asRecommendedReading',
'recommendedReading',
'cites_as_recommended_reading',
'as_recommended_reading',
'recommended_reading'
},
citesAsRelated = {
'cites_as_related',
'related',
},
citesAsSourceDocument = {
'cites_as_source_document',
'sourceDocument',
'source_document'
},
citesForInformation = {
'cites_for_information',
'information',
},
compiles = {
},
confirms = {
},
containsAssertionFrom = {
},
corrects = {
},
credits = {
},
critiques = {
},
derides = {
},
describes = {
},
disagreesWith = {
'disagrees_with',
'disagree',
'disagrees'
},
discusses = {
},
disputes = {
},
documents = {
},
extends = {
},
includesExcerptFrom = {
'excerptFrom',
'excerpt',
'excerpt_from',
'includes_excerpt_from',
},
includesQuotationFrom = {
'quotationFrom',
'includes_quotation_from',
'quotation',
'quotation_from'
},
linksTo = {
'links_to',
'link'
},
obtainsBackgroundFrom = {
'backgroundFrom',
'obtains_background_from',
'background',
'background_from'
},
providesDataFor = {
},
obtainsSupportFrom = {
},
qualifies = {
},
parodies = {
},
refutes = {
},
repliesTo = {
'replies_to',
},
retracts = {
},
reviews = {
},
ridicules = {
},
speculatesOn = {
},
supports = {
},
updates = {
},
usesConclusionsFrom = {
'uses_conclusions_from'
},
usesDataFrom = {
'dataFrom',
'uses_data_from',
'data',
'data_from'
},
usesMethodIn = {
'methodIn',
'uses_method_in',
'method',
'method_in'
},
}

local default_cito_property = 'citation'

--- Map from cito aliases to the actual cito property.
local properties_by_alias = {}
for property, aliases in pairs(properties_and_aliases) do
-- every property is an alias for itself
properties_by_alias[property] = property
for _, alias in pairs(aliases) do
properties_by_alias[alias] = property
end
end

--- Split citation ID into cito property and the actual citation ID. If
--- the ID does not seem to contain a CiTO property, the
--- `default_cito_property` will be returned, together with the
--- unchanged input ID.
local function split_cito_from_id (citation_id)
local split_citation_id = {}
local cito_props = {}
local id_started = false

for part in citation_id:gmatch('[^:]+') do
if not id_started and properties_by_alias[part] then
table.insert(cito_props, properties_by_alias[part])
else
id_started = true
end

if id_started then
table.insert(split_citation_id, 1, part)
end
end

if next(split_citation_id) == nill then
table.insert(split_citation_id, table.remove(cito_props))
end

return cito_props, table.concat(split_citation_id, ':')
end

--- CiTO properties by citation.
local function store_cito (cito_cites, prop, cite_id)
if not prop then
return
end
if not cito_cites[cite_id] then
cito_cites[cite_id] = {}
end
table.insert(cito_cites[cite_id], prop)
end


--- Returns a Cite filter function which extracts CiTO information and
--- add it to the given collection table.
local function extract_cito (cito_cites)
return function (cite)
for k, citation in pairs(cite.citations) do
local cito_props, cite_id = split_cito_from_id(citation.id)
for l, cito_prop in pairs(cito_props) do
store_cito(cito_cites, cito_prop, cite_id)
end
citation.id = cite_id
end
return cite
end
end

--- Lists of citation IDs, indexed by CiTO properties.
local properties_by_citation = {}

return {
{
Cite = extract_cito(properties_by_citation)
},
{
Meta = function (meta)
meta.citation_properties = properties_by_citation
meta.bibliography = meta.bibliography or
meta.cito_bibliography or
meta['cito-bibliography']
return meta
end
}
}
55 changes: 55 additions & 0 deletions data/filters/insert-cito-in-ref.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
-- Copyright © 2021 Albert Krewinkel
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the MIT license. See LICENSE for details.

local List = require 'pandoc.List'
local utils = require 'pandoc.utils'
local citation_properties

local function meta_citation_properties (meta)
citation_properties = meta.citation_properties
end

local function cito_properties(cite_id)
local props = citation_properties[cite_id]
if not props then return {} end

-- remove duplicates
local deduplicated_props = List()
local seen = {}
for _, prop in ipairs(props) do
if not seen[prop] then
deduplicated_props:insert(prop)
seen[prop] = true
end
end

return List(deduplicated_props):map(
function (x)
return pandoc.Strong{
pandoc.Space(),
pandoc.Str '[cito:',
pandoc.Str(utils.stringify(x)),
pandoc.Str ']'
}
end
)
end

local function add_cito (div)
local cite_id = div.identifier:match 'ref%-(.*)'
if cite_id and div.classes:includes 'csl-entry' then
for k, v in ipairs( div.content ) do
if k == 1 then
v.content:extend(cito_properties(cite_id))
return div
end
end
end
end

return {
{Meta = meta_citation_properties},
{Div = add_cito},
}
2 changes: 1 addition & 1 deletion example/paper.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ tags:
# Summary

This article describes the features of the Journal of Open Source
Software [@smith2018] publishing pipeline. The publishing method
Software [@usesMethodIn:smith2018] publishing pipeline. The publishing method
is similar to the model described by @krewinkel2017, in that
Markdown is used as the input format. The author-provided files
serves as the source for all generated publishing artifacts.
Expand Down
1 change: 1 addition & 0 deletions scripts/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ for format in $(printf "%s" "$outformats" | sed -e 's/,/ /g'); do
# Note that the output file must be defined in the format's defaults file.
/usr/local/bin/pandoc \
--data-dir="$OPENJOURNALS_PATH"/data \
--lua-filter="$OPENJOURNALS_PATH"/data/filters/extract-cito.lua \
--defaults=shared \
--defaults="${format}" \
--defaults="$OPENJOURNALS_PATH"/"${JOURNAL}"/defaults.yaml \
Expand Down