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.

128 lines
3.2 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. function ind(_: number | undefined | null, str: string): string {
  2. return 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("");
  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 createTextNode(t: string): HtmlEnt {
  81. return new HtmlText(t);
  82. }
  83. static createElement<T>(fn: (props: T) => HtmlEnt, props: T, ...content: Content[]): HtmlEnt;
  84. static createElement(name: string, props: ElemProps, ...content: Content[]): HtmlEnt;
  85. static createElement<P, T extends JSXName<P>>(name: T, arg: T extends 'string' ? ElemProps : P, ...content: Content[]): HtmlEnt {
  86. if (typeof name !== 'string') {
  87. return name(arg);
  88. } else {
  89. const element = new HtmlElem(name);
  90. const props = (arg as { [id: string]: string | boolean }) || {};
  91. for (let name in props) {
  92. if (name && props.hasOwnProperty(name)) {
  93. let value = props[name];
  94. if (value === true) {
  95. element.setAttribute(name, name);
  96. } else if (value !== false && value != null) {
  97. element.setAttribute(name, value.toString());
  98. }
  99. }
  100. }
  101. for (let i = 0; i < content.length; i++) {
  102. let child = content[i];
  103. add(element, child);
  104. }
  105. return element;
  106. }
  107. }
  108. };
  109. export default JSX;