aboutsummaryrefslogtreecommitdiffhomepage
path: root/nuldoc-src/slide/slide.ts
blob: a982d4f25c42e599c3bf265b005d517938996c78 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { Config } from "../config.ts";
import { SlideError } from "../errors.ts";
import { Revision, stringToDate } from "../revision.ts";
import {
  Element,
  findChildElements,
  findFirstChildElement,
  innerText,
} from "../dom.ts";

export type Slide = {
  sourceFilePath: string;
  title: string;
  event: string;
  talkType: string;
  slideLink: string;
  tags: string[];
  revisions: Revision[];
};

export function createNewSlideFromRootElement(
  root: Element,
  sourceFilePath: string,
  _config: Config,
): Slide {
  const slide = findFirstChildElement(root, "slide");
  if (!slide) {
    throw new SlideError(
      `[slide.new] <slide> element not found`,
    );
  }
  const info = findFirstChildElement(slide, "info");
  if (!info) {
    throw new SlideError(
      `[slide.new] <info> element not found`,
    );
  }

  const titleElement = findFirstChildElement(info, "title");
  if (!titleElement) {
    throw new SlideError(
      `[slide.new] <title> element not found`,
    );
  }
  const title = innerText(titleElement).trim();

  const eventElement = findFirstChildElement(info, "event");
  if (!eventElement) {
    throw new SlideError(
      `[slide.new] <event> element not found`,
    );
  }
  const event = innerText(eventElement).trim();

  const talkTypeElement = findFirstChildElement(info, "talktype");
  if (!talkTypeElement) {
    throw new SlideError(
      `[slide.new] <talktype> element not found`,
    );
  }
  const talkType = innerText(talkTypeElement).trim();

  const slideLinkElement = findFirstChildElement(info, "link");
  if (!slideLinkElement) {
    throw new SlideError(
      `[slide.new] <link> element not found`,
    );
  }
  const slideLink = innerText(slideLinkElement).trim();

  const keywordsetElement = findFirstChildElement(info, "keywordset");
  let tags: string[];
  if (!keywordsetElement) {
    tags = [];
  } else {
    tags = findChildElements(keywordsetElement, "keyword").map((x) =>
      innerText(x).trim()
    );
  }
  const revhistoryElement = findFirstChildElement(info, "revhistory");
  if (!revhistoryElement) {
    throw new SlideError(
      `[slide.new] <revhistory> element not found`,
    );
  }
  const revisions = findChildElements(revhistoryElement, "revision").map(
    (x, i) => {
      const dateElement = findFirstChildElement(x, "date");
      if (!dateElement) {
        throw new SlideError(
          `[slide.new] <date> element not found`,
        );
      }
      const revremarkElement = findFirstChildElement(x, "revremark");
      if (!revremarkElement) {
        throw new SlideError(
          `[slide.new] <revremark> element not found`,
        );
      }
      return {
        number: i + 1,
        date: stringToDate(innerText(dateElement).trim()),
        remark: innerText(revremarkElement).trim(),
      };
    },
  );
  if (revisions.length === 0) {
    throw new SlideError(
      `[slide.new] <revision> element not found`,
    );
  }

  return {
    sourceFilePath: sourceFilePath,
    title: title,
    event: event,
    talkType: talkType,
    slideLink: slideLink,
    tags: tags,
    revisions: revisions,
  };
}