converts irc logs to html files that look like discord
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.1 KiB

2 years ago
  1. function ind(x: number | undefined | null, str: string): string {
  2. return " ".repeat(x ?? 0) + str;
  3. };
  4. var tagsToReplace: Record<string, string> = {
  5. '&': '&amp;',
  6. '<': '&lt;',
  7. '>': '&gt;'
  8. };
  9. export function escapeHtmlShitty(x: string): string {
  10. return x.replace(/[&<>]/g, (tag) => tagsToReplace[tag] || tag);
  11. }
  12. export interface HtmlEnt {
  13. toHtmlStr(indent?: number): string;
  14. };
  15. class HtmlElem implements HtmlEnt {
  16. public name: string;
  17. public attrs: { [id: string]: string | boolean | undefined } = {};
  18. public content: HtmlEnt[] = [];
  19. constructor(n: string) {
  20. this.name = n;
  21. }
  22. setAttribute(id: string, val: string | boolean) {
  23. this.attrs[id] = val;
  24. }
  25. appendChild(...c: HtmlEnt[]) {
  26. this.content.push(...c);
  27. }
  28. toHtmlStr(indent?: number) {
  29. const out: string[] = [];
  30. const tag: string[] = [`<${this.name}`];
  31. for (const key of Object.keys(this.attrs)) {
  32. if (!this.attrs.hasOwnProperty(key)) continue;
  33. const x = this.attrs[key];
  34. if (x === undefined) continue;
  35. if (typeof x === "boolean" && !!x) {
  36. tag.push(key);
  37. } else {
  38. tag.push(`${key}="${x}"`);
  39. }
  40. }
  41. if (this.content.length === 0) {
  42. return ind(indent, tag.join(" ")) + " />";
  43. }
  44. out.push(ind(indent, tag.join(" ")) + '>');
  45. for (const e of this.content) {
  46. try {
  47. out.push(e.toHtmlStr((indent ?? 0) + 2));
  48. } catch(exc) {
  49. out.push(e.toString());
  50. }
  51. }
  52. out.push(ind(indent, `</${this.name}>`));
  53. return out.join("\n");
  54. }
  55. }
  56. class HtmlText implements HtmlEnt {
  57. public value: string;
  58. constructor(v: string) {
  59. this.value = v;
  60. }
  61. toHtmlStr(indent?: number) {
  62. return ind(indent, this.value);
  63. }
  64. }
  65. type Content = HtmlEnt | string | Content[] | undefined;
  66. const add = (element: HtmlElem, child: Content) => {
  67. if (typeof child === 'string') {
  68. element.appendChild(new HtmlText(child.toString()))
  69. } else if (child instanceof Array) {
  70. child.forEach((x) => add(element, x));
  71. } else if (child === undefined) {
  72. return;
  73. } else {
  74. element.appendChild(child);
  75. }
  76. };
  77. type JSXName<T> = string | ((props: T) => HtmlEnt);
  78. type ElemProps = { [id: string]: string | boolean }
  79. export class JSX {
  80. static createElement<T>(fn: (props: T) => HtmlEnt, props: T, ...content: Content[]): HtmlEnt;
  81. static createElement<P, T extends JSXName<P>>(name: T, arg: T extends 'string' ? ElemProps : P, ...content: Content[]): HtmlEnt {
  82. if (typeof name !== 'string') {
  83. return name(arg);
  84. } else {
  85. const element = new HtmlElem(name);
  86. const props = (arg as { [id: string]: string | boolean }) || {};
  87. for (let name in props) {
  88. if (name && props.hasOwnProperty(name)) {
  89. let value = props[name];
  90. if (value === true) {
  91. element.setAttribute(name, name);
  92. } else if (value !== false && value != null) {
  93. element.setAttribute(name, value.toString());
  94. }
  95. }
  96. }
  97. for (let i = 0; i < content.length; i++) {
  98. let child = content[i];
  99. add(element, child);
  100. }
  101. return element;
  102. }
  103. }
  104. };
  105. export default JSX;