123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- /*
- * This file is the main code of Texdi
- * Copyright (C) <2017> <alkeon> [alkeon@autistici.org]
- *
- * Texdi is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Texdi is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Texdi. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include "tedi2html.h"
- #include "exception.h"
- using namespace std;
- /*
- * This function converts a string with Tedi format and return
- * a string with HTML format.
- * Header and footer are by default void.
- */
- string tedi2html(string text, string header, string footer){
- stringstream index(text);
- string line;
- string return_text;
- int control=0;
- int open_brackets=0;
- while(getline(index,line)){
- if(line[0]!='<'){
- size_t start=line.find("!head");
- size_t end=line.find("!end");
- if(start!=string::npos){
- return_text+=header + "\n";
- }else if(end!=string::npos){
- return_text+=footer + "\n";
-
- }else{
- size_t hash=line.find('#');
- if(hash!=string::npos){
- line=titles_ht(line);
- return_text+=line+"\n";
- }else{
- size_t ul=line.find("__");
- if(ul!=string::npos){
- return_text+="<ul>\n";
- }
- size_t li=line.find("--");
- if(li!=string::npos){
- line=line.erase(li,2);
- line=line.insert(li,"<li>");
- }
- size_t ulf=line.find(",,");
- if(ulf!=string::npos){
- return_text+="</ul>\n";
- }
- size_t comi=line.find("\"");
- if(comi!=string::npos){
- if(line[line.size()-1]=='"')
- line=line.substr(1,line.size()-2);
- else
- throw Invalid("Missing end quotes.",line);
- }else{
- int etiq=main_tag(line);
- while(etiq!=-1){
- //cout << line << etiq << endl;
- if(etiq==2){
- size_t lla2=line.find("}");
- if(lla2==string::npos){
- ++open_brackets;
- line=incomplete_blocks_ht(line);
- }else{
- line=blocks_ht(line);
- }
- }else{
- if(etiq==0){
- line=images_ht(line);
- }else{
- line=links_ht(line);
- }
- }
- etiq=main_tag(line);
- }
- size_t lla2=line.find("}");
- if(lla2!=string::npos){
- if(open_brackets==0)
- throw Invalid("Missing '{' in block tag.",line);
- --open_brackets;
- line=line.erase(lla2,1);
- line=line.insert(lla2,"</div>");
- }
- }
-
- size_t hor=line.find("|");
- if(control==0){
- size_t hor1=line.rfind("|");
- if(hor!=string::npos && hor!=hor1){
- return_text+="<table>\n<tr>\n";
- table_ht(line,return_text);
- control=1;
- }
- }else{
- if(hor==string::npos){
- return_text+="</table>\n";
- control=0;
- }else{
- return_text+="<tr>\n";
- table_ht(line,return_text);
- }
- }
- size_t minor=line.rfind(" ");
- if(minor==line.size()-1 && line.size()>1){
- return_text+=line+ "<p>\n";
- }else{
- if(ulf==string::npos && ul==string::npos && hash==string::npos){
- return_text+=line+ "\n";
- }
- }
- }
- }
- }else{
- if(line[1]!='!'){
- if(line[1]=='>'){
- line=line.erase(0,2);
- return_text+=line+ "\n";
- }else{
- line=line.erase(0,1);
- return_text+=line+ "<p>\n";
- }
- }
- }
- }
- if(open_brackets>0)
- throw Invalid("Missing '}' in document.","End of file");
- return return_text;
- }
- /*
- * Detects next tag
- * Returns
- * 0: images
- * 1: links
- * 2: blocks
- * -1: No tag
- */
- int main_tag(string line){
- int etiq=-1;
- size_t cont=line.size();
- size_t lla=line.find("{");
- size_t paren=line.find("(");
- size_t corch=line.find("[");
- if(lla!= string::npos && lla<cont){
- etiq=2;
- cont=lla;
- }
- if(corch != string::npos && corch<cont){
- cont=corch;
- etiq=1;
- }
- if(paren != string::npos && paren<cont){
- etiq=0;
- }
- return etiq;
- }
- /*
- * Convert Tedi table to a HTML link (line by line)
- *
- */
- void table_ht(string& line,string& return_text){
- return_text+="<tr>\n";
- size_t hor=line.find("|");
- if(hor!=string::npos){
- ++hor;
- line=line.substr(hor,line.size()-1);
- hor=line.find("|");
- if(hor!=string::npos){
- int tam=line.size()-1;
- while(hor!=string::npos && line[tam]=='|'){
- string elemento;
- elemento=line.substr(0,hor);
- return_text+="<td>"+elemento+"</td>\n";
- ++hor;
- line=line.substr(hor,line.size()-1);
- hor=line.find("|");
- tam=line.size()-1;
- }
- return_text+="</tr>\n";
- }else{
- throw Invalid("Expected '|' in table", line);
- }
- }else{
- throw Invalid("Expected '|' in table", line);
- }
- }
- /*
- * Convert Tedi link to a HTML link
- *
- */
- string links_ht(string line){
- size_t start_tag=line.find("[(");
- string aux="<a href=\"";
- if(start_tag!= string::npos){
- line=line.erase(start_tag,2);
- line=line.insert(start_tag,aux);
- size_t par1=line.find(")");
- if(par1!=string::npos){
- line=line.erase(par1,1);
- aux="\">";
- line=line.insert(par1,aux);
- size_t brack=line.rfind("]");
- if(brack!=string::npos){
- line=line.erase(brack,1);
- aux="</a>";
- line=line.insert(brack,aux);
- }else
- throw Invalid("Missing ']' in link tag.",line);
- }else
- throw Invalid("Missing ')' in link tag.",line);
- }else
- throw Invalid("Missing link tag.",line);
- return line;
- }
- /*
- * Deletes parentheses and adds HTML text of a image.
- *
- */
- string images_ht(string line){
- size_t par1=line.find("(");
- size_t par2=line.find(")");
- string aux="<img src=\"";
- if(par2!= string::npos){
- size_t pos=par2-par1;
- size_t point=line.find(".");
- if(point!=string::npos && point<par2 && point>par1)
- pos=point-par1;
- string alt=line.substr(par1+1,pos-1);
- line=line.erase(par1,1);
- line=line.insert(par1,aux);
- par2=line.find(")");
- if(par2!=string::npos){
- aux="\" alt=\""+alt+"\">";
- line=line.erase(par2,1);
- line=line.insert(par2,aux);
- }else
- throw Invalid("Missing ')' in image tag.",line);
- }else{
- throw Invalid("Missing image tag.",line);
- }
- return line;
- }
- /*
- * Deletes hashes and adds HTML text of a title.
- *
- */
- string titles_ht(string line){
- size_t exa1=line.find("#");
- int ti=0;
- while(exa1!=string::npos && line[exa1]=='#'){
- ++exa1;
- line=line.substr(exa1,line.size()-1);
- exa1=line.find("#");
- ++ti;
- }
- string ti2=to_string(ti);
- line="<h"+ti2+">"+line+"</h"+ti2+">";
- return line;
- }
- /*
- * Deletes brackes and parentheses. Adds HTML text
- * of a <div> container.
- *
- */
- string blocks_ht(string line){
- size_t lla1=line.find("{(");
- if(lla1!=string::npos){
- line=line.erase(lla1,2);
- string nop="<div class=\"";
- line=line.insert(lla1,nop);
- lla1=line.find(")");
- if(lla1!=string::npos){
- nop="\">";
- line=line.erase(lla1,1);
- line=line.insert(lla1,nop);
- lla1=line.rfind("}");
- if(lla1!=string::npos){
- nop="</div>";
- line=line.erase(lla1,1);
- line=line.insert(lla1,nop);
- }else
- throw Invalid("Missing '}' in block tag.",line);
- }else
- throw Invalid("Missing ')' in block tag.",line);
- }else
- throw Invalid("Missing block tag.",line);
- return line;
- }
- /*
- * Deletes brackes and parentheses. Adds HTML text
- * of a <div> container.
- *
- */
- string incomplete_blocks_ht(string line){
- size_t key1=line.find("{(");
- if(key1!= string::npos){
- line.erase(key1,2);
- line.insert(key1,"<div class=\"");
-
- size_t par=line.find(")");
- if(par!=string::npos){
- line.erase(par,1);
- line.insert(par,"\">");
- size_t key2=line.find("}");
- if(key2!=string::npos)
- throw Invalid("Unexpected '}' in incomplete block tag.",line);
- }else
- throw Invalid("Missing ')' in block tag.",line);
- }else
- throw Invalid("Missing block tag.",line);
- return line;
- }
|