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.

127 lines
3.2 KiB

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<P, T extends JSXName<P>>(name: T, arg: T extends 'string' ? ElemProps : P, ...content: Content[]): HtmlEnt {
  85. if (typeof name !== 'string') {
  86. return name(arg);
  87. } else {
  88. const element = new HtmlElem(name);
  89. const props = (arg as { [id: string]: string | boolean }) || {};
  90. for (let name in props) {
  91. if (name && props.hasOwnProperty(name)) {
  92. let value = props[name];
  93. if (value === true) {
  94. element.setAttribute(name, name);
  95. } else if (value !== false && value != null) {
  96. element.setAttribute(name, value.toString());
  97. }
  98. }
  99. }
  100. for (let i = 0; i < content.length; i++) {
  101. let child = content[i];
  102. add(element, child);
  103. }
  104. return element;
  105. }
  106. }
  107. };
  108. export default JSX;