summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/nuldoc-src/ndoc/parse.ts
blob: 7c33c414acf032d553a0b0ad32d110070ce2976b (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
import { parse as parseToml } from "std/encoding/toml.ts";
import { Config } from "../config.ts";
import { parseXmlString } from "../xml.ts";
import { createNewDocumentFromRootElement, Document } from "./document.ts";
import toHtml from "./to_html.ts";

export async function parseNulDocFile(
  filePath: string,
  config: Config,
): Promise<Document> {
  try {
    const fileContent = await Deno.readTextFile(filePath);
    const parts = fileContent.split(/^---$/m);
    const meta = parseMetaInfo(parts[1]);
    const root = parseXmlString("<?xml ?>" + parts[2]);
    const doc = createNewDocumentFromRootElement(root, meta, filePath, config);
    return toHtml(doc);
  } catch (e) {
    e.message = `${e.message} in ${filePath}`;
    throw e;
  }
}

function parseMetaInfo(s: string): {
  article: {
    uuid: string;
    title: string;
    description: string;
    tags: string[];
    revisions: {
      date: string;
      remark: string;
    }[];
  };
} {
  const root = parseToml(s) as {
    article: {
      uuid: string;
      title: string;
      description: string;
      tags: string[];
      revisions: {
        date: string;
        remark: string;
      }[];
    };
  };
  return root;
}