main.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * This file is the main code of Texdi
  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 Texdi. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <string>
  18. #include <iostream>
  19. #include <fstream>
  20. #include "subprojects/tedi2lang/tedi2lang.h"
  21. #include "subprojects/tedi2html/tedi2html.h"
  22. #include "subprojects/tedi2tex/tedi2tex.h"
  23. #include "subprojects/tedi2md/tedi2md.h"
  24. #include "subprojects/tedi2ad/tedi2ad.h"
  25. #include "preprocessor.h"
  26. #include "exception.h"
  27. #define HTML 1
  28. #define LATEX 2
  29. #define MD 3
  30. #define AD 4
  31. using namespace std;
  32. /*
  33. * Generate latex header
  34. * author_s : author name
  35. * input : filename input file
  36. *
  37. * input will be the name of the document
  38. *
  39. */
  40. string latex_header(string author_s, const string& input);
  41. /*
  42. * clean_stdin()
  43. * clean keyboard buffer
  44. */
  45. void clean_stdin();
  46. /*
  47. * Print --help option info.
  48. *
  49. */
  50. void help_info();
  51. /*
  52. *
  53. * get html styles
  54. *
  55. */
  56. string get_styles();
  57. int main(int argc, char* argv[]) {
  58. int num = 0;
  59. string input;
  60. string output;
  61. string author_s;
  62. string header, end;
  63. int i = 1;
  64. bool help = false, has_wfile = false, has_rfile = false;
  65. bool embed_html_styles = true;
  66. while(i < argc && !help) {
  67. string arg(argv[i]);
  68. if(arg == "--help") {
  69. help_info();
  70. help = true;
  71. } else if(arg == "--latex") {
  72. num = LATEX;
  73. author_s = "0";
  74. } else if(arg == "--html")
  75. num = HTML;
  76. else if(arg == "--asciidoc")
  77. num = AD;
  78. else if(arg == "--markdown")
  79. num = MD;
  80. else if(arg == "--own-styles")
  81. embed_html_styles = false;
  82. else if(arg == "--header-file") {
  83. if(i + 1 < argc) {
  84. ifstream header_file(string(argv[++i]));
  85. string line;
  86. while(getline(header_file, line)){
  87. header += line + "\n";
  88. }
  89. } else
  90. cout << "--header-file option requires one argument." << endl;
  91. } else if(arg == "--end-file") {
  92. if(i + 1 < argc) {
  93. ifstream end_file(string(argv[++i]));
  94. string line;
  95. while(getline(end_file, line)){
  96. end += line + "\n";
  97. }
  98. } else
  99. cout << "--end-file option requires one argument." << endl;
  100. } else if(arg == "-o" or arg == "--output") {
  101. if(i + 1 < argc) {
  102. output = string(argv[++i]);
  103. has_wfile = true;
  104. } else
  105. cout << "--output option requires one argument." << endl;
  106. } else if(arg.find(".te") != string::npos) {
  107. has_rfile = true;
  108. input = string(argv[i]);
  109. output = input.substr(0, input.length() - 3);
  110. }
  111. ++i;
  112. }
  113. if(!help) {
  114. if(!has_rfile) {
  115. cout << "Write filename" << endl;
  116. char char_input[100];
  117. cin.getline(char_input,100);
  118. input = string(char_input);
  119. if(!has_wfile)
  120. output = input.substr(0, input.length() - 3);
  121. clean_stdin();
  122. }
  123. while(num < 1 or num > 4) {
  124. cout << "Which format do yo want?" << endl;
  125. cout << "1.- HTML" << endl;
  126. cout << "2.- TEX" << endl;
  127. cout << "3.- MD" << endl;
  128. cout << "4.- AD" << endl;
  129. cin >> num;
  130. }
  131. try {
  132. string full_text = one_file(input);
  133. if(test_file(full_text)){
  134. switch(num){
  135. case HTML: {
  136. if(!has_wfile)
  137. output += ".html";
  138. if(header.size() == 0){
  139. header = "<!DOCTYPE html>\n\n"
  140. "<!-- Done with Texdi -->\n\n "
  141. "<head>\n";
  142. if(embed_html_styles)
  143. header += get_styles();
  144. header += "\n\t<link type=\"text/css\""
  145. " rel=\"stylesheet\" href=\"styles.css\">\n"
  146. "\t<meta charset=\"utf-8\">\n</head>\n<body>";
  147. }
  148. if(end.size() == 0)
  149. end = "\n</body>\n</html>";
  150. string text(tedi2html().convert(full_text, header, end));
  151. ofstream writer(output);
  152. writer << text;
  153. writer.close();
  154. };break;
  155. case LATEX: {
  156. if(!has_wfile)
  157. output += ".tex";
  158. if(header.size() == 0)
  159. header = latex_header(author_s,input);
  160. if(end.size() == 0)
  161. end = "\\end{document}";
  162. tedi2tex t2t;
  163. string text(t2t.convert(full_text, header, end));
  164. ofstream writer(output);
  165. writer << text;
  166. writer.close();
  167. };break;
  168. case MD: {
  169. if(!has_wfile)
  170. output += ".md";
  171. if(header.size() == 0)
  172. header = "<!-- Texdi ---> ";
  173. string text = tedi2md().convert(full_text, header, end);
  174. ofstream writer(output);
  175. writer << text;
  176. writer.close();
  177. };break;
  178. case AD:{
  179. if(!has_wfile)
  180. output += ".txt";
  181. string text = tedi2ad().convert(full_text, header, end);
  182. ofstream writer(output);
  183. writer << text;
  184. writer.close();
  185. };break;
  186. default: cout << "Wrong format chosed" << endl;
  187. }
  188. } else
  189. cout << "Conversion failed" << endl;
  190. }catch(Invalid i){
  191. cout << i.reason() << endl;
  192. cout << i.line() << endl;
  193. }
  194. }
  195. }
  196. string latex_header(string author_s, const string& input){
  197. string header = "\\documentclass[11pt,a4paper]{article}\n"
  198. "\\usepackage[utf8]{inputenc}\n";
  199. cout << "Language used in the document" << endl;
  200. cout << "1.- Spanish" << endl;
  201. cout << "2.- Other" << endl;
  202. char char_input[2];
  203. clean_stdin();
  204. cin.getline(char_input, 2);
  205. if(char_input[0]=='1')
  206. header += "\\usepackage[spanish]{babel}\n";
  207. header += "\\usepackage{hyperref}\n"
  208. "\\usepackage{verbatim}\n\\usepackage{graphicx}\n"
  209. "\\usepackage{underscore}\n\\usepackage[protrusion = true,final]"
  210. "{microtype}\n\\setlength{\\emergencystretch}{10pt}\n"
  211. "\\hypersetup{pageanchor=false}\n"
  212. "\\title{"+input.substr(0,input.length()-3)+"}\n\\date{}";
  213. if(author_s!="0"){
  214. char author[100];
  215. cout << "Write author if you don't want an author use 0"<<endl;
  216. cin.getline(author,100);
  217. if(author[0] != '0'){
  218. author_s=string(author);
  219. header+="\n\\author{"+author_s+"}";
  220. }
  221. }
  222. header+="\n\\setlength\\parindent{0pt}\n\\begin{document}\n"
  223. "\\maketitle\n\\newpage\n\\thispagestyle{empty}\n"
  224. "\\tableofcontents\n\\newpage\n\\setcounter{page}{1}\n";
  225. return header;
  226. }
  227. void clean_stdin(){
  228. int c;
  229. do{
  230. c = getchar();
  231. } while (c != '\n' && c != EOF);
  232. }
  233. void help_info(){
  234. cout << "--html \t\t Convert to HTML\n"
  235. "--latex\t\t Convert to Latex\n"
  236. "--asciidoc \t Convert to Asciidoc\n"
  237. "--markdown \t Convert to Markdown\n"
  238. "--own-styles \t Avoid default CSS styles \n"
  239. "--help \t\t Print this help text and exit \n"
  240. "-o FILE \t Use FILE as output filename\n"
  241. "--output FILE"
  242. << endl;
  243. }
  244. string get_styles(){
  245. string aux="\t<style>\n"
  246. "\t\ta {text-decoration:none;}\n"
  247. "\t\ttable {border-collapse: collapse;}\n"
  248. "\t\ttable, th, td {\n"
  249. "\t\t\tborder: 1px solid black;\n"
  250. "\t\t\tpadding: 15px;\n"
  251. "\t\t}\n"
  252. "\t\tth {height: 50px;}\n"
  253. "\t\th1,h2,h3{text-align:center;}\n"
  254. "\t\thtml{\n"
  255. "\t\t\tmargin-left:10%;\n"
  256. "\t\t\tbackground-color:#fff;\n"
  257. "\t\t\tfont-size:20px;\n"
  258. "\t\t\tfont-family:sans-serif;\n"
  259. "\t\t\tcolor:#000;\n"
  260. "\t\t}\n"
  261. "\t</style>\n";
  262. return aux;
  263. }