From fd8b4aebb0ee6404a4532abb9934886b19edd108 Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Sun, 22 Jan 2017 10:27:06 +0100 Subject: [PATCH] Generalize the protection of brackets in citation arguments Whenever an argument delimiter is used inside the argument, the argument needs to be grouped, that is \cites({text (text) text}) or \cite[{text [text] text}] This fixes the original case reported in #2751 which is independent from the general issue that the pre- and postnote field take literal code. --- src/insets/InsetCitation.cpp | 45 +++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/insets/InsetCitation.cpp b/src/insets/InsetCitation.cpp index 9004c9819a..40b8c1d3ce 100644 --- a/src/insets/InsetCitation.cpp +++ b/src/insets/InsetCitation.cpp @@ -312,6 +312,15 @@ inline docstring wrapCitation(docstring const & key, html::htmlize(content, XHTMLStream::ESCAPE_ALL) + ""; } +docstring protectArgument(docstring & arg, char const l = '[', + char const r = ']') +{ + if (contains(arg, l) || contains(arg, r)) + // protect brackets + arg = '{' + arg + '}'; + return arg; +} + } // anonymous namespace @@ -571,24 +580,17 @@ void InsetCitation::latex(otexstream & os, OutputParams const & runparams) const docstring before = getParam("before"); docstring after = getParam("after"); if (!before.empty() && cs.textBefore) { - if (qualified) { - if (contains(before, '(') || contains(before, ')')) - // protect parens - before = '{' + before + '}'; - if (contains(after, '(') || contains(after, ')')) - // protect parens - after = '{' + after + '}'; - os << '(' << before << ")(" << after << ')'; - } else - os << '[' << before << "][" << after << ']'; + if (qualified) + os << '(' << protectArgument(before, '(', ')') + << ")(" << protectArgument(after, '(', ')') << ')'; + else + os << '[' << protectArgument(before) << "][" + << protectArgument(after) << ']'; } else if (!after.empty() && cs.textAfter) { - if (qualified) { - if (contains(after, '(') || contains(after, ')')) - // protect parens - after = '{' + after + '}'; - os << '(' << after << ')'; - } else - os << '[' << after << ']'; + if (qualified) + os << '(' << protectArgument(after, '(', ')') << ')'; + else + os << '[' << protectArgument(after) << ']'; } if (!bi.isBibtex(key)) @@ -599,12 +601,13 @@ void InsetCitation::latex(otexstream & os, OutputParams const & runparams) const map pres = getQualifiedLists(getParam("pretextlist")); map posts = getQualifiedLists(getParam("posttextlist")); for (docstring const & k: keys) { - docstring const bef = pres[k]; - docstring const aft = posts[k]; + docstring bef = pres[k]; + docstring aft = posts[k]; if (!bef.empty()) - os << '[' << bef << "][" << aft << ']'; + os << '[' << protectArgument(bef) + << "][" << protectArgument(aft) << ']'; else if (!aft.empty()) - os << '[' << aft << ']'; + os << '[' << protectArgument(aft) << ']'; os << '{' << k << '}'; } } else -- 2.39.2