Guest User

Untitled

a guest
Nov 15th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. "use strict";
  2.  
  3. Caesura.System.IO = function (args) {
  4. args = args || {};
  5. let hidden = Caesura.System.class(args, "Caesura.System.IO");
  6. let reveal = hidden.reveal;
  7.  
  8. reveal.outputMethod = undefined;
  9. reveal.defaultHTMLoutputElementId = args.defaultHTMLoutputElementId || "CaesuraIO";
  10.  
  11. reveal.newlineOptions = {
  12. None: { detector: undefined, value: "" },
  13. System: { detector: /[\n\r]/, value: "\n" },
  14. HTML: { detector: /<br \/>/, value: "<br id='newline' />" },
  15. };
  16.  
  17. reveal.indentationOptions = {
  18. None: { detector: undefined, value: "" },
  19. System: { detector: / /, value: " " },
  20. HTML: { detector: /&nbsp;/, value: "&nbsp;" },
  21. };
  22.  
  23. reveal.outputOptions = {
  24. None: function (str) { },
  25. System: function (str) {
  26. if (str === reveal.newlineOptions.System.value) return; // console.log already newlines for us.
  27. let out = str.toString();
  28. out = out.replace(new RegExp(reveal.indentationOptions.HTML.detector, 'g'), reveal.indentationOptions.System.value);
  29. if (Caesura.System.Backend.Platform == "Browser") {
  30. // for some reason, the browser's console.log ignores the first \n in a string.
  31. // this isn't a problem in desktop runtimes, so we need to account for that here.
  32. out += reveal.newlineOptions.System.value;
  33. }
  34. console.log(out);
  35. },
  36. HTML: function (str) {
  37. if (str === null || str === undefined) return;
  38. let out = str.toString();
  39. out = out.replace(new RegExp(reveal.newlineOptions.System.detector, 'g'), reveal.newlineOptions.HTML.value);
  40. document.getElementById(reveal.defaultHTMLoutputElementId).innerHTML += out;
  41. },
  42. Browser: function (str) {
  43. reveal.outputOptions.System(str);
  44. reveal.outputOptions.HTML(str);
  45. }
  46. // TODO: runtime option that uses native runtime output. console.log won't cut it, automatically appends a newline.
  47. }
  48.  
  49. if (args.outputMethod instanceof Function) {
  50. reveal.outputMethod = args.outputMethod;
  51. } else if (args.outputGenerator instanceof Function) {
  52. reveal.outputMethod = args.outputGenerator(reveal);
  53. } else if (args.Browser) {
  54. reveal.outputMethod = reveal.outputOptions.Browser;
  55. } else if (args.System) {
  56. reveal.outputMethod = reveal.outputOptions.System;
  57. } else if (args.HTML) {
  58. reveal.outputMethod = reveal.outputOptions.HTML;
  59. } else if (Caesura.System.Backend.Platform == "Browser" && !args.noAutomaticDefaultOptions) {
  60. reveal.outputMethod = reveal.outputOptions.Browser;
  61. } else if (Caesura.System.Backend.Platform == "Runtime" && !args.noAutomaticDefaultOptions) {
  62. reveal.outputMethod = reveal.outputOptions.System;
  63. }
  64.  
  65. reveal.write = function (str) {
  66. hidden.raw_write(str, arguments)
  67. }
  68.  
  69. reveal.writeln = function (str) {
  70. if (str !== undefined) {
  71. hidden.raw_write(str, arguments);
  72. }
  73. hidden.raw_write(reveal.newlineOptions.System.value);
  74. }
  75.  
  76. hidden.raw_write = function (str, rest) {
  77. let out = str;
  78. // TODO: check if there are not an equal amount of {x} elements
  79. // than arguments, and throw an exception.
  80. if (rest !== undefined && rest.length > 0) {
  81. for (let i = 1; i < rest.length; i++) {
  82. out = out.replace(new RegExp("\{?(" + (i - 1) + ")\}", 'g'), rest[i]);
  83. }
  84. }
  85.  
  86. let regex = /\{s:(\d+)\}/g;
  87. let match;
  88. while ((match = regex.exec(out)) != null) {
  89. out = out.replace(/\{s:(\d+)\}/g, reveal.indentationOptions.HTML.value.repeat(Number(match[1])));
  90. }
  91. reveal.outputMethod(out);
  92. }
  93.  
  94. return reveal;
  95. }
Add Comment
Please, Sign In to add comment