-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
88 changed files
with
1,059 additions
and
897 deletions.
There are no files selected for viewing
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,62 @@ | ||
import { RR0Data } from "./RR0Data" | ||
import { TimeContext } from "./time/TimeContext" | ||
|
||
import { RR0DataFactory } from "./RR0DataFactory" | ||
import path from "path" | ||
import fs from "fs" | ||
import { RR0Event } from "./event/RR0Event" | ||
import { RR0EventFactory } from "./event/RR0EventFactory" | ||
|
||
export class AbstractDataFactory<T extends RR0Data> implements RR0DataFactory<T> { | ||
|
||
static readonly defaultImageFileNames = ["portrait.jpg", "portrait.gif", "portrait.png", "portrait.webp"] | ||
|
||
constructor(protected eventFactory: RR0EventFactory) { | ||
} | ||
|
||
createTimeFromString(timeStr: string): TimeContext | undefined { | ||
if (timeStr) { | ||
const time = new TimeContext({}) | ||
time.updateFromStr(timeStr) | ||
return time | ||
} else { | ||
return undefined | ||
} | ||
} | ||
|
||
createFromData(data: RR0Data): T { | ||
data.time = this.createTimeFromString(data.time as any) | ||
const events = data.events || [] | ||
const birthTime = (data as any).birthTime as unknown as string | ||
if (birthTime) { | ||
delete (data as any).birthTime | ||
events.push({type: "birth", time: birthTime as any, events: []}) | ||
} | ||
const deathTime = (data as any).deathTime as unknown as string | ||
if (deathTime) { | ||
delete (data as any).deathTime | ||
events.push({type: "death", time: deathTime as any, events: []}) | ||
} | ||
if (!data.image) { | ||
let hasPortrait = false | ||
for (const defaultImageFile of AbstractDataFactory.defaultImageFileNames) { | ||
const portraitPath = path.join(data.dirName, defaultImageFile) | ||
hasPortrait = fs.existsSync(path.join(data.dirName, defaultImageFile)) | ||
if (hasPortrait) { | ||
events.push({type: "image", url: defaultImageFile as any, name: data.name, events: []}) | ||
break | ||
} | ||
} | ||
} | ||
data.events = this.parseEvents(events) | ||
return data as T | ||
} | ||
|
||
protected parseEvents(events: RR0Data[] = []): RR0Event[] { | ||
const parsed: RR0Event[] = [] | ||
for (const event of events) { | ||
parsed.push(this.eventFactory.createFromData(event)) | ||
} | ||
return parsed | ||
} | ||
} |
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,28 @@ | ||
import path from "path" | ||
import { RR0Data } from "./RR0Data" | ||
import { AbstractDataFactory } from "./AbstractDataFactory" | ||
import { FileContents } from "ssg-api" | ||
import { RR0EventFactory } from "./event/RR0EventFactory" | ||
|
||
/** | ||
* A RR0Data factory which can read either <someType>.json files of index.json with a "type": "<someType>" property. | ||
*/ | ||
export class DefaultDataFactory<T extends RR0Data> extends AbstractDataFactory<T> { | ||
|
||
constructor(eventFactory: RR0EventFactory, readonly type: string, readonly fileNames: string[] = [type]) { | ||
super(eventFactory) | ||
} | ||
|
||
create(file: FileContents): T | undefined { | ||
const data = JSON.parse(file.contents) as RR0Data | ||
const basename = path.basename(file.name) | ||
let datum: T | undefined | ||
if (data.type === this.type || this.fileNames.reduce( | ||
(hasIt: boolean, fileName) => basename.startsWith(fileName) ? true : hasIt, | ||
false)) { | ||
data.dirName = path.dirname(file.name) | ||
datum = this.createFromData(data) | ||
} | ||
return datum | ||
} | ||
} |
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,9 @@ | ||
import { RR0Data } from "./RR0Data" | ||
|
||
/** | ||
* Instantiates RR0Data from (JSON) file contents. | ||
*/ | ||
export interface RR0DataFactory<T extends RR0Data> { | ||
|
||
createFromData(data: RR0Data): T | ||
} |
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { RR0Data } from "./RR0Data" | ||
import { RR0Data } from "../RR0Data" | ||
|
||
export type RR0EventType = | ||
"birth" | ||
|
Oops, something went wrong.