From: Julien Rioux Date: Tue, 8 Jan 2013 14:35:50 +0000 (+0100) Subject: Move richtext escaping to its own postprocessing routine. X-Git-Tag: 2.1.0beta1~959 X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=24a2c57ff0d261194226756d49a6bcf639b1f01e;p=features.git Move richtext escaping to its own postprocessing routine. --- diff --git a/src/BiblioInfo.cpp b/src/BiblioInfo.cpp index a62fea7249..533941dddb 100644 --- a/src/BiblioInfo.cpp +++ b/src/BiblioInfo.cpp @@ -196,6 +196,49 @@ docstring convertLaTeXCommands(docstring const & str) 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 @@ -407,7 +450,7 @@ string parseOptions(string const & format, string & optkey, 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; @@ -484,10 +527,10 @@ docstring BibTeXInfo::expandFormat(string const & format, 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; } @@ -495,6 +538,7 @@ docstring BibTeXInfo::expandFormat(string const & format, // beginning of rich text scanning_rich = true; fmt = fmt.substr(2); + ret += from_ascii("{!"); continue; } } @@ -507,26 +551,13 @@ docstring BibTeXInfo::expandFormat(string const & format, // 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) { @@ -557,7 +588,8 @@ docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref, 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_); @@ -580,11 +612,13 @@ docstring const BibTeXInfo::getLabel(BibTeXInfo const * const xref, */ 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; } diff --git a/src/BiblioInfo.h b/src/BiblioInfo.h index f45297816e..477f0a10e4 100644 --- a/src/BiblioInfo.h +++ b/src/BiblioInfo.h @@ -119,17 +119,20 @@ private: /// {%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