| 1 |
RANDOM_SEED = math.randomseed(os.time()) |
| 2 |
MATRIX_AND = {{0,0},{0,1}} |
| 3 |
MATRIX_OR = {{0,1},{1,1}} |
| 4 |
HEXES = '0123456789abcdef' |
| 5 |
local SciKGTeX = {} |
| 6 |
SciKGTeX.whole_string = "" |
| 7 |
SciKGTeX.properties_used = {} |
| 8 |
SciKGTeX.property_commands = {} |
| 9 |
SciKGTeX.mandatory_properties = { |
| 10 |
'researchproblem', |
| 11 |
'objective', |
| 12 |
'method', |
| 13 |
'result', |
| 14 |
'conclusion' |
| 15 |
} |
| 16 |
SciKGTeX.PRODUCE_XMP_FILE = true |
| 17 |
SciKGTeX.WARNING_LEVEL = 1 |
| 18 |
|
| 19 |
local XMP = {} |
| 20 |
XMP.lines = {} |
| 21 |
XMP.namespaces = {} |
| 22 |
XMP.property_ns = {} |
| 23 |
XMP.XMP_TOP = [[<x:xmpmeta xmlns:x="adobe:ns:meta/">]] |
| 24 |
XMP.XMP_BOTTOM = [[</x:xmpmeta>]] |
| 25 |
XMP.PACKET_END = [[<?xpacket end="r"?>]] |
| 26 |
|
| 27 |
local UUID = {} |
| 28 |
|
| 29 |
---------------------------- utilities ------------------------------- |
| 30 |
|
| 31 |
-- performs the bitwise operation specified by truth matrix on two numbers. |
| 32 |
function BITWISE(x, y, matrix) |
| 33 |
local z = 0 |
| 34 |
local pow = 1 |
| 35 |
while x > 0 or y > 0 do |
| 36 |
z = z + (matrix[x%2+1][y%2+1] * pow) |
| 37 |
pow = pow * 2 |
| 38 |
x = math.floor(x/2) |
| 39 |
y = math.floor(y/2) |
| 40 |
end |
| 41 |
return z |
| 42 |
end |
| 43 |
|
| 44 |
function INT2HEX(x) |
| 45 |
local s,base,pow = '',16,0 |
| 46 |
local d |
| 47 |
while x > 0 do |
| 48 |
d = x % base + 1 |
| 49 |
x = math.floor(x/base) |
| 50 |
s = string.sub(HEXES, d, d)..s |
| 51 |
end |
| 52 |
if #s == 1 then s = "0" .. s end |
| 53 |
return s |
| 54 |
end |
| 55 |
|
| 56 |
function get_output_dir() |
| 57 |
if arg ~= nil then |
| 58 |
for k,v in ipairs(arg) do |
| 59 |
val, is_output_argument = v:gsub('%-%-output%-directory=(.*)','%1') |
| 60 |
if is_output_argument > 0 then |
| 61 |
return val |
| 62 |
end |
| 63 |
end |
| 64 |
return nil |
| 65 |
end |
| 66 |
return nil |
| 67 |
end |
| 68 |
|
| 69 |
function read_header_of_file(path) |
| 70 |
local fh = io.open(path, "rb") |
| 71 |
if fh then |
| 72 |
local first_line = assert(fh:read()) |
| 73 |
fh:close() |
| 74 |
return first_line |
| 75 |
else |
| 76 |
print ("No xmp metadata file found!") |
| 77 |
return nil |
| 78 |
end |
| 79 |
end |
| 80 |
|
| 81 |
function extract_uuid_from_header(header) |
| 82 |
return header:gsub('.*id=\"(.-)\".*','%1') |
| 83 |
end |
| 84 |
|
| 85 |
function generate_UUID() |
| 86 |
UUID:initialize('00:0c:29:69:41:c6') |
| 87 |
return UUID:toString() |
| 88 |
end |
| 89 |
|
| 90 |
function string:split(sep) |
| 91 |
if sep == nil then |
| 92 |
sep = "%s" |
| 93 |
end |
| 94 |
local t = {} |
| 95 |
for str in self:gmatch("([^"..sep.."]+)") do |
| 96 |
table.insert(t, str) |
| 97 |
end |
| 98 |
return t |
| 99 |
end |
| 100 |
|
| 101 |
function spaces_to_underscores(s) |
| 102 |
return s:gsub('%s+','_') |
| 103 |
end |
| 104 |
|
| 105 |
function remove_environments(s) |
| 106 |
s,c = s:gsub('\\begin%s*{.-}{.-}%s*','') |
| 107 |
s,c = s:gsub('\\begin%s*{.-}%s*','') |
| 108 |
s,c = s:gsub('\\end%s*{.-}%s*','') |
| 109 |
return s |
| 110 |
end |
| 111 |
|
| 112 |
function remove_any_latex_command(s) |
| 113 |
s, c = s:gsub('\\%w+%s*%[%d*%]%s*{(.*)}','%1') |
| 114 |
if c > 0 then |
| 115 |
return remove_latex_commands(s) |
| 116 |
end |
| 117 |
s, c = s:gsub('\\%w+%s*{(.*)}','%1') |
| 118 |
if c > 0 then |
| 119 |
return remove_latex_commands(s) |
| 120 |
end |
| 121 |
s, c = s:gsub('\\%w+%s*','') |
| 122 |
if c > 0 then |
| 123 |
return remove_latex_commands(s) |
| 124 |
end |
| 125 |
return s |
| 126 |
end |
| 127 |
|
| 128 |
function find_last_occurence(s, repls) |
| 129 |
occurences = {} |
| 130 |
for pattern, repl in pairs(repls) do |
| 131 |
i, j = s:find(pattern) |
| 132 |
if i ~= nil then |
| 133 |
table.insert(occurences, {i,j,pattern}) |
| 134 |
end |
| 135 |
end |
| 136 |
table.sort(occurences, function(l, r) return l[1]>r[1] end) |
| 137 |
if #occurences > 0 then |
| 138 |
return occurences[1] |
| 139 |
else |
| 140 |
return nil |
| 141 |
end |
| 142 |
end |
| 143 |
|
| 144 |
function exhaustively_replace_last_occurence_of_pattern(s, repls) |
| 145 |
last_occurence = find_last_occurence(s, repls) |
| 146 |
if last_occurence ~= nil then |
| 147 |
starts, ends, pattern = table.unpack(last_occurence) |
| 148 |
to_replace = s:sub(starts,ends) |
| 149 |
else |
| 150 |
return s |
| 151 |
end |
| 152 |
new_string = s:sub(0,starts-1) .. to_replace:gsub(pattern, repls[pattern], 1) .. s:sub(ends+1) |
| 153 |
return exhaustively_replace_last_occurence_of_pattern(new_string, repls) |
| 154 |
end |
| 155 |
|
| 156 |
function remove_latex_commands(s) |
| 157 |
replacements = { |
| 158 |
-- contribution with * and [] |
| 159 |
['\\contribution%s*%*%s*%[%d*%]%s*{.-}{.*}%s*'] = '', |
| 160 |
-- contribution with * |
| 161 |
['\\contribution%s*%*%s*{.-}%s*{.*}%s*'] = '', |
| 162 |
-- contribution with [] |
| 163 |
['\\contribution%s*%[%d*%]%s*{.-}{(.*)}'] = '%1', |
| 164 |
-- contribution normal |
| 165 |
['\\contribution%s*{.-}%s*{(.*)}'] = '%1', |
| 166 |
|
| 167 |
['\\uri%s*{.-}%s*{(.*)}'] = '%1', |
| 168 |
} |
| 169 |
for cmd, used in pairs(SciKGTeX.property_commands) do |
| 170 |
-- [] |
| 171 |
replacements['\\'.. cmd .. '%s*%[%d*%]%s*{(.*)}'] = '%1' |
| 172 |
-- normal command |
| 173 |
replacements['\\'.. cmd .. '%s*{(.*)}'] = '%1' |
| 174 |
-- with * and [] |
| 175 |
replacements['\\'.. cmd .. '%s*%*%s*%[%d*%]%s*{.*}%s*'] = '' |
| 176 |
-- with * |
| 177 |
replacements['\\'.. cmd .. '%s*%*%s*{.*}%s*'] ='' |
| 178 |
end |
| 179 |
s = remove_environments(s) |
| 180 |
s = exhaustively_replace_last_occurence_of_pattern(s, replacements) |
| 181 |
s = remove_any_latex_command(s) |
| 182 |
-- remove escape chars |
| 183 |
s = s:gsub('\\','') |
| 184 |
return s |
| 185 |
end |
| 186 |
|
| 187 |
function uri_valid(s) |
| 188 |
if s:find('http') ~= 1 then |
| 189 |
return false |
| 190 |
else |
| 191 |
return true |
| 192 |
end |
| 193 |
end |
| 194 |
|
| 195 |
function resolve_entity(s) |
| 196 |
-- make sure the entity is only resolved at the innermost of nested commands. |
| 197 |
for _, cmd in ipairs(SciKGTeX.mandatory_properties) do |
| 198 |
if s:find('\\' .. cmd) then |
| 199 |
return false |
| 200 |
end |
| 201 |
end |
| 202 |
if s:find('\\contribution') then |
| 203 |
return false |
| 204 |
end |
| 205 |
uri, found = s:gsub('.*\\uri%s*{(.-)}%s*{.*}.*', '%1') |
| 206 |
if found == 1 then |
| 207 |
label = s:gsub('.*\\uri%s*{.-}%s*{(.*)}.*', '%1') |
| 208 |
entity = string.format('<rdf:Description rdf:about=\"%s\"><rdfs:label>%s</rdfs:label></rdf:Description>', uri, label) |
| 209 |
return entity |
| 210 |
else |
| 211 |
uri, found = s:gsub('.*\\uri%s*{(.-)}.*', '%1') |
| 212 |
if found == 1 then |
| 213 |
entity = string.format('<rdf:Description rdf:about=\"%s\"></rdf:Description>', uri) |
| 214 |
return entity |
| 215 |
else |
| 216 |
return false |
| 217 |
end |
| 218 |
end |
| 219 |
end |
| 220 |
|
| 221 |
---------------------------- UUID class methods ------------------------------------- |
| 222 |
|
| 223 |
-- hwaddr is a string: hexes delimited by colons. e.g.: 00:0c:29:69:41:c6 |
| 224 |
function UUID:initialize(hwaddr) |
| 225 |
self._bytes = { |
| 226 |
math.random(0, 255), |
| 227 |
math.random(0, 255), |
| 228 |
math.random(0, 255), |
| 229 |
math.random(0, 255), |
| 230 |
math.random(0, 255), |
| 231 |
math.random(0, 255), |
| 232 |
math.random(0, 255), |
| 233 |
math.random(0, 255), |
| 234 |
math.random(0, 255), |
| 235 |
math.random(0, 255), |
| 236 |
-- should come from mac address |
| 237 |
tonumber(hwaddr:sub(1, 2), 16), |
| 238 |
tonumber(hwaddr:sub(4, 5), 16), |
| 239 |
tonumber(hwaddr:sub(7, 8), 16), |
| 240 |
tonumber(hwaddr:sub(10, 11), 16), |
| 241 |
tonumber(hwaddr:sub(13, 14), 16), |
| 242 |
tonumber(hwaddr:sub(16, 17), 16) |
| 243 |
} |
| 244 |
-- set the version |
| 245 |
self._bytes[7] = BITWISE(self._bytes[7], 0x0f, MATRIX_AND) |
| 246 |
self._bytes[7] = BITWISE(self._bytes[7], 0x40, MATRIX_OR) |
| 247 |
-- set the variant |
| 248 |
self._bytes[9] = BITWISE(self._bytes[7], 0x3f, MATRIX_AND) |
| 249 |
self._bytes[9] = BITWISE(self._bytes[7], 0x80, MATRIX_OR) |
| 250 |
self._string = nil |
| 251 |
end |
| 252 |
|
| 253 |
-- lazy string creation. |
| 254 |
function UUID:toString() |
| 255 |
if self._string == nil then |
| 256 |
self._string = INT2HEX(self._bytes[1])..INT2HEX(self._bytes[2])..INT2HEX(self._bytes[3])..INT2HEX(self._bytes[4]).."-".. |
| 257 |
INT2HEX(self._bytes[5])..INT2HEX(self._bytes[6]).."-".. |
| 258 |
INT2HEX(self._bytes[7])..INT2HEX(self._bytes[8]).."-".. |
| 259 |
INT2HEX(self._bytes[9])..INT2HEX(self._bytes[10]).."-".. |
| 260 |
INT2HEX(self._bytes[11])..INT2HEX(self._bytes[12])..INT2HEX(self._bytes[13])..INT2HEX(self._bytes[14])..INT2HEX(self._bytes[15])..INT2HEX(self._bytes[16]) |
| 261 |
end |
| 262 |
return self._string |
| 263 |
end |
| 264 |
|
| 265 |
---------------------------- Main class methods ------------------------------- |
| 266 |
|
| 267 |
function SciKGTeX:set_warning_level(wl) |
| 268 |
self.WARNING_LEVEL = wl |
| 269 |
end |
| 270 |
|
| 271 |
function SciKGTeX:warn(warning_message, ...) |
| 272 |
if self.WARNING_LEVEL > 0 then |
| 273 |
texio.write_nl("term and log", |
| 274 |
[[Package SciKGTeX Warning: ]] .. string.format(warning_message, ...)) |
| 275 |
texio.write_nl("term and log","\n") |
| 276 |
end |
| 277 |
end |
| 278 |
|
| 279 |
function SciKGTeX:error(warning_message, ...) |
| 280 |
tex.error([[Package SciKGTeX Error: ]] .. string.format(warning_message, ...)) |
| 281 |
end |
| 282 |
|
| 283 |
SciKGTeX.command_factory = {} |
| 284 |
|
| 285 |
SciKGTeX.command_factory.cmd_top = [[\newcommand{\%s}[2][]{]] |
| 286 |
|
| 287 |
SciKGTeX.command_factory.cmd_top_star = [[\WithSuffix\newcommand\%s*[2][]{]] |
| 288 |
|
| 289 |
SciKGTeX.command_factory.cmd_top_override = [[\renewcommand{\%s}[2][]{]] |
| 290 |
|
| 291 |
SciKGTeX.command_factory.cmd_top_star_override = [[\WithSuffix\renewcommand\%s*[2][]{]] |
| 292 |
|
| 293 |
SciKGTeX.command_factory.directlua_part = [[ \directlua{ |
| 294 |
local content = "\luaescapestring{\unexpanded{#2}}" |
| 295 |
local belongs_to_contribution = "\luaescapestring{\unexpanded{#1}}" |
| 296 |
SciKGTeX.XMP:add_annotation(belongs_to_contribution, '%s', content, 'annotation-id') |
| 297 |
}]] |
| 298 |
|
| 299 |
SciKGTeX.command_factory.cmd_bottom = [[}]] |
| 300 |
SciKGTeX.command_factory.cmd_bottom_star = [[\ignorespaces}]] |
| 301 |
|
| 302 |
function SciKGTeX.command_factory:build_command(command_name) |
| 303 |
full_cmd = self.cmd_top .. "\n" .. self.directlua_part .. "\n #2\n" .. self.cmd_bottom |
| 304 |
formatted_cmd = string.format(full_cmd, command_name, command_name) |
| 305 |
for i, line in ipairs(formatted_cmd:split("\n")) do |
| 306 |
tex.print(line .. "%") |
| 307 |
end |
| 308 |
end |
| 309 |
|
| 310 |
function SciKGTeX.command_factory:build_star_command(command_name) |
| 311 |
full_cmd = self.cmd_top_star .. "\n" .. self.directlua_part .. "\n" .. self.cmd_bottom_star |
| 312 |
formatted_cmd = string.format(full_cmd, command_name, command_name) |
| 313 |
for i, line in ipairs(formatted_cmd:split("\n")) do |
| 314 |
tex.print(line .. "%") |
| 315 |
end |
| 316 |
end |
| 317 |
|
| 318 |
function SciKGTeX.command_factory:override_command(command_name) |
| 319 |
full_cmd = self.cmd_top_override .. "\n" .. self.directlua_part .. "\n #2\n" .. self.cmd_bottom |
| 320 |
formatted_cmd = string.format(full_cmd, command_name, command_name) |
| 321 |
for i, line in ipairs(formatted_cmd:split("\n")) do |
| 322 |
tex.print(line .. "%") |
| 323 |
end |
| 324 |
end |
| 325 |
|
| 326 |
function SciKGTeX.command_factory:override_star_command(command_name) |
| 327 |
full_cmd = self.cmd_top_star_override .. "\n" .. self.directlua_part .. "\n" .. self.cmd_bottom_star |
| 328 |
formatted_cmd = string.format(full_cmd, command_name, command_name) |
| 329 |
for i, line in ipairs(formatted_cmd:split("\n")) do |
| 330 |
tex.print(line .. "%") |
| 331 |
end |
| 332 |
end |
| 333 |
|
| 334 |
function SciKGTeX:make_new_command(new_property, namespace) |
| 335 |
-- check if property already exists |
| 336 |
if self.property_commands[new_property]~=nil then |
| 337 |
self:warn([[Method newpropertycommand: Repeated definition. |
| 338 |
Command %s already exists! |
| 339 |
Are you sure you want to override it?]], new_property) |
| 340 |
self:add_property(new_property, namespace) |
| 341 |
self.command_factory:override_command(new_property, namespace) |
| 342 |
self.command_factory:override_star_command(new_property, namespace) |
| 343 |
else |
| 344 |
self.property_commands[new_property] = false |
| 345 |
self:add_property(new_property, namespace) |
| 346 |
self.command_factory:build_command(new_property, namespace) |
| 347 |
self.command_factory:build_star_command(new_property, namespace) |
| 348 |
end |
| 349 |
end |
| 350 |
|
| 351 |
function SciKGTeX:add_property(new_property, namespace) |
| 352 |
new_property = self.XMP:escape_xml_tags(new_property) |
| 353 |
-- check if property already exists |
| 354 |
if self.properties_used[new_property]~=nil then |
| 355 |
self:warn([[Method addmetaproperty: Repeated definition. |
| 356 |
Property %s already added! |
| 357 |
Are you sure you want to replace it?]], new_property) |
| 358 |
-- if not make it known to the object |
| 359 |
else |
| 360 |
self.properties_used[new_property] = false |
| 361 |
end |
| 362 |
ns_prefix = self.XMP:extract_namespace_prefix(namespace) |
| 363 |
self.XMP.property_ns[new_property] = ns_prefix |
| 364 |
end |
| 365 |
|
| 366 |
function SciKGTeX:register_property(prop_type) |
| 367 |
self.properties_used[prop_type] = true |
| 368 |
end |
| 369 |
|
| 370 |
function SciKGTeX:warn_unused_command() |
| 371 |
warning_message = [[No %s annotation found! |
| 372 |
Are you sure you don't want to mark an entity with %s?]] |
| 373 |
for i, p in ipairs(self.mandatory_properties) do |
| 374 |
used = self.properties_used[p] |
| 375 |
if not used then |
| 376 |
self:warn(warning_message, p, p); |
| 377 |
end |
| 378 |
end |
| 379 |
end |
| 380 |
|
| 381 |
function SciKGTeX:print_entity(uri, label, hyperrefloaded) |
| 382 |
if label ~= "" and hyperrefloaded then |
| 383 |
tex.print(string.format('\\href{%s}{%s}', uri , label)) |
| 384 |
elseif label ~= "" then |
| 385 |
tex.print(label) |
| 386 |
elseif hyperrefloaded then |
| 387 |
tex.print(string.format('\\url{%s}',uri)) |
| 388 |
else |
| 389 |
tex.print(uri) |
| 390 |
end |
| 391 |
end |
| 392 |
|
| 393 |
---------------------------- XMP class methods ------------------------------- |
| 394 |
|
| 395 |
function XMP:escape_xml_tags(s) |
| 396 |
s = spaces_to_underscores(s) |
| 397 |
s, i = s:gsub('[^%a%d%.-_]','') |
| 398 |
if i > 0 then |
| 399 |
SciKGTeX:warn([[Method escape_xml_tags: Forbidden characters. |
| 400 |
Property %s can only contain letters, digits, underscores, hyphens and periods! |
| 401 |
Forbidden characters removed.]], s) |
| 402 |
end |
| 403 |
s, i = s:gsub('^([Xx][Mm][Ll])','_%1') |
| 404 |
if i > 0 then |
| 405 |
SciKGTeX:warn([[Method escape_xml_tags: Forbidden characters. |
| 406 |
Property %s can not start with xml! |
| 407 |
Changed to _xml.]], s) |
| 408 |
end |
| 409 |
return s |
| 410 |
end |
| 411 |
|
| 412 |
function XMP:escape_xml_content(s) |
| 413 |
s = s:gsub('&', '&') |
| 414 |
s = s:gsub('>', '>') |
| 415 |
return s:gsub('<', '<') |
| 416 |
end |
| 417 |
|
| 418 |
function XMP:add_line(...) |
| 419 |
table.insert(self.lines, string.format(...)) |
| 420 |
end |
| 421 |
|
| 422 |
function XMP:add_paper_node(paper_iri) |
| 423 |
self.paper = {} |
| 424 |
self.paper.contributions = {} |
| 425 |
self.paper.id = paper_iri |
| 426 |
self.paper.title = nil |
| 427 |
self.paper.authors = {} |
| 428 |
self.paper.researchfield = nil |
| 429 |
end |
| 430 |
|
| 431 |
function XMP:add_contribution(key, contribution_iri) |
| 432 |
local contribution = {} |
| 433 |
contribution.properties = {} |
| 434 |
contribution.id = contribution_iri:gsub("<(default_contribution)>", "ORKG_default") |
| 435 |
self.paper.contributions[key] = contribution |
| 436 |
end |
| 437 |
|
| 438 |
function XMP:extract_namespace_prefix(ns_arg) |
| 439 |
if ns_arg == '' then |
| 440 |
return nil |
| 441 |
end |
| 442 |
uri_and_prefix = ns_arg:split(',%s+?') |
| 443 |
|
| 444 |
if #uri_and_prefix < 2 then |
| 445 |
SciKGTeX:error([[Method addmetaproperty: No prefix found. |
| 446 |
Unknown prefix, URI specification: %s. |
| 447 |
Please specify the arguments as [prefix, URI]!]], ns_arg) |
| 448 |
return nil |
| 449 |
elseif #uri_and_prefix > 2 then |
| 450 |
SciKGTeX:warn([[Method addmetaproperty: Too many arguments. |
| 451 |
Too many arguments in prefix, URI specification: %s. |
| 452 |
Excess arguments are ignored.]], ns_arg) |
| 453 |
end |
| 454 |
|
| 455 |
if not uri_valid(uri_and_prefix[2]) then |
| 456 |
message = [[Method addmetaproperty: Invalid URI. |
| 457 |
The given URI %s is not a valid choice! |
| 458 |
Please use a resolvable URI starting with 'http'.]] |
| 459 |
SciKGTeX:error(message, uri_and_prefix[2]) |
| 460 |
return nil |
| 461 |
end |
| 462 |
-- add the namespace if it has not been added yet |
| 463 |
if self.namespaces[uri_and_prefix[1]]==nil then |
| 464 |
self:add_namespace(uri_and_prefix[1], uri_and_prefix[2]) |
| 465 |
end |
| 466 |
return uri_and_prefix[1] |
| 467 |
end |
| 468 |
|
| 469 |
function XMP:process_content(c) |
| 470 |
c = self:escape_xml_content(c) |
| 471 |
entity = resolve_entity(c) |
| 472 |
if entity ~= false then |
| 473 |
return entity |
| 474 |
end |
| 475 |
c = remove_latex_commands(c) |
| 476 |
return c |
| 477 |
end |
| 478 |
|
| 479 |
function XMP:property_has_namespace(annotation_type) |
| 480 |
annotation_type_t = annotation_type:split(':') |
| 481 |
|
| 482 |
if #annotation_type_t > 1 then |
| 483 |
annotation_type = annotation_type_t[2] |
| 484 |
prefix = annotation_type_t[1] |
| 485 |
else |
| 486 |
annotation_type = annotation_type_t[1] |
| 487 |
prefix = nil |
| 488 |
end |
| 489 |
return prefix, annotation_type |
| 490 |
end |
| 491 |
|
| 492 |
function XMP:set_title(title) |
| 493 |
self.paper.title = title |
| 494 |
end |
| 495 |
|
| 496 |
function XMP:add_author(author) |
| 497 |
table.insert(self.paper.authors, author) |
| 498 |
end |
| 499 |
|
| 500 |
function XMP:set_researchfield(researchfield) |
| 501 |
self.paper.researchfield = researchfield |
| 502 |
end |
| 503 |
|
| 504 |
function XMP:add_annotation(contribution_ids, annotation_type, content, annotation_id) |
| 505 |
local annotation = {} |
| 506 |
-- check if a namespace is attached to the property specification |
| 507 |
prefix, annotation_type = self:property_has_namespace(annotation_type) |
| 508 |
|
| 509 |
annotation.content = content |
| 510 |
annotation.id = annotation_id |
| 511 |
annotation.type = self:escape_xml_tags(annotation_type) |
| 512 |
|
| 513 |
-- take the prefix given, the prefix saved in the namespace dictionary or the default ns |
| 514 |
annotation.prefix = prefix or self.property_ns[annotation.type] or 'orkg_property' |
| 515 |
|
| 516 |
-- register the use of the property in text |
| 517 |
SciKGTeX:register_property(annotation.type) |
| 518 |
|
| 519 |
-- check if the annotation was numbered |
| 520 |
if contribution_ids == '' then |
| 521 |
contribution_ids = '<default_contribution>' |
| 522 |
end |
| 523 |
contributions_ids_t = contribution_ids:split(',%s+?') |
| 524 |
-- add the annotations at the specified contribution |
| 525 |
for i, contribution_id in ipairs(contributions_ids_t) do |
| 526 |
-- add a new contribution if it has not been added yet |
| 527 |
if self.paper.contributions[contribution_id] == nil then |
| 528 |
self:add_contribution(contribution_id, 'contribution_'..contribution_id) |
| 529 |
end |
| 530 |
-- add the property annotation to the list of properties of a contribution |
| 531 |
-- check if the same annotation already exists (in case of double evaluation of the LaTeX command for example) |
| 532 |
already_there = false |
| 533 |
for _, prop in pairs(self.paper.contributions[contribution_id].properties) do |
| 534 |
if prop.content == annotation.content and prop.type == annotation.type then |
| 535 |
already_there = true |
| 536 |
break |
| 537 |
end |
| 538 |
end |
| 539 |
if not already_there then |
| 540 |
table.insert(self.paper.contributions[contribution_id].properties, annotation) |
| 541 |
end |
| 542 |
end |
| 543 |
end |
| 544 |
|
| 545 |
function XMP:add_namespace(abbr, uri) |
| 546 |
self.namespaces[abbr] = uri |
| 547 |
end |
| 548 |
|
| 549 |
function XMP:generate_rdf_root() |
| 550 |
ns_key_array = {} |
| 551 |
for ns, uri in pairs(self.namespaces) do table.insert(ns_key_array, ns) end |
| 552 |
root_string = [[<rdf:RDF ]] |
| 553 |
table.sort(ns_key_array) |
| 554 |
for i, key in ipairs(ns_key_array) do |
| 555 |
root_string = root_string .. "\n xmlns:" .. key .. [[="]] .. self.namespaces[key] .. [["]] |
| 556 |
end |
| 557 |
root_string = root_string .. [[>]] |
| 558 |
return root_string |
| 559 |
end |
| 560 |
|
| 561 |
function XMP:generate_xmp_string(lb_char) |
| 562 |
lb_char = lb_char or "\n" |
| 563 |
if lb_char == "r" then |
| 564 |
lb_char = "\r" |
| 565 |
end |
| 566 |
output_string = "" |
| 567 |
sorted_contributions = {} |
| 568 |
for cb_id, contribution in pairs(XMP.paper.contributions) do |
| 569 |
table.insert(sorted_contributions,cb_id) |
| 570 |
end |
| 571 |
table.sort(sorted_contributions) |
| 572 |
self:add_line('<?xpacket begin="?" id="%s"?>',self.paper.id) |
| 573 |
self:add_line(self.XMP_TOP) |
| 574 |
self:add_line(self:generate_rdf_root()) |
| 575 |
--print(debug.traceback()) |
| 576 |
if self.paper then |
| 577 |
self:add_line( |
| 578 |
' <rdf:Description rdf:about="https://www.orkg.org/orkg/paper/%s">', |
| 579 |
self.paper.id |
| 580 |
) |
| 581 |
self:add_line(' <rdf:type rdf:resource="http://orkg.org/core#Paper"/>') |
| 582 |
if self.paper.title ~= nil then |
| 583 |
self:add_line( |
| 584 |
' <orkg:hasTitle>%s</orkg:hasTitle>', |
| 585 |
self:process_content(self.paper.title) |
| 586 |
) |
| 587 |
end |
| 588 |
for i, author in ipairs(self.paper.authors) do |
| 589 |
self:add_line( |
| 590 |
' <orkg:hasAuthor>%s</orkg:hasAuthor>', |
| 591 |
self:process_content(author)) |
| 592 |
end |
| 593 |
if self.paper.researchfield ~= nil then |
| 594 |
self:add_line( |
| 595 |
' <orkg:hasResearchField>%s</orkg:hasResearchField>', |
| 596 |
self:process_content(self.paper.researchfield) |
| 597 |
) |
| 598 |
end |
| 599 |
for i, cb_id in pairs(sorted_contributions) do |
| 600 |
contribution = self.paper.contributions[cb_id] |
| 601 |
if i==1 then |
| 602 |
if #sorted_contributions > 1 then |
| 603 |
self:add_line(' <orkg:hasResearchContribution rdf:parseType="Collection">') |
| 604 |
else |
| 605 |
self:add_line(' <orkg:hasResearchContribution>') |
| 606 |
end |
| 607 |
end |
| 608 |
self:add_line( |
| 609 |
' <orkg:ResearchContribution rdf:about="https://www.orkg.org/orkg/paper/%s">', |
| 610 |
self.paper.id .. "/" ..contribution.id |
| 611 |
) |
| 612 |
for j, property in ipairs(contribution.properties) do |
| 613 |
self:add_line( |
| 614 |
' <%s:%s>%s</%s:%s>', |
| 615 |
property.prefix, |
| 616 |
property.type, |
| 617 |
self:process_content(property.content), |
| 618 |
property.prefix, |
| 619 |
property.type |
| 620 |
) |
| 621 |
end |
| 622 |
self:add_line(' </orkg:ResearchContribution>') |
| 623 |
if i == #sorted_contributions then |
| 624 |
self:add_line(' </orkg:hasResearchContribution>') |
| 625 |
end |
| 626 |
end |
| 627 |
self:add_line(' </rdf:Description>') |
| 628 |
end |
| 629 |
self:add_line('</rdf:RDF>') |
| 630 |
self:add_line(self.XMP_BOTTOM) |
| 631 |
self:add_line(self.PACKET_END) |
| 632 |
|
| 633 |
return table.concat(self.lines, lb_char) |
| 634 |
|
| 635 |
end |
| 636 |
|
| 637 |
function XMP:attach_metadata_pdfstream(metadata_type) |
| 638 |
local xmp_string = self:generate_xmp_string() |
| 639 |
local new_pdf = pdf.obj { |
| 640 |
type = 'stream', |
| 641 |
attr = '/Type /'..metadata_type..' /Subtype /XML', |
| 642 |
immediate = true, |
| 643 |
compresslevel = 0, |
| 644 |
string = xmp_string, |
| 645 |
} |
| 646 |
self.lines = {} |
| 647 |
return new_pdf |
| 648 |
end |
| 649 |
|
| 650 |
function XMP:dump_metadata() |
| 651 |
local xmp_string = self:generate_xmp_string() |
| 652 |
local dir = get_output_dir() or '.' |
| 653 |
f = io.open(dir .. '/' .. tex.jobname .. '.xmp_metadata.xml','w') |
| 654 |
io.output(f) |
| 655 |
io.write(xmp_string) |
| 656 |
io.close(f) |
| 657 |
end |
| 658 |
|
| 659 |
luatexbase.add_to_callback('stop_run', function() |
| 660 |
SciKGTeX:warn_unused_command() |
| 661 |
if SciKGTeX.PRODUCE_XMP_FILE then |
| 662 |
XMP:dump_metadata() |
| 663 |
end |
| 664 |
end, 'at_end') |
| 665 |
|
| 666 |
-- Writing metadata packets |
| 667 |
luatexbase.add_to_callback('finish_pdffile', function() |
| 668 |
if XMP.paper then |
| 669 |
if CONFORM_TO_PDFA then |
| 670 |
catalog_key='SciKGMetadata' |
| 671 |
else |
| 672 |
catalog_key='Metadata' |
| 673 |
end |
| 674 |
local metadata_obj = XMP:attach_metadata_pdfstream(catalog_key) |
| 675 |
local catalog = pdf.getcatalog() or '' |
| 676 |
pdf.setcatalog(catalog..string.format('/%s %s 0 R', catalog_key, metadata_obj)) |
| 677 |
end |
| 678 |
end, 'finish') |
| 679 |
|
| 680 |
-- TODO: real identifier assigned |
| 681 |
|
| 682 |
-- get the id or generate UUID |
| 683 |
local output_dir = get_output_dir() or '.' |
| 684 |
local header = read_header_of_file(output_dir .. '/' .. tex.jobname .. '.xmp_metadata.xml') |
| 685 |
if header ~= nil then |
| 686 |
id = extract_uuid_from_header(header) |
| 687 |
end |
| 688 |
if id == nil then |
| 689 |
id = generate_UUID() |
| 690 |
print('generate new id:', id) |
| 691 |
end |
| 692 |
XMP:add_paper_node(id) |
| 693 |
XMP:add_namespace("rdf","http://www.w3.org/1999/02/22-rdf-syntax-ns#") |
| 694 |
XMP:add_namespace("rdfs","http://www.w3.org/2000/01/rdf-schema#") |
| 695 |
XMP:add_namespace("orkg","http://orkg.org/core#") |
| 696 |
XMP:add_namespace("orkg_property","http://orkg.org/property/") |
| 697 |
|
| 698 |
SciKGTeX.XMP = XMP |
| 699 |
return SciKGTeX |