Skip to content

Add support for v3 manifests #7

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

Merged
merged 7 commits into from
Sep 16, 2024
Merged
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
104 changes: 104 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@
"jsonld": "^5.2.0",
"rollup": "^3.20.2",
"rollup-plugin-license": "^3.0.1"
},
"dependencies": {
"iiif-builder": "^2.0.0"
}
}
45 changes: 23 additions & 22 deletions src/manifest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert')
const { CONTEXTS, dc, dcterms, oa, rdf, rdfs, sc, svcs } = require('./ns')
const { CONTEXTS, as, dc, dcterms, oa, rdf, rdfs, iiif_prezi, schema } = require('./ns')

async function expand(jsonld, data) {
return jsonld.expand(data, {
Expand Down Expand Up @@ -41,8 +41,9 @@ function blank(value) {
}

class Resource {
constructor(data = {}) {
constructor(data = {}, isUpgraded = false) {
this.data = data
this.isUpgraded = isUpgraded
}

get props() {
Expand All @@ -61,7 +62,7 @@ class Resource {
}

get metadata() {
return this.data[sc('metadataLabels')]?.[0]['@list'] || []
return this.data[iiif_prezi('metadataEntries')]?.[0]['@list'] || []
}

getDescriptiveProperties() {
Expand All @@ -70,7 +71,7 @@ class Resource {
let [title, description, date] = this.values(
rdfs('label'),
dc('description'),
sc('presentationDate')
iiif_prezi('navigationDate')
)

if (!blank(title))
Expand All @@ -88,7 +89,7 @@ class Resource {

let [rights, attribution] = this.values(
dcterms('rights'),
sc('attributionLabel')
iiif_prezi('requiredStatement')
)

if (!blank(rights))
Expand Down Expand Up @@ -189,44 +190,44 @@ class Image extends Resource {
get url() {
let [body] = this.values(oa('hasBody'))[0]
let format = body[dc('format')]?.[0]['@value']
let service = body[svcs('has_service')]?.[0]

if (service)
return `${service['@id']}/full/full/0/default${Image.ext(format)}`
else
return body['@id']
let service = body[schema('potentialAction')]?.[0]
return service ?
(this.isUpgraded ?
`${service['@id']}/full/full/0/default${Image.ext(format)}` :
`${service['@id']}/full/max/0/default${Image.ext(format)}`) :
body['@id']
}
}

class Canvas extends Resource {
get images() {
return (this.data[sc('hasImageAnnotations')]?.[0]['@list'] || []).map(
(data) => new Image(data)
return (this.data[
as('items')]?.[0]['@list'][0][as('items')]?.[0]['@list'] || []).map(
(data) => new Image(data, this.isUpgraded)
)
}
}

class Manifest extends Resource {
static async parse(data, jsonld) {
static async parse(data, jsonld, isUpgraded) {
let expanded = await expand(jsonld, data)

return expanded.map((manifest) => {
assert.equal(
sc('Manifest'),
iiif_prezi('Manifest'),
manifest['@type']?.[0],
'not a IIIF Presentation API 2.0 manifest'
'not a IIIF Presentation API 3.0 manifest'
)
return new this(manifest)
return new this(manifest, isUpgraded)
})
}

get canvases() {
// Currently returns only the primary sequence!
return (
this.data[sc('hasSequences')]?.[0]['@list'][0][sc('hasCanvases')]?.[0][
'@list'
] || []
).map((data) => new Canvas(data))
this.data[as('items')]?.[0]['@list'] || []
).map(
(data) => new Canvas(data, this.isUpgraded)
)
}

get images() {
Expand Down
18 changes: 10 additions & 8 deletions src/ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ const alias = (ctx, name) => ns((ctx['@context'][0] || ctx['@context'])[name])

module.exports = {
ns,
dc: alias(pv2, 'dc'),
dcterms: alias(pv2, 'dcterms'),
iiif: alias(image, 'iiif'),
oa: alias(pv2, 'oa'),
rdf: alias(pv2, 'rdf'),
rdfs: alias(pv2, 'rdfs'),
sc: alias(pv2, 'sc'),
svcs: alias(pv2, 'svcs'),
as: alias(pv3, 'as'),
dc: alias(pv3, 'dc'),
dcterms: alias(pv3, 'dcterms'),
iiif_image: alias(image, 'iiif_image'),
oa: alias(pv3, 'oa'),
rdf: alias(pv3, 'rdf'),
rdfs: alias(pv3, 'rdfs'),
iiif_prezi: alias(pv3, 'iiif_prezi'),
schema: alias(pv3, 'schema'),
svcs: alias(pv3, 'svcs'),

CONTEXTS: {
'http://iiif.io/api/image/2/context.json': image,
Expand Down
12 changes: 11 additions & 1 deletion src/plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { readFile } = require('fs/promises')
const { IIIFBuilder } = require('iiif-builder')
const { Manifest } = require('./manifest')
const LABELS = require('./labels.json')

Expand All @@ -24,7 +25,16 @@ class IIIFPlugin {
for (let file of files) {
try {
let data = JSON.parse(await readFile(file))
let [manifest] = await Manifest.parse(data, this.context.json)
let isUpgraded = false
if (data['@context'] === 'http://iiif.io/api/presentation/2/context.json') {
const builder = new IIIFBuilder()
await builder.vault.loadManifest(data['@id'], data)
data = builder.toPresentation3({ id: data['@id'], type: 'Manifest' })
isUpgraded = true
}
let [manifest] = await Manifest.parse(
data, this.context.json, isUpgraded
)

payload.data.push(this.convert(manifest))
} catch (e) {
Expand Down
Loading