]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetBibtex.cpp
Pure HTML output for math macros.
[lyx.git] / src / insets / InsetBibtex.cpp
index 55a20edefb3578735703665f4764b394ca1400ef..dff066f12c69a0551ac05ae433005286d8fb5cab 100644 (file)
@@ -21,7 +21,7 @@
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "LaTeXFeatures.h"
-#include "MetricsInfo.h"
+#include "output_xhtml.h"
 #include "OutputParams.h"
 #include "TextClass.h"
 
@@ -49,9 +49,18 @@ namespace Alert = frontend::Alert;
 namespace os = support::os;
 
 
-InsetBibtex::InsetBibtex(InsetCommandParams const & p)
-       : InsetCommand(p, "bibtex")
-{}
+InsetBibtex::InsetBibtex(Buffer * buf, InsetCommandParams const & p)
+       : InsetCommand(buf, p, "bibtex")
+{
+       buffer_->invalidateBibinfoCache();
+}
+
+
+InsetBibtex::~InsetBibtex()
+{
+       if (isBufferLoaded())
+               buffer_->invalidateBibinfoCache();
+}
 
 
 ParamInfo const & InsetBibtex::findInfo(string const & /* cmdName */)
@@ -209,7 +218,7 @@ static string normalizeName(Buffer const & buffer,
        OutputParams const & runparams, string const & name, string const & ext)
 {
        string const fname = makeAbsPath(name, buffer.filePath()).absFilename();
-       if (FileName(name).isAbsolute() || !FileName(fname + ext).isReadableFile())
+       if (FileName::isAbsolute(name) || !FileName(fname + ext).isReadableFile())
                return name;
        if (!runparams.nice)
                return fname;
@@ -258,7 +267,6 @@ int InsetBibtex::latex(odocstream & os, OutputParams const & runparams) const
 
                if (!runparams.inComment && !runparams.dryrun && !runparams.nice &&
                    not_from_texmf) {
-
                        // mangledFilename() needs the extension
                        DocFileName const in_file = DocFileName(try_in_file);
                        database = removeExtension(in_file.mangledFilename());
@@ -293,7 +301,6 @@ int InsetBibtex::latex(odocstream & os, OutputParams const & runparams) const
        if (!warned_about_spaces &&
            runparams.nice && db_out.find(' ') != docstring::npos) {
                warned_about_spaces = true;
-
                Alert::warning(_("Export Warning!"),
                               _("There are spaces in the paths to your BibTeX databases.\n"
                                              "BibTeX will be unable to find them."));
@@ -307,7 +314,6 @@ int InsetBibtex::latex(odocstream & os, OutputParams const & runparams) const
                        style = split(style, bibtotoc, ',');
        }
 
-
        // line count
        int nlines = 0;
 
@@ -401,8 +407,7 @@ support::FileNameList InsetBibtex::getBibFiles() const
        vector<docstring>::const_iterator it = bibfilelist.begin();
        vector<docstring>::const_iterator en = bibfilelist.end();
        for (; it != en; ++it) {
-               FileName const file = 
-                       findtexfile(changeExtension(to_utf8(*it), "bib"), "bib");
+               FileName const file = getBibTeXPath(*it, buffer());
 
                if (!file.empty())
                        vec.push_back(file);
@@ -424,7 +429,7 @@ namespace {
        /// and further whitespace characters from the stream.
        /// @return true if a comma was found, false otherwise
        ///
-       bool removeWSAndComma(idocfstream & ifs) {
+       bool removeWSAndComma(ifdocstream & ifs) {
                char_type ch;
 
                if (!ifs)
@@ -467,7 +472,7 @@ namespace {
        ///
        /// @return true if a string of length > 0 could be read.
        ///
-       bool readTypeOrKey(docstring & val, idocfstream & ifs,
+       bool readTypeOrKey(docstring & val, ifdocstream & ifs,
                docstring const & delimChars, docstring const &illegalChars, 
                charCase chCase) {
 
@@ -522,7 +527,7 @@ namespace {
        /// the variable strings.
        /// @return true if reading was successfull (all single parts were delimited
        /// correctly)
-       bool readValue(docstring & val, idocfstream & ifs, const VarMap & strings) {
+       bool readValue(docstring & val, ifdocstream & ifs, const VarMap & strings) {
 
                char_type ch;
 
@@ -678,7 +683,7 @@ void InsetBibtex::fillWithBibKeys(BiblioInfo & keylist,
        support::FileNameList::const_iterator it = files.begin();
        support::FileNameList::const_iterator en = files.end();
        for (; it != en; ++ it) {
-               idocfstream ifs(it->toFilesystemEncoding().c_str(),
+               ifdocstream ifs(it->toFilesystemEncoding().c_str(),
                        ios_base::in, buffer().params().encoding().iconvName());
 
                char_type ch;
@@ -891,6 +896,77 @@ void InsetBibtex::validate(LaTeXFeatures & features) const
 {
        if (features.bufferParams().use_bibtopic)
                features.require("bibtopic");
+       // FIXME XHTML
+       // It'd be better to be able to get this from an InsetLayout, but at present
+       // InsetLayouts do not seem really to work for things that aren't InsetTexts.
+       if (features.runparams().flavor == OutputParams::HTML)
+               features.addPreambleSnippet("<style type=\"text/css\">\n"
+                       "div.bibtexentry { margin-left: 2em; text-indent: -2em; }\n"
+                       "span.bibtexlabel:before{ content: \"[\"; }\n"
+                       "span.bibtexlabel:after{ content: \"] \"; }\n"
+                       "</style>");
+}
+
+
+// FIXME 
+// docstring InsetBibtex::entriesAsXHTML(vector<docstring> const & entries)
+// And then here just: entriesAsXHTML(buffer().masterBibInfo().citedEntries())
+docstring InsetBibtex::xhtml(XHTMLStream & xs, OutputParams const &) const
+{
+       BiblioInfo const & bibinfo = buffer().masterBibInfo();
+       vector<docstring> const & cites = bibinfo.citedEntries();
+       CiteEngine const engine = buffer().params().citeEngine();
+       bool const numbers = 
+               (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL);
+
+       xs << html::StartTag("h2", "class='bibtex'")
+               << _("References")
+               << html::EndTag("h2")
+               << html::StartTag("div", "class='bibtex'");
+
+       // Now we loop over the entries
+       vector<docstring>::const_iterator vit = cites.begin();
+       vector<docstring>::const_iterator const ven = cites.end();
+       for (; vit != ven; ++vit) {
+               BiblioInfo::const_iterator const biit = bibinfo.find(*vit);
+               if (biit == bibinfo.end())
+                       continue;
+               BibTeXInfo const & entry = biit->second;
+               xs << html::StartTag("div", "class='bibtexentry'");
+               // FIXME XHTML
+               // The same name/id problem we have elsewhere.
+               string const attr = "id='" + to_utf8(entry.key()) + "'";
+               xs << html::CompTag("a", attr);
+               docstring citekey;
+               if (numbers)
+                       citekey = entry.citeNumber();
+               else {
+                       docstring const auth = entry.getAbbreviatedAuthor();
+                       // we do it this way so as to access the xref, if necessary
+                       // note that this also gives us the modifier
+                       docstring const year = bibinfo.getYear(*vit, true);
+                       if (!auth.empty() && !year.empty())
+                               citekey = auth + ' ' + year;
+               }
+               if (citekey.empty()) {
+                       citekey = entry.label();
+                       if (citekey.empty())
+                               citekey = entry.key();
+               }
+               xs << html::StartTag("span", "class='bibtexlabel'")
+                       << citekey
+                       << html::EndTag("span");
+               // FIXME Right now, we are calling BibInfo::getInfo on the key,
+               // which will give us all the cross-referenced info. But for every
+               // entry, so there's a lot of repitition. This should be fixed.
+               xs << html::StartTag("span", "class='bibtexinfo'") 
+                       << bibinfo.getInfo(entry.key(), buffer(), true)
+                       << html::EndTag("span")
+                       << html::EndTag("div");
+               xs.cr();
+       }
+       xs << html::EndTag("div");
+       return docstring();
 }