Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 29.83 KB | None | 0 0
  1. //// ////////////////////////////////////////////////////// /////
  2. // Algotype.js, version 1.61 by Rodion "(code)rodde" Efremov //
  3. /////////////////////////////////////////////////////////////////
  4.  
  5. var Algotype = {};
  6.  
  7. // The string beginning the comments of the algorithm declaration.
  8. Algotype.ALGORITHM_HEADER_COMMENT_TAG = "#";
  9.  
  10. // The string beginning the step comments.
  11. Algotype.ALGORITHM_STEP_COMMENT_TAG = "#";
  12.  
  13. // The width of code line numbers. This default works well. If you, however,
  14. // need to typeset an algorithm with at least 100 rows (in which case the space
  15. // is tight) just increase this constant.
  16. Algotype.LINE_NUMBER_WIDTH = 25;
  17.  
  18. // The indentation in pixels.
  19. Algotype.INDENTATION_WIDTH = 30;
  20.  
  21. // Number of pixels between the line number span and the pseudocode span.
  22. Algotype.DISTANCE_BETWEEN_LINE_NUMBER_AND_CODE = 8;
  23.  
  24. // The URL from which to download the MathJax math typesetting facilities.
  25. Algotype.MATHJAX_SCRIPT_URL =
  26. "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_CHTML";
  27.  
  28. // Configuration for MathJax. This provides in the pseudocode macros for such
  29. // keywords as 'and', 'or', 'not', and others, with the font that matches the
  30. // actual keywords, such as 'for each', 'repeat ... until', etc.
  31. Algotype.MATHJAX_CONFIG =
  32. 'MathJax.Hub.Config({' +
  33. "tex2jax: {inlineMath: [['$','$']]}," +
  34. 'TeX: {' +
  35. 'Macros: {' +
  36. 'And: "\\mathbf{and}",' +
  37. 'Or: "\\mathbf{or}",' +
  38. 'Not: "\\mathbf{not}",' +
  39. 'Is: "\\mathbf{is}",' +
  40. 'In: "\\mathbf{in}",' +
  41. 'Mapped: "\\mathbf{mapped}",' +
  42. 'Nil: "\\mathbf{nil}"' +
  43. '}' +
  44. '}' +
  45. '});';
  46.  
  47. Algotype.MATHJAX_CONFIG_MIME_TYPE = "text/x-mathjax-config";
  48.  
  49. Algotype.UNNAMED_ALGORITHM = "UnnamedAlgorithm";
  50.  
  51. Algotype.loadMathJax = function() {
  52. // Load the MathJax.
  53. var importedScript = document.createElement("script");
  54. importedScript.async = "true";
  55. importedScript.src = Algotype.MATHJAX_SCRIPT_URL;
  56. document.head.appendChild(importedScript);
  57.  
  58. // Make MathJax process the configuration.
  59. var mathJaxSettingsScript = document.createElement("script");
  60. mathJaxSettingsScript.type = Algotype.MATHJAX_CONFIG_MIME_TYPE;
  61. mathJaxSettingsScript.innerHTML = Algotype.MATHJAX_CONFIG;
  62. document.head.appendChild(mathJaxSettingsScript);
  63. };
  64.  
  65. Algotype.getAlgorithmHeaderComment = function (algorithmElement) {
  66. var algorithmHeaderComment =
  67. algorithmElement.getAttribute("comment");
  68.  
  69. if (!algorithmHeaderComment) {
  70. return "";
  71. }
  72.  
  73. return " " +
  74. Algotype.ALGORITHM_HEADER_COMMENT_TAG +
  75. " " + algorithmHeaderComment;
  76. };
  77.  
  78. Algotype.getAlgorithmParameterList = function(algorithmElement) {
  79. var algorithmParameterList =
  80. algorithmElement.getAttribute("parameters") || "";
  81.  
  82. algorithmParameterList = algorithmParameterList.trim();
  83.  
  84. if (!algorithmParameterList) {
  85. return "$()$";
  86. }
  87.  
  88. // Remove the beginning parenthesis, if present.
  89. if (algorithmParameterList[0] === "(") {
  90. algorithmParameterList =
  91. algorithmParameterList.substring(1,
  92. algorithmParameterList.length);
  93. }
  94.  
  95. // Remove the ending parenthesis, if present.
  96. if (algorithmParameterList[algorithmParameterList.length - 1] === ")") {
  97. algorithmParameterList =
  98. algorithmParameterList
  99. .substring(0, algorithmParameterList.length - 1);
  100. }
  101.  
  102. // Remove possible leading and trailing space within the parentheses.
  103. algorithmParameterList = algorithmParameterList.trim();
  104.  
  105. // Split the string into parameter tokens.
  106. var algorithmParameters = algorithmParameterList.split(/s*,s*|s+/);
  107.  
  108. // Construct the TeX for the algorithm parameter list.
  109. var tex = "$(";
  110. var separator = "";
  111.  
  112. for (var i = 0; i < algorithmParameters.length; ++i) {
  113. tex += separator;
  114. tex += algorithmParameters[i];
  115. separator = ", ";
  116. }
  117.  
  118. return tex + ")$";
  119. };
  120.  
  121. Algotype.getLabelHtml = function(state, label) {
  122. return "<table class='algotype-code-row-table'>n" +
  123. " <tbody class='algotype-code-row-tbody'n" +
  124. " <tr class='algotype-algorithm-line'>n" +
  125. " <td class='algotype-algorithm-line-number'></td>n" +
  126. " <td class='algotype-line-number-space' width='" +
  127. (Algotype.INDENTATION_WIDTH * state["indentation"] +
  128. Algotype.DISTANCE_BETWEEN_LINE_NUMBER_AND_CODE) +
  129. "px'></td>n" +
  130. " <td class='algotype-label algotype-text'>" + label +
  131. "</td>n" +
  132. " </tr>n" +
  133. " </tbody>n" +
  134. "</table>n";
  135. };
  136.  
  137. Algotype.typesetConditionalBranch = function(element, state, keyword) {
  138. var conditionTeX =
  139. Algotype.typesetCondition(element.getAttribute("condition"));
  140.  
  141. var htmlText = "";
  142. var comment = element.getAttribute("comment");
  143. var commentId = (element.getAttribute("comment-id") || "").trim();
  144. var idText = "";
  145.  
  146. if (commentId) {
  147. idText = " id='" + commentId + "' ";
  148. }
  149.  
  150. if (comment) {
  151. comment = " <span class='algotype-step-comment' " + idText + ">" +
  152. Algotype.ALGORITHM_STEP_COMMENT_TAG + " " +
  153. comment.trim() + "</span>";
  154. }
  155.  
  156. var id = (element.getAttribute("id") || "").trim();
  157. var idTextBegin = "";
  158. var idTextEnd = "";
  159.  
  160. if (id) {
  161. idTextBegin = "<span id='" + id + "'>";
  162. idTextEnd = "</span>";
  163. }
  164.  
  165. htmlText += "<table class='algotype-code-row-table'>n" +
  166. " <tbody class='algotype-code-row-tbody'>n" +
  167. " <tr class='algotype-algorithm-line'>n" +
  168. " <td class='algotype-algorithm-line-number'>" +
  169. state["lineNumber"] +
  170. " </td> " +
  171. " <td class='algotype-line-number-space' width='" +
  172. (Algotype.INDENTATION_WIDTH * state["indentation"] +
  173. Algotype.DISTANCE_BETWEEN_LINE_NUMBER_AND_CODE) +
  174. "px'></td>n" +
  175. " <td class='algotype-text algotype-keyword'>" +
  176. idTextBegin + keyword + " " +
  177. conditionTeX + " then" + idTextEnd +
  178. (comment ? comment : "") +
  179. " </td> " +
  180. " </tr>n" +
  181. " </tbody>n" +
  182. "</table>n";
  183.  
  184. var saveIndentation = state["indentation"];
  185. state["lineNumber"]++;
  186. state["indentation"]++;
  187. htmlText += Algotype.processInnerElements(element, state);
  188.  
  189. // Reset the indentation counter.
  190. state["indentation"] = saveIndentation;
  191. return htmlText;
  192. };
  193.  
  194. Algotype.typesetIf = function(ifElement, state) {
  195. return Algotype.typesetConditionalBranch(ifElement, state, "if");
  196. };
  197.  
  198. Algotype.typesetElseIf = function(elseIfElement, state) {
  199. return Algotype.typesetConditionalBranch(elseIfElement, state, "else if");
  200. };
  201.  
  202. Algotype.typesetElse = function(elseElement, state) {
  203. return Algotype.typesetUnconditionalBlock(elseElement, state, "else");
  204. };
  205.  
  206. Algotype.typesetCondition = function(conditionText) {
  207. if (!conditionText) {
  208. return "";
  209. }
  210.  
  211. conditionText = conditionText.trim();
  212.  
  213. if (!conditionText) {
  214. return "";
  215. }
  216.  
  217. var inTeX = false;
  218. var htmlText = "";
  219. var call = "";
  220.  
  221. for (var i = 0; i < conditionText.length; ++i) {
  222. var character = conditionText[i];
  223.  
  224. switch (character) {
  225. case '$':
  226. if (!inTeX) {
  227. if (call) {
  228. // Dump the current call.
  229. htmlText +=
  230. " <span " +
  231. "class='algotype-text algotype-algorithm-name'>" +
  232. call +
  233. "</span>";
  234.  
  235. call = "";
  236. }
  237.  
  238. inTeX = true;
  239. } else {
  240. inTeX = false;
  241. }
  242.  
  243. htmlText += "$";
  244. break;
  245.  
  246. default:
  247.  
  248. if (inTeX) {
  249. htmlText += character;
  250. } else {
  251. call += character;
  252. }
  253. }
  254. }
  255.  
  256. if (call) {
  257. htmlText += " <span class='algotype-text algotype-algorithm-name'>" +
  258. call +
  259. "</span>";
  260. }
  261.  
  262. return htmlText;
  263. };
  264.  
  265. Algotype.typesetStep = function(stepElement, state, keyword) {
  266. if (!keyword) {
  267. keyword = "";
  268. }
  269.  
  270. var htmlText = Algotype.typesetCondition(stepElement.innerHTML || "");
  271. var comment = stepElement.getAttribute("comment") || "";
  272. var commentId = (stepElement.getAttribute("comment-id") || "").trim();
  273. var idText = "";
  274.  
  275. if (commentId) {
  276. idText = " id='" + commentId + "'";
  277. }
  278.  
  279. if (comment) {
  280. comment = " <span class='algotype-step-comment'" + idText + ">" +
  281. Algotype.ALGORITHM_STEP_COMMENT_TAG + " " +
  282. comment.trim() + "</span>";
  283. }
  284.  
  285. var keywordHtml = "<span class='algotype-text algotype-keyword'>" +
  286. keyword +
  287. "</span>";
  288.  
  289. var stepId = (stepElement.getAttribute("id") || "").trim();
  290. var stepIdTextBegin = "";
  291. var stepIdTextEnd = "";
  292.  
  293. if (stepId) {
  294. stepIdTextBegin = "<span id='" + stepId + "'>";
  295. stepIdTextEnd = "</span>";
  296. }
  297.  
  298. htmlText = "<table class='algotype-code-row-table'>n" +
  299. " <tbody class='algotype-code-row-tbody'>n" +
  300. " <tr class='algotype-algorithm-line'>n" +
  301. " <td class='algotype-algorithm-line-number'>" +
  302. state["lineNumber"] +
  303. "</td> " +
  304. "<td class='algotype-line-number-space' width='" +
  305. (Algotype.INDENTATION_WIDTH * state["indentation"] +
  306. Algotype.DISTANCE_BETWEEN_LINE_NUMBER_AND_CODE) +
  307. "px'></td>" +
  308. "<td class='algotype-text'>" +
  309. stepIdTextBegin + keywordHtml + " " + htmlText + stepIdTextEnd +
  310. comment +
  311. "</td>n" +
  312. " </tr>n" +
  313. " </tbody>n" +
  314. "</table>n";
  315.  
  316. state["lineNumber"]++;
  317. return htmlText;
  318. };
  319.  
  320. Algotype.typesetReturn = function(returnElement, state) {
  321. return Algotype.typesetStep(returnElement, state, "return");
  322. };
  323.  
  324. Algotype.typesetPrint = function(printElement, state) {
  325. return Algotype.typesetStep(printElement, state, "print");
  326. };
  327.  
  328. Algotype.typesetOutput = function(outputElement, state) {
  329. return Algotype.typesetStep(outputElement, state, "output");
  330. };
  331.  
  332. Algotype.typesetYield = function(yieldElement, state) {
  333. return Algotype.typesetStep(yieldElement, state, "yield");
  334. };
  335.  
  336. Algotype.typesetLabelControl = function(element, state, keyword) {
  337. var comment = element.getAttribute("comment") || "";
  338. var commentId = (element.getAttribute("comment-id") || "").trim();
  339. var idText = "";
  340.  
  341. if (commentId) {
  342. idText = " id='" + commentId + "'";
  343. }
  344.  
  345. if (comment) {
  346. comment = " <span class='algotype-step-comment'" + idText + ">" +
  347. Algotype.ALGORITHM_STEP_COMMENT_TAG + " " +
  348. comment.trim() + "</span>";
  349. }
  350.  
  351. var label = element.innerHTML;
  352. var elementId = (element.getAttribute("id") || "").trim();
  353. var elementIdTextBegin = "";
  354. var elementIdTextEnd = "";
  355.  
  356. if (elementId) {
  357. elementIdTextBegin = "<span id='" + elementId + "'>";
  358. elementIdTextEnd = "</span>";
  359. }
  360.  
  361. var htmlText =
  362. "<table class='algotype-code-row-table'>n" +
  363. " <tbody class='algotype-code-row-tbody'>n" +
  364. " <tr class='algotype-algorithm-line'>n" +
  365. " <td class='algotype-algorithm-line-number'>" +
  366. state["lineNumber"] + "</td>n" +
  367. " <td class='algotype-line-number-space' width='" +
  368. (Algotype.INDENTATION_WIDTH * state["indentation"] +
  369. Algotype.DISTANCE_BETWEEN_LINE_NUMBER_AND_CODE) +
  370. "px'></td>n" +
  371. " <td class='algotype-text algotype-keyword'>" +
  372. elementIdTextBegin + keyword + " " +
  373. (label ? "<span class='algotype-label'>" + label + "</span>" : "") +
  374. elementIdTextEnd + comment +
  375. "</td>n" +
  376. " </tr>n" +
  377. " </tbody>n" +
  378. "</table>n";
  379.  
  380. state["lineNumber"]++;
  381. return htmlText;
  382.  
  383. };
  384.  
  385. Algotype.typesetBreak = function(breakElement, state) {
  386. return Algotype.typesetLabelControl(breakElement, state, "break");
  387. };
  388.  
  389. Algotype.typesetContinue = function(continueElement, state) {
  390. return Algotype.typesetLabelControl(continueElement, state, "continue");
  391. };
  392.  
  393. Algotype.typesetConditionalLoop = function(element, state, keyword) {
  394. var conditionTeX =
  395. Algotype.typesetCondition(element.getAttribute("condition"));
  396.  
  397. if (conditionTeX[0] !== "$") {
  398. conditionTeX = "$" + conditionTeX;
  399. }
  400.  
  401. if (conditionTeX[conditionTeX.length - 1] !== "$") {
  402. conditionTeX += "$";
  403. }
  404.  
  405. var label = element.getAttribute("label");
  406. var htmlText = "";
  407. var comment = element.getAttribute("comment");
  408. var commentId = (element.getAttribute("comment-id") || "").trim();
  409. var idText = "";
  410.  
  411. if (commentId) {
  412. idText = " id='" + commentId + "' ";
  413. }
  414.  
  415. if (comment) {
  416. comment = " <span class='algotype-step-comment' " + idText + ">" +
  417. Algotype.ALGORITHM_STEP_COMMENT_TAG + " " +
  418. comment.trim() + "</span>";
  419. }
  420.  
  421. if (label) {
  422. label = label.trim();
  423.  
  424. if (label[label.length - 1] !== ":") {
  425. label += ":";
  426. }
  427.  
  428. htmlText += Algotype.getLabelHtml(state, label);
  429. }
  430.  
  431. var id = (element.getAttribute("id") || "").trim();
  432. var idTextBegin = "";
  433. var idTextEnd = "";
  434.  
  435. if (id) {
  436. idTextBegin = "<span id='" + id + "'>";
  437. idTextEnd = "</span>";
  438. }
  439.  
  440. htmlText += "<table class='algotype-code-row-table'>n" +
  441. " <tbody class='algotype-code-row-tbody'>n" +
  442. " <tr class='algotype-algorithm-line'>n" +
  443. " <td class='algotype-algorithm-line-number'>" +
  444. state["lineNumber"] +
  445. " </td> " +
  446. " <td class='algotype-line-number-space' width='" +
  447. (Algotype.INDENTATION_WIDTH * state["indentation"] +
  448. Algotype.DISTANCE_BETWEEN_LINE_NUMBER_AND_CODE) +
  449. "px'></td>n" +
  450. " <td class='algotype-text algotype-keyword'>" +
  451. idTextBegin + keyword + " " +
  452. conditionTeX + ":" + idTextEnd +
  453. (comment ? comment : "") +
  454. " </td> " +
  455. " </tr>n" +
  456. " </tbody>n" +
  457. "</table>n";
  458.  
  459. var saveIndentation = state["indentation"];
  460. state["lineNumber"]++;
  461. state["indentation"]++;
  462. htmlText += Algotype.processInnerElements(element, state);
  463.  
  464. // Reset the indentation counter.
  465. state["indentation"] = saveIndentation;
  466. return htmlText;
  467. };
  468.  
  469. Algotype.typesetForEach = function(forEachElement, state) {
  470. return Algotype.typesetConditionalLoop(forEachElement, state, "for each");
  471. };
  472.  
  473. Algotype.typesetWhile = function(whileElement, state) {
  474. return Algotype.typesetConditionalLoop(whileElement, state, "while");
  475. };
  476.  
  477. Algotype.typesetCountingLoop = function(element,
  478. state,
  479. fromKeyword,
  480. toKeyword,
  481. stepKeyword) {
  482. var initConditionTeX =
  483. Algotype.typesetCondition(element.getAttribute("init"));
  484.  
  485. var toConditionTeX =
  486. Algotype.typesetCondition(element.getAttribute("to"));
  487.  
  488. var stepConditionTeX =
  489. Algotype.typesetCondition(element.getAttribute("step"));
  490.  
  491. var label = element.getAttribute("label");
  492. var htmlText = "";
  493. var comment = element.getAttribute("comment");
  494. var commentId = (element.getAttribute("comment-id") || "").trim();
  495. var idText = "";
  496. var stepText = "";
  497.  
  498. if (stepConditionTeX) {
  499. stepText = " " + stepKeyword + " " + stepConditionTeX;
  500. }
  501.  
  502. if (commentId) {
  503. idText = "id='" + commentId + "' ";
  504. }
  505.  
  506. if (comment) {
  507. comment = " <span class='algotype-step-comment' " + idText + ">" +
  508. Algotype.ALGORITHM_STEP_COMMENT_TAG + " " +
  509. comment.trim() + "</span>";
  510. }
  511.  
  512. if (label) {
  513. label = label.trim();
  514.  
  515. if (label[label.length - 1] !== ":") {
  516. label += ":";
  517. }
  518.  
  519. htmlText += Algotype.getLabelHtml(state, label);
  520. }
  521.  
  522. var id = (element.getAttribute("id") || "").trim();
  523. var idTextBegin = "";
  524. var idTextEnd = "";
  525.  
  526. if (id) {
  527. idTextBegin = "</span id='" + id + "'>";
  528. idTextEnd = "</span>";
  529. }
  530.  
  531. htmlText += "<table class='algotype-code-row-table'>n" +
  532. " <tbody class='algotype-code-row-tbody'>n" +
  533. " <tr class='algotype-algorithm-line'>n" +
  534. " <td class='algotype-algorithm-line-number'>" +
  535. state["lineNumber"] +
  536. " </td> " +
  537. " <td class='algotype-line-number-space' width='" +
  538. (Algotype.INDENTATION_WIDTH * state["indentation"] +
  539. Algotype.DISTANCE_BETWEEN_LINE_NUMBER_AND_CODE) +
  540. "px'></td>n" +
  541. " <td class='algotype-text algotype-keyword'>" +
  542. idTextBegin + fromKeyword + " " +
  543. initConditionTeX + " " + toKeyword + " " + toConditionTeX +
  544. stepText + ":" + idTextEnd +
  545. (comment ? comment : "") +
  546. " </td> " +
  547. " </tr>n" +
  548. " </tbody>n" +
  549. "</table>n";
  550.  
  551. var saveIndentation = state["indentation"];
  552. state["lineNumber"]++;
  553. state["indentation"]++;
  554. htmlText += Algotype.processInnerElements(element, state);
  555.  
  556. // Reset the indentation counter.
  557. state["indentation"] = saveIndentation;
  558. return htmlText;
  559. };
  560.  
  561. Algotype.typesetFor = function(forElement, state) {
  562. return Algotype.typesetCountingLoop(forElement, state, "for", "to", "step");
  563. };
  564.  
  565. Algotype.typesetForDownto = function(forDowntoElement, state) {
  566. return Algotype.typesetCountingLoop(forDowntoElement,
  567. state,
  568. "for",
  569. "downto",
  570. "step");
  571. };
  572.  
  573. Algotype.typesetUnconditionalBlock = function(element, state, keyword) {
  574. var label = element.getAttribute("label");
  575. var htmlText = "";
  576. var comment = element.getAttribute("comment");
  577. var commentId = (element.getAttribute("comment-id") || "").trim();
  578. var idText = "";
  579.  
  580. if (commentId) {
  581. idText = "id='" + commentId + "' ";
  582. }
  583.  
  584. if (comment) {
  585. comment = " <span class='algotype-step-comment' " + idText + ">" +
  586. Algotype.ALGORITHM_STEP_COMMENT_TAG + " " +
  587. comment.trim() + "</span>";
  588. }
  589.  
  590. if (label) {
  591. label = label.trim();
  592.  
  593. if (label[label.length - 1] !== ":") {
  594. label += ":";
  595. }
  596.  
  597. htmlText += Algotype.getLabelHtml(state, label);
  598. }
  599.  
  600. var id = (element.getAttribute("id") || "").trim();
  601. var idTextBegin = "";
  602. var idTextEnd = "";
  603.  
  604. if (id) {
  605. idTextBegin = "<span id='" + id + "'>";
  606. idTextEnd = "</span>";
  607. }
  608.  
  609. htmlText += "<table class='algotype-code-row-table'>n" +
  610. " <tbody class='algotype-code-row-tbody'>n" +
  611. " <tr class='algotype-algorithm-line'>n" +
  612. " <td class='algotype-algorithm-line-number'>" +
  613. state["lineNumber"] +
  614. " </td> " +
  615. " <td class='algotype-line-number-space' width='" +
  616. (Algotype.INDENTATION_WIDTH * state["indentation"] +
  617. Algotype.DISTANCE_BETWEEN_LINE_NUMBER_AND_CODE) +
  618. "px'></td>n" +
  619. " <td class='algotype-text algotype-keyword'>" +
  620. idTextBegin + keyword + ":" + idTextEnd +
  621. (comment ? comment : "") +
  622. " </td> " +
  623. " </tr>n" +
  624. " </tbody>n" +
  625. "</table>n";
  626.  
  627. var saveIndentation = state["indentation"];
  628. state["lineNumber"]++;
  629. state["indentation"]++;
  630. htmlText += Algotype.processInnerElements(element, state);
  631.  
  632. // Reset the indentation counter.
  633. state["indentation"] = saveIndentation;
  634. return htmlText;
  635. };
  636.  
  637. Algotype.processInnerElements = function(element, state) {
  638. var childElements = element.children;
  639. var htmlText = "";
  640.  
  641. for (var i = 0; i < childElements.length; ++i) {
  642. var elementName = childElements[i].tagName.toLowerCase();
  643. var handlerFunction = Algotype.dispatchTable[elementName];
  644.  
  645. if (handlerFunction) {
  646. htmlText += handlerFunction(childElements[i], state);
  647. } else {
  648. throw new Error("Unknown element: '" + elementName + "'.");
  649. }
  650. }
  651.  
  652. return htmlText;
  653. };
  654.  
  655. Algotype.typesetForever = function(foreverElement, state) {
  656. return Algotype.typesetUnconditionalBlock(foreverElement, state, "forever");
  657. };
  658.  
  659. Algotype.typesetRepeatUntil = function(repeatUntilElement, state) {
  660. var conditionTeX =
  661. Algotype.typesetCondition(
  662. repeatUntilElement.getAttribute("condition"));
  663.  
  664. var label = repeatUntilElement.getAttribute("label");
  665. var htmlText = "";
  666. var comment = repeatUntilElement.getAttribute("comment");
  667. var commentId =
  668. (repeatUntilElement.getAttribute("comment-id") || "").trim();
  669.  
  670. var idText = "";
  671.  
  672. if (commentId) {
  673. idText = " id='" + commentId + "' ";
  674. }
  675.  
  676. if (comment) {
  677. comment = " <span class='algotype-step-comment' " + idText + ">" +
  678. Algotype.ALGORITHM_STEP_COMMENT_TAG + " " +
  679. comment.trim() + "</span>";
  680. }
  681.  
  682. if (label) {
  683. label = label.trim();
  684.  
  685. if (label[label.length - 1] !== ":") {
  686. label += ":";
  687. }
  688.  
  689. htmlText += Algotype.getLabelHtml(state, label);
  690. }
  691.  
  692. var repeatUntilId = (repeatUntilElement.getAttribute("id") || "").trim();
  693. var repeatUntilIdTextBegin = "";
  694. var repeatUntilIdTextEnd = "";
  695.  
  696. if (repeatUntilId) {
  697. repeatUntilIdTextBegin = "<span id='" + repeatUntilId + "'>";
  698. repeatUntilIdTextEnd = "</span>";
  699. }
  700.  
  701. htmlText += "<table class='algotype-code-row-table'>n" +
  702. " <tbody class='algotype-code-row-tbody'>n" +
  703. " <tr class='algotype-algorithm-line'>n" +
  704. " <td class='algotype-algorithm-line-number'>" +
  705. state["lineNumber"] +
  706. " </td> " +
  707. " <td class='algotype-line-number-space' width='" +
  708. (Algotype.INDENTATION_WIDTH * state["indentation"] +
  709. Algotype.DISTANCE_BETWEEN_LINE_NUMBER_AND_CODE) +
  710. "px'></td>n" +
  711. " <td class='algotype-text algotype-keyword'>" +
  712. repeatUntilIdTextBegin + "repeat" + repeatUntilIdTextEnd +
  713. " </td> " +
  714. " </tr>n" +
  715. " </tbody>n" +
  716. "</table>n";
  717.  
  718. var saveIndentation = state["indentation"];
  719. state["lineNumber"]++;
  720. state["indentation"]++;
  721. htmlText += Algotype.processInnerElements(repeatUntilElement, state);
  722.  
  723. // Reset the indentation counter.
  724. state["indentation"] = saveIndentation;
  725.  
  726. htmlText += "<table class='algotype-code-row-table'>n" +
  727. " <tbody class='algotype-code-row-tbody'>n" +
  728. " <tr class='algotype-algorithm-line'>n" +
  729. " <td class='algotype-algorithm-line-number'>" +
  730. state["lineNumber"] +
  731. " </td> " +
  732. " <td class='algotype-line-number-space' width='" +
  733. (Algotype.INDENTATION_WIDTH * state["indentation"] +
  734. Algotype.DISTANCE_BETWEEN_LINE_NUMBER_AND_CODE) +
  735. "px'></td>n" +
  736. " <td class='algotype-text algotype-keyword'>" +
  737. repeatUntilIdTextBegin + "until " +
  738. conditionTeX + repeatUntilIdTextEnd +
  739. (comment ? comment : "") +
  740. " </td> " +
  741. " </tr>n" +
  742. " </tbody>n" +
  743. "</table>n";
  744.  
  745. state["lineNumber"]++;
  746. return htmlText;
  747. };
  748.  
  749. Algotype.typesetAlgorithm = function(algorithmElement) {
  750. var algorithmName =
  751. algorithmElement.getAttribute("name") || Algotype.UNNAMED_ALGORITHM;
  752.  
  753. var algorithmParameterList =
  754. Algotype.getAlgorithmParameterList(algorithmElement);
  755.  
  756. var commentText = Algotype.getAlgorithmHeaderComment(algorithmElement);
  757.  
  758. var parentNode = algorithmElement.parentNode;
  759.  
  760. var htmlText =
  761. "<table class='algotype-algorithm-header'>n" +
  762. " <tbody class='algotype-no-padding-no-margin'>n" +
  763. " <tr class='algotype-no-padding-no-margin'>n" +
  764. " <td class='algotype-no-padding-no-margin'>" +
  765. "<span class='algotype-text algotype-algorithm-name'>" +
  766. algorithmName +
  767. "</span><span class='algotype-text'>" + algorithmParameterList +
  768. commentText +
  769. "</span></td>n" +
  770. " </tr>n" +
  771. " </tbody>n" +
  772. "</table>n";
  773.  
  774. var childElements = algorithmElement.children;
  775.  
  776. var state = {
  777. lineNumber: 1,
  778. indentation: 0
  779. };
  780.  
  781. for (var i = 0; i < childElements.length; ++i) {
  782. var elementName = childElements[i].tagName.toLowerCase();
  783. var handlerFunction = Algotype.dispatchTable[elementName];
  784.  
  785. if (handlerFunction) {
  786. htmlText += handlerFunction(childElements[i], state);
  787. } else {
  788. throw new Error("Unknown element: '" + elementName + "'.");
  789. }
  790. }
  791.  
  792. var paragraphElement = document.createElement("p");
  793. paragraphElement.style.textAlign = "left";
  794. paragraphElement.innerHTML = htmlText;
  795. parentNode.appendChild(paragraphElement);
  796. };
  797.  
  798. Algotype.setCSSRules = function() {
  799. var styleElement = document.createElement("style");
  800. styleElement.type = "text/css";
  801. styleElement.innerHTML =
  802. "alg-algorithm { n
  803. display: none; n
  804. } n
  805. n
  806. .algotype-text { n
  807. padding-bottom: 2px; n
  808. font-family: Times New Roman; n
  809. font-size: 18px; n
  810. } n
  811. n
  812. .algotype-keyword { n
  813. font-weight: bold; n
  814. } n
  815. n
  816. table.algotype-code-row-table { n
  817. padding: 0; n
  818. margin: 0; n
  819. border-collapse: collapse; n
  820. margin-bottom: -3px; n
  821. } n
  822. n
  823. tbody.algotype-code-row-tbody { n
  824. padding: 0; n
  825. margin: 0; n
  826. margin-bottom: -3px; n
  827. } n
  828. n
  829. tr.algotype-algorithm-line { n
  830. padding: 0; n
  831. margin: 0; n
  832. margin-bottom: -3px; n
  833. } n
  834. n
  835. td.algotype-algorithm-line-number { n
  836. padding: 0; n
  837. margin: 0; n
  838. font-family: Times New Roman; n
  839. font-size: 16px; n
  840. font-weight: bold; n
  841. width: 20px; n
  842. text-align: right; n
  843. margin-bottom: 0px; n
  844. } n
  845. n
  846. td.algotype-line-number-space { n
  847. padding: 0; n
  848. margin: 0; n
  849. margin-bottom: -3px; n
  850. } n
  851. n
  852. .algotype-algorithm-name { n
  853. font-variant: small-caps; n
  854. font-weight: bolder; n
  855. } n
  856. .algotype-label {} n
  857. n
  858. .algotype-label { n
  859. font-size: 14px; n
  860. font-family: monospace; n
  861. font-weight: normal;n
  862. }n
  863. .algotype-no-padding-no-margin {n
  864. margin: 0; n
  865. padding: 0; n
  866. }n
  867. n
  868. .algotype-algorithm-header { n
  869. border-bottom: 2px solid black; n
  870. border-top: 3px solid black; n
  871. border-colapse: collapse; n
  872. margin: 0; n
  873. padding: 0; n
  874. }n
  875. n
  876. .algotype-step-comment { n
  877. font-family: Times New Roman; n
  878. font-size: 18px; n
  879. font-weight: normal; n
  880. font-variant: normal; n
  881. }n";
  882. document.head.appendChild(styleElement);
  883. };
  884.  
  885. Algotype.setup = function() {
  886. // Load MathJax.
  887. Algotype.setCSSRules();
  888. Algotype.loadMathJax();
  889.  
  890. // Typeset all algorithms present in the DOM.
  891. var algorithmList = document.getElementsByTagName("alg-algorithm");
  892.  
  893. for (var i = 0; i < algorithmList.length; ++i) {
  894. Algotype.typesetAlgorithm(algorithmList[i]);
  895. }
  896. };
  897.  
  898. Algotype.dispatchTable = {};
  899.  
  900. Algotype.dispatchTable["alg-break"] = Algotype.typesetBreak;
  901. Algotype.dispatchTable["alg-continue"] = Algotype.typesetContinue;
  902. Algotype.dispatchTable["alg-else"] = Algotype.typesetElse;
  903. Algotype.dispatchTable["alg-else-if"] = Algotype.typesetElseIf;
  904. Algotype.dispatchTable["alg-for"] = Algotype.typesetFor;
  905. Algotype.dispatchTable["alg-foreach"] = Algotype.typesetForEach;
  906. Algotype.dispatchTable["alg-forever"] = Algotype.typesetForever;
  907. Algotype.dispatchTable["alg-for-downto"] = Algotype.typesetForDownto;
  908. Algotype.dispatchTable["alg-if"] = Algotype.typesetIf;
  909. Algotype.dispatchTable["alg-output"] = Algotype.typesetOutput;
  910. Algotype.dispatchTable["alg-print"] = Algotype.typesetPrint;
  911. Algotype.dispatchTable["alg-repeat-until"] = Algotype.typesetRepeatUntil;
  912. Algotype.dispatchTable["alg-return"] = Algotype.typesetReturn;
  913. Algotype.dispatchTable["alg-step"] = Algotype.typesetStep;
  914. Algotype.dispatchTable["alg-while"] = Algotype.typesetWhile;
  915. Algotype.dispatchTable["alg-yield"] = Algotype.typesetYield;
  916.  
  917. var oldOnloadHandler = window.onload;
  918.  
  919. window.onload = function() {
  920. if (oldOnloadHandler) {
  921. oldOnloadHandler();
  922. }
  923.  
  924. Algotype.setup();
  925. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement