2 * \file InsetBibtex.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
7 * \author Richard Kimberly Heck (BibTeX parser improvements)
8 * \author Jürgen Spitzmüller
10 * Full author contact details are available in file CREDITS.
15 #include "InsetBibtex.h"
17 #include "BiblioInfo.h"
19 #include "BufferParams.h"
20 #include "CiteEnginesList.h"
22 #include "DispatchResult.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
29 #include "output_latex.h"
31 #include "PDFOptions.h"
32 #include "texstream.h"
33 #include "TextClass.h"
34 #include "TocBackend.h"
36 #include "frontends/alert.h"
38 #include "support/convert.h"
39 #include "support/debug.h"
40 #include "support/docstream.h"
41 #include "support/docstring_list.h"
42 #include "support/ExceptionMessage.h"
43 #include "support/FileNameList.h"
44 #include "support/filetools.h"
45 #include "support/gettext.h"
46 #include "support/lstrings.h"
47 #include "support/os.h"
48 #include "support/PathChanger.h"
49 #include "support/textutils.h"
59 using namespace lyx::support;
63 namespace Alert = frontend::Alert;
64 namespace os = support::os;
67 InsetBibtex::InsetBibtex(Buffer * buf, InsetCommandParams const & p)
68 : InsetCommand(buf, p)
72 ParamInfo const & InsetBibtex::findInfo(string const & /* cmdName */)
74 static ParamInfo param_info_;
75 if (param_info_.empty()) {
76 param_info_.add("btprint", ParamInfo::LATEX_OPTIONAL);
77 param_info_.add("bibfiles", ParamInfo::LATEX_REQUIRED);
78 param_info_.add("options", ParamInfo::LYX_INTERNAL);
79 param_info_.add("encoding", ParamInfo::LYX_INTERNAL);
80 param_info_.add("file_encodings", ParamInfo::LYX_INTERNAL);
81 param_info_.add("biblatexopts", ParamInfo::LATEX_OPTIONAL);
87 void InsetBibtex::doDispatch(Cursor & cur, FuncRequest & cmd)
89 switch (cmd.action()) {
92 editDatabases(cmd.argument());
95 case LFUN_INSET_MODIFY: {
96 InsetCommandParams p(BIBTEX_CODE);
98 if (!InsetCommand::string2params(to_utf8(cmd.argument()), p)) {
102 } catch (ExceptionMessage const & message) {
103 if (message.type_ == WarningException) {
104 Alert::warning(message.title_, message.details_);
105 cur.noScreenUpdate();
113 cur.buffer()->clearBibFileCache();
114 cur.forceBufferUpdate();
119 InsetCommand::doDispatch(cur, cmd);
125 bool InsetBibtex::getStatus(Cursor & cur, FuncRequest const & cmd,
126 FuncStatus & flag) const
128 switch (cmd.action()) {
129 case LFUN_INSET_EDIT:
130 flag.setEnabled(true);
134 return InsetCommand::getStatus(cur, cmd, flag);
139 void InsetBibtex::editDatabases(docstring const & db) const
141 vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
143 if (bibfilelist.empty())
146 size_t nr_databases = bibfilelist.size();
147 if (nr_databases > 1 && db.empty()) {
148 docstring const engine = usingBiblatex() ? _("Biblatex") : _("BibTeX");
149 docstring message = bformat(_("The %1$s[[BibTeX/Biblatex]] inset includes %2$s databases.\n"
150 "If you proceed, all of them will be opened."),
151 engine, convert<docstring>(nr_databases));
152 int const ret = Alert::prompt(_("Open Databases?"),
153 message, 0, 1, _("&Cancel"), _("&Proceed"));
159 vector<docstring>::const_iterator it = bibfilelist.begin();
160 vector<docstring>::const_iterator en = bibfilelist.end();
161 for (; it != en; ++it) {
162 if (!db.empty() && db != *it)
164 FileName const bibfile = buffer().getBibfilePath(*it);
165 theFormats().edit(buffer(), bibfile,
166 theFormats().getFormatFromFile(bibfile));
171 bool InsetBibtex::usingBiblatex() const
173 return buffer().masterParams().useBiblatex();
177 docstring InsetBibtex::screenLabel() const
179 return usingBiblatex() ? _("Biblatex Generated Bibliography")
180 : _("BibTeX Generated Bibliography");
184 docstring InsetBibtex::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
186 docstring tip = _("Databases:");
187 vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
190 if (bibfilelist.empty())
191 tip += "<li>" + _("none") + "</li>";
193 for (docstring const & bibfile : bibfilelist)
194 tip += "<li>" + bibfile + "</li>";
199 docstring style = getParam("options"); // maybe empty! and with bibtotoc
200 docstring bibtotoc = from_ascii("bibtotoc");
201 if (prefixIs(style, bibtotoc)) {
203 if (contains(style, char_type(',')))
204 style = split(style, bibtotoc, char_type(','));
207 docstring const btprint = getParam("btprint");
208 if (!usingBiblatex()) {
209 tip += _("Style File:");
210 tip += "<ul><li>" + (style.empty() ? _("none") : style) + "</li></ul>";
212 tip += _("Lists:") + " ";
213 if (btprint == "btPrintAll")
214 tip += _("all references");
215 else if (btprint == "btPrintNotCited")
216 tip += _("all uncited references");
218 tip += _("all cited references");
221 tip += _("included in TOC");
223 if (!buffer().parent()
224 && buffer().params().multibib == "child") {
226 tip += _("Note: This bibliography is not output, since bibliographies in the master file "
227 "are not allowed with the setting 'Multiple bibliographies per child document'");
230 tip += _("Lists:") + " ";
231 if (btprint == "bibbysection")
232 tip += _("all reference units");
233 else if (btprint == "btPrintAll")
234 tip += _("all references");
236 tip += _("all cited references");
239 tip += _("included in TOC");
241 if (!getParam("biblatexopts").empty()) {
243 tip += _("Options: ") + getParam("biblatexopts");
251 void InsetBibtex::latex(otexstream & os, OutputParams const & runparams) const
253 // The sequence of the commands:
254 // With normal BibTeX:
255 // 1. \bibliographystyle{style}
256 // 2. \addcontentsline{...} - if option bibtotoc set
257 // 3. \bibliography{database}
259 // 1. \bibliographystyle{style}
260 // 2. \begin{btSect}{database}
261 // 3. \btPrint{Cited|NotCited|All}
264 // \printbibliography[biblatexopts]
266 // \bibbysection[biblatexopts] - if btprint is "bibbysection"
268 // chapterbib does not allow bibliographies in the master
269 if (!usingBiblatex() && !runparams.is_child
270 && buffer().params().multibib == "child")
273 if (runparams.inDeletedInset) {
274 // We cannot strike-out bibligraphies,
275 // so we just output a note.
277 << buffer().B_("[BIBLIOGRAPHY DELETED!]")
282 string style = to_utf8(getParam("options")); // maybe empty! and with bibtotoc
284 if (prefixIs(style, "bibtotoc")) {
285 bibtotoc = "bibtotoc";
286 if (contains(style, ','))
287 style = split(style, bibtotoc, ',');
290 if (usingBiblatex()) {
292 string opts = to_utf8(getParam("biblatexopts"));
294 if (!bibtotoc.empty())
295 opts = opts.empty() ? "heading=bibintoc" : "heading=bibintoc," + opts;
296 // The bibliography command
297 docstring btprint = getParam("btprint");
298 if (btprint == "btPrintAll")
299 os << "\\nocite{*}\n";
300 if (btprint == "bibbysection" && !buffer().masterParams().multibib.empty())
301 os << "\\bibbysection";
303 os << "\\printbibliography";
305 os << "[" << opts << "]";
307 } else {// using BibTeX
309 vector<pair<docstring, string>> const dbs =
310 buffer().prepareBibFilePaths(runparams, getBibFiles(), false);
311 vector<docstring> db_out;
312 db_out.reserve(dbs.size());
313 for (pair<docstring, string> const & db : dbs)
314 db_out.push_back(db.first);
316 if (style == "default")
317 style = buffer().masterParams().defaultBiblioStyle();
318 if (!style.empty() && !buffer().masterParams().useBibtopic()) {
319 string base = buffer().masterBuffer()->prepareFileNameForLaTeX(style, ".bst", runparams.nice);
320 FileName const try_in_file =
321 makeAbsPath(base + ".bst", buffer().filePath());
322 bool const not_from_texmf = try_in_file.isReadableFile();
323 // If this style does not come from texmf and we are not
324 // exporting to .tex copy it to the tmp directory.
325 // This prevents problems with spaces and 8bit characters
327 if (!runparams.inComment && !runparams.dryrun && !runparams.nice &&
329 // use new style name
330 DocFileName const in_file = DocFileName(try_in_file);
331 base = removeExtension(in_file.mangledFileName());
332 FileName const out_file = makeAbsPath(base + ".bst",
333 buffer().masterBuffer()->temppath());
334 bool const success = in_file.copyTo(out_file);
336 LYXERR0("Failed to copy '" << in_file
337 << "' to '" << out_file << "'");
341 os << "\\bibliographystyle{"
342 << from_utf8(latex_path(buffer().prepareFileNameForLaTeX(base, ".bst", runparams.nice)))
345 // Warn about spaces in bst path. Warn only once.
346 static bool warned_about_bst_spaces = false;
347 if (!warned_about_bst_spaces && runparams.nice && contains(style, ' ')) {
348 warned_about_bst_spaces = true;
349 Alert::warning(_("Export Warning!"),
350 _("There are spaces in the path to your BibTeX style file.\n"
351 "BibTeX will be unable to find it."));
354 bool encoding_switched = false;
355 Encoding const * const save_enc = runparams.encoding;
356 docstring const encoding = getParam("encoding");
357 if (!encoding.empty() && encoding != from_ascii("default")) {
358 Encoding const * const enc = encodings.fromLyXName(to_ascii(encoding));
359 if (enc != runparams.encoding) {
361 switchEncoding(os.os(), buffer().params(), runparams, *enc, true);
362 runparams.encoding = enc;
363 encoding_switched = true;
366 // Handle the bibtopic case
367 if (!db_out.empty() && buffer().masterParams().useBibtopic()) {
368 os << "\\begin{btSect}";
370 os << "[" << style << "]";
371 os << "{" << getStringFromVector(db_out) << "}\n";
372 docstring btprint = getParam("btprint");
375 btprint = from_ascii("btPrintCited");
376 os << "\\" << btprint << "\n"
377 << "\\end{btSect}\n";
380 if (!bibtotoc.empty() && !buffer().masterParams().useBibtopic()
381 && !buffer().masterParams().documentClass().bibInToc()) {
382 // set label for hyperref, see http://www.lyx.org/trac/ticket/6470
383 if (buffer().masterParams().pdfoptions().use_hyperref)
384 os << "\\phantomsection";
385 if (buffer().masterParams().documentClass().hasLaTeXLayout("chapter"))
386 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
387 else if (buffer().masterParams().documentClass().hasLaTeXLayout("section"))
388 os << "\\addcontentsline{toc}{section}{\\refname}";
390 // The bibliography command
391 if (!db_out.empty() && !buffer().masterParams().useBibtopic()) {
392 docstring btprint = getParam("btprint");
393 if (btprint == "btPrintAll") {
394 os << "\\nocite{*}\n";
396 os << "\\bibliography{" << getStringFromVector(db_out) << "}\n";
398 if (encoding_switched){
400 switchEncoding(os.os(), buffer().params(),
401 runparams, *save_enc, true, true);
402 os << "\\egroup" << breakln;
403 runparams.encoding = save_enc;
409 docstring_list InsetBibtex::getBibFiles() const
411 return getVectorFromString(getParam("bibfiles"));
416 // methods for parsing bibtex files
418 typedef map<docstring, docstring> VarMap;
420 /// remove whitespace characters, optionally a single comma,
421 /// and further whitespace characters from the stream.
422 /// @return true if a comma was found, false otherwise
424 bool removeWSAndComma(ifdocstream & ifs) {
433 } while (ifs && isSpace(ch));
446 } while (ifs && isSpace(ch));
461 /// remove whitespace characters, read character sequence
462 /// not containing whitespace characters or characters in
463 /// delimChars, and remove further whitespace characters.
465 /// @return true if a string of length > 0 could be read.
467 bool readTypeOrKey(docstring & val, ifdocstream & ifs,
468 docstring const & delimChars, docstring const & illegalChars,
481 } while (ifs && isSpace(ch));
487 while (ifs && !isSpace(ch) &&
488 delimChars.find(ch) == docstring::npos &&
489 illegalChars.find(ch) == docstring::npos)
491 if (chCase == makeLowerCase)
492 val += lowercase(ch);
498 if (illegalChars.find(ch) != docstring::npos) {
504 while (ifs && isSpace(ch)) {
512 return val.length() > 0;
515 /// read subsequent bibtex values that are delimited with a #-character.
516 /// Concatenate all parts and replace names with the associated string in
517 /// the variable strings.
518 /// @return true if reading was successful (all single parts were delimited
520 bool readValue(docstring & val, ifdocstream & ifs, const VarMap & strings) {
533 } while (ifs && isSpace(ch));
538 // check for field type
539 if (isDigitASCII(ch)) {
541 // read integer value
545 } while (ifs && isDigitASCII(ch));
550 } else if (ch == '"' || ch == '{') {
552 char_type delim = ch == '"' ? '"': '}';
557 } while (ifs && isSpace(ch));
562 // We now have the first non-whitespace character
563 // We'll collapse adjacent whitespace.
564 bool lastWasWhiteSpace = false;
566 // inside this delimited text braces must match.
567 // Thus we can have a closing delimiter only
568 // when nestLevel == 0
571 while (ifs && (nestLevel > 0 || ch != delim)) {
573 lastWasWhiteSpace = true;
577 // We output the space only after we stop getting
578 // whitespace so as not to output any whitespace
579 // at the end of the value.
580 if (lastWasWhiteSpace) {
581 lastWasWhiteSpace = false;
587 // update nesting level
606 // FIXME Why is this here?
614 // reading a string name
617 while (ifs && !isSpace(ch) && ch != '#' && ch != ',' && ch != '}' && ch != ')') {
618 strName += lowercase(ch);
625 // replace the string with its assigned value or
626 // discard it if it's not assigned
627 if (strName.length()) {
628 VarMap::const_iterator pos = strings.find(strName);
629 if (pos != strings.end()) {
636 while (ifs && isSpace(ch)) {
643 // continue reading next value on concatenate with '#'
653 void InsetBibtex::collectBibKeys(InsetIterator const & /*di*/, FileNameList & checkedFiles) const
655 parseBibTeXFiles(checkedFiles);
659 void InsetBibtex::parseBibTeXFiles(FileNameList & checkedFiles) const
661 // This bibtex parser is a first step to parse bibtex files
664 // - it reads the whole bibtex entry and does a syntax check
665 // (matching delimiters, missing commas,...
666 // - it recovers from errors starting with the next @-character
667 // - it reads @string definitions and replaces them in the
669 // - it accepts more characters in keys or value names than
672 // Officially bibtex does only support ASCII, but in practice
673 // you can use any encoding as long as some elements like keys
674 // and names are pure ASCII. We support specifying an encoding,
675 // and we convert the file from that (default is buffer encoding).
676 // We don't restrict keys to ASCII in LyX, since our own
677 // InsetBibitem can generate non-ASCII keys, and nonstandard
678 // 8bit clean bibtex forks exist.
682 docstring_list const files = getBibFiles();
683 for (auto const & bf : files) {
684 FileName const bibfile = buffer().getBibfilePath(bf);
685 if (bibfile.empty()) {
686 LYXERR0("Unable to find path for " << bf << "!");
689 if (find(checkedFiles.begin(), checkedFiles.end(), bibfile) != checkedFiles.end())
690 // already checked this one. Skip.
693 // record that we check this.
694 checkedFiles.push_back(bibfile);
695 string encoding = buffer().masterParams().encoding().iconvName();
696 string ienc = buffer().masterParams().bibFileEncoding(to_utf8(bf));
697 if (ienc.empty() || ienc == "general")
698 ienc = to_ascii(params()["encoding"]);
700 if (!ienc.empty() && ienc != "auto-legacy-plain" && ienc != "auto-legacy" && encodings.fromLyXName(ienc))
701 encoding = encodings.fromLyXName(ienc)->iconvName();
702 ifdocstream ifs(bibfile.toFilesystemEncoding().c_str(),
703 ios_base::in, encoding);
718 if (!readTypeOrKey(entryType, ifs, from_ascii("{("), docstring(), makeLowerCase)) {
719 lyxerr << "BibTeX Parser: Error reading entry type." << std::endl;
724 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
728 if (entryType == from_ascii("comment")) {
729 ifs.ignore(numeric_limits<int>::max(), '\n');
735 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
739 if ((ch != '(') && (ch != '{')) {
740 lyxerr << "BibTeX Parser: Invalid entry delimiter." << std::endl;
746 if (entryType == from_ascii("string")) {
748 // read string and add it to the strings map
749 // (or replace it's old value)
753 if (!readTypeOrKey(name, ifs, from_ascii("="), from_ascii("#{}(),"), makeLowerCase)) {
754 lyxerr << "BibTeX Parser: Error reading string name." << std::endl;
759 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
763 // next char must be an equal sign
765 if (!ifs || ch != '=') {
766 lyxerr << "BibTeX Parser: No `=' after string name: " <<
767 name << "." << std::endl;
771 if (!readValue(value, ifs, strings)) {
772 lyxerr << "BibTeX Parser: Unable to read value for string: " <<
773 name << "." << std::endl;
777 strings[name] = value;
779 } else if (entryType == from_ascii("preamble")) {
781 // preamble definitions are discarded.
782 // can they be of any use in lyx?
785 if (!readValue(value, ifs, strings)) {
786 lyxerr << "BibTeX Parser: Unable to read preamble value." << std::endl;
792 // Citation entry. Try to read the key.
795 if (!readTypeOrKey(key, ifs, from_ascii(","), from_ascii("}"), keepCase)) {
796 lyxerr << "BibTeX Parser: Unable to read key for entry type:" <<
797 entryType << "." << std::endl;
802 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
806 /////////////////////////////////////////////
807 // now we have a key, so we will add an entry
808 // (even if it's empty, as bibtex does)
810 // we now read the field = value pairs.
811 // all items must be separated by a comma. If
812 // it is missing the scanning of this entry is
813 // stopped and the next is searched.
817 BibTeXInfo keyvalmap(key, entryType);
819 bool readNext = removeWSAndComma(ifs);
821 while (ifs && readNext) {
824 if (!readTypeOrKey(name, ifs, from_ascii("="),
825 from_ascii("{}(),"), makeLowerCase) || !ifs)
828 // next char must be an equal sign
829 // FIXME Whitespace??
832 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
836 lyxerr << "BibTeX Parser: Missing `=' after field name: " <<
837 name << ", for key: " << key << "." << std::endl;
843 if (!readValue(value, ifs, strings)) {
844 lyxerr << "BibTeX Parser: Unable to read value for field: " <<
845 name << ", for key: " << key << "." << std::endl;
849 keyvalmap[name] = value;
850 data += "\n\n" + value;
851 keylist.addFieldName(name);
852 readNext = removeWSAndComma(ifs);
856 keylist.addEntryType(entryType);
857 keyvalmap.setAllData(data);
858 keylist[key] = keyvalmap;
859 } //< else (citation entry)
861 } //< for loop over files
863 buffer().addBiblioInfo(keylist);
867 bool InsetBibtex::addDatabase(docstring const & db)
869 docstring bibfiles = getParam("bibfiles");
870 if (tokenPos(bibfiles, ',', db) != -1)
872 if (!bibfiles.empty())
874 setParam("bibfiles", bibfiles + db);
879 bool InsetBibtex::delDatabase(docstring const & db)
881 docstring bibfiles = getParam("bibfiles");
882 if (contains(bibfiles, db)) {
883 int const n = tokenPos(bibfiles, ',', db);
886 // this is not the first database
887 docstring tmp = ',' + bd;
888 setParam("bibfiles", subst(bibfiles, tmp, docstring()));
890 // this is the first (or only) database
891 setParam("bibfiles", split(bibfiles, bd, ','));
899 void InsetBibtex::validate(LaTeXFeatures & features) const
901 BufferParams const & mparams = features.buffer().masterParams();
902 if (mparams.useBibtopic())
903 features.require("bibtopic");
904 else if (!mparams.useBiblatex() && mparams.multibib == "child")
905 features.require("chapterbib");
907 // It'd be better to be able to get this from an InsetLayout, but at present
908 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
909 if (features.runparams().flavor == Flavor::Html)
910 features.addCSSSnippet("div.bibtexentry { margin-left: 2em; text-indent: -2em; }\n"
911 "span.bibtexlabel:before{ content: \"[\"; }\n"
912 "span.bibtexlabel:after{ content: \"] \"; }");
916 void InsetBibtex::updateBuffer(ParIterator const &, UpdateType, bool const /*deleted*/)
918 buffer().registerBibfiles(getBibFiles());
919 // record encoding of bib files for biblatex
920 string const enc = (params()["encoding"] == from_ascii("default")) ?
921 string() : to_ascii(params()["encoding"]);
922 bool invalidate = false;
923 if (buffer().params().bibEncoding() != enc) {
924 buffer().params().setBibEncoding(enc);
927 map<string, string> encs = getFileEncodings();
928 map<string, string>::const_iterator it = encs.begin();
929 for (; it != encs.end(); ++it) {
930 if (buffer().params().bibFileEncoding(it->first) != it->second) {
931 buffer().params().setBibFileEncoding(it->first, it->second);
936 buffer().invalidateBibinfoCache();
940 map<string, string> InsetBibtex::getFileEncodings() const
943 getVectorFromString(to_utf8(getParam("file_encodings")), "\t");
944 std::map<string, string> res;
945 for (string const & s: ps) {
947 string val = split(s, key, ' ');
954 docstring InsetBibtex::getRefLabel() const
956 if (buffer().masterParams().documentClass().hasLaTeXLayout("chapter"))
957 return buffer().B_("Bibliography");
958 return buffer().B_("References");
962 void InsetBibtex::addToToc(DocIterator const & cpit, bool output_active,
963 UpdateType, TocBackend & backend) const
965 if (!prefixIs(to_utf8(getParam("options")), "bibtotoc"))
968 docstring const str = getRefLabel();
969 shared_ptr<Toc> toc = backend.toc("tableofcontents");
970 // Assign to appropriate level
971 int const item_depth =
972 (buffer().masterParams().documentClass().hasLaTeXLayout("chapter"))
974 toc->push_back(TocItem(cpit, item_depth, str, output_active));
978 int InsetBibtex::plaintext(odocstringstream & os,
979 OutputParams const & op, size_t max_length) const
981 docstring const reflabel = getRefLabel();
983 // We could output more information here, e.g., what databases are included
984 // and information about options. But I don't necessarily see any reason to
985 // do this right now.
986 if (op.for_tooltip || op.for_toc || op.find_effective()) {
987 os << '[' << reflabel << ']' << '\n';
988 return PLAINTEXT_NEWLINE;
991 BiblioInfo bibinfo = buffer().masterBibInfo();
992 bibinfo.makeCitationLabels(buffer());
993 vector<docstring> const & cites = bibinfo.citedEntries();
995 size_t start_size = os.str().size();
997 refoutput += reflabel + "\n\n";
999 // Tell BiblioInfo our purpose
1001 ci.context = CiteItem::Export;
1003 // Now we loop over the entries
1004 vector<docstring>::const_iterator vit = cites.begin();
1005 vector<docstring>::const_iterator const ven = cites.end();
1006 for (; vit != ven; ++vit) {
1007 if (start_size + refoutput.size() >= max_length)
1009 BiblioInfo::const_iterator const biit = bibinfo.find(*vit);
1010 if (biit == bibinfo.end())
1012 BibTeXInfo const & entry = biit->second;
1013 refoutput += "[" + entry.label() + "] ";
1014 // FIXME Right now, we are calling BibInfo::getInfo on the key,
1015 // which will give us all the cross-referenced info. But for every
1016 // entry, so there's a lot of repetition. This should be fixed.
1017 refoutput += bibinfo.getInfo(entry.key(), buffer(), ci) + "\n\n";
1020 return int(refoutput.size());
1025 // docstring InsetBibtex::entriesAsXHTML(vector<docstring> const & entries)
1026 // And then here just: entriesAsXHTML(buffer().masterBibInfo().citedEntries())
1027 docstring InsetBibtex::xhtml(XMLStream & xs, OutputParams const &) const
1029 BiblioInfo const & bibinfo = buffer().masterBibInfo();
1030 bool const all_entries = getParam("btprint") == "btPrintAll";
1031 vector<docstring> const & cites =
1032 all_entries ? bibinfo.getKeys() : bibinfo.citedEntries();
1034 docstring const reflabel = buffer().B_("References");
1036 // tell BiblioInfo our purpose
1038 ci.context = CiteItem::Export;
1040 ci.max_key_size = UINT_MAX;
1042 xs << xml::StartTag("h2", "class='bibtex'")
1044 << xml::EndTag("h2")
1045 << xml::StartTag("div", "class='bibtex'");
1047 // Now we loop over the entries
1048 vector<docstring>::const_iterator vit = cites.begin();
1049 vector<docstring>::const_iterator const ven = cites.end();
1050 for (; vit != ven; ++vit) {
1051 BiblioInfo::const_iterator const biit = bibinfo.find(*vit);
1052 if (biit == bibinfo.end())
1055 BibTeXInfo const & entry = biit->second;
1056 string const attr = "class='bibtexentry' id='LyXCite-"
1057 + to_utf8(xml::cleanAttr(entry.key())) + "'";
1058 xs << xml::StartTag("div", attr);
1060 // don't print labels if we're outputting all entries
1062 xs << xml::StartTag("span", "class='bibtexlabel'")
1064 << xml::EndTag("span");
1067 // FIXME Right now, we are calling BibInfo::getInfo on the key,
1068 // which will give us all the cross-referenced info. But for every
1069 // entry, so there's a lot of repetition. This should be fixed.
1070 xs << xml::StartTag("span", "class='bibtexinfo'")
1071 << XMLStream::ESCAPE_AND
1072 << bibinfo.getInfo(entry.key(), buffer(), ci)
1073 << xml::EndTag("span")
1074 << xml::EndTag("div")
1077 xs << xml::EndTag("div");
1082 void InsetBibtex::docbook(XMLStream & xs, OutputParams const &) const
1084 BiblioInfo const & bibinfo = buffer().masterBibInfo();
1085 bool const all_entries = getParam("btprint") == "btPrintAll";
1086 vector<docstring> const & cites =
1087 all_entries ? bibinfo.getKeys() : bibinfo.citedEntries();
1089 docstring const reflabel = buffer().B_("References");
1091 // Check that the bibliography is not empty, to ensure that the document is valid.
1092 if (cites.empty()) {
1093 xs << XMLStream::ESCAPE_NONE << "<!-- The bibliography is empty! -->";
1098 // Tell BiblioInfo our purpose (i.e. generate HTML rich text).
1100 ci.context = CiteItem::Export;
1102 ci.max_key_size = UINT_MAX;
1104 // Header for bibliography (title required).
1105 xs << xml::StartTag("bibliography");
1107 xs << xml::StartTag("title");
1109 xs << xml::EndTag("title");
1112 // Translation between keys in each entry and DocBook tags.
1113 // IDs for publications; list: http://tdg.docbook.org/tdg/5.2/biblioid.html.
1114 vector<pair<string, string>> biblioId = { // <bibtex, docbook>
1115 make_pair("doi", "doi"),
1116 make_pair("isbn", "isbn"),
1117 make_pair("issn", "issn"),
1118 make_pair("isrn", "isrn"),
1119 make_pair("istc", "istc"),
1120 make_pair("lccn", "libraryofcongress"),
1121 make_pair("number", "pubsnumber"),
1122 make_pair("url", "uri")
1124 // Relations between documents.
1125 vector<pair<string, string>> relations = { // <bibtex, docbook biblioset relation>
1126 make_pair("journal", "journal"),
1127 make_pair("booktitle", "book"),
1128 make_pair("series", "series")
1130 // Various things that do not fit DocBook.
1131 vector<string> misc = { "language", "school", "note" };
1133 // Store the mapping between BibTeX and DocBook.
1134 map<string, string> toDocBookTag;
1135 toDocBookTag["fullnames:author"] = "SPECIFIC"; // No direct translation to DocBook: <authorgroup>.
1136 toDocBookTag["publisher"] = "SPECIFIC"; // No direct translation to DocBook: <publisher>.
1137 toDocBookTag["address"] = "SPECIFIC"; // No direct translation to DocBook: <publisher>.
1138 toDocBookTag["editor"] = "editor";
1139 toDocBookTag["institution"] = "SPECIFIC"; // No direct translation to DocBook: <org>.
1141 toDocBookTag["title"] = "title";
1142 toDocBookTag["volume"] = "volumenum";
1143 toDocBookTag["edition"] = "edition";
1144 toDocBookTag["pages"] = "artpagenums";
1146 toDocBookTag["abstract"] = "SPECIFIC"; // No direct translation to DocBook: <abstract>.
1147 toDocBookTag["keywords"] = "SPECIFIC"; // No direct translation to DocBook: <keywordset>.
1148 toDocBookTag["year"] = "SPECIFIC"; // No direct translation to DocBook: <pubdate>.
1149 toDocBookTag["month"] = "SPECIFIC"; // No direct translation to DocBook: <pubdate>.
1151 toDocBookTag["journal"] = "SPECIFIC"; // No direct translation to DocBook: <biblioset>.
1152 toDocBookTag["booktitle"] = "SPECIFIC"; // No direct translation to DocBook: <biblioset>.
1153 toDocBookTag["series"] = "SPECIFIC"; // No direct translation to DocBook: <biblioset>.
1155 for (auto const & id: biblioId)
1156 toDocBookTag[id.first] = "SPECIFIC"; // No direct translation to DocBook: <biblioid>.
1157 for (auto const & id: relations)
1158 toDocBookTag[id.first] = "SPECIFIC"; // No direct translation to DocBook: <biblioset>.
1159 for (auto const & id: misc)
1160 toDocBookTag[id] = "SPECIFIC"; // No direct translation to DocBook: <bibliomisc>.
1162 // Loop over the entries. If there are no entries, add a comment to say so.
1163 auto vit = cites.begin();
1164 auto ven = cites.end();
1166 for (; vit != ven; ++vit) {
1167 auto const biit = bibinfo.find(*vit);
1168 if (biit == bibinfo.end())
1171 BibTeXInfo const & entry = biit->second;
1172 string const attr = "xml:id=\"" + to_utf8(xml::cleanID(entry.key())) + "\"";
1173 xs << xml::StartTag("biblioentry", attr);
1176 // FIXME Right now, we are calling BibInfo::getInfo on the key,
1177 // which will give us all the cross-referenced info. But for every
1178 // entry, so there's a lot of repetition. This should be fixed.
1180 // Parse the results of getInfo and emit the corresponding DocBook tags. Interesting pieces have the form
1181 // "<span class="bib-STH">STH</span>", the rest of the text may be discarded.
1182 // Could have written a DocBook version of expandFormat (that parses a citation into HTML), but it implements
1183 // some kind of recursion. Still, a (static) conversion step between the citation format and DocBook would have
1184 // been required. All in all, both codes approaches would have been similar, but this parsing allows relying
1185 // on existing building blocks.
1187 string html = to_utf8(bibinfo.getInfo(entry.key(), buffer(), ci));
1188 regex tagRegex("<span class=\"bib-([^\"]*)\">([^<]*)</span>");
1190 auto tagIt = sregex_iterator(html.cbegin(), html.cend(), tagRegex, regex_constants::match_default);
1191 auto tagEnd = sregex_iterator();
1192 map<string, string> delayedTags;
1194 // Read all tags from HTML and convert those that have a 1:1 matching.
1195 while (tagIt != tagEnd) {
1196 string tag = tagIt->str(); // regex_match cannot work with temporary strings.
1199 if (regex_match(tag, match, tagRegex)) {
1200 if (toDocBookTag[match[1]] == "SPECIFIC") {
1201 delayedTags[match[1]] = match[2];
1203 xs << xml::StartTag(toDocBookTag[match[1]]);
1204 xs << from_utf8(match[2].str());
1205 xs << xml::EndTag(toDocBookTag[match[1]]);
1209 LYXERR0("The BibTeX field " << match[1].str() << " is unknown.");
1210 xs << XMLStream::ESCAPE_NONE << from_utf8("<!-- Output Error: The BibTeX field " + match[1].str() + " is unknown -->\n");
1214 // Type of document (book, journal paper, etc.).
1215 xs << xml::StartTag("bibliomisc", "role=\"type\"");
1216 xs << entry.entryType();
1217 xs << xml::EndTag("bibliomisc");
1220 // Handle tags that have complex transformations.
1221 if (! delayedTags.empty()) {
1222 unsigned long remainingTags = delayedTags.size(); // Used as a workaround. With GCC 7, when erasing all
1223 // elements one by one, some elements may still pop in later on (even though they were deleted previously).
1224 auto hasTag = [&delayedTags](const string & key) { return delayedTags.find(key) != delayedTags.end(); };
1225 auto getTag = [&delayedTags](const string & key) { return from_utf8(delayedTags[key]); };
1226 auto eraseTag = [&delayedTags, &remainingTags](const string & key) {
1228 delayedTags.erase(key);
1231 // Notes on order of checks.
1232 // - address goes with publisher if there is one, so check this first. Otherwise, the address goes with
1233 // the entry without other details.
1236 if (hasTag("publisher")) {
1237 xs << xml::StartTag("publisher");
1239 xs << xml::StartTag("publishername");
1240 xs << getTag("publisher");
1241 xs << xml::EndTag("publishername");
1244 if (hasTag("address")) {
1245 xs << xml::StartTag("address");
1246 xs << getTag("address");
1247 xs << xml::EndTag("address");
1248 eraseTag("address");
1251 xs << xml::EndTag("publisher");
1253 eraseTag("publisher");
1256 if (hasTag("address")) {
1257 xs << xml::StartTag("address");
1258 xs << getTag("address");
1259 xs << xml::EndTag("address");
1260 eraseTag("address");
1264 if (hasTag("keywords")) {
1265 // Split the keywords on comma.
1266 docstring keywordSet = getTag("keywords");
1267 vector<docstring> keywords;
1268 if (keywordSet.find(from_utf8(",")) == string::npos) {
1269 keywords = { keywordSet };
1272 while ((pos = keywordSet.find(from_utf8(","))) != string::npos) {
1273 keywords.push_back(keywordSet.substr(0, pos));
1274 keywordSet.erase(0, pos + 1);
1276 keywords.push_back(keywordSet);
1279 xs << xml::StartTag("keywordset") << xml::CR();
1280 for (auto & kw: keywords) {
1281 kw.erase(kw.begin(), std::find_if(kw.begin(), kw.end(),
1282 [](char_type c) {return !lyx::isSpace(c);}));
1283 xs << xml::StartTag("keyword");
1285 xs << xml::EndTag("keyword");
1288 xs << xml::EndTag("keywordset") << xml::CR();
1289 eraseTag("keywords");
1293 // Example: http://tdg.docbook.org/tdg/5.1/biblioset.html
1294 if (hasTag("year")) {
1295 docstring value = getTag("year");
1298 // Follow xsd:gYearMonth format (http://books.xmlschemata.org/relaxng/ch19-77135.html).
1299 if (hasTag("month")) {
1300 value += "-" + getTag("month");
1304 xs << xml::StartTag("pubdate");
1306 xs << xml::EndTag("pubdate");
1311 if (hasTag("institution")) {
1312 xs << xml::StartTag("org");
1314 xs << xml::StartTag("orgname");
1315 xs << getTag("institution");
1316 xs << xml::EndTag("orgname");
1318 xs << xml::EndTag("org");
1320 eraseTag("institution");
1324 // Example: http://tdg.docbook.org/tdg/5.1/biblioset.html
1325 for (auto const & id: relations) {
1326 if (hasTag(id.first)) {
1327 xs << xml::StartTag("biblioset", "relation=\"" + id.second + "\"");
1329 xs << xml::StartTag("title");
1330 xs << getTag(id.first);
1331 xs << xml::EndTag("title");
1333 xs << xml::EndTag("biblioset");
1340 // Example: http://tdg.docbook.org/tdg/5.1/authorgroup.html
1341 if (hasTag("fullnames:author")) {
1342 // Perform full parsing of the BibTeX string, dealing with the many corner cases that might
1344 authorsToDocBookAuthorGroup(getTag("fullnames:author"), xs, buffer());
1345 eraseTag("fullnames:author");
1349 if (hasTag("abstract")) {
1350 // Split the paragraphs on new line.
1351 docstring abstract = getTag("abstract");
1352 vector<docstring> paragraphs;
1353 if (abstract.find(from_utf8("\n")) == string::npos) {
1354 paragraphs = { abstract };
1357 while ((pos = abstract.find(from_utf8(","))) != string::npos) {
1358 paragraphs.push_back(abstract.substr(0, pos));
1359 abstract.erase(0, pos + 1);
1361 paragraphs.push_back(abstract);
1364 xs << xml::StartTag("abstract");
1366 for (auto const & para: paragraphs) {
1369 xs << xml::StartTag("para");
1371 xs << xml::EndTag("para");
1374 xs << xml::EndTag("abstract");
1376 eraseTag("abstract");
1380 for (auto const & id: biblioId) {
1381 if (hasTag(id.first)) {
1382 xs << xml::StartTag("biblioid", "class=\"" + id.second + "\"");
1383 xs << getTag(id.first);
1384 xs << xml::EndTag("biblioid");
1391 for (auto const & id: misc) {
1393 xs << xml::StartTag("bibliomisc", "role=\"" + id + "\"");
1395 xs << xml::EndTag("bibliomisc");
1401 // After all tags are processed, check for errors.
1402 if (remainingTags > 0) {
1403 LYXERR0("Still delayed tags not yet handled.");
1404 xs << XMLStream::ESCAPE_NONE << from_utf8("<!-- Output Error: still delayed tags not yet handled.\n");
1405 for (auto const & item: delayedTags) {
1406 xs << from_utf8(" " + item.first + ": " + item.second + "\n");
1408 xs << XMLStream::ESCAPE_NONE << from_utf8(" -->\n");
1412 xs << xml::EndTag("biblioentry");
1416 // Footer for bibliography.
1417 xs << xml::EndTag("bibliography");
1422 void InsetBibtex::write(ostream & os) const
1424 params().Write(os, &buffer());
1428 string InsetBibtex::contextMenuName() const
1430 return "context-bibtex";