feat: add typescript lib, add daletl parser, add html converter

This commit is contained in:
Artemy Egorov 2024-07-24 23:48:18 +03:00
parent dfa42ae830
commit bc6b6a7db4
10 changed files with 224 additions and 0 deletions

View file

@ -0,0 +1,51 @@
import { Root, Tag } from "../daletl/types";
interface Props {
[key: string]: string;
}
function hy(tag: string, body?: string, props?: Props) {
return `<${tag} ${
props
? Object.entries(props)
.map(([key, value]) => `${key}="${value}"`)
.join(" ")
: ""
}>${body}</${tag}>`;
}
function comp(
name: string,
body: string | Tag[],
classNames: string,
classes: boolean,
props: Props = {}
): string {
let realBody = Array.isArray(body)
? body.map((b) => htmlTag(b, classes)).join("")
: body;
return hy(name, realBody, {
class: classes ? classNames : undefined,
...props,
});
}
function element(tag: Tag, classes: boolean): string {
return comp("section", tag.body, "el", classes);
}
function heading(tag: Tag, classes: boolean): string {
if (typeof tag.argument !== "number") {
return comp("h1", tag.body, "h", classes);
}
return comp(`h${tag.argument}`, tag.body, `h hl${tag.argument}`, classes);
}
function htmlTag(tag: Tag, classes: boolean): string {
return [element, heading][tag.id](tag, classes);
}
export function toHtml(root: Root, classes: boolean = true): string {
return root.map((t) => htmlTag(t, classes)).join("");
}

View file

@ -0,0 +1 @@
export { toHtml } from "./html";

View file

@ -0,0 +1,64 @@
import { encode, decode } from "@msgpack/msgpack";
import { ParseError, Root, Tag } from "./types";
export function parseRawTag(raw_tag: any[] | string): Tag {
if (typeof raw_tag === "string") {
return {
id: 0,
body: raw_tag,
argument: null,
};
}
if (Array.isArray(raw_tag)) {
if (Array.isArray(raw_tag[0])) {
return {
id: 0,
body: parseRawTagBody(raw_tag),
argument: null,
};
}
if (typeof raw_tag[0] === "number") {
return {
id: raw_tag[0],
body: parseRawTagBody(raw_tag[1]),
argument: raw_tag[3] || null,
};
}
}
throw new ParseError("Invalid tag");
}
export function parseRawTagBody(body: string | null | any[]): Tag["body"] {
if (typeof body === "string") {
return body;
}
if (body === null) {
return null;
}
if (Array.isArray(body)) {
if (Array.isArray(body[0])) {
return body.map(parseRawTag);
}
if (typeof body[0] === "number") {
return [parseRawTag(body)];
}
}
throw new ParseError("Invalid tag body");
}
export function parse(root_data?: Uint8Array): Root {
let root = root_data ? decode(root_data) : [];
if (!Array.isArray(root)) {
throw new ParseError("Daletl root must be array");
}
return root.map(parseRawTag);
}

View file

@ -0,0 +1,14 @@
export interface Tag {
id: number;
body: string | null | Tag[];
argument: number | string | null;
}
export type Root = Tag[];
export class ParseError extends Error {
constructor(message: string = "Parse error") {
super(message);
this.name = "ParseError";
}
}

View file

@ -0,0 +1,2 @@
export { parse } from "./daletl/main";
export * as converters from "./converters/main";