📄 refere~1.c
字号:
} /** replaces the .url in all directory references with ALIAS with the relative path of the other directory with the same title. Also sets the children pointer to point to the correct place. * dir is the number of "../../" needed to get back to the top. * index is the varvalues[INDEX] * backdirsep is varvalues[BACKDIRSEP] * * It also sets the .url of all other folders to point * to SEARCHTOROOTPATH + path to folder + index.html. * * It also sorts all folders, as per the comment section in the folder. */void referenceTree::fixAliases(referenceTree * rootRT, const string & dir, const string varValues [], const string & path){ doSort(); //sort, if needed (as per comment) for (vector<reference>::iterator i = contents.begin(); i != contents.end(); ++i){ reference& r = *i; if (r.isFolder() && r.isAliasof()){ string realPath; string title; if (r.aliasDir == "") { realPath = rootRT->getPath(r.dirname, dir, varValues[SUBDIRSEP]); title = r.title; } else { realPath = rootRT->getPath(r.aliasDir, dir, varValues[SUBDIRSEP]); title = r.aliasDir; } if (realPath == "") { cerr << "ERROR: Folder " << title << " has an ALIAS on it, but I cannot find the folder it points to." << endl; cerr << " I am ignoring it." << endl; continue; }; r.url = realPath + varValues[INDEX]; r.children = rootRT->getSubtree(title); r.comment = (r.children)->comment; } else if (r.isFolder()) { r.url = varValues[SEARCHTOROOTPATH] + path + name2filename(r.dirname) + varValues[SUBDIRSEP] + varValues[INDEX]; } }; for (vector<reference>::iterator i = contents.begin(); i != contents.end(); ++i){ reference& r = *i; if (r.isFolder() && !(r.isAliasof())){ string newpath = path + name2filename(r.dirname) + varValues[SUBDIRSEP]; string newdir = dir + varValues[BACKDIRSEP]; (r.children)->fixAliases(rootRT, newdir , varValues, newpath); r.comment = (r.children)->comment; //since the doSort changes the comments. } };}/** returns a subtree of this rt which has topName as its title * does a BFS. topName MUST be a directory (i.e. have some children) * otherwise it will not be found. */referenceTree * referenceTree::getSubtree(const string & topName){ if (topName == "") return this; for (vector<reference>::const_iterator i = contents.begin(); i != contents.end(); ++i){ const reference& r = *i; if ((r.title == topName) && (r.children !=0) && !(r.isAliasof())) { return r.children; } } for (vector<reference>::const_iterator i = contents.begin(); i != contents.end(); ++i){ const reference& r = *i; if (r.children){ referenceTree * res = (r.children)->getSubtree(topName); if (res != 0) return res; } } return 0;}/**Fix the numLeafs variable for all nodes * returns the number of leafs below me. */int referenceTree::fixNumChildren() { int num = 0; for (vector<reference>::iterator i = contents.begin(); i != contents.end(); ++i){ reference& r = *i; if (r.children != 0 ) num += r.children->fixNumChildren(); else num++; } numLeafs = num; return num;}/** Add rt to me (i.e. this referenceTree). * if folder == "" then add it here */void referenceTree::addTree(const referenceTree & rt){ for (vector<reference>::const_iterator i = rt.contents.begin(); i != rt.contents.end(); ++i){ const reference &r = *i; if (!r.isFolder()){ int alreadyThere = 0; for (vector<reference>::const_iterator j = contents.begin(); j != contents.end(); ++j){ if ((*j).url == r.url) { alreadyThere = 1; break; } }; if (!alreadyThere){ reference r2(r); if (r2.aliasOf != ""){ int x = atoi(r.aliasOf.c_str()) + numMerges; r2.aliasOf = myItoa(x); }; if (r2.aliasID != ""){ int x = atoi(r.aliasID.c_str()) + numMerges; r2.aliasID = myItoa(x); }; cout << "Adding " << r2.title << "-" << r2.url << " to main tree." << endl; contents.push_back(r2);//add a leaf } } else { //its a folder referenceTree * rtchild = 0; for (vector<reference>::iterator j = contents.begin(); j != contents.end(); ++j){ reference &rc = *j; if (rc.isFolder() && (rc.title == r.title)) { rtchild = rc.children; break; }; } if (rtchild == 0) { //did not find folder, so create new one. reference newref(r); contents.push_back(newref); } else rtchild->addTree(*(r.children)); //add a folder } } fixNumChildren();}/** Merge me with rt. A merge of two trees is done by taking * the second tree (rt) and findin a folder under it with the * comment PUBLISH. The title of that folder is then found on * this tree, thereby matching the two roots of the trees. All * the descendants of these root nodes are merged together. If * rt has folders that do not appear under "this" tree, the folders * are created. */void referenceTree::merge(const referenceTree & rt){ numMerges +=100; //increase by 100, we add this number to all the aliases for (vector<reference>::const_iterator i = rt.contents.begin(); i != rt.contents.end(); ++i){ const reference& r = *i; if (r.isFolder() && (r.comment == PUBLISH)) { cout << "Found PUBLISH in " << r.title << endl; referenceTree * myNode = getSubtree(r.title); if (myNode == 0){ cerr << "ERROR: Tried to merge folder " << r.title << " but it was not found in main tree" << endl; continue; }; myNode->addTree(*(r.children)); fixNumChildren(); } else if (r.isFolder()) { //try the subtrees merge(*(r.children)); } }}/** Flattens out rt and adds all the LEAFS (urls)(only) to the list lr. returns the number of elements added. Only adds URLS that are not Private and not Aliasof another one and are not under avoidFolder. */void referenceTree::makeVector(vector<reference> & vr, const string & avoidFolder = ""){ for (vector<reference>::iterator i = contents.begin(); i != contents.end(); ++i){ reference & r = *i; if (!(r.isFolder()) && !(r.isPrivate()) && !(r.isAliasof())) vr.push_back(r); else if (r.isFolder() && !(r.isPrivate()) && !(r.isAliasof())) if (r.title != avoidFolder) (r.children)->makeVector(vr, avoidFolder); }}/**Generates the website. */void referenceTree::createSite(const string varValues[], channelContainer & channels) { vector<reference> allReferences; makeVector(allReferences, varValues[NEWSTOPFOLDER]); int numReferences = allReferences.size(); cout << "We are using " << numReferences << " unique bookmarks." << endl; string ifileName; ifileName = varValues[INDEXFILENAME]; fileView baseView(ifileName); ifileName = varValues[OTHERFILENAME]; fileView otherView(ifileName); vector<reference> newsItems; if (varValues[NEWSTOPFOLDER] != ""){ referenceTree * rt = getSubtree(varValues[NEWSTOPFOLDER]); if (rt == 0) { cerr << "ERROR: News folder named " << varValues[NEWSTOPFOLDER] << " was not found in " << varValues[BOOKMARKFILE] << endl; return; } newsItems = rt->contents; } // cout << "newsItems.size()=" << newsItems.size() << endl; createSiteH(varValues,allReferences, channels, baseView, otherView, newsItems, 0, "","", varValues[TOP], "", ""); //write out the urls.db for the search program (if needed) if (varValues[SEARCH] != ""){ ofstream dbase(varValues[SEARCH].c_str(), ios::out); if (dbase == 0){ cout << "Could not open " << varValues[SEARCH] << " for output." << endl; return; } cout << "Writing " << varValues[SEARCH] << endl; dbase << urlsDBHeader << endl; dbase << varValues[SEARCHURLTEMPLATE] << endl << varValues[NEWGIF] << endl << varValues[TIMECUTOFF] << endl; // dbase << *this; writeAsFlatFile(dbase); }}/** createsiteHelper, recursively calls itself for each page.*/void referenceTree::createSiteH(const string varValues[], vector<reference> & allReferences, channelContainer & channels, fileView & baseView, fileView & otherView, vector<reference> & newsItems, int depth = 0, string navigateBar = "", string searchNavBar = "", string parentTitle = "", string folderTitle="", string filePath = "") { string ofileName; fileView & fv = baseView; string fileContents; if (depth == 0){ ofileName = varValues[DESTDIR] + varValues[INDEX]; // els = elsBase; // fileContents = fileContentsB; fv = baseView; } else { unsigned short mode = 493; if (varValues[SUBDIRSEP]=="/") { //hierarchical filesystem string dirName = varValues[DESTDIR] + filePath; mkdir(dirName.c_str(), mode); }; ofileName = varValues[DESTDIR] + filePath + varValues[INDEX]; // els = elsOther; // fileContents = fileContentsO; fv = otherView; } //replace all the comments for their values. fileContents = fv.sendAsHTML(contents, allReferences, newsItems, varValues, channels); //replace title and navigatebar in fileContents string key = placeholder + ":" + NAVIGATEBAR_TAG + placeholderEnd; string fullNavBar = navigateBar + " " + parentTitle; replaceAll(fileContents, key, fullNavBar); key= placeholder + ":" + TITLE_TAG + placeholderEnd; string newTitle = varValues[TITLE] + ": " + parentTitle; replaceAll(fileContents, key, newTitle); // replace search in fileContents - btb@debian.org key = placeholder + ":" + SEARCH_TAG + placeholderEnd; replaceAll(fileContents, key, varValues[SEARCH]); // replace the path key = placeholder + ":" + PATH_TAG + placeholderEnd; replaceAll(fileContents, key, filePath); // replace current date - august.hoerandl@gmx.at key= placeholder + ":" + DATE_TAG + placeholderEnd; time_t now = time(0); string newDate = asctime( localtime(&now) ); replaceAll(fileContents, key, newDate); //write out the file cout << "Writing " << ofileName << endl; ofstream ofile(ofileName.c_str(), ios::out); if (!(ofile)){ cerr << "Could not open output file " << ofileName << endl; return; } ofile << fileContents; ofile.close(); // time_t currentTime2 = time(0); //.tv_sec; // cout << "Time = " << currentTime2 - currentTime << endl; //determine the naviatebar for this folder. string newNavigateBar; string newSearchNavBar; //for navBar, which is only used by search.pl. if (varValues[SUBDIRSEP] == "/") { replaceAll(navigateBar, "href=\"" + varValues[BACKDIRSEP], "href=\"" +varValues[BACKDIRSEP] + varValues[BACKDIRSEP]); newNavigateBar= navigateBar + "<a href=\"" + varValues[BACKDIRSEP] + varValues[INDEX] + "\"><B>" + parentTitle + "</B></A><B>:</B> "; } else //flat file system newNavigateBar= navigateBar + "<a href=\"" + filePath + varValues[INDEX] + "\"><B>" + parentTitle + "</B></A><B>:</B> "; newSearchNavBar = searchNavBar + "<a href=\"" + varValues[SEARCHTOROOTPATH] + filePath + varValues[INDEX] + "\"><B>" + parentTitle + "</B></A><B>:</B> "; //Now, do a page for each of the directories. for (vector<reference>::iterator j = contents.begin(); j != contents.end(); ++j){ reference & r = *j; r.navBar = newSearchNavBar; if ((r.children != 0) && !(r.isAliasof()) && !(r.isPrivate())){ string newFilePath = filePath + name2filename(r.dirname) + varValues[SUBDIRSEP]; r.children->createSiteH(varValues, allReferences, channels, baseView, otherView, newsItems, depth+1, newNavigateBar, newSearchNavBar, r.title, folderTitle, newFilePath); } }}/** Creates a single page. This implements the extrafilebase, extrafilename feature.*/void referenceTree::createPage(const string indexFileName, const string ofileName, const string varValues [], channelContainer & channels){ vector<reference> allReferences; makeVector(allReferences, varValues[NEWSTOPFOLDER]); // for (vector<reference>::iterator i = allReferences.begin(); i != allReferences.end(); ++i){ // reference &r = *i; // cout << r.title << endl; // }; vector<reference> newsItems; if (varValues[NEWSTOPFOLDER] != ""){ referenceTree * rt = getSubtree(varValues[NEWSTOPFOLDER]); if (rt == 0) { cerr << "ERROR: News folder named " << varValues[NEWSTOPFOLDER] << " was not found in " << varValues[BOOKMARKFILE] << endl; return; } newsItems = rt->contents; } fileView fv(indexFileName); vector<reference> contents; string fileContents = fv.sendAsHTML(contents, allReferences, newsItems, varValues, channels); string key= placeholder + ":" + TITLE_TAG + placeholderEnd; replaceAll(fileContents, key, varValues[TITLE]); // replace search in fileContents - btb@debian.org key = placeholder + ":" + SEARCH_TAG + placeholderEnd; replaceAll(fileContents, key, varValues[SEARCH]); // replace the path with nothing, in case they left it by mistake. key = placeholder + ":" + PATH_TAG + placeholderEnd; replaceAll(fileContents, key, ""); // replace current date - august.hoerandl@gmx.at key= placeholder + ":" + DATE_TAG + placeholderEnd; time_t now = time(0); string newDate = asctime( localtime(&now) ); replaceAll(fileContents, key, newDate); cout << "Writing " << ofileName << endl; ofstream ofile(ofileName.c_str(), ios::out); if (!(ofile)){ cerr << "Could not open output file " << ofileName << endl; return; } ofile << fileContents; ofile.close();}/** Does sort of all folders, as per comment on folder, then eliminates the instructions from the comment.*/void referenceTree::doSort(){ string::size_type start = comment.find("sort"); // if (cmpNoCase(comment.substr(0,4), "sort") == 0) { //do we need to sort this folder? if (start != string::npos){ //yes, need to sort // cout << "Found Sort: " << comment << endl; if (cmpNoCase(comment.substr(start,20), "sort creation normal") == 0){ sort(contents.begin(), contents.end(), referenceCmpCreation()); replaceAllNC(comment, "sort creation normal", ""); } else if (cmpNoCase(comment.substr(start,21), "sort creation inverse") == 0){ sort(contents.begin(), contents.end(), referenceCmpCreationI()); replaceAllNC(comment, "sort creation inverse", ""); } else if (cmpNoCase(comment.substr(start,17), "sort title normal") == 0){ sort(contents.begin(), contents.end(), referenceCmpTitle()); replaceAllNC(comment, "sort title normal", ""); } else if (cmpNoCase(comment.substr(start,18), "sort title inverse") == 0){ sort(contents.begin(), contents.end(), referenceCmpTitleI()); replaceAllNC(comment, "sort title inverse", ""); } else if (cmpNoCase(comment.substr(start,20), "sort modified normal") == 0){ sort(contents.begin(), contents.end(), referenceCmpModified()); replaceAllNC(comment, "sort modified normal", ""); } else if (cmpNoCase(comment.substr(start,21), "sort modified inverse") == 0){ sort(contents.begin(), contents.end(), referenceCmpModifiedI()); replaceAllNC(comment, "sort modified inverse", ""); } else if (cmpNoCase(comment.substr(start,17), "sort visit normal") == 0){ sort(contents.begin(), contents.end(), referenceCmpVisit()); replaceAllNC(comment, "sort visit normal", ""); } else if (cmpNoCase(comment.substr(start,18), "sort visit inverse") == 0){ sort(contents.begin(), contents.end(), referenceCmpVisitI()); replaceAllNC(comment, "sort visit inverse", ""); } else if (cmpNoCase(comment.substr(start,18), "sort hits normal") == 0){ sort(contents.begin(), contents.end(), referenceCmpHits()); replaceAllNC(comment, "sort hits normal", ""); } else if (cmpNoCase(comment.substr(start,18), "sort hits inverse") == 0){ sort(contents.begin(), contents.end(), referenceCmpHitsI()); replaceAllNC(comment, "sort hits inverse", ""); } else cerr << "Did not understand sort comment: " << comment << endl; } //recursively sort all children for (vector<reference>::iterator i = contents.begin(); i != contents.end(); ++i){ reference& r = *i; if (r.isFolder() && !r.isAliasof()) { r.commentCommands = r.children->comment; r.children->doSort(); } } }// -*-// Local Variables:// mode: C++// tab-width: 4// c-basic-offset:2// indent-tabs-mode: nil // End:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -