From: Angus Leeming Date: Fri, 14 May 2004 15:27:13 +0000 (+0000) Subject: a new biblio::asValidLatexString helper function. X-Git-Tag: 1.6.10~15226 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=24bd12178de86d3265d9f82efca58e498e77bda2;p=features.git a new biblio::asValidLatexString helper function. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8754 a592a061-630c-0410-9148-cb99ea01b6c8 --- diff --git a/src/frontends/controllers/ChangeLog b/src/frontends/controllers/ChangeLog index 3544d44e27..edeb3ef775 100644 --- a/src/frontends/controllers/ChangeLog +++ b/src/frontends/controllers/ChangeLog @@ -1,3 +1,9 @@ +2004-05-14 Angus Leeming + + * biblio.[Ch] (asValidLatexCommand): new function which examines + the input string to return a latex citation command that is + valid for the current citation engine. + 2004-05-14 Angus Leeming * ControlCitation.[Ch]: small changes to use the CiteEngine_enum diff --git a/src/frontends/controllers/biblio.C b/src/frontends/controllers/biblio.C index 8fdc33bd2d..7e92ed61c9 100644 --- a/src/frontends/controllers/biblio.C +++ b/src/frontends/controllers/biblio.C @@ -30,6 +30,7 @@ using lyx::support::compare_ascii_no_case; using lyx::support::contains; using lyx::support::getVectorFromString; using lyx::support::ltrim; +using lyx::support::prefixIs; using lyx::support::rtrim; using lyx::support::split; using lyx::support::subst; @@ -43,6 +44,113 @@ using std::vector; namespace biblio { +namespace { + +vector const init_possible_cite_commands() +{ + char const * const pos[] = { + "cite", + "citet", "citep", "citealt", "citealp", + "citeauthor", "citeyear", "citeyearpar", + "citet*", "citep*", "citealt*", "citealp*", "citeauthor*", + "Citet", "Citep", "Citealt", "Citealp", "Citeauthor", + "Citet*", "Citep*", "Citealt*", "Citealp*", "Citeauthor*", + "fullcite", + "footcite", "footcitet", "footcitep", "footcitealt", + "footcitealp", "footciteauthor", "footciteyear", + "footciteyearpar", + "citefield", + "citetitle", + "cite*" + }; + size_t const size_pos = sizeof(pos) / sizeof(pos[0]); + + return vector(pos, pos + size_pos); +} + + +vector const & possible_cite_commands() +{ + static vector const pos = init_possible_cite_commands(); + return pos; +} + + +bool is_possible_cite_command(string const & input) +{ + vector const & possibles = possible_cite_commands(); + vector::const_iterator const end = possibles.end(); + return std::find(possibles.begin(), end, input) != end; +} + + +string const default_cite_command(CiteEngine engine) +{ + string str; + switch (engine) { + case ENGINE_BASIC: + str = "cite"; + break; + case ENGINE_NATBIB_AUTHORYEAR: + str = "citet"; + break; + case ENGINE_NATBIB_NUMERICAL: + str = "citep"; + break; + case ENGINE_JURABIB: + str = "cite"; + break; + } + return str; +} + +} // namespace anon + + +string const asValidLatexCommand(string const & input, + CiteEngine_enum const & engine) +{ + string const default_str = default_cite_command(engine); + if (!is_possible_cite_command(input)) + return default_str; + + string output; + switch (engine) { + case ENGINE_BASIC: + output = default_str; + break; + + case ENGINE_NATBIB_AUTHORYEAR: + case ENGINE_NATBIB_NUMERICAL: + if (input == "cite" || input == "citefield" || + input == "citetitle" || input == "cite*") + output = default_str; + else if (prefixIs(input, "foot")) + output = input.substr(4); + else + output = input; + break; + + case ENGINE_JURABIB: { + // Jurabib does not support the 'uppercase' natbib style. + if (input[0] == 'C') + output = string(1, 'c') + input.substr(1); + else + output = input; + + // Jurabib does not support the 'full' natbib style. + string::size_type const n = output.size() - 1; + if (output != "cite*" && output[n] == '*') + output = output.substr(0, n); + + break; + } + } + + return output; +} + + string const familyName(string const & name) { // Very simple parser diff --git a/src/frontends/controllers/biblio.h b/src/frontends/controllers/biblio.h index 602ad6b2fc..cc39875b4d 100644 --- a/src/frontends/controllers/biblio.h +++ b/src/frontends/controllers/biblio.h @@ -49,6 +49,14 @@ enum Direction { BACKWARD }; + +/** Each citation engine recognizes only a subset of all possible + * citation commands. Given a latex command \c input, this function + * returns an appropriate command, valid for \c engine. + */ +std::string const asValidLatexCommand(std::string const & input, + CiteEngine_enum const & engine); + /// First entry is the bibliography key, second the data typedef std::map InfoMap; diff --git a/src/insets/ChangeLog b/src/insets/ChangeLog index 4c0547a5a3..649cfb32cd 100644 --- a/src/insets/ChangeLog +++ b/src/insets/ChangeLog @@ -1,3 +1,8 @@ +2004-05-14 Angus Leeming + + * insetcite.C (getNatbibLabel, generateLabel, latex): use the + new biblio::asValidLatexString function. + 2004-05-12 Angus Leeming * insetcite.C: use BufferParams::cite_engine rather than the three diff --git a/src/insets/insetcite.C b/src/insets/insetcite.C index 203b96b8d1..86e675f0fc 100644 --- a/src/insets/insetcite.C +++ b/src/insets/insetcite.C @@ -80,12 +80,11 @@ string const getNatbibLabel(Buffer const & buffer, // CITE: author/ // We don't currently use the full or forceUCase fields. - // bool const forceUCase = citeType[0] == 'C'; - bool const full = citeType[citeType.size() - 1] == '*'; - - string const cite_type = full ? - ascii_lowercase(citeType.substr(0, citeType.size() - 1)) : - ascii_lowercase(citeType); + string cite_type = biblio::asValidLatexCommand(citeType, engine); + if (cite_type[0] == 'C') + cite_type = string(1, 'c') + cite_type.substr(1); + if (cite_type[cite_type.size() - 1] == '*') + cite_type = cite_type.substr(0, cite_type.size() - 1); string before_str; if (!before.empty()) { @@ -270,19 +269,7 @@ string const InsetCitation::generateLabel(Buffer const & buffer) const string label; biblio::CiteEngine const engine = buffer.params().cite_engine; if (engine != biblio::ENGINE_BASIC) { - string cmd = getCmdName(); - if (cmd == "cite") { - // We may be "upgrading" from an older LyX version. - // If, however, we use "cite" because the necessary - // author/year info is not present in the biblio - // database, then getNatbibLabel will exit gracefully - // and we'll call getBasicLabel. - if (engine == biblio::ENGINE_NATBIB_NUMERICAL) - cmd = "citep"; - else if (engine == biblio::ENGINE_NATBIB_AUTHORYEAR) - cmd = "citet"; - } - label = getNatbibLabel(buffer, cmd, getContents(), + label = getNatbibLabel(buffer, getCmdName(), getContents(), before, after, engine); } @@ -343,30 +330,10 @@ int InsetCitation::latex(Buffer const & buffer, ostream & os, OutputParams const &) const { biblio::CiteEngine const cite_engine = buffer.params().cite_engine; + string const cite_str = + biblio::asValidLatexCommand(getCmdName(), cite_engine); - os << "\\"; - switch (cite_engine) { - case biblio::ENGINE_BASIC: - os << "cite"; - break; - case biblio::ENGINE_NATBIB_AUTHORYEAR: - case biblio::ENGINE_NATBIB_NUMERICAL: - os << getCmdName(); - break; - case biblio::ENGINE_JURABIB: - { - // jurabib does not (yet) support "force upper case" - // and "full author name". Fallback. - string cmd = getCmdName(); - if (cmd[0] == 'C') - cmd[0] = 'c'; - size_t n = cmd.size() - 1; - if (cmd[n] == '*') - cmd = cmd.substr(0,n); - os << cmd; - break; - } - } + os << "\\" << cite_str; string const before = getSecOptions(); string const after = getOptions();