summaryrefslogtreecommitdiff
path: root/paper/lua-filters/cito/cito.lua
blob: a1a34214b1e15a4df17ad6daed26e6d9f30fa831 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
-- Copyright © 2017–2019 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 = {
  agrees_with = {
    'agree_with'
  },
  citation = {
  },
  cites = {
  },
  cites_as_authority = {
    'as_authority',
    'authority'
  },
  cites_as_data_source = {
    "as_data_source",
    "data_source"
  },
  cites_as_evidence = {
    'as_evidence',
    'evidence'
  },
  cites_as_metadata_document = {
    'as_metadata_document',
    'metadata_document',
    'metadata'
  },
  cites_as_recommended_reading = {
    'as_recommended_reading',
    'recommended_reading'
  },
  disagrees_with = {
    'disagree',
    'disagrees'
  },
  disputes = {
  },
  documents = {
  },
  extends = {
  },
  includes_excerpt_from = {
    'excerpt',
    'excerpt_from'
  },
  includes_quotation_from = {
    'quotation',
    'quotation_from'
  },
  obtains_background_from = {
    'background',
    'background_from'
  },
  refutes = {
  },
  replies_to = {
  },
  updates = {
  },
  uses_data_from = {
    'data',
    'data_from'
  },
  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 pattern = '^(.+):(.+)$'
  local prop_alias, split_citation_id = citation_id:match(pattern)

  if properties_by_alias[prop_alias] then
    return properties_by_alias[prop_alias], split_citation_id
  end

  return default_cito_property, citation_id
end

--- Citations by CiTO properties.
local function store_cito (cito_cites, prop, cite_id)
  if not prop then
    return
  end
  if not cito_cites[prop] then
    cito_cites[prop] = {}
  end
  table.insert(cito_cites[prop], cite_id)
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_prop, cite_id = split_cito_from_id(citation.id)
      store_cito(cito_cites, cito_prop, cite_id)
      citation.id = cite_id
    end
    return cite
  end
end

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

return {
  {
    Cite = extract_cito(citations_by_property)
  },
  {
    Meta = function (meta)
      meta.cito_cites = citations_by_property
      return meta
    end
  }
}