3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
9 * Full author contact details are available in file CREDITS.
17 #include "bufferparams.h"
20 #include "support/lstrings.h"
22 #include <boost/regex.hpp>
28 using std::ostringstream;
34 using support::ascii_lowercase;
35 using support::bformat;
36 using support::compare_ascii_no_case;
37 using support::contains;
38 using support::getVectorFromString;
40 using support::prefixIs;
51 vector<string> const init_possible_cite_commands()
53 char const * const pos[] = {
55 "citet", "citep", "citealt", "citealp",
56 "citeauthor", "citeyear", "citeyearpar",
57 "citet*", "citep*", "citealt*", "citealp*", "citeauthor*",
58 "Citet", "Citep", "Citealt", "Citealp", "Citeauthor",
59 "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*",
61 "footcite", "footcitet", "footcitep", "footcitealt",
62 "footcitealp", "footciteauthor", "footciteyear",
68 size_t const size_pos = sizeof(pos) / sizeof(pos[0]);
70 return vector<string>(pos, pos + size_pos);
74 vector<string> const & possible_cite_commands()
76 static vector<string> const pos = init_possible_cite_commands();
81 bool is_possible_cite_command(string const & input)
83 vector<string> const & possibles = possible_cite_commands();
84 vector<string>::const_iterator const end = possibles.end();
85 return std::find(possibles.begin(), end, input) != end;
89 string const default_cite_command(CiteEngine engine)
96 case ENGINE_NATBIB_AUTHORYEAR:
99 case ENGINE_NATBIB_NUMERICAL:
112 string const asValidLatexCommand(string const & input,
113 CiteEngine_enum const & engine)
115 string const default_str = default_cite_command(engine);
116 if (!is_possible_cite_command(input))
122 output = default_str;
125 case ENGINE_NATBIB_AUTHORYEAR:
126 case ENGINE_NATBIB_NUMERICAL:
127 if (input == "cite" || input == "citefield" ||
128 input == "citetitle" || input == "cite*")
129 output = default_str;
130 else if (prefixIs(input, "foot"))
131 output = input.substr(4);
136 case ENGINE_JURABIB: {
137 // Jurabib does not support the 'uppercase' natbib style.
139 output = string(1, 'c') + input.substr(1);
143 // Jurabib does not support the 'full' natbib style.
144 string::size_type const n = output.size() - 1;
145 if (output != "cite*" && output[n] == '*')
146 output = output.substr(0, n);
156 string const familyName(string const & name)
158 // Very simple parser
161 // possible authorname combinations are:
162 // "Surname, FirstName"
164 // "FirstName Surname"
166 string::size_type idx = fname.find(',');
167 if (idx != string::npos)
168 return ltrim(fname.substr(0, idx));
169 idx = fname.rfind('.');
170 if (idx != string::npos)
171 fname = ltrim(fname.substr(idx + 1));
172 // test if we have a LaTeX Space in front
173 if (fname[0] == '\\')
174 return fname.substr(2);
180 string const getAbbreviatedAuthor(InfoMap const & map, string const & key)
182 BOOST_ASSERT(!map.empty());
184 InfoMap::const_iterator it = map.find(key);
187 string const & data = it->second;
189 // Is the entry a BibTeX one or one from lyx-layout "bibliography"?
190 string::size_type const pos = data.find("TheBibliographyRef");
191 if (pos != string::npos) {
196 string const opt = trim(data.substr(0, pos - 1));
201 split(opt, authors, '(');
205 string author = parseBibTeX(data, "author");
208 author = parseBibTeX(data, "editor");
210 if (author.empty()) {
211 author = parseBibTeX(data, "key");
217 vector<string> const authors = getVectorFromString(author, " and ");
221 if (authors.size() == 2)
223 return lyx::to_utf8(bformat(_("%1$s and %2$s"),
224 lyx::from_utf8(familyName(authors[0])), lyx::from_utf8(familyName(authors[1]))));
226 if (authors.size() > 2)
228 return lyx::to_utf8(bformat(_("%1$s et al."), lyx::from_utf8(familyName(authors[0]))));
230 return familyName(authors[0]);
234 string const getYear(InfoMap const & map, string const & key)
236 BOOST_ASSERT(!map.empty());
238 InfoMap::const_iterator it = map.find(key);
241 string const & data = it->second;
243 // Is the entry a BibTeX one or one from lyx-layout "bibliography"?
244 string::size_type const pos = data.find("TheBibliographyRef");
245 if (pos != string::npos) {
251 trim(data.substr(0, pos - 1));
256 string const tmp = split(opt, authors, '(');
258 split(tmp, year, ')');
263 string year = parseBibTeX(data, "year");
265 year = lyx::to_utf8(_("No year"));
273 // A functor for use with std::sort, leading to case insensitive sorting
274 class compareNoCase: public std::binary_function<string, string, bool>
277 bool operator()(string const & s1, string const & s2) const {
278 return compare_ascii_no_case(s1, s2) < 0;
285 vector<string> const getKeys(InfoMap const & map)
287 vector<string> bibkeys;
288 InfoMap::const_iterator it = map.begin();
289 InfoMap::const_iterator end = map.end();
290 for (; it != end; ++it) {
291 bibkeys.push_back(it->first);
294 std::sort(bibkeys.begin(), bibkeys.end(), compareNoCase());
299 string const getInfo(InfoMap const & map, string const & key)
301 BOOST_ASSERT(!map.empty());
303 InfoMap::const_iterator it = map.find(key);
306 string const & data = it->second;
308 // is the entry a BibTeX one or one from lyx-layout "bibliography"?
309 string const separator("TheBibliographyRef");
310 string::size_type const pos = data.find(separator);
311 if (pos != string::npos) {
312 string::size_type const pos2 = pos + separator.size();
313 string const info = trim(data.substr(pos2));
317 // Search for all possible "required" keys
318 string author = parseBibTeX(data, "author");
320 author = parseBibTeX(data, "editor");
322 string year = parseBibTeX(data, "year");
323 string title = parseBibTeX(data, "title");
324 string booktitle = parseBibTeX(data, "booktitle");
325 string chapter = parseBibTeX(data, "chapter");
326 string number = parseBibTeX(data, "number");
327 string volume = parseBibTeX(data, "volume");
328 string pages = parseBibTeX(data, "pages");
329 string annote = parseBibTeX(data, "annote");
331 string media = parseBibTeX(data, "journal");
333 media = parseBibTeX(data, "publisher");
335 media = parseBibTeX(data, "school");
337 media = parseBibTeX(data, "institution");
339 ostringstream result;
341 result << author << ", ";
344 if (!booktitle.empty())
345 result << ", in " << booktitle;
346 if (!chapter.empty())
347 result << ", Ch. " << chapter;
349 result << ", " << media;
351 result << ", vol. " << volume;
353 result << ", no. " << number;
355 result << ", pp. " << pages;
357 result << ", " << year;
359 result << "\n\n" << annote;
361 string const result_str = rtrim(result.str());
362 if (!result_str.empty())
365 // This should never happen (or at least be very unusual!)
372 // Escape special chars.
373 // All characters are literals except: '.|*?+(){}[]^$\'
374 // These characters are literals when preceded by a "\", which is done here
375 // @todo: This function should be moved to support, and then the test in tests
376 // should be moved there as well.
377 string const escape_special_chars(string const & expr)
379 // Search for all chars '.|*?+(){}[^$]\'
380 // Note that '[' and '\' must be escaped.
381 // This is a limitation of boost::regex, but all other chars in BREs
382 // are assumed literal.
383 boost::regex reg("[].|*?+(){}^$\\[\\\\]");
385 // $& is a perl-like expression that expands to all
386 // of the current match
387 // The '$' must be prefixed with the escape character '\' for
388 // boost to treat it as a literal.
389 // Thus, to prefix a matched expression with '\', we use:
390 return boost::regex_replace(expr, reg, "\\\\$&");
394 // A functor for use with std::find_if, used to ascertain whether a
395 // data entry matches the required regex_
396 // @throws: boost::regex_error if the supplied regex pattern is not valid
397 // @todo: This function should be moved to support.
398 class RegexMatch : public std::unary_function<string, bool>
401 // re and icase are used to construct an instance of boost::RegEx.
402 // if icase is true, then matching is insensitive to case
403 RegexMatch(InfoMap const & m, string const & re, bool icase)
404 : map_(m), regex_(re, icase) {}
406 bool operator()(string const & key) const {
407 // the data searched is the key + its associated BibTeX/biblio
410 InfoMap::const_iterator info = map_.find(key);
411 if (info != map_.end())
412 data += ' ' + info->second;
414 // Attempts to find a match for the current RE
415 // somewhere in data.
416 return boost::regex_search(data, regex_);
420 mutable boost::regex regex_;
426 vector<string>::const_iterator
427 searchKeys(InfoMap const & theMap,
428 vector<string> const & keys,
429 string const & search_expr,
430 vector<string>::const_iterator start,
435 // Preliminary checks
436 if (start < keys.begin() || start >= keys.end())
439 string expr = trim(search_expr);
444 // We must escape special chars in the search_expr so that
445 // it is treated as a simple string by boost::regex.
446 expr = escape_special_chars(expr);
449 // Build the functor that will be passed to find_if.
450 RegexMatch const match(theMap, expr, !caseSensitive);
452 // Search the vector of 'keys' from 'start' for one
453 // that matches the predicate 'match'. Searching can
454 // be forward or backward from start.
456 return std::find_if(start, keys.end(), match);
458 vector<string>::const_reverse_iterator rit(start);
459 vector<string>::const_reverse_iterator rend = keys.rend();
460 rit = std::find_if(rit, rend, match);
464 // This is correct and always safe.
465 // (See Meyer's Effective STL, Item 28.)
466 return (++rit).base();
468 catch (boost::regex_error &) {
474 string const parseBibTeX(string data, string const & findkey)
477 // at first we delete all characters right of '%' and
478 // replace tabs through a space and remove leading spaces
479 // we read the data line by line so that the \n are
483 string dummy = token(data,'\n', Entries);
484 while (!dummy.empty()) {
485 dummy = subst(dummy, '\t', ' '); // no tabs
486 dummy = ltrim(dummy); // no leading spaces
487 // ignore lines with a beginning '%' or ignore all right of %
488 string::size_type const idx =
489 dummy.empty() ? string::npos : dummy.find('%');
490 if (idx != string::npos)
491 // Check if this is really a comment or just "\%"
492 if (idx == 0 || dummy[idx - 1] != '\\')
493 dummy.erase(idx, string::npos);
494 else // This is "\%", so just erase the '\'
495 dummy.erase(idx - 1, 1);
496 // do we have a new token or a new line of
497 // the same one? In the first case we ignore
498 // the \n and in the second we replace it
500 if (!dummy.empty()) {
501 if (!contains(dummy, '='))
502 data_ += ' ' + dummy;
506 dummy = token(data, '\n', ++Entries);
509 // replace double commas with "" for easy scanning
510 data = subst(data_, ",,", "\"\"");
516 // now get only the important line of the bibtex entry.
517 // all entries are devided by ',' except the last one.
518 data += ','; // now we have same behaviour for all entries
519 // because the last one is "blah ... }"
522 // parsing of title and booktitle is different from the
523 // others, because booktitle contains title
525 dummy = token(data, ',', Entries++);
526 if (!dummy.empty()) {
527 found = contains(ascii_lowercase(dummy), findkey);
528 if (findkey == "title" &&
529 contains(ascii_lowercase(dummy), "booktitle"))
532 } while (!found && !dummy.empty());
537 // we are not sure, if we get all, because "key= "blah, blah" is
539 // Therefore we read all until the next "=" character, which follows a
542 dummy = token(data, ',', Entries++);
543 while (!contains(dummy, '=') && !dummy.empty()) {
544 keyvalue += ',' + dummy;
545 dummy = token(data, ',', Entries++);
548 // replace double "" with originals ,, (two commas)
549 // leaving us with the all-important line
550 data = subst(keyvalue, "\"\"", ",,");
555 // 2. if there is no opening '{' then a closing '{' is probably cruft.
556 if (!contains(data, '{'))
557 data = rtrim(data, "}");
558 // happens, when last keyword
559 string::size_type const idx =
560 !data.empty() ? data.find('=') : string::npos;
562 if (idx == string::npos)
565 data = trim(data.substr(idx));
567 if (data.length() < 2 || data[0] != '=') { // a valid entry?
570 // delete '=' and the following spaces
571 data = ltrim(data, " =");
572 if (data.length() < 2) {
573 return data; // not long enough to find delimiters
575 string::size_type keypos = 1;
577 if (data[0] == '{') {
579 } else if (data[0] == '"') {
582 // no {} and no "", pure data but with a
583 // possible ',' at the end
584 return rtrim(data, ",");
586 string tmp = data.substr(keypos);
587 while (tmp.find('{') != string::npos &&
588 tmp.find('}') != string::npos &&
589 tmp.find('{') < tmp.find('}') &&
590 tmp.find('{') < tmp.find(enclosing)) {
592 keypos += tmp.find('{') + 1;
593 tmp = data.substr(keypos);
594 keypos += tmp.find('}') + 1;
595 tmp = data.substr(keypos);
597 if (tmp.find(enclosing) == string::npos)
600 keypos += tmp.find(enclosing);
601 return data.substr(1, keypos - 1);
611 char const * const citeCommands[] = {
612 "cite", "citet", "citep", "citealt", "citealp", "citeauthor",
613 "citeyear", "citeyearpar" };
615 unsigned int const nCiteCommands =
616 sizeof(citeCommands) / sizeof(char *);
618 CiteStyle const citeStyles[] = {
619 CITE, CITET, CITEP, CITEALT, CITEALP,
620 CITEAUTHOR, CITEYEAR, CITEYEARPAR };
622 unsigned int const nCiteStyles =
623 sizeof(citeStyles) / sizeof(CiteStyle);
625 CiteStyle const citeStylesFull[] = {
626 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
628 unsigned int const nCiteStylesFull =
629 sizeof(citeStylesFull) / sizeof(CiteStyle);
631 CiteStyle const citeStylesUCase[] = {
632 CITET, CITEP, CITEALT, CITEALP, CITEAUTHOR };
634 unsigned int const nCiteStylesUCase =
635 sizeof(citeStylesUCase) / sizeof(CiteStyle);
640 CitationStyle::CitationStyle(string const & command)
641 : style(CITE), full(false), forceUCase(false)
646 string cmd = command;
652 string::size_type const n = cmd.size() - 1;
653 if (cmd != "cite" && cmd[n] == '*') {
655 cmd = cmd.substr(0,n);
658 char const * const * const last = citeCommands + nCiteCommands;
659 char const * const * const ptr = std::find(citeCommands, last, cmd);
662 size_t idx = ptr - citeCommands;
663 style = citeStyles[idx];
668 string const CitationStyle::asLatexStr() const
670 string cite = citeCommands[style];
672 CiteStyle const * last = citeStylesFull + nCiteStylesFull;
673 if (std::find(citeStylesFull, last, style) != last)
678 CiteStyle const * last = citeStylesUCase + nCiteStylesUCase;
679 if (std::find(citeStylesUCase, last, style) != last)
687 CiteEngine_enum getEngine(Buffer const & buffer)
689 return buffer.params().cite_engine;
693 vector<CiteStyle> const getCiteStyles(CiteEngine_enum const & engine)
695 unsigned int nStyles = 0;
696 unsigned int start = 0;
703 case ENGINE_NATBIB_AUTHORYEAR:
704 case ENGINE_NATBIB_NUMERICAL:
705 nStyles = nCiteStyles - 1;
709 nStyles = nCiteStyles;
714 typedef vector<CiteStyle> cite_vec;
716 cite_vec styles(nStyles);
717 cite_vec::size_type i = 0;
719 for (; i != styles.size(); ++i, ++j)
720 styles[i] = citeStyles[j];
727 getNumericalStrings(string const & key,
728 InfoMap const & map, vector<CiteStyle> const & styles)
731 return vector<string>();
734 string const author = getAbbreviatedAuthor(map, key);
735 string const year = getYear(map, key);
736 if (author.empty() || year.empty())
737 return vector<string>();
739 vector<string> vec(styles.size());
740 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
750 str = author + " [#ID]";
754 str = author + " #ID";
770 str = '(' + year + ')';
782 getAuthorYearStrings(string const & key,
783 InfoMap const & map, vector<CiteStyle> const & styles)
786 return vector<string>();
789 string const author = getAbbreviatedAuthor(map, key);
790 string const year = getYear(map, key);
791 if (author.empty() || year.empty())
792 return vector<string>();
794 vector<string> vec(styles.size());
795 for (vector<string>::size_type i = 0; i != vec.size(); ++i) {
800 // jurabib only: Author/Annotator
801 // (i.e. the "before" field, 2nd opt arg)
802 str = author + "/<" + lyx::to_utf8(_("before")) + '>';
806 str = author + " (" + year + ')';
810 str = '(' + author + ", " + year + ')';
814 str = author + ' ' + year ;
818 str = author + ", " + year ;
830 str = '(' + year + ')';
840 } // namespace biblio