@rr0/cms 0.3.68 → 0.3.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -27,6 +27,11 @@ export type RR0TimeMessages = {
27
27
  };
28
28
  };
29
29
  fromTo(startReplacement: string, endReplacement: string): string;
30
+ /**
31
+ * The word that, written right before an interval, asks for it to be rendered as `betweenAnd` ("between", "entre").
32
+ */
33
+ between: RegExp;
34
+ betweenAnd(startReplacement: string, endReplacement: string): string;
30
35
  on(approximate: boolean): string;
31
36
  in(approximate: boolean): string;
32
37
  starting(approximate: boolean): string;
@@ -30,5 +30,7 @@ export declare class RR0TimeMessages_en implements RR0TimeMessages {
30
30
  on: (approximate: boolean) => string;
31
31
  in: (approximate: boolean) => string;
32
32
  fromTo: (startReplacement: string, endReplacement: string) => string;
33
+ between: RegExp;
34
+ betweenAnd: (startReplacement: string, endReplacement: string) => string;
33
35
  starting: (approximate: boolean) => string;
34
36
  }
@@ -33,6 +33,8 @@ export class RR0TimeMessages_en {
33
33
  };
34
34
  this.in = (approximate) => (approximate ? "around " : "");
35
35
  this.fromTo = (startReplacement, endReplacement) => startReplacement + " to " + endReplacement;
36
+ this.between = /\bbetween\s*$/i;
37
+ this.betweenAnd = (startReplacement, endReplacement) => startReplacement + " and " + endReplacement;
36
38
  this.starting = (approximate) => "starting " + (approximate ? "" : "the ");
37
39
  }
38
40
  }
@@ -30,5 +30,7 @@ export declare class RR0TimeMessages_fr implements RR0TimeMessages {
30
30
  on: (approximate: boolean) => string;
31
31
  in: (approximate: boolean) => string;
32
32
  fromTo: (startReplacement: string, endReplacement: string) => string;
33
+ between: RegExp;
34
+ betweenAnd: (startReplacement: string, endReplacement: string) => string;
33
35
  starting: (approximate: boolean) => string;
34
36
  }
@@ -33,6 +33,8 @@ export class RR0TimeMessages_fr {
33
33
  };
34
34
  this.in = (approximate) => (approximate ? "vers " : "en ");
35
35
  this.fromTo = (startReplacement, endReplacement) => `${startReplacement} à ${endReplacement}`;
36
+ this.between = /\bentre\s*$/i;
37
+ this.betweenAnd = (startReplacement, endReplacement) => `${startReplacement} et ${endReplacement}`;
36
38
  this.starting = (approximate) => "à partir " + (approximate ? "de " : "du ");
37
39
  }
38
40
  }
@@ -112,6 +112,11 @@ export class TimeContext {
112
112
  }
113
113
  catch (e) {
114
114
  try {
115
+ // Only a duration starts with P ("P10M", "~P1H"): otherwise EdtfDuration also accepts a date whose
116
+ // time zone contains a P ("21:45PST", "00:38:42PDT") as an empty duration.
117
+ if (!/^\s*~?P/.test(timeStr)) {
118
+ throw new Error("Not a duration");
119
+ }
115
120
  this.duration = EdtfDuration.fromString(timeStr);
116
121
  this.interval = undefined;
117
122
  this.date = undefined;
@@ -59,6 +59,20 @@ describe("TimeContext", () => {
59
59
  expect(timeContext.getHour()).toBe(12);
60
60
  expect(timeContext.getMinutes()).toBe(55);
61
61
  });
62
+ test("updateFromStr with a time zone containing a P is a date, not a duration", () => {
63
+ for (const str of ["1957-02-27 21:45PST", "1991-09-18 00:38:42PDT", "1957-02-27 21:45 (PST)"]) {
64
+ const context = new TimeContext();
65
+ expect(context.updateFromStr(str)).toBe(true);
66
+ expect(context.duration).toBeUndefined();
67
+ expect(context.getYear()).toBeDefined();
68
+ expect(context.getHour()).toBeDefined();
69
+ }
70
+ });
71
+ test("updateFromStr duration", () => {
72
+ const context = new TimeContext();
73
+ expect(context.updateFromStr("~P1H")).toBe(true);
74
+ expect(context.duration).toBeDefined();
75
+ });
62
76
  test("fromDate", () => {
63
77
  const month = 12;
64
78
  const date = new Date(2001, month - 1, 13);
@@ -37,7 +37,10 @@ export class TimeElementFactory {
37
37
  if (endReplacement && endReplacement.outerHTML !== startReplacement.outerHTML) {
38
38
  replacement = fromContext.file.document.createElement("span");
39
39
  replacement.className = "time-interval";
40
- replacement.innerHTML = fromContext.messages.context.time.fromTo(startReplacement.outerHTML, endReplacement.outerHTML);
40
+ const timeMessages = fromContext.messages.context.time;
41
+ replacement.innerHTML = options.between
42
+ ? timeMessages.betweenAnd(startReplacement.outerHTML, endReplacement.outerHTML)
43
+ : timeMessages.fromTo(startReplacement.outerHTML, endReplacement.outerHTML);
41
44
  }
42
45
  }
43
46
  return replacement;
@@ -5,6 +5,10 @@ import { TimeUrlBuilder } from "../TimeUrlBuilder.js";
5
5
  export interface TimeRenderOptions {
6
6
  url: boolean;
7
7
  contentOnly: boolean;
8
+ /**
9
+ * The text says "between" right before this interval, so it reads "between X and Y" rather than "between X to Y".
10
+ */
11
+ between?: boolean;
8
12
  }
9
13
  export declare class TimeRenderer {
10
14
  readonly urlBuilder: TimeUrlBuilder;
@@ -6,7 +6,16 @@ import { TimeElementFactory } from "./TimeElementFactory.js";
6
6
  */
7
7
  export declare class TimeReplacer implements DomReplacement<HtmlRR0Context, HTMLTimeElement> {
8
8
  readonly factory: TimeElementFactory;
9
+ /**
10
+ * Elements that don't break a sentence, so that the text before them is the text before the <time> they contain
11
+ * ("between <strong><time>1989/1994</time></strong>").
12
+ */
13
+ static readonly inlineTags: string[];
9
14
  constructor(factory: TimeElementFactory);
10
15
  static resolvedTime(context: HtmlRR0Context, dateTime: string): HTMLTimeElement;
16
+ /**
17
+ * @return The text that precedes an element in its sentence.
18
+ */
19
+ static precedingText(el: Element): string;
11
20
  replacement(context: HtmlRR0Context, origEl: HTMLTimeElement): Promise<HTMLElement>;
12
21
  }
@@ -11,6 +11,17 @@ export class TimeReplacer {
11
11
  replacement.dateTime = dateTime;
12
12
  return replacement;
13
13
  }
14
+ /**
15
+ * @return The text that precedes an element in its sentence.
16
+ */
17
+ static precedingText(el) {
18
+ var _a, _b;
19
+ let node = el;
20
+ while (!node.previousSibling && node.parentElement && TimeReplacer.inlineTags.includes(node.parentElement.tagName)) {
21
+ node = node.parentElement;
22
+ }
23
+ return (_b = (_a = node.previousSibling) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : "";
24
+ }
14
25
  async replacement(context, origEl) {
15
26
  let replacement;
16
27
  if (origEl.dateTime) { // Already done?
@@ -20,7 +31,8 @@ export class TimeReplacer {
20
31
  const previousContext = origEl.dataset.context === "none" ? undefined : context.clone();
21
32
  const timeStr = origEl.textContent;
22
33
  const valid = context.time.updateFromStr(timeStr);
23
- replacement = valid && this.factory.create(context, previousContext, { url: true, contentOnly: true });
34
+ const between = context.messages.context.time.between.test(TimeReplacer.precedingText(origEl));
35
+ replacement = valid && this.factory.create(context, previousContext, { url: true, contentOnly: true, between });
24
36
  if (!replacement) {
25
37
  replacement = origEl;
26
38
  // replacement.setAttribute("datetime", context.time.toString())
@@ -30,3 +42,8 @@ export class TimeReplacer {
30
42
  return replacement;
31
43
  }
32
44
  }
45
+ /**
46
+ * Elements that don't break a sentence, so that the text before them is the text before the <time> they contain
47
+ * ("between <strong><time>1989/1994</time></strong>").
48
+ */
49
+ TimeReplacer.inlineTags = ["STRONG", "EM", "B", "I", "U", "SPAN", "A"];
@@ -83,6 +83,25 @@ describe("TimeReplacer", async () => {
83
83
  const replaced = await replacer.replacement(context, interval);
84
84
  expect(replaced.outerHTML).toBe(`<span class="time-interval"><span class="time-resolved"><time datetime="1951">1951</time></span> à <span class="time-resolved"><time datetime="1955">1955</time></span></span>`);
85
85
  });
86
+ describe("renders an interval after \"between\" as \"between X and Y\"", () => {
87
+ async function renderIn(locale, html) {
88
+ const context = cmsTestUtil.time.newHtmlContext("1/9/9/0/08/index.html", "", locale);
89
+ const p = context.file.document.createElement("p");
90
+ p.innerHTML = html;
91
+ const replaced = await replacer.replacement(context, p.querySelector("time"));
92
+ return replaced.textContent;
93
+ }
94
+ test("in French", async () => {
95
+ expect(await renderIn("fr", "entre <time>1951/1955</time>")).toBe("1951 et 1955");
96
+ });
97
+ test("in English, through an inline element", async () => {
98
+ expect(await renderIn("en", "anywhere between <strong><time>1951/1955</time></strong>")).toBe("1951 and 1955");
99
+ });
100
+ test("but not without the word", async () => {
101
+ expect(await renderIn("fr", "de <time>1951/1955</time>")).toBe("1951 à 1955");
102
+ expect(await renderIn("en", "from <time>1951/1955</time>")).toBe("1951 to 1955");
103
+ });
104
+ });
86
105
  test("parses unsupported", async () => {
87
106
  const interval = "moi";
88
107
  const context = cmsTestUtil.time.newHtmlContext("1/9/9/0/08/index.html", "");
@@ -64,8 +64,10 @@ export class RelativeTimeTextBuilder {
64
64
  if (deltaContext.time.isDefined()) {
65
65
  newContext = deltaContext;
66
66
  }
67
+ // A relative wording ("the day after", "the month after"...) only makes sense against a previous time
68
+ // that is as precise: a month page (1947-07) is not "the day before" its first entry (1947-07-01).
67
69
  const messages = newContext.messages.context.time.relative;
68
- if (dayOfMonthDelta && sameYear && sameMonth) {
70
+ if (dayOfMonthDelta && sameYear && sameMonth && !noDay) {
69
71
  switch (dayOfMonthDelta) {
70
72
  case -1:
71
73
  text = messages.day.before;
@@ -76,7 +78,7 @@ export class RelativeTimeTextBuilder {
76
78
  }
77
79
  }
78
80
  else {
79
- if (!dayOfMonth && sameYear) {
81
+ if (!dayOfMonth && sameYear && !noMonth) {
80
82
  switch (deltaDurationMonth) {
81
83
  case -1:
82
84
  text = messages.month.before;
@@ -86,7 +88,7 @@ export class RelativeTimeTextBuilder {
86
88
  break;
87
89
  }
88
90
  }
89
- if (!month) {
91
+ if (!month && !noYear) {
90
92
  switch (yearDelta) {
91
93
  case -1:
92
94
  text = messages.year.before;
@@ -96,7 +98,7 @@ export class RelativeTimeTextBuilder {
96
98
  break;
97
99
  }
98
100
  }
99
- if (!minutes) {
101
+ if (!minutes && previousTime.getHour() !== undefined) {
100
102
  switch (hourDelta) {
101
103
  case -1:
102
104
  text = messages.hour.before;
@@ -90,6 +90,24 @@ describe("RelativeTimeTextBuilder", () => {
90
90
  expect(relativeTimeTextBuilder.build(previousContext, context)).toBe("the day after");
91
91
  }
92
92
  });
93
+ test("first day of a month page is not the day after the month", () => {
94
+ const monthContext = cmsTestUtil.time.newHtmlContext("1/9/4/7/07", "");
95
+ const firstDay = monthContext.clone();
96
+ firstDay.time.updateFromStr("1947-07-01");
97
+ expect(relativeTimeTextBuilder.build(monthContext, firstDay)).toBe("mardi 1");
98
+ const secondDay = firstDay.clone();
99
+ secondDay.time.updateFromStr("1947-07-02");
100
+ expect(relativeTimeTextBuilder.build(firstDay, secondDay)).toBe("le lendemain");
101
+ });
102
+ test("first month of a year page is not the month after the year", () => {
103
+ const yearContext = cmsTestUtil.time.newHtmlContext("1/9/4/7", "");
104
+ const january = yearContext.clone();
105
+ january.time.updateFromStr("1947-01");
106
+ expect(relativeTimeTextBuilder.build(yearContext, january)).toBe("janvier");
107
+ const february = january.clone();
108
+ february.time.updateFromStr("1947-02");
109
+ expect(relativeTimeTextBuilder.build(january, february)).toBe("le mois suivant");
110
+ });
93
111
  describe("hour", () => {
94
112
  test("next", () => {
95
113
  {
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ import { RelativeTimeTextBuilder } from "./RelativeTimeTextBuilder.js";
2
+ import { cmsTestUtil } from "../../test/CMSTestUtil.js";
3
+ import { test } from "vitest";
4
+ import { TimeTextBuilder } from "./TimeTextBuilder.js";
5
+ import { Level0Duration } from "@rr0/time";
6
+ test("x", () => {
7
+ const b = new RelativeTimeTextBuilder(new TimeTextBuilder(cmsTestUtil.intlOptions));
8
+ const prev = cmsTestUtil.time.newHtmlContext("1/9/4/7/07", "");
9
+ console.log("prev", prev.time.toString(), prev.time.getDayOfMonth());
10
+ const ctx = prev.clone();
11
+ ctx.time.updateFromStr("1947-07-01");
12
+ const ms = ctx.time.date.compare(prev.time.date);
13
+ console.log("ctx", ctx.time.toString(), ms, JSON.stringify(Level0Duration.toSpec(ms)));
14
+ console.log(b.build(prev, ctx));
15
+ const c2 = ctx.clone();
16
+ c2.time.updateFromStr("1947-07-02");
17
+ const ms2 = c2.time.date.compare(prev.time.date);
18
+ console.log(ms2, JSON.stringify(Level0Duration.toSpec(ms2)), b.build(prev, c2));
19
+ });
20
+ test("y", () => {
21
+ const b = new RelativeTimeTextBuilder(new TimeTextBuilder(cmsTestUtil.intlOptions));
22
+ const prev = cmsTestUtil.time.newHtmlContext("1/9/4/7", "");
23
+ const ctx = prev.clone();
24
+ ctx.time.updateFromStr("1947-01");
25
+ const ms = ctx.time.date.compare(prev.time.date);
26
+ console.log("Y", prev.time.toString(), ctx.time.toString(), ms, JSON.stringify(Level0Duration.toSpec(ms)), b.build(prev, ctx));
27
+ });
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@rr0/cms",
3
3
  "type": "module",
4
4
  "author": "Jérôme Beau <rr0@rr0.org> (https://rr0.org)",
5
- "version": "0.3.68",
5
+ "version": "0.3.70",
6
6
  "description": "RR0 Content Management System (CMS)",
7
7
  "exports": "./dist/index.js",
8
8
  "types": "./dist/index.d.ts",
@@ -29,7 +29,7 @@
29
29
  "test-ci": "vitest run"
30
30
  },
31
31
  "dependencies": {
32
- "@javarome/fileutil": "^0.3.7",
32
+ "@javarome/fileutil": "^0.3.9",
33
33
  "@rr0/common": "^1.1.5",
34
34
  "@rr0/data": "^0.3.43",
35
35
  "@rr0/lang": "^0.1.13",
@@ -41,7 +41,7 @@
41
41
  "image-size": "^2.0.2",
42
42
  "jsdom": "^27.4.0",
43
43
  "selenium-webdriver": "^4.39.0",
44
- "ssg-api": "^1.18.2"
44
+ "ssg-api": "^1.18.3"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@types/jsdom": "^21.1.7",