tedi2lang.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*jslint browser: true*/
  2. "use strict";
  3. /*jshint esversion: 6 */
  4. String.prototype.find = function(string, position = -1) {
  5. return this.indexOf(string, position);
  6. };
  7. String.prototype.erase = function(start, offset) {
  8. if(start != 0)
  9. return this.substring(0, start) + this.substring(start + offset, this.length);
  10. else
  11. return this.substring(start + offset, this.length);
  12. };
  13. String.prototype.insert = function(start, text) {
  14. if(start != 0)
  15. return this.substring(0, start) + text + this.substring(start, this.length);
  16. else
  17. return text + this.substring(start, this.length);
  18. };
  19. String.prototype.regexIndexOf = function(regex, startpos) {
  20. var indexOf = this.substring(startpos || 0).search(regex);
  21. return (indexOf >= 0) ? (indexOf + (startpos || 0)) : indexOf;
  22. };
  23. String.prototype.splice = function(idx, rem, str) {
  24. return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
  25. };
  26. String.prototype.replace_last_one = function (to_be_replaced, replace_to_this) {
  27. var lIndex = this.lastIndexOf(to_be_replaced);
  28. var str = this.substring(0, lIndex) + replace_to_this + this.substr(lIndex + to_be_replaced.length);
  29. return str;
  30. };
  31. String.prototype.strip_tags = function (to_be_replaced, replace_to_this) {
  32. var lIndex = this.lastIndexOf(to_be_replaced);
  33. var str = this.substring(0, lIndex) + replace_to_this + this.substr(lIndex + to_be_replaced.length);
  34. return str;
  35. };
  36. String.prototype.size = function() {
  37. return this.length;
  38. };
  39. const BLOCK = 2;
  40. const IMAGE = 1;
  41. const LINK = 0;
  42. const NO_TAG = -1;
  43. /*
  44. * Changes from original:
  45. * - this.line is a reference shared from list_item until unquoted_tags
  46. * is a way to simulate reference string argument
  47. * - size() to js idiom
  48. * - add this. before every object property
  49. */
  50. class tedi2lang{
  51. constructor(td) {
  52. this._start_heading_first_level_tag = td.start_heading_first_level_tag;
  53. this._start_heading_second_level_tag = td.start_heading_second_level_tag;
  54. this._start_heading_third_level_tag = td.start_heading_third_level_tag;
  55. this._start_heading_fourth_level_tag = td.start_heading_fourth_level_tag;
  56. this._end_heading_first_level_tag = td.end_heading_first_level_tag;
  57. this._end_heading_second_level_tag = td.end_heading_second_level_tag;
  58. this._end_heading_third_level_tag = td.end_heading_third_level_tag;
  59. this._end_heading_fourth_level_tag = td.end_heading_fourth_level_tag;
  60. this._start_list_tag = td.start_list_tag;
  61. this._list_item_tag = td.list_item_tag;
  62. this._end_list_tag = td.end_list_tag;
  63. this._start_container_tag = td.start_container_tag;
  64. this._middle_container_tag = td.middle_container_tag;
  65. this._end_container_tag = td.end_container_tag;
  66. this._start_link_tag = td.start_link_tag;
  67. this._middle_link_tag = td.middle_link_tag;
  68. this._end_link_tag = td.end_link_tag;
  69. this._start_image_tag = td.start_image_tag;
  70. this._middle_image_tag = td.middle_image_tag;
  71. this._end_image_tag = td.end_image_tag;
  72. this._start_table_tag = td.start_table_tag;
  73. this._end_table_tag = td.end_table_tag;
  74. this._start_table_row_tag = td.start_table_row_tag;
  75. this._end_table_row_tag = td.end_table_row_tag;
  76. this._start_table_data_tag = td.start_table_data_tag;
  77. this._end_table_data_tag = td.end_table_data_tag;
  78. this._end_paragraph_tag = td.end_paragraph_tag;
  79. this._line = "";
  80. }
  81. found(position) { return (position != -1); }
  82. /*
  83. * Split string into array with lines
  84. */
  85. get_lines(text) {
  86. let lines_array = [];
  87. if(this.found(text.indexOf('\n'))) {
  88. lines_array = text.split('\n');
  89. //JS when splits strings with \n at end
  90. //creates a fake newline in array
  91. if(text[text.size() - 1] == '\n') {
  92. //Delete last fake line
  93. lines_array.pop();
  94. }
  95. }else
  96. if(text.size() > 0)
  97. lines_array = [text];
  98. return lines_array;
  99. }
  100. /*
  101. * Add header, convert line by line through text and finish with footer
  102. *
  103. */
  104. convert(text, header = "", footer = "") {
  105. let index = this.get_lines(text);
  106. let return_text = header;
  107. this._open_brackets = 0;
  108. this._is_converting_table = false;
  109. this._is_unordered_list = false;
  110. for(let i = 0; i < index.length; ++i) {
  111. this._has_block = false;
  112. this._has_image = false;
  113. let line = index[i];
  114. return_text += this.convert_line(line);
  115. }
  116. if(this._open_brackets > 0)
  117. throw "Missing '}' in document. End of file";
  118. if(this._is_converting_table) return_text += this._end_table_tag + "\n";
  119. return_text += footer;
  120. return return_text;
  121. }
  122. /*
  123. * Main logic converting line.
  124. *
  125. */
  126. convert_line(line) {
  127. if(line[0] != '<') {
  128. let end_table = this.convert_end_table(line);
  129. let hash = line.find('#');
  130. if(this.found(hash) && this.is_first_tag(line, hash))
  131. return end_table + this.convert_line_heading(line, hash);
  132. else {
  133. let ul = line.find("__");
  134. if(this.found(ul))
  135. return end_table + this.convert_line_list_start(line);
  136. else {
  137. this._line = line;
  138. let li = this._line.find("--");
  139. if(this.found(li) && this._is_unordered_list)
  140. this.convert_line_list_item(li);
  141. let ul_end = this._line.find(",,");
  142. if(this.found(ul_end) && this._is_unordered_list)
  143. return end_table + this.convert_line_list_end(line);
  144. else {
  145. if(this._line[0] == '"')
  146. this.convert_line_quote();
  147. else
  148. this.convert_unquoted_tags();
  149. let first_pipe = this._line.find("|");
  150. if(this.found(first_pipe))
  151. return this.convert_line_table(first_pipe);
  152. else
  153. return end_table + this.convert_line_ending(this._line);
  154. }
  155. }
  156. }
  157. } else
  158. return this.convert_end_table_control_tags(line) + this.convert_line_control_tags(line);
  159. }
  160. /*
  161. * Deletes heading tag and insert new heading tag
  162. *
  163. */
  164. convert_line_heading(line, hash_position) {
  165. let level = 0;
  166. while(this.found(hash_position) && line[hash_position + level] == '#')
  167. ++level;
  168. if((hash_position + level) >= line.size())
  169. throw "Unexpected heading size" + line;
  170. else
  171. line = line.substring(hash_position + level, line.size());
  172. line = this.strip_escaping(line);
  173. switch(level) {
  174. case 1: line = this._start_heading_first_level_tag + line + this._end_heading_first_level_tag; break;
  175. case 2: line = this._start_heading_second_level_tag + line + this._end_heading_second_level_tag; break;
  176. case 3: line = this._start_heading_third_level_tag + line + this._end_heading_third_level_tag; break;
  177. case 4: line = this._start_heading_fourth_level_tag + line + this._end_heading_fourth_level_tag; break;
  178. default: line = this._start_heading_fourth_level_tag + line + this._end_heading_fourth_level_tag; break;
  179. }
  180. return line + "\n";
  181. }
  182. // List tags
  183. convert_line_list_start() {
  184. ++this._is_unordered_list;
  185. return this._start_list_tag + "\n";
  186. }
  187. convert_line_list_item(li) {
  188. this._line = this._line.erase(li, 2);
  189. this._line = this._line.insert(li, this._list_item_tag);
  190. }
  191. convert_line_list_end() {
  192. --this._is_unordered_list;
  193. return this._end_list_tag + "\n";
  194. }
  195. convert_line_quote() {
  196. if(this._line[this._line.size() - 1] == '"')
  197. this._line = this.strip_escaping(this._line.substr(1, this._line.size() - 2));
  198. else
  199. throw "Missing end quotes." + this._line;
  200. }
  201. /*
  202. * Convert Tedi link to a link tag
  203. *
  204. */
  205. convert_line_link() {
  206. let start_tag = this.get_not_escaped_tag("[(");
  207. if(this.found(start_tag) && !this.is_tag_escaped(start_tag)) {
  208. this._line = this._line.erase(start_tag, 2);
  209. this._line = this._line.insert(start_tag, this._start_link_tag);
  210. let end_parenthesis = this.get_not_escaped_tag(") ");
  211. if(this.found(end_parenthesis) && !this.is_tag_escaped(end_parenthesis)) {
  212. this._line = this._line.erase(end_parenthesis, 2);
  213. this._line = this._line.insert(end_parenthesis, this._middle_link_tag);
  214. let bracket = this.correct_position(end_parenthesis + this._middle_link_tag.size(), '[', ']');
  215. if(this.found(bracket)) {
  216. this._line = this._line.erase(bracket, 1);
  217. this._line = this._line.insert(bracket, this._end_link_tag);
  218. } else
  219. throw "Missing ']' in link tag." + this._line;
  220. } else
  221. throw "Missing ')' in link tag." + this._line;
  222. } else
  223. throw "Missing link tag." + this._line;
  224. }
  225. /*
  226. * Deletes image tag and insert image tag
  227. *
  228. */
  229. convert_line_image() {
  230. let start_tag = this.get_not_escaped_tag("([");
  231. if(this.found(start_tag) && !this.is_tag_escaped(start_tag)) {
  232. this._line = this._line.erase(start_tag, 2);
  233. this._line = this._line.insert(start_tag, this._start_image_tag);
  234. let square_bracket = this.get_not_escaped_tag("] ");
  235. if(this.found(square_bracket) && !this.is_tag_escaped(square_bracket)) {
  236. this._line = this._line.erase(square_bracket, 2);
  237. this._line = this._line.insert(square_bracket, this._middle_image_tag);
  238. let last_bracket = this.correct_position(square_bracket + this._middle_image_tag.size(), '(', ')');
  239. if(this.found(last_bracket)) {
  240. this._line = this._line.erase(last_bracket, 1);
  241. this._line = this._line.insert(last_bracket, this._end_image_tag);
  242. this._has_image = true;
  243. } else
  244. throw "Missing ')' in images tag." + this._line;
  245. } else
  246. throw "Missing ']' in images tag." + this._line;
  247. } else
  248. throw "Missing images tag." + this._line;
  249. }
  250. /*
  251. * Deletes block tag and insert block tag
  252. *
  253. */
  254. convert_line_block() {
  255. let start_tag = this.get_not_escaped_tag("{(");
  256. if(this.found(start_tag) && !this.is_tag_escaped(start_tag)) {
  257. this._line = this._line.erase(start_tag, 2);
  258. this._line = this._line.insert(start_tag, this._start_container_tag);
  259. start_tag = this.get_not_escaped_tag(") ");
  260. if(this.found(start_tag) && !this.is_tag_escaped(start_tag)) {
  261. this._line = this._line.erase(start_tag, 2);
  262. this._line = this._line.insert(start_tag, this._middle_container_tag);
  263. let end_tag = this.correct_position(start_tag + this._middle_container_tag.size(), '{', '}');
  264. if(this.found(end_tag)) {
  265. this._line = this._line.erase(end_tag, 1);
  266. this._line = this._line.insert(end_tag, this._end_container_tag);
  267. } else
  268. ++this._open_brackets;
  269. } else
  270. throw "Missing ')' in block tag." + this._line;
  271. } else
  272. throw "Missing block tag." + this._line;
  273. this._has_block = true;
  274. }
  275. /*
  276. * Start or continue table conversion
  277. *
  278. */
  279. convert_line_table(first_pipe) {
  280. let return_text = "";
  281. if(!this._is_converting_table) {
  282. if(first_pipe != -1 && first_pipe != this._line.lastIndexOf("\|")) {
  283. return_text = this._start_table_tag + "\n";
  284. this._is_converting_table = true;
  285. return return_text + this.convert_line_table_row(this._line);
  286. } else
  287. return this.convert_line_ending(this._line);
  288. }else
  289. return this.convert_line_table_row(this._line);
  290. }
  291. /*
  292. * Convert every cell of one table row
  293. *
  294. */
  295. convert_line_table_row(line) {
  296. let pipe = line.find("|");
  297. if(this.found(pipe)) {
  298. ++pipe;
  299. let return_text = this._start_table_row_tag + "\n";
  300. line = line.substr(pipe, line.size() - 1);
  301. pipe = line.find("|");
  302. if(this.found(pipe)) {
  303. let size = line.size() - 1;
  304. if(line[size] == '|') {
  305. while(this.found(pipe) && line[size] == '|') {
  306. return_text += this._start_table_data_tag + this.strip_escaping(line.substr(0, pipe)) + this._end_table_data_tag + "\n";
  307. ++pipe;
  308. line = line.substr(pipe, size);
  309. pipe = line.find("|");
  310. size = line.size() - 1;
  311. }
  312. return return_text + this._end_table_row_tag + "\n";
  313. } else
  314. throw "Table not correctly written\n" +
  315. "Maybe there's a whitespace at end of line" + line;
  316. } else
  317. throw "Expected '|' in table" + this._line;
  318. } else
  319. throw "Expected '|' in table" + this._line;
  320. }
  321. /*
  322. * Checks if there is a non-space character (every char except \n \t \r)
  323. * before the given position
  324. *
  325. */
  326. is_first_tag(line, position) {
  327. let i = 0;
  328. while(i < position && this.isspace(line[i]))
  329. ++i;
  330. return (i == position);
  331. }
  332. isspace(character) {
  333. return ([' ', '\t','\n','\v', '\f', '\r'].includes(character));
  334. }
  335. /*
  336. * End table if there isn't a pipe char and there's a open
  337. * table tag
  338. *
  339. */
  340. convert_end_table(line) {
  341. let pipe = line.find("|");
  342. if(!this.found(pipe) && this._is_converting_table) {
  343. this._is_converting_table = false;
  344. return this._end_table_tag + "\n";
  345. }
  346. return "";
  347. }
  348. /*
  349. * Check line ending and convert depending ending whitespace
  350. *
  351. */
  352. convert_line_ending(line) {
  353. line = this.strip_escaping(line);
  354. if(line[line.size() - 1] == ' ')
  355. return line + this._end_paragraph_tag + "\n";
  356. else
  357. return line + "\n";
  358. }
  359. /*
  360. * Control tags:
  361. * <!, <>, <+. <
  362. */
  363. convert_line_control_tags(line) {
  364. if(line[1] != '!') {
  365. if(line[1] == '>') {
  366. line = line.erase(0,2);
  367. return line + "\n";
  368. } else if(line[1] == '+') {
  369. line = line.erase(0,2);
  370. return line + this._end_paragraph_tag + "\n";
  371. } else {
  372. line = line.erase(0,1);
  373. return line + "\n";
  374. }
  375. } else
  376. return "";
  377. }
  378. /*
  379. * Get next unquoted tag (link, image, container) from line
  380. *
  381. */
  382. convert_unquoted_tags() {
  383. let tag = this.main_tag();
  384. while(tag != NO_TAG) {
  385. switch(tag) {
  386. case LINK : this.convert_line_link(); break;
  387. case IMAGE : this.convert_line_image(); break;
  388. case BLOCK : this.convert_line_block(); break;
  389. }
  390. tag = this.main_tag();
  391. }
  392. this.check_ending_container();
  393. }
  394. main_tag(line) {
  395. let tag = NO_TAG;
  396. let line_size = this._line.size();
  397. let first_bracket = this.get_not_escaped_tag("{(");
  398. let first_parenthesis = this.get_not_escaped_tag("([");
  399. let first_square_bracket = this.get_not_escaped_tag("[(");
  400. if(first_bracket != -1 && first_bracket < line_size && this._line[first_bracket - 1] != '\\') {
  401. tag = BLOCK;
  402. line_size = first_bracket;
  403. }
  404. if(first_square_bracket != -1 && first_square_bracket < line_size && this._line[first_square_bracket - 1] != '\\') {
  405. line_size = first_square_bracket;
  406. tag = LINK;
  407. }
  408. if(first_parenthesis != -1 && first_parenthesis < line_size && this._line[first_parenthesis - 1] != '\\')
  409. tag = IMAGE;
  410. return tag;
  411. }
  412. /*
  413. * Get ending char position or -1 if doesn't exist
  414. *
  415. */
  416. correct_position(position, starting_char, ending_char) {
  417. let char_founds = 1;
  418. while(char_founds != 0 && this._line.size() > position) {
  419. if(this._line[position] == starting_char)
  420. ++char_founds;
  421. else if(this._line[position] == ending_char && position == 0)
  422. --char_founds;
  423. else if(this._line[position] == ending_char && position > 0 && !this.is_tag_escaped(position))
  424. --char_founds;
  425. ++position;
  426. }
  427. if(this._line.size() == position && this._line[position - 1] != ending_char)
  428. return -1;
  429. else
  430. return position - 1;
  431. }
  432. /*
  433. * Convert every container ending if there are open container tags.
  434. *
  435. */
  436. check_ending_container() {
  437. let first_bracket_closed = this._line.find("}");
  438. let first_bracket_open = this._line.find("{");
  439. if(this.found(first_bracket_closed) && !this.is_tag_escaped(first_bracket_closed) && !this.found(first_bracket_open)) {
  440. if(this._open_brackets == 0)
  441. throw "Missing '{' in block tag." + this._line;
  442. --this._open_brackets;
  443. this._has_block = true;
  444. this._line = this._line.erase(first_bracket_closed, 1);
  445. this._line = this._line.insert(first_bracket_closed, this._end_container_tag);
  446. }
  447. }
  448. /*
  449. *
  450. * End table if there's a open table tag
  451. * (Added for control tags that
  452. */
  453. convert_end_table_control_tags(line) {
  454. if(this._is_converting_table) {
  455. this._is_converting_table = false;
  456. return this._end_table_tag + "\n";
  457. }
  458. return "";
  459. }
  460. /*
  461. * Check if position is escaped with '\' char
  462. *
  463. */
  464. is_tag_escaped(position) {
  465. let is_escaped = false;
  466. if(position > 0 && this._line[position - 1] == '\\')
  467. is_escaped = true;
  468. return is_escaped;
  469. }
  470. /*
  471. * Check if position is escaped with '\' char
  472. *
  473. */
  474. get_not_escaped_tag(find_character, position = -1) {
  475. if(!this.found(position))
  476. position = this._line.find(find_character);
  477. while(this.found(position) && this.is_tag_escaped(position)) {
  478. position = this._line.find(find_character, position + find_character.size());
  479. }
  480. return position;
  481. }
  482. /*
  483. * Convert escaped characters to their equivalents
  484. *
  485. */
  486. strip_escaping(line) {
  487. line = line.replace(/\\\\/g, "\\");
  488. line = line.replace(/\\\[/g, "[");
  489. line = line.replace(/\\\]/g, "]");
  490. line = line.replace(/\\\{/g, "{");
  491. line = line.replace(/\\\}/g, "}");
  492. line = line.replace(/\\\)/g, ")");
  493. line = line.replace(/\\\(/g, "(");
  494. return line;
  495. }
  496. }