summaryrefslogtreecommitdiff
path: root/biblio.js
blob: 0e4daaef8840295f3fb8cddb859f817e384497fe (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
/**
 * Load the No.6092 datasheet into the OKCMS JSON
 */

import { loadJSON, loadCSV, writeJSON, writeFile } from "./file_utils.js";
import { readdir } from "fs/promises";
import parseRTF from "rtf-parser";
import fs from "fs";
import sizeOf from "image-size";

async function main() {
  const bib = {};
  await loadText("./data_store/biblio.rtf", bib);
  await writeFile("./biblio.html", bib.text);
}

/**
 * Load the text from an RTF
 */
async function loadText(path, bib) {
  // const warn = path.match("McCurry", "i");
  return new Promise((resolve, reject) => {
    parseRTF.stream(fs.createReadStream(path), (err, doc) => {
      // Separate paragraphs from spans since this library doesn't handle
      // the last paragraph correctly.
      const paragraphs = doc.content.filter((para) => para.content);
      const finalParagraph = doc.content.filter((para) => !para.content);
      let content = "";

      paragraphs.forEach((para, paragraphIndex) => {
        const paragraph = [];
        para.content.forEach((clip) => {
          appendClip(paragraph, clip);
        });
        const text = paragraph.join("");
        if (text) {
          content += "<p>\n" + text + "\n</p>\n\n";
        }
      });

      // The last paragraph is just spans for some reason
      const finalParagraphExtract = [];
      finalParagraph.forEach((clip) => {
        appendClip(finalParagraphExtract, clip);
      });
      if (finalParagraphExtract.length) {
        content += "<p>\n" + finalParagraphExtract.join("") + "\n</p>\n\n";
      }
      bib.text = content;
      resolve();
    });
  });
}

/**
 * Append a clip to a paragraph, adding formating (i.e. italics)
 */
function appendClip(paragraph, clip) {
  paragraph.push(getClipValue(clip));
}
function getClipValue(clip) {
  let value = clip.value;
  if (clip.style.italic) {
    value = "<i>" + value + "</i>";
  } else if (clip.style.bold) {
    value = "<b>" + value + "</b>";
  } else if (clip.style.underline) {
    value = "<u>" + value + "</u>";
  }
  return value;
}

/**
 * Load everything and then exit!
 */
main().then(() => process.exit(0));