tedi2ad.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * tedi2ad library code
  3. * Copyright (C) <2022> <alkeon> [alkeon@autistici.org]
  4. * Texdi is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * Texdi is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with tedi2ad. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <iostream>
  18. #include <string>
  19. #include <fstream>
  20. #include <sstream>
  21. #include "../tedi2lang/tedi2lang.h"
  22. #include "tedi2ad.h"
  23. #include "../tedi2lang/exception.h"
  24. using namespace std;
  25. #define BLOCK 2
  26. #define IMAGE 1
  27. #define LINK 0
  28. #define NO_TAG -1
  29. tedi2ad::tedi2ad(tags_definition td): tedi2lang(td){}
  30. tedi2ad::tedi2ad(): tedi2lang(default_tags_value()){}
  31. tags_definition tedi2ad::default_tags_value(){
  32. tags_definition td;
  33. td.start_heading_first_level_tag = "= ";
  34. td.start_heading_second_level_tag = "== ";
  35. td.start_heading_third_level_tag = "=== ";
  36. td.start_heading_fourth_level_tag = "==== ";
  37. td.start_list_tag = "";
  38. td.list_item_tag = "* ";
  39. td.end_list_tag = "";
  40. td.start_container_tag = "";
  41. td.middle_container_tag = "";
  42. td.end_container_tag = "";
  43. td.start_link_tag = "";
  44. td.middle_link_tag = "[";
  45. td.end_link_tag = "]";
  46. td.start_image_tag = "image::";
  47. td.middle_image_tag = "[";
  48. td.end_image_tag = "]";
  49. td.start_table_tag = "|=======";
  50. td.end_table_tag = "|=======";
  51. td.start_table_row_tag = "";
  52. td.end_table_row_tag = "";
  53. td.start_table_data_tag = "|";
  54. td.end_table_data_tag = "";
  55. td.end_paragraph_tag = "";
  56. return td;
  57. }
  58. /*
  59. * Deletes heading tag and insert image tag
  60. * - ([caption] image.png)
  61. * - image::image.png[caption]
  62. *
  63. */
  64. void tedi2ad::convert_line_image(string& line) {
  65. size_t start_tag = get_not_escaped_tag(line, "([");
  66. if(found(start_tag) && !is_tag_escaped(line, start_tag)) {
  67. line = line.erase(start_tag, 2);
  68. size_t square_bracket = get_not_escaped_tag(line, "] ");
  69. if(found(square_bracket) && !is_tag_escaped(line, square_bracket)) {
  70. string caption = line.substr(start_tag, square_bracket - start_tag);
  71. line = line.erase(start_tag, square_bracket + 2 - start_tag);
  72. line = line.insert(start_tag, _start_image_tag);
  73. size_t last_parenthesis = correct_position(line, start_tag + _start_image_tag.size(), '(', ')');
  74. if(found(last_parenthesis)) {
  75. line = line.erase(last_parenthesis, 1);
  76. line = line.insert(last_parenthesis, "[" + caption + "]");
  77. } else
  78. throw Invalid("Missing ')' in images tag.",line);
  79. } else
  80. throw Invalid("Missing ']' in images tag.",line);
  81. } else
  82. throw Invalid("Missing images tag.",line);
  83. }
  84. /*
  85. * Deletes block tag and insert block tag
  86. *
  87. */
  88. void tedi2ad::convert_line_block(string& line) {
  89. size_t start_tag = get_not_escaped_tag(line, "{(");
  90. if(found(start_tag) && !is_tag_escaped(line, start_tag)) {
  91. line = line.erase(start_tag, 2);
  92. line = line.insert(start_tag, _start_container_tag);
  93. size_t parenthesis = get_not_escaped_tag(line, ") ");
  94. if(found(parenthesis) && !is_tag_escaped(line, parenthesis)) {
  95. line = line.erase(start_tag, parenthesis - start_tag + 2);
  96. size_t end_tag = correct_position(line, start_tag, '{', '}');
  97. if(found(end_tag))
  98. line = line.erase(end_tag, 1);
  99. else
  100. ++_open_brackets;
  101. } else
  102. throw Invalid("Missing ')' in block tag.",line);
  103. } else
  104. throw Invalid("Missing block tag.",line);
  105. _has_block = true;
  106. }