Back to index

texmacs  1.0.7.15
bibtex.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : bibtex.cpp
00004 * DESCRIPTION: generating bibliographies using BiBTeX
00005 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "Bibtex/bibtex.hpp"
00013 #include "file.hpp"
00014 #include "sys_utils.hpp"
00015 #include "convert.hpp"
00016 
00017 #ifdef OS_WIN32
00018 #include <sys/misc.h>
00019 #endif
00020 
00021 static string bibtex_command= "bibtex";
00022 
00023 void
00024 set_bibtex_command (string cmd) {
00025   bibtex_command= cmd;
00026 }
00027 
00028 bool
00029 bibtex_present () {
00030   return exists_in_path (bibtex_command);
00031 }
00032 
00033 tree
00034 remove_start_space (tree t) {
00035   if (is_atomic (t)) {
00036     string s= t->label;
00037     if (starts (s, " ")) return s (1, N(s));
00038     else return s;
00039   }
00040   else return t;
00041 }
00042 
00043 tree
00044 search_bib (tree t) {
00045   if (is_atomic (t)) return "";
00046   else if (is_compound (t, "thebibliography", 2) && is_document (t[1]))
00047     return t;
00048   else {
00049     int i, n= N(t);
00050     for (i=0; i<n; i++) {
00051       tree r= search_bib (t[i]);
00052       if (r != "") return r;
00053     }
00054     return "";
00055   }
00056 }
00057 
00058 tree
00059 bibtex_load_bbl (string bib, url bbl_file) {
00060   string result;
00061   if (load_string (bbl_file, result, false))
00062     return "Error: bibtex failed to create bibliography";
00063 
00064   int count=1;
00065   tree t= generic_to_tree (result, "latex-snippet");
00066   t= search_bib (t);
00067   if (t == "") return "";
00068   tree largest= t[0];
00069   t= t[1];
00070 
00071   tree u (DOCUMENT);
00072   for (int i=0; i<arity(t); i++) {
00073     if (is_concat (t[i]) &&
00074        (is_compound (t[i][0], "bibitem") ||
00075         is_compound (t[i][0], "bibitem*")))
00076       {
00077        tree item= t[i][0];
00078        if (is_compound (item, "bibitem"))
00079          item= compound ("bibitem*", as_string (count++), item[0]);
00080        t[i][0]= item;
00081        tree v (CONCAT, compound ("bibitem*", item[0]));
00082        if (is_atomic (item[1]))
00083          v << tree (LABEL, bib * "-" * item[1]->label);
00084        if (N(t[i])>1) {
00085          v << remove_start_space (t[i][1]);
00086          v << A (t[i] (2, N(t[i])));
00087        }
00088        u << v;
00089       }
00090   }
00091 
00092   if (N(u) == 0) u= tree (DOCUMENT, "");
00093   return compound ("bib-list", largest, u);
00094 }
00095 
00096 tree
00097 bibtex_run (string bib, string style, url bib_file, tree bib_t) {
00098   int i;
00099   string bib_s= "\\bibstyle{" * style * "}\n";
00100   for (i=0; i<arity(bib_t); i++)
00101     bib_s << "\\citation{" << as_string (bib_t[i]) << "}\n";
00102 
00103   string dir= concretize (head (bib_file));
00104   string bib_name= as_string (tail (bib_file));
00105   if ((N(bib_name) >= 4) && (bib_name (N(bib_name)-4, N(bib_name)) == ".bib"))
00106     bib_name= bib_name (0, N(bib_name)- 4);
00107   bib_s << "\\bibdata{" << bib_name << "}\n";
00108   save_string ("$TEXMACS_HOME_PATH/system/bib/temp.aux", bib_s);
00109 
00110 #ifdef OS_WIN32
00111   char *directory = as_charp (dir);
00112   RunBibtex(directory, "$TEXMACS_HOME_PATH/system/bib", "temp");
00113   tm_delete_array (directory);
00114 #else
00115   string cmdln= "cd $TEXMACS_HOME_PATH/system/bib; ";
00116   cmdln << "BIBINPUTS=" << dir << ":$BIBINPUTS "
00117        << "BSTINPUTS=" << dir << ":$BSTINPUTS "
00118        << bibtex_command << " temp";
00119   if (DEBUG_AUTO) cout << "TeXmacs] BibTeX command: " << cmdln << "\n";
00120   system (cmdln);
00121 #endif
00122 
00123   return bibtex_load_bbl (bib, "$TEXMACS_HOME_PATH/system/bib/temp.bbl");
00124   /*
00125   string result;
00126   if (load_string ("$TEXMACS_HOME_PATH/system/bib/temp.bbl", result, false))
00127     return "Error: bibtex failed to create bibliography";
00128 
00129   int count=1;
00130   tree t= generic_to_tree (result, "latex-snippet");
00131   t= search_bib (t);
00132   if (t == "") return "";
00133   tree largest= t[0];
00134   t= t[1];
00135   tree u (DOCUMENT);
00136   for (i=0; i<arity(t); i++) {
00137     if (is_concat (t[i]) &&
00138        (is_compound (t[i][0], "bibitem") ||
00139         is_compound (t[i][0], "bibitem*")))
00140       {
00141        tree item= t[i][0];
00142        if (is_compound (item, "bibitem"))
00143          item= compound ("bibitem*", as_string (count++), item[0]);
00144        t[i][0]= item;
00145        tree v (CONCAT, compound ("bibitem*", item[0]));
00146        if (is_atomic (item[1]))
00147          v << tree (LABEL, bib * "-" * item[1]->label);
00148        if (N(t[i])>1) {
00149          v << remove_start_space (t[i][1]);
00150          v << A (t[i] (2, N(t[i])));
00151        }
00152        u << v;
00153       }
00154   }
00155   if (N(u) == 0) u= tree (DOCUMENT, "");
00156   return compound ("bib-list", largest, u);
00157   */
00158 }