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.

263 lines
5.9 KiB

  1. package main
  2. // html templates are here instead of an .html file to avoid depending on external files
  3. // in this way, everything is inside the binary
  4. const dirTemplate = `
  5. <!DOCTYPE html>
  6. <html>
  7. <title>{{.Title}}</title>
  8. <style>
  9. body {
  10. font-family: Arial, Helvetica, sans-serif;
  11. }
  12. .dark {
  13. background:#1d2021;
  14. color:#cccccc;
  15. }
  16. .dark a, a:hover, a:visited {
  17. color: #458588;
  18. }
  19. </style>
  20. <body class="dark">
  21. <input onclick="switchThemeClick()" type="checkbox" id="themeSwitcher" style="float:right;">
  22. {{.Content}}
  23. <script>
  24. let theme = localStorage.getItem("theme");
  25. if (theme === "light") {
  26. document.getElementById("themeSwitcher").checked = false;
  27. document.body.className = theme;
  28. }
  29. function switchThemeClick() {
  30. theme = localStorage.getItem("theme");
  31. if (theme === "light") {
  32. document.getElementById("themeSwitcher").checked = true;
  33. theme = "dark";
  34. localStorage.setItem("theme", theme);
  35. } else {
  36. document.getElementById("themeSwitcher").checked = false;
  37. theme = "light";
  38. localStorage.setItem("theme", theme);
  39. }
  40. document.body.className = theme;
  41. }
  42. </script>
  43. </body>
  44. </html>
  45. `
  46. const htmlTemplate = `
  47. <!DOCTYPE html>
  48. <html>
  49. <title>{{.Title}}</title>
  50. <style>
  51. body {
  52. font-family: Arial, Helvetica, sans-serif;
  53. }
  54. .dark {
  55. background:#1d2021;
  56. color:#cccccc;
  57. }
  58. a, a:hover, a:visited {
  59. color: #458588;
  60. }
  61. pre, code{
  62. padding-left: 3px;
  63. padding-right: 3px;
  64. border-radius: 3px;
  65. background: #cccccc;
  66. }
  67. .dark pre, .dark code{
  68. padding-left: 3px;
  69. padding-right: 3px;
  70. border-radius: 3px;
  71. background: #333333;
  72. }
  73. pre{
  74. padding: 5px;
  75. }
  76. pre>code {
  77. color: #000000;
  78. }
  79. .dark pre>code {
  80. color: #c0fffa;
  81. }
  82. h1:after{
  83. content:' ';
  84. display:block;
  85. border:1px solid #cccccc;
  86. }
  87. h2:after{
  88. content:' ';
  89. display:block;
  90. border:0.7px solid #cccccc;
  91. }
  92. .dark th{
  93. padding: 3px;
  94. border: 1px solid #234b4f;
  95. background-color: #143134;
  96. }
  97. td{
  98. padding: 3px;
  99. border: 1px solid #234b4f;
  100. }
  101. .container {
  102. width: 100%;
  103. padding-right: 1rem;
  104. padding-left: 1rem;
  105. margin-right: auto;
  106. margin-left: auto;
  107. }
  108. @media (min-width: 576px) {
  109. .container {
  110. max-width: 540px;
  111. }
  112. }
  113. @media (min-width: 768px) {
  114. .container {
  115. max-width: 720px;
  116. }
  117. }
  118. @media (min-width: 992px) {
  119. .container {
  120. max-width: 800px;
  121. }
  122. }
  123. @media (min-width: 1200px) {
  124. .container {
  125. max-width: 800px;
  126. }
  127. }
  128. @media (min-width: 1400px) {
  129. .container {
  130. max-width: 800px;
  131. }
  132. }
  133. </style>
  134. <body class="dark">
  135. <input onclick="switchThemeClick()" type="checkbox" id="themeSwitcher" style="float:right;">
  136. <a href="/" title="go to root">root</a>
  137. <div id="content" class="container">
  138. {{.Content}}
  139. </div>
  140. <!-- TODO: make all the imports locally, to allow working offline -->
  141. <!-- LaTex renderization -->
  142. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.13.11/dist/katex.min.css" integrity="sha384-Um5gpz1odJg5Z4HAmzPtgZKdTBHZdw8S29IecapCSB31ligYPhHQZMIlWLYQGVoc" crossorigin="anonymous">
  143. <!-- The loading of KaTeX is deferred to speed up page rendering -->
  144. <script defer src="https://cdn.jsdelivr.net/npm/katex@0.13.11/dist/katex.min.js" integrity="sha384-YNHdsYkH6gMx9y3mRkmcJ2mFUjTd0qNQQvY9VYZgQd7DcN7env35GzlmFaZ23JGp" crossorigin="anonymous"></script>
  145. <!-- To automatically render math in text elements, include the auto-render extension: -->
  146. <script defer src="https://cdn.jsdelivr.net/npm/katex@0.13.11/dist/contrib/auto-render.min.js" integrity="sha384-vZTG03m+2yp6N6BNi5iM4rW4oIwk5DfcNdFfxkk9ZWpDriOkXX8voJBFrAO7MpVl" crossorigin="anonymous"></script>
  147. <!-- Mermaidjs -->
  148. <script type="module">
  149. import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
  150. </script>
  151. <script>
  152. document.addEventListener("DOMContentLoaded", function() {
  153. renderMathInElement(document.body, {
  154. // customised options
  155. // • auto-render specific keys, e.g.:
  156. delimiters: [
  157. {left: '$$', right: '$$', display: true},
  158. {left: '$', right: '$', display: true},
  159. {left: '\\(', right: '\\)', display: false},
  160. {left: '\\[', right: '\\]', display: true}
  161. ],
  162. // • rendering keys, e.g.:
  163. throwOnError : false
  164. });
  165. });
  166. </script>
  167. <script>
  168. (function() {
  169. var conn = new WebSocket("ws://127.0.0.1:8080/ws/{{.Title}}");
  170. conn.onclose = function(evt) {
  171. console.log('Connection closed');
  172. alert('Connection closed');
  173. }
  174. conn.onmessage = function(evt) {
  175. console.log('file updated');
  176. // location.reload();
  177. // console.log("evt", evt);
  178. document.getElementById("content").innerHTML = evt.data;
  179. renderMathInElement(document.body, {
  180. // customised options
  181. // • auto-render specific keys, e.g.:
  182. delimiters: [
  183. {left: '$$', right: '$$', display: true},
  184. {left: '$', right: '$', display: false},
  185. {left: '\\(', right: '\\)', display: false},
  186. {left: '\\[', right: '\\]', display: true}
  187. ],
  188. // • rendering keys, e.g.:
  189. throwOnError : false
  190. });
  191. }
  192. })();
  193. let theme = localStorage.getItem("theme");
  194. if (theme === "light") {
  195. document.getElementById("themeSwitcher").checked = false;
  196. document.body.className = theme;
  197. }
  198. function switchThemeClick() {
  199. theme = localStorage.getItem("theme");
  200. if (theme === "light") {
  201. document.getElementById("themeSwitcher").checked = true;
  202. theme = "dark";
  203. localStorage.setItem("theme", theme);
  204. } else {
  205. document.getElementById("themeSwitcher").checked = false;
  206. theme = "light";
  207. localStorage.setItem("theme", theme);
  208. }
  209. document.body.className = theme;
  210. }
  211. </script>
  212. </body>
  213. </html>
  214. `
  215. const errTemplate = `
  216. <!DOCTYPE html>
  217. <html>
  218. <title>{{.Title}}</title>
  219. <style>
  220. body {
  221. font-family: Arial, Helvetica, sans-serif;
  222. background:#000000;
  223. color:#cccccc;
  224. }
  225. </style>
  226. <body>
  227. <a href="/" title="go to root">root</a>
  228. <h3>File doesn't exist</h3>
  229. <br><br>
  230. <a href="/">Back to dir menu</a>
  231. </body>
  232. </html>
  233. `