diff options
Diffstat (limited to 'src/cite.cpp')
-rw-r--r-- | src/cite.cpp | 343 |
1 files changed, 118 insertions, 225 deletions
diff --git a/src/cite.cpp b/src/cite.cpp index d19dfc1..c684083 100644 --- a/src/cite.cpp +++ b/src/cite.cpp @@ -29,6 +29,10 @@ static const char *doxygen_bst = #include "doxygen_bst.h" ; +static const char *bib2xhtml_pl = +#include "bib2xhtml.h" +; + //-------------------------------------------------------------------------- const QCString CiteConsts::fileName("citelist"); @@ -38,63 +42,28 @@ const QCString CiteConsts::anchorPrefix("CITEREF_"); CiteDict::CiteDict(int size) : m_entries(size, FALSE) { - m_ordering.setAutoDelete(TRUE); m_entries.setAutoDelete(TRUE); } -bool CiteDict::writeAux() +static QCString getListOfBibFiles(const QCString &sep,bool stripExtension) { - //msg("..writing aux file\n"); - QCString auxFileName(m_baseFileName + ".aux"); - QFile auxFile(auxFileName); - if (!auxFile.open(IO_WriteOnly)) - // point it to something valid, because warn() relies on it - { - err("Error opening file %s for output\n", auxFileName.data()); - return FALSE; - } - FTextStream t(&auxFile); - - QDictIterator<CiteInfo> cdi(m_entries); - for (CiteInfo *ci = 0; (ci=cdi.current()); ++cdi) - { - t << "\\citation{" << ci->label << "}\n"; - } - - t << "\\bibstyle{" << m_baseFileName << "}\n"; - - t << "\\bibdata{"; + QCString result; QStrList &citeDataList = Config_getList("CITE_BIB_FILES"); const char *bibdata = citeDataList.first(); while (bibdata) { QCString bibFile = bibdata; - if (!bibFile.isEmpty() && bibFile.right(4)!=".bib") bibFile+=".bib"; + if (stripExtension && bibFile.right(4)==".bib") + { + bibFile = bibFile.left(bibFile.length()-4); + } if (!bibFile.isEmpty()) { - QFileInfo fi(bibFile); // open file (with .bib extension) - if (fi.exists()) - { - if (!copyFile(bibFile,m_baseFileName+"_"+fi.fileName().data())) - { - return FALSE; - } - bibFile = fi.fileName().data(); - if (bibFile.right(4)==".bib") - { - bibFile = bibFile.left(bibFile.length()-4); - } - t << m_baseFileName+"_"+bibFile; // write name (without bib extension) - bibdata = citeDataList.next(); - if (bibdata) - { - t << ","; - } - } - else + result+=bibFile; + bibdata = citeDataList.next(); + if (bibdata) { - err("The file %s specified at CITE_BIB_FILES could not be read!\n",bibdata); - return FALSE; + result+=sep; } } else @@ -102,23 +71,7 @@ bool CiteDict::writeAux() bibdata = citeDataList.next(); } } - t << "}\n"; - return TRUE; -} - -bool CiteDict::writeBst() -{ - //msg("..writing bst file\n"); - QCString fileName = m_baseFileName + ".bst"; - QCString bstData = doxygen_bst; - QFile f(fileName); - if (!f.open(IO_WriteOnly)) - { - err("error: could not open file %s for writing\n",fileName.data()); - return FALSE; - } - f.writeBlock(bstData, bstData.length()); - return TRUE; + return result; } void CiteDict::writeLatexBibliography(FTextStream &t) @@ -127,31 +80,7 @@ void CiteDict::writeLatexBibliography(FTextStream &t) QCString style = Config_getString("LATEX_BIB_STYLE"); if (style.isEmpty()) style="plain"; t << "\\newpage \\bibliographystyle{" << style << "}" << endl; - t << "\\bibliography{"; - QStrList &citeDataList = Config_getList("CITE_BIB_FILES"); - const char *bibdata = citeDataList.first(); - while (bibdata) - { - QCString bibFile = bibdata; - if (bibFile.right(4)==".bib") - { - bibFile = bibFile.left(bibFile.length()-4); - } - if (!bibFile.isEmpty()) - { - t << bibFile; - bibdata = citeDataList.next(); - if (bibdata) - { - t << ","; - } - } - else - { - bibdata = citeDataList.next(); - } - } - t << "}" << endl; + t << "\\bibliography{" << getListOfBibFiles(",",TRUE) << "}" << endl; } void CiteDict::insert(const char *label) @@ -167,130 +96,123 @@ CiteInfo *CiteDict::find(const char *label) const void CiteDict::clear() { m_entries.clear(); - m_ordering.clear(); } -bool CiteDict::execute() +bool CiteDict::isEmpty() const { - //msg("..running bibtex\n"); - bool result=TRUE; - QCString auxFileName(m_baseFileName + ".aux"); - int splitPoint = auxFileName.findRev('/'); - QCString dirname = auxFileName.left(splitPoint); - QCString basename = auxFileName.mid(splitPoint + 1); - QCString oldDir = convertToQCString(QDir::currentDirPath()); - QDir::setCurrent(dirname); - QCString args; - args += "-terse "; - args += basename; - portable_system("bibtex", args); - int exitCode; - if ((exitCode=portable_system("bibtex",args))!=0) - { - err("Problems running bibtex: exit code=%d, command='bibtex', arguments='%s'\n", - exitCode,args.data()); - result=FALSE; - } - QDir::setCurrent(oldDir); - return result; + QStrList &citeBibFiles = Config_getList("CITE_BIB_FILES"); + return (citeBibFiles.count()==0 || m_entries.isEmpty()); } -void CiteDict::parse() +void CiteDict::generatePage() const { - //msg("..parsing bbl file\n"); - QCString bblFileName(m_baseFileName + ".bbl"); - QFile f(bblFileName); - if (!f.open(IO_ReadOnly)) - { - err("error: could not open file %s\n",bblFileName.data()); - return; - } + //printf("** CiteDict::generatePage() count=%d\n",m_ordering.count()); - m_ordering.clear(); + // do not generate an empty citations page + if (isEmpty()) return; // nothing to cite - QTextStream t(&f); - QCString label; - QCString line; - while (!t.eof()) + // 1. generate file with markers and citations to OUTPUT_DIRECTORY + QFile f; + QCString outputDir = Config_getString("OUTPUT_DIRECTORY"); + QCString citeListFile = outputDir+"/citelist.doc"; + f.setName(citeListFile); + if (!f.open(IO_WriteOnly)) { - label=t.readLine(); - if (label.isEmpty()) - { - continue; - } - - CiteInfo* ci = m_entries.find(label); - if (!ci) insert(label); - - // BibTeX has its own way of sorting references, - // depending on the .bst file - m_ordering.append(new QCString(label)); - - ci->text=t.readLine(); - - while (!(line=t.readLine()).isEmpty()) - { - ci->fullText += line + '\n'; - } - - // FIXME: process LaTeX-style accents properly - ci->fullText = substitute(substitute(substitute(ci->fullText, - "{", ""), - "}", ""), - "~", " "); - - //printf("BIB ENTRY: %s -> %s: %s\n", ci->label.data(), ci->text.data(), ci->fullText.data()); + err("error: could not open file %s for writing\n",citeListFile.data()); } -} - -void CiteDict::clean() -{ - // clean - QDir thisDir; - QStrList &citeDataList = Config_getList("CITE_BIB_FILES"); - const char *bibdata = citeDataList.first(); - while (bibdata) + FTextStream t(&f); + t << "<!-- BEGIN CITATIONS -->" << endl; + t << "<!--" << endl; + QDictIterator<CiteInfo> it(m_entries); + CiteInfo *ci; + for (it.toFirst();(ci=it.current());++it) { - QCString bibFile = bibdata; - if (!bibFile.isEmpty() && bibFile.right(4)!=".bib") bibFile+=".bib"; - if (!bibFile.isEmpty()) thisDir.remove(m_baseFileName+"_"+bibFile); - bibdata = citeDataList.next(); + t << "\\citation{" << ci->label << "}" << endl; } - - thisDir.remove(m_baseFileName + ".aux"); - thisDir.remove(m_baseFileName + ".bbl"); - thisDir.remove(m_baseFileName + ".blg"); - thisDir.remove(m_baseFileName + ".tmp"); - thisDir.remove(m_baseFileName + ".bst"); - - QCString &outputDirectory = Config_getString("OUTPUT_DIRECTORY"); - if (outputDirectory.isEmpty()) + t << "-->" << endl; + t << "<!-- END CITATIONS -->" << endl; + t << "<!-- BEGIN BIBLIOGRAPHY -->" << endl; + t << "<!-- END BIBLIOGRAPHY -->" << endl; + f.close(); + + // 2. generate bib2xhtml + QCString bib2xhtmlFile = outputDir+"/bib2xhtml.pl"; + f.setName(bib2xhtmlFile); + QCString bib2xhtml = bib2xhtml_pl; + if (!f.open(IO_WriteOnly)) { - outputDirectory=QDir::currentDirPath(); + err("error: could not open file %s for writing\n",bib2xhtmlFile.data()); } - QDir d(outputDirectory); - d.rmdir("bib"); -} + f.writeBlock(bib2xhtml, bib2xhtml.length()); + f.close(); -void CiteDict::resolve() -{ - QStrList &citeBibFiles = Config_getList("CITE_BIB_FILES"); - if (citeBibFiles.count()==0 || m_entries.count()==0) return; // nothing to cite - - QCString outputDirectory = Config_getString("OUTPUT_DIRECTORY"); - QDir d(outputDirectory); - d.mkdir("bib"); + // 3. generate doxygen.bst + QCString doxygenBstFile = outputDir+"/doxygen.bst"; + QCString bstData = doxygen_bst; + f.setName(doxygenBstFile); + if (!f.open(IO_WriteOnly)) + { + err("error: could not open file %s for writing\n",doxygenBstFile.data()); + } + f.writeBlock(bstData, bstData.length()); + f.close(); - uint pid = portable_pid(); - m_baseFileName.sprintf("doxygen_bibtex_%d",pid); - m_baseFileName.prepend(outputDirectory+"/bib/"); + // 4. run bib2xhtml perl script on the generated file which will insert the + // bibliography in citelist.doc + portable_system("perl",bib2xhtmlFile+" "+getListOfBibFiles(" ",FALSE)+" "+ + citeListFile); - if (writeAux() && writeBst() && execute()) + // 5. read back the file + f.setName(citeListFile); + if (!f.open(IO_ReadOnly)) { - parse(); - clean(); + err("error: could not open file %s/citelist.doc for reading\n",outputDir.data()); } + bool insideBib=FALSE; + + QCString doc; + QFileInfo fi(citeListFile); + QCString input(fi.size()+1); + f.readBlock(input.data(),fi.size()); + input.at(fi.size())='\0'; + int p=0,s; + //printf("input=[%s]\n",input.data()); + while ((s=input.find('\n',p))!=-1) + { + QCString line = input.mid(p,s-p); + //printf("p=%d s=%d line=[%s]\n",p,s,line.data()); + p=s+1; + + if (line.find("<!-- BEGIN BIBLIOGRAPHY")!=-1) insideBib=TRUE; + else if (line.find("<!-- END BIBLIOGRAPH")!=-1) insideBib=FALSE; + else if (insideBib) doc+=line+"\n"; + int i; + // determine text to use at the location of the @cite command + if (insideBib && (i=line.find("<a name=\"CITEREF_"))!=-1) + { + int j=line.find("\">["); + int k=line.find("]</a>"); + if (j!=-1 && k!=-1) + { + QCString label = line.mid(i+17,j-i-17); + QCString number = line.mid(j+2,k-j-1); + CiteInfo *ci = m_entries.find(label); + //printf("label='%s' number='%s' => %p\n",label.data(),number.data(),ci); + if (ci) + { + ci->text = number; + } + } + } + } + //printf("doc=[%s]\n",doc.data()); + + // 6. add it as a page + addRelatedPage(CiteConsts::fileName, + theTranslator->trCiteReferences(),doc,0,CiteConsts::fileName,1,0,0,0); + // 7. for latex we just copy the bib files to the output and let + // latex do this work. if (Config_getBool("GENERATE_LATEX")) { // copy bib files to the latex output dir @@ -316,41 +238,12 @@ void CiteDict::resolve() bibdata = citeDataList.next(); } } -} -bool CiteDict::isEmpty() const -{ - QStrList &citeBibFiles = Config_getList("CITE_BIB_FILES"); - return (citeBibFiles.count()==0 || m_ordering.isEmpty()); -} - -void CiteDict::generatePage() const -{ - //printf("** CiteDict::generatePage() count=%d\n",m_ordering.count()); - - // do not generate an empty citations page - if (isEmpty()) return; // nothing to cite - - QCString doc; - doc += "<dl class=\"citelist\">"; + // 8. Remove temporary files + QDir thisDir; + thisDir.remove(citeListFile); + thisDir.remove(doxygenBstFile); + thisDir.remove(bib2xhtmlFile); - QListIterator<QCString> it(m_ordering); - QCString *s; - for (it.toFirst();(s=it.current());++it) - { - CiteInfo* ci = m_entries.find(*s); - doc += " <dt>"; - doc += "\\anchor "; - doc += CiteConsts::anchorPrefix + ci->label; - doc += "\n"; - doc += ci->text; - doc += "</dt><dd> "; - doc += ci->fullText; - doc += "</dd>"; - } - doc += "</dl>\n"; - //printf("addRelatedPage with doc='%s'\n",doc.data()); - addRelatedPage(CiteConsts::fileName, - theTranslator->trCiteReferences(),doc,0,CiteConsts::fileName,1,0,0,0); } |