-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0dac12
commit 1394b9c
Showing
14 changed files
with
277 additions
and
24 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
config/util/representation-conversion/converters/errors.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^1.0.0/components/context.jsonld", | ||
"import": [ | ||
"files-scs:config/util/representation-conversion/converters/content-type-replacer.json", | ||
"files-scs:config/util/representation-conversion/converters/quad-to-rdf.json", | ||
"files-scs:config/util/representation-conversion/converters/rdf-to-quad.json", | ||
"files-scs:config/util/representation-conversion/converters/markdown.json" | ||
], | ||
"@graph": [ | ||
{ | ||
"@id": "urn:solid-server:default:ErrorToQuadConverter", | ||
"@type": "ErrorToQuadConverter", | ||
}, | ||
{ | ||
"comment": "Converts an error into a Markdown description of its details.", | ||
"@id": "urn:solid-server:default:ErrorToTemplateConverter", | ||
"@type": "ErrorToTemplateConverter", | ||
"templateEngine": { | ||
"@type": "HandlebarsTemplateEngine", | ||
"template": "$PACKAGE_ROOT/templates/error/main.md.hbs" | ||
}, | ||
"templatePath": "$PACKAGE_ROOT/templates/error/descriptions/", | ||
"extension": ".md.hbs", | ||
"contentType": "text/markdown" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import type { Readable } from 'stream'; | ||
import orderBy from 'lodash.orderby'; | ||
import type { Quad } from 'rdf-js'; | ||
import { BasicRepresentation } from '../../ldp/representation/BasicRepresentation'; | ||
import type { Representation } from '../../ldp/representation/Representation'; | ||
import { INTERNAL_QUADS } from '../../util/ContentTypes'; | ||
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError'; | ||
import { isContainerIdentifier, isContainerPath } from '../../util/PathUtil'; | ||
import { endOfStream } from '../../util/StreamUtil'; | ||
import type { TemplateEngine } from '../../util/templates/TemplateEngine'; | ||
import { LDP } from '../../util/Vocabularies'; | ||
import type { RepresentationConverterArgs } from './RepresentationConverter'; | ||
import { TypedRepresentationConverter } from './TypedRepresentationConverter'; | ||
|
||
interface ResourceDetails { | ||
name: string; | ||
identifier: string; | ||
container: boolean; | ||
} | ||
|
||
/** | ||
* A {@link RepresentationConverter} that creates a templated representation of a container. | ||
*/ | ||
export class ContainerToTemplateConverter extends TypedRepresentationConverter { | ||
private readonly templateEngine: TemplateEngine; | ||
private readonly contentType: string; | ||
|
||
public constructor(templateEngine: TemplateEngine, contentType: string) { | ||
super(INTERNAL_QUADS, contentType); | ||
this.templateEngine = templateEngine; | ||
this.contentType = contentType; | ||
} | ||
|
||
public async canHandle(args: RepresentationConverterArgs): Promise<void> { | ||
if (!isContainerIdentifier(args.identifier)) { | ||
throw new NotImplementedHttpError('Can only convert containers.'); | ||
} | ||
await super.canHandle(args); | ||
} | ||
|
||
public async handle({ identifier, representation }: RepresentationConverterArgs): Promise<Representation> { | ||
const rendered = await this.templateEngine.render({ | ||
container: this.getLocalName(identifier.path), | ||
children: await this.getChildResources(identifier.path, representation.data), | ||
}); | ||
return new BasicRepresentation(rendered, representation.metadata, this.contentType); | ||
} | ||
|
||
/** | ||
* Collects the children of the container as simple objects. | ||
*/ | ||
private async getChildResources(container: string, quads: Readable): Promise<ResourceDetails[]> { | ||
// Collect the needed bits of information from the containment triples | ||
const resources = new Set<string>(); | ||
quads.on('data', ({ subject, predicate, object }: Quad): void => { | ||
if (subject.value === container && predicate.equals(LDP.terms.contains)) { | ||
resources.add(object.value); | ||
} | ||
}); | ||
await endOfStream(quads); | ||
|
||
// Create a simplified object for every resource | ||
const children = [ ...resources ].map((resource: string): ResourceDetails => ({ | ||
identifier: resource, | ||
name: this.getLocalName(resource), | ||
container: isContainerPath(resource), | ||
})); | ||
|
||
// Sort the resulting list | ||
return orderBy(children, [ 'container', 'identifier' ], [ 'desc', 'asc' ]); | ||
} | ||
|
||
/** | ||
* Derives a short name for the given resource. | ||
*/ | ||
private getLocalName(iri: string, keepTrailingSlash = false): string { | ||
const match = /:\/+[^/]+.*\/(([^/]+)\/?)$/u.exec(iri); | ||
return match ? decodeURIComponent(match[keepTrailingSlash ? 1 : 2]) : '/'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Contents of {{container}} | ||
<ul class="container"> | ||
{{#each children}} | ||
<li class="{{#if container}}container{{else}}document{{/if}}"> | ||
<a href="{{identifier}}">{{name}}</a> | ||
</li> | ||
{{/each}} | ||
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.