Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Javarome committed Jul 2, 2024
1 parent 56c169d commit 3bef444
Show file tree
Hide file tree
Showing 13 changed files with 376 additions and 331 deletions.
107 changes: 52 additions & 55 deletions ImageCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DomReplaceCommand, DomReplacer } from "ssg-api"
import { AnchorReplacer } from "./anchor/AnchorReplacer"
import { HtmlRR0SsgContext } from "./RR0SsgContext"
import * as path from "path"
import * as fs from "fs"
Expand All @@ -10,111 +9,109 @@ import sizeOf from "image-size"
*/
export class ImageCommand extends DomReplaceCommand<HTMLImageElement> {

protected readonly singleton: AnchorReplacer;

constructor(protected outBaseDir: string, protected maxWidth: number, protected maxHeight: number,
protected baseUrl = '') {
super('img');
protected baseUrl = "") {
super("img")
}

protected async createReplacer(context: HtmlRR0SsgContext): Promise<DomReplacer<HTMLImageElement>> {
return {
replace: async (imgEl: HTMLImageElement): Promise<HTMLImageElement> => {
const src = imgEl.src;
const imgParentEl = imgEl.parentElement;
if (imgParentEl.tagName === 'FIGURE') {
const captionEl = imgParentEl.querySelector('figcaption');
const src = imgEl.src
const imgParentEl = imgEl.parentElement
if (imgParentEl.tagName === "FIGURE") {
const captionEl = imgParentEl.querySelector("figcaption")
if (captionEl) {
const caption = captionEl.textContent;
imgEl.alt = caption.replace(/\n+/g, '').trim();
const caption = captionEl.textContent
imgEl.alt = caption.replace(/\n+/g, "").trim()
} else {
const caption = imgEl.alt;
const caption = imgEl.alt
if (caption) {
const newCaptionEl = imgParentEl.ownerDocument.createElement('figcaption');
newCaptionEl.textContent = caption;
imgParentEl.appendChild(newCaptionEl);
const newCaptionEl = imgParentEl.ownerDocument.createElement("figcaption")
newCaptionEl.textContent = caption
imgParentEl.appendChild(newCaptionEl)
}
}
}
context.debug(context.file.name, "requires image", src)
try {
let isExternal = src.startsWith('http');
let isAbsolute = src.startsWith('/');
let isExternal = src.startsWith("http")
let isAbsolute = src.startsWith("/")
if (isAbsolute) {
imgEl.src = this.baseUrl + src;
imgEl.src = this.baseUrl + src
}
let imgPath = isExternal ? src : isAbsolute ? path.join('.', src) : path.join(
let imgPath = isExternal ? src : isAbsolute ? path.join(".", src) : path.join(
path.dirname(context.file.name),
src);
imgEl.loading = 'lazy';
src)
imgEl.loading = "lazy"
if (!isExternal && !imgEl.width && !imgEl.height) {
const dimensions = sizeOf(imgPath);
let width = dimensions.width;
let height = dimensions.height;
const dimensions = sizeOf(imgPath)
let width = dimensions.width
let height = dimensions.height
if (width > this.maxWidth) {
const ratio = this.maxWidth / width;
width = this.maxWidth;
height *= ratio;
const ratio = this.maxWidth / width
width = this.maxWidth
height *= ratio
}
if (height > this.maxHeight) {
const ratio = this.maxHeight / height;
height = this.maxHeight;
width *= ratio;
const ratio = this.maxHeight / height
height = this.maxHeight
width *= ratio
}
imgEl.width = width;
imgEl.height = height;
imgEl.setAttribute('onclick',
`this.classList.contains('zoomed') ? document.exitFullscreen() && this.classList.toggle('zoomed', false): this.classList.toggle('zoomed', true) && this.requestFullscreen()`);
imgEl.width = width
imgEl.height = height
imgEl.setAttribute("onclick",
`this.classList.contains('zoomed') ? document.exitFullscreen() && this.classList.toggle('zoomed', false): this.classList.toggle('zoomed', true) && this.requestFullscreen()`)
}
} catch (e) {
context.warn("Could not determine size of image ", src, e)
}
context.images.add(src);
return imgEl;
context.images.add(src)
return imgEl
}
};
}
}

protected async postExecute(context: HtmlRR0SsgContext) {
const imagesUrls = context.images;
const imagesUrls = context.images
if (imagesUrls.size > 0) {
for (const imageUrl of imagesUrls) {
this.handleImage(context, imageUrl);
this.handleImage(context, imageUrl)
}
imagesUrls.clear();
imagesUrls.clear()
}
}

private handleImage(context: HtmlRR0SsgContext, imageUrl: string) {
const inputFile = context.file.name
if (imageUrl) {
const isLocal = !imageUrl.startsWith('http');
const isLocal = !imageUrl.startsWith("http")
if (isLocal) {
const contextDir = path.dirname(inputFile);
const isAbsolute = path.isAbsolute(imageUrl);
const inFile = isAbsolute ? path.resolve('.' + imageUrl) : path.resolve(path.join(contextDir, imageUrl));
const outBaseDir = isAbsolute ? this.outBaseDir : path.join(this.outBaseDir, contextDir);
const outRel = path.join(outBaseDir, imageUrl);
context.debug('Copying', imageUrl, 'to', outRel);
const outFile = path.resolve(outRel);
const contextDir = path.dirname(inputFile)
const isAbsolute = path.isAbsolute(imageUrl)
const inFile = isAbsolute ? path.resolve("." + imageUrl) : path.resolve(path.join(contextDir, imageUrl))
const outBaseDir = isAbsolute ? this.outBaseDir : path.join(this.outBaseDir, contextDir)
const outRel = path.join(outBaseDir, imageUrl)
context.debug("Copying", imageUrl, "to", outRel)
const outFile = path.resolve(outRel)
try {
const outDir = path.dirname(outFile);
const outDir = path.dirname(outFile)
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir, {recursive: true});
fs.mkdirSync(outDir, {recursive: true})
}
fs.copyFileSync(inFile, outFile);
fs.copyFileSync(inFile, outFile)
} catch (e) {
if (e.code === 'ENOENT') {
context.warn(`File ${inFile} does not exist`);
if (e.code === "ENOENT") {
context.warn(`File ${inFile} does not exist`)
} else {
throw e;
throw e
}
}
} else {
context.warn(`File ${imageUrl} is external; will not copy it.`)
}
} else {
context.warn(`Empty image src in ${inputFile}`);
context.warn(`Empty image src in ${inputFile}`)
}
}
}
2 changes: 2 additions & 0 deletions build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import { SourceRenderer } from "./source/SourceRenderer"
import { TimeService } from "./time/TimeService"
import { CaseService } from "./science/crypto/ufo/enquete/dossier/CaseService"
import { TimeReplacer } from "./time/TimeReplacer"
import { UnitReplaceCommand } from "./value/UnitReplaceCommand"

interface RR0BuildArgs {
reindex?: "true" | "false"
Expand Down Expand Up @@ -254,6 +255,7 @@ timeService.getFiles().then(async (timeFiles) => {
new ClassDomRegexReplaceCommand("temoin(.?)", new WitnessReplacerFactory()),
new ClassDomReplaceCommand("note", new NoteReplacerFactory()),
new ClassDomReplaceCommand("indexed", new IndexedReplacerFactory()),
new UnitReplaceCommand(),
new MetaLinkReplaceCommand(new TimeLinkDefaultHandler(timeFiles))
]
const ssg = new Ssg(config)
Expand Down
6 changes: 6 additions & 0 deletions lang/RR0Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,10 @@ export interface RR0Messages {
prev: string
next: string
}
unit: {
smi: (miles: number) => string
fot: (feet: number) => string
hm: (milesPerHour: number) => string
inh: (inches: number) => string
}
}
6 changes: 6 additions & 0 deletions lang/RR0Messages_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,10 @@ export class RR0Messages_en implements RR0Messages {
prev: "Previous",
next: "Next"
}
unit = {
smi: (miles: number): string => (miles / 1.60934).toFixed(0) + " km",
fot: (feet: number): string => (feet * 0.3048).toFixed(0) + " m",
hm: (milesPerHour: number): string => this.unit.smi(milesPerHour) + "/h",
inh: (inches: number): string => (inches / 2.54).toFixed(0) + " cm"
}
}
6 changes: 6 additions & 0 deletions lang/RR0Messages_fr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,10 @@ export class RR0Messages_fr implements RR0Messages {
prev: "Précédent",
next: "Suivant"
}
unit = {
smi: (miles: number): string => (miles / 1.60934).toFixed(0) + " km",
fot: (feet: number): string => (feet * 0.3048).toFixed(0) + " m",
hm: (milesPerHour: number): string => this.unit.smi(milesPerHour) + "/h",
inh: (inches: number): string => (inches * 2.54).toFixed(0) + " cm"
}
}
Loading

0 comments on commit 3bef444

Please sign in to comment.