forked from tools/josh
1
0
Fork 0

Add overriding of file extension

This commit is contained in:
Florine W. Dekker 2019-11-14 23:21:06 +01:00
parent 2304413727
commit 534246a7bc
Signed by: FWDekker
GPG Key ID: B1B567AF58D6EE0F
3 changed files with 41 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{
"name": "fwdekker.com",
"version": "0.17.0",
"version": "0.18.0",
"description": "The source code of [my personal website](https://fwdekker.com/).",
"author": "Felix W. Dekker",
"repository": {

View File

@ -42,7 +42,8 @@ export class FileSystem {
"stackoverflow.lnk": new File("https://stackoverflow.com/u/3307872"),
"linkedin.lnk": new File("https://www.linkedin.com/in/fwdekker/")
}),
"resume.pdf": new File("https://static.fwdekker.com/misc/resume.pdf")
"pgp.pub": new File("https://static.fwdekker.com/misc/pgp.pub.txt", "lnk"),
"resume.pdf": new File("https://static.fwdekker.com/misc/resume.pdf", "lnk")
})
}),
"root": new Directory()
@ -564,6 +565,11 @@ export class Directory extends Node {
export class File extends Node {
protected type: string = "File";
/**
* The type of file; overrides behavior inferred from the extension.
*/
private readonly mime: string | undefined;
/**
* The contents of this file. !!Do not use this field directly!!
*/
@ -574,11 +580,13 @@ export class File extends Node {
* Constructs a new file.
*
* @param contents the contents of this file
* @param mime the type of file; overrides behavior inferred from the extension.
*/
constructor(contents: string = "") {
constructor(contents: string = "", mime: string | undefined = undefined) {
super();
this._contents = contents;
this.mime = mime;
}
@ -613,12 +621,15 @@ export class File extends Node {
}
nameString(name: string, path: Path): string {
const extension = getFileExtension(name);
switch (extension) {
case "lnk":
case "pdf":
switch (this.mime ?? getFileExtension(name)) {
case "txt": {
const script = `execute('cat ${path.toString(true)}')`;
return `<a href="#" class="fileLink" onclick="${script}">${name}</a>`;
}
case "lnk": {
const script = `execute('open ${path.toString(true)}'); return false`;
return `<a href="${this.contents}" class="fileLink" onclick="${script}">${name}</a>`;
}
default:
return name;
}
@ -643,7 +654,7 @@ export class File extends Node {
if (obj["type"] !== "File")
throw `Cannot deserialize node of type '${obj["type"]}'.`;
return new File(obj["_contents"]);
return new File(obj["_contents"], obj["mime"]);
}
}

View File

@ -1,7 +1,7 @@
import "mocha";
import {expect} from "chai";
import {File, NullFile} from "../main/js/FileSystem";
import {File, NullFile, Path} from "../main/js/FileSystem";
describe("file", () => {
@ -44,6 +44,27 @@ describe("file", () => {
});
});
});
describe("nameString", () => {
it("returns the name if no type is known", () => {
expect(new File("contents").nameString("file", new Path("/file"))).to.equal("file");
});
it("uses the file extension to determine the value", () => {
expect(new File("contents").nameString("file.txt", new Path("/file")))
.to.equal(`<a href="#" class="fileLink" onclick="execute('cat /file')">file.txt</a>`);
});
it("uses the mime type if no type is known", () => {
expect(new File("contents", "txt").nameString("file", new Path("/file")))
.to.equal(`<a href="#" class="fileLink" onclick="execute('cat /file')">file</a>`);
});
it("overrides the file extension with the mime type", () => {
expect(new File("link", "lnk").nameString("file.txt", new Path("/file")))
.to.equal(`<a href="link" class="fileLink" onclick="execute('open /file'); return false">file.txt</a>`);
});
});
});
describe("null file", () => {