]> git.lyx.org Git - features.git/commitdiff
Allow the use of rich text in citation formats. And use them in the
authorRichard Heck <rgheck@comcast.net>
Sat, 27 Mar 2010 13:54:14 +0000 (13:54 +0000)
committerRichard Heck <rgheck@comcast.net>
Sat, 27 Mar 2010 13:54:14 +0000 (13:54 +0000)
citaton dialog.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@33880 a592a061-630c-0410-9148-cb99ea01b6c8

src/BiblioInfo.cpp
src/BiblioInfo.h
src/frontends/qt4/GuiCitation.cpp

index 99e3bd91acbdc80fccbf240b7fa2dd2b4251a858..d484dd212f98d2d6920fbcc4ee268c609adba111 100644 (file)
@@ -395,12 +395,13 @@ namespace {
 
 
 docstring BibTeXInfo::expandFormat(docstring const & format, 
-               BibTeXInfo const * const xref) const
+               BibTeXInfo const * const xref, bool richtext) const
 {
        // return value
        docstring ret;
        docstring key;
        bool scanning_key = false;
+       bool scanning_rich = false;
 
        docstring fmt = format;
        // we'll remove characters from the front of fmt as we 
@@ -427,36 +428,58 @@ docstring BibTeXInfo::expandFormat(docstring const & format,
                                LYXERR0("ERROR: Found `{' when scanning key in `" << format << "'.");
                                return _("ERROR!");
                        }
-                       if (fmt.size() > 1 && fmt[1] == '%') {
-                               // it is the beginning of an optional format
-                               docstring optkey;
-                               docstring ifpart;
-                               docstring elsepart;
-                               docstring const newfmt = 
+                       if (fmt.size() > 1) {
+                               if (fmt[1] == '%') {
+                                       // it is the beginning of an optional format
+                                       docstring optkey;
+                                       docstring ifpart;
+                                       docstring elsepart;
+                                       docstring const newfmt = 
                                                parseOptions(fmt, optkey, ifpart, elsepart);
-                               if (newfmt == fmt) // parse error
-                                       return _("ERROR!");
-                               fmt = newfmt;
-                               docstring const val = getValueForKey(to_utf8(optkey), xref);
-                               if (!val.empty())
-                                       ret += expandFormat(ifpart, xref);
-                               else if (!elsepart.empty())
-                                       ret += expandFormat(elsepart, xref);
-                               // fmt will have been shortened for us already
-                               continue; 
-                       } 
+                                       if (newfmt == fmt) // parse error
+                                               return _("ERROR!");
+                                       fmt = newfmt;
+                                       docstring const val = getValueForKey(to_utf8(optkey), xref);
+                                       if (!val.empty())
+                                               ret += expandFormat(ifpart, xref, richtext);
+                                       else if (!elsepart.empty())
+                                               ret += expandFormat(elsepart, xref, richtext);
+                                       // fmt will have been shortened for us already
+                                       continue; 
+                               }
+                               if (fmt[1] == '!') {
+                                       // beginning of rich text
+                                       scanning_rich = true;
+                                       fmt = fmt.substr(2);
+                                       continue;
+                               }
+                       }
+                       // we are here if the '{' was at the end of the format. hmm.
                        ret += thischar;
-               } 
+               }
+               else if (scanning_rich && thischar == '!' 
+                        && fmt.size() > 1 && fmt[1] == '}') {
+                       // end of rich text
+                       scanning_rich = false;
+                       fmt = fmt.substr(2);
+                       continue;
+               }
                else if (scanning_key)
                        key += thischar;
-               else
+               else if (richtext || !scanning_rich)
                        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) {
                LYXERR0("Never found end of key in `" << format << "'!");
                return _("ERROR!");
        }
+       if (scanning_rich) {
+               LYXERR0("Never found end of rich text in `" << format << "'!");
+               return _("ERROR!");
+       }
        return ret;
 }
 
@@ -466,19 +489,20 @@ namespace {
 // FIXME These would be better read from a file, so that they
 // could be customized.
 
-       static docstring articleFormat = from_ascii("%author%, \"%title%\", %journal% {%volume%[[ %volume%{%number%[[, %number%]]}]]} (%year%){%pages%[[, pp. %pages%]]}.{%note%[[ %note%]]}");
+       static docstring articleFormat = from_ascii("%author%, \"%title%\", {!<i>!}%journal%{!</i>!} {%volume%[[ %volume%{%number%[[, %number%]]}]]} (%year%){%pages%[[, pp. %pages%]]}.{%note%[[ %note%]]}");
 
-static docstring bookFormat = from_ascii("{%author%[[%author%]][[%editor%, ed.]]}, %title%{%volume%[[ vol. %volume%]][[{%number%[[no. %number%]]}]]}{%edition%[[%edition%]]} ({%address%[[%address%: ]]}%publisher%, %year%).{%note%[[ %note%]]}");
+       static docstring bookFormat = from_ascii("{%author%[[%author%]][[%editor%, ed.]]}, {!<i>!}%title%{!</i>!}{%volume%[[ vol. %volume%]][[{%number%[[no. %number%]]}]]}{%edition%[[%edition%]]} ({%address%[[%address%: ]]}%publisher%, %year%).{%note%[[ %note%]]}");
 
-static docstring inSomething = from_ascii("%author%, \"%title%\", in{%editor%[[ %editor%, ed.,]]} %booktitle%{%volume%[[ vol. %volume%]][[{%number%[[no. %number%]]}]]}{%edition%[[%edition%]]} ({%address%[[%address%: ]]}%publisher%, %year%){%pages%[[, pp. %pages%]]}.{%note%[[ %note%]]}");
+static docstring inSomething = from_ascii("%author%, \"%title%\", in{%editor%[[ %editor%, ed.,]]} {!<i>!}%booktitle%{!</i>!}{%volume%[[ vol. %volume%]][[{%number%[[no. %number%]]}]]}{%edition%[[%edition%]]} ({%address%[[%address%: ]]}%publisher%, %year%){%pages%[[, pp. %pages%]]}.{%note%[[ %note%]]}");
 
 static docstring thesis = from_ascii("%author%, %title% ({%address%[[%address%: ]]}%school%, %year%).{%note%[[ %note%]]}");
 
-static docstring defaultFormat = from_ascii("{%author%[[%author%, ]][[{%editor%[[%editor%, ed., ]]}]]}%title%{%journal%[[, %journal%]][[{%publisher%[[, %publisher%]][[{%institution%[[, %institution%]]}]]}]]}{%year%[[ (%year%)]]}{%pages%[[, %pages%]]}.");
+static docstring defaultFormat = from_ascii("{%author%[[%author%, ]][[{%editor%[[%editor%, ed., ]]}]]}\"%title%\"{%journal%[[, {!<i>!}%journal%{!</i>!}]][[{%publisher%[[, %publisher%]][[{%institution%[[, %institution%]]}]]}]]}{%year%[[ (%year%)]]}{%pages%[[, %pages%]]}.");
 
 }
 
-docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref) const
+docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref,
+       bool richtext) const
 {
        if (!info_.empty())
                return info_;
@@ -490,15 +514,15 @@ docstring const & BibTeXInfo::getInfo(BibTeXInfo const * const xref) const
        }
 
        if (entry_type_ == "article")
-               info_ = expandFormat(articleFormat, xref);
+               info_ = expandFormat(articleFormat, xref, richtext);
        else if (entry_type_ == "book")
-               info_ = expandFormat(bookFormat, xref);
+               info_ = expandFormat(bookFormat, xref, richtext);
        else if (entry_type_.substr(0,2) == "in")
-               info_ = expandFormat(inSomething, xref);
+               info_ = expandFormat(inSomething, xref, richtext);
        else if (entry_type_ == "phdthesis" || entry_type_ == "mastersthesis")
-               info_ = expandFormat(thesis, xref);
+               info_ = expandFormat(thesis, xref, richtext);
        else 
-               info_ = expandFormat(defaultFormat, xref);
+               info_ = expandFormat(defaultFormat, xref, richtext);
 
        if (!info_.empty())
                info_ = convertLaTeXCommands(info_);
@@ -629,7 +653,7 @@ docstring const BiblioInfo::getYear(docstring const & key, bool use_modifier) co
 }
 
 
-docstring const BiblioInfo::getInfo(docstring const & key) const
+docstring const BiblioInfo::getInfo(docstring const & key, bool richtext) const
 {
        BiblioInfo::const_iterator it = find(key);
        if (it == end())
@@ -642,7 +666,7 @@ docstring const BiblioInfo::getInfo(docstring const & key) const
                if (xrefit != end())
                        xrefptr = &(xrefit->second);
        }
-       return data.getInfo(xrefptr);
+       return data.getInfo(xrefptr, richtext);
 }
 
 
index 24fa2caa27ab60aeb18bf5d6c9acaa9a08ba496a..b4d8ccf5f2d323f5998883fa1b6966ad4971abdb 100644 (file)
@@ -61,7 +61,8 @@ public:
        docstring const getXRef() const;
        /// \return formatted BibTeX data suitable for framing.
        /// \param pointer to crossref information
-       docstring const & getInfo(BibTeXInfo const * const xref = 0) const;
+       docstring const & getInfo(BibTeXInfo const * const xref,
+                       bool richtext) const;
        ///
        const_iterator find(docstring const & f) const { return bimap_.find(f); }
        ///
@@ -113,7 +114,11 @@ 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]]}
-       docstring expandFormat(docstring const & fmt, BibTeXInfo const * const xref) const;
+       /// material intended only for rich text (HTML) output should be 
+       /// wrapped in "{!" and "!}". it will be removed if richtext is
+       /// false.
+       docstring expandFormat(docstring const & fmt, 
+                       BibTeXInfo const * const xref, bool richtext) const;
        /// true if from BibTeX; false if from bibliography environment
        bool is_bibtex_;
        /// the BibTeX key for this entry
@@ -163,7 +168,8 @@ public:
        /// \return formatted BibTeX data associated with a given key.
        /// Empty if no info exists. 
        /// Note that this will retrieve data from the crossref as needed.
-       docstring const getInfo(docstring const & key) const;
+       docstring const getInfo(docstring const & key, 
+                       bool richtext = false) const;
        /// Is this a reference from a bibtex database
        /// or from a bibliography environment?
        bool isBibtex(docstring const & key) const;
index 4bab50a204181afdb6adb82b92f5413afc6be666..ef0840cc659d8bc2e8c39e1d3fa02b701b0b9721 100644 (file)
@@ -364,8 +364,8 @@ void GuiCitation::updateInfo(BiblioInfo const & bi, QModelIndex const & idx)
        }
 
        QString const keytxt = toqstr(
-               bi.getInfo(qstring_to_ucs4(idx.data().toString())));
-       infoML->document()->setPlainText(keytxt);
+               bi.getInfo(qstring_to_ucs4(idx.data().toString()), true));
+       infoML->document()->setHtml(keytxt);
 }