00001 #include "htmlbox.h" 00002 #include <fstream> 00003 #include <sstream> 00004 #include <iostream> 00005 //#include <boost/regex.hpp> 00006 00007 Node* HTMLBox::Putter::transform(Node* node) const{ 00008 if (node->getName() == CInlineHTML){ 00009 InlineHTML *inlineHTML = dynamic_cast<InlineHTML*>(node); 00010 htmlBox.putHTML(inlineHTML->getHTML()); 00011 } 00012 return node; 00013 }; 00014 00015 Node* HTMLBox::Replacer::transform(Node* node) const{ 00016 if (node->getName() == CInlineHTML){ 00017 InlineHTML *inlineHTML = dynamic_cast<InlineHTML*>(node); 00018 inlineHTML->setHTML(htmlBox.getHTML(inlineHTML->getHTML())); 00019 } 00020 return node; 00021 }; 00022 00023 00024 string HTMLBox::generateIncludeName(){ 00025 usedNames++; 00026 stringstream str; 00027 str << "include" << usedNames << ".tpl"; 00028 return str.str(); 00029 }; 00030 00032 00036 //#define ciebie_debug 00037 string HTMLBox::normalize(const string &html) 00038 { 00039 /* 00040 boost::regex e; 00041 string norm; 00042 //TODO: lowercase of tags 00043 try 00044 { 00045 e.assign("([[:space:]]+)"); 00046 } 00047 catch (...) 00048 { 00049 cout << "Zepsute wyrazenie regularne" << endl; 00050 exit(-1); 00051 } 00052 #ifdef ciebie_debug 00053 cout << "Normalizing " << html << "gives" << 00054 boost::regex_merge(html, e, "(?1 )") << endl; 00055 #endif*/ 00056 return html; 00057 } 00058 00059 00060 00062 00067 void HTMLBox::putHTML(const string &html){ 00068 normalize(html); 00069 switch (collectedHTML[html].occurences){ 00070 case 0: 00071 collectedHTML[html].html = html; 00072 collectedHTML[html].occurences++; 00073 break; 00074 case 1:{ 00075 string includeName = generateIncludeName(); 00076 ofstream includeOfs((outDir+includeName).c_str()); 00077 includeOfs << collectedHTML[html].html; 00078 collectedHTML[html].html = "{include file=\"" +includeName + "\"}"; 00079 collectedHTML[html].occurences++; 00080 } 00081 break; 00082 default: 00083 collectedHTML[html].occurences++; 00084 } 00085 } 00086 00087 void HTMLBox::regenerate() 00088 { 00089 regenerated = 1; 00090 cout << "Regenerating" << endl; 00091 }; 00092 00094 string HTMLBox::getHTML(const string &html){ 00095 if (regenerated == 0) regenerate(); 00096 return collectedHTML[html].html; 00097 }; 00098 00099