return ret;
}
+
+// Escape '<' and '>' and remove richtext markers (e.g. {!this is richtext!}) from a string.
+docstring processRichtext(docstring const & str, bool richtext)
+{
+ docstring val = str;
+ docstring ret;
+
+ bool scanning_rich = false;
+ while (!val.empty()) {
+ char_type const ch = val[0];
+ if (ch == '{' && val.size() > 1 && val[1] == '!') {
+ // beginning of rich text
+ scanning_rich = true;
+ val = val.substr(2);
+ continue;
+ }
+ if (scanning_rich && ch == '!' && val.size() > 1 && val[1] == '}') {
+ // end of rich text
+ scanning_rich = false;
+ val = val.substr(2);
+ continue;
+ }
+ if (richtext) {
+ if (scanning_rich)
+ ret += ch;
+ else {
+ // we need to escape '<' and '>'
+ if (ch == '<')
+ ret += "<";
+ else if (ch == '>')
+ ret += ">";
+ else
+ ret += ch;
+ }
+ } else if (!scanning_rich /* && !richtext */)
+ ret += ch;
+ // else the character is discarded, which will happen only if
+ // richtext == false and we are scanning rich text
+ val = val.substr(1);
+ }
+ return ret;
+}
+
} // anon namespace
docstring BibTeXInfo::expandFormat(string const & format,
BibTeXInfo const * const xref, int & counter, Buffer const & buf,
- bool richtext, docstring before, docstring after, docstring dialog, bool next) const
+ docstring before, docstring after, docstring dialog, bool next) const
{
// incorrect use of macros could put us in an infinite loop
static int max_passes = 5000;
ret += from_utf8(ifpart); // without expansion
else if (!val.empty())
ret += expandFormat(ifpart, xref, counter, buf,
- richtext, before, after, dialog, next);
+ before, after, dialog, next);
else if (!elsepart.empty())
ret += expandFormat(elsepart, xref, counter, buf,
- richtext, before, after, dialog, next);
+ before, after, dialog, next);
// fmt will have been shortened for us already
continue;
}
// beginning of rich text
scanning_rich = true;
fmt = fmt.substr(2);
+ ret += from_ascii("{!");
continue;
}
}
// end of rich text
scanning_rich = false;
fmt = fmt.substr(2);
+ ret += from_ascii("!}");
continue;
}
else if (scanning_key)
key += char(thischar);
- else if (richtext) {
- if (scanning_rich)
- ret += thischar;
- else {
- // we need to escape '<' and '>'
- if (thischar == '<')
- ret += "<";
- else if (thischar == '>')
- ret += ">";
- else
- ret += thischar;
- }
- } else if (!scanning_rich /* && !richtext */)
+ else
ret += thischar;
- // else the character is discarded, which will happen only if
- // richtext == false and we are scanning rich text
fmt = fmt.substr(1);
} // for loop
if (scanning_key) {
DocumentClass const & dc = buf.params().documentClass();
string const & format = dc.getCiteFormat(engine_type, to_utf8(entry_type_));
int counter = 0;
- info_ = expandFormat(format, xref, counter, buf, richtext);
+ info_ = expandFormat(format, xref, counter, buf,
+ docstring(), docstring(), docstring(), false);
if (!info_.empty())
info_ = convertLaTeXCommands(info_);
*/
int counter = 0;
- loclabel = expandFormat(format, xref, counter, buf, richtext,
+ loclabel = expandFormat(format, xref, counter, buf,
before, after, dialog, next);
- if (!loclabel.empty())
+ if (!loclabel.empty() && !next) {
+ loclabel = processRichtext(loclabel, richtext);
loclabel = convertLaTeXCommands(loclabel);
+ }
return loclabel;
}
/// {%key%[[format]]}, which prints format if key is non-empty
/// the latter may optionally contain an `else' clause as well:
/// {%key%[[if format]][[else format]]}
- /// material intended only for rich text (HTML) output should be
- /// wrapped in "{!" and "!}". it will be removed if richtext is
- /// false.
+ /// Material intended only for rich text (HTML) output should be
+ /// wrapped in "{!" and "!}". These markers are to be postprocessed
+ /// by processRichtext(); this step is left as a separate routine since
+ /// expandFormat() is recursive while postprocessing should be done
+ /// only once on the final string (just like convertLaTeXCommands).
/// a simple macro facility is also available. keys that look like
/// "%!key%" are substituted with their definition.
/// moreover, keys that look like "%_key%" are treated as translatable
/// so that things like "pp." and "vol." can be translated.
docstring expandFormat(std::string const & fmt,
BibTeXInfo const * const xref, int & counter,
- Buffer const & buf, bool richtext, docstring before = docstring(),
- docstring after = docstring(), docstring dialog = docstring(), bool next = false) const;
+ Buffer const & buf, docstring before = docstring(),
+ docstring after = docstring(), docstring dialog = docstring(),
+ bool next = false) const;
/// true if from BibTeX; false if from bibliography environment
bool is_bibtex_;
/// the BibTeX key for this entry