]> git.lyx.org Git - lyx.git/blobdiff - src/Paragraph.cpp
Routines for calculating numerical labels for BibTeX citations.
[lyx.git] / src / Paragraph.cpp
index 39d5540c68697606bec3596101984300943df615..233057e9163ad9058363f378066dbef466f92de7 100644 (file)
@@ -5,6 +5,7 @@
  *
  * \author Asger Alstrup
  * \author Lars Gullik Bjønnes
+ * \author Richard Heck (XHTML output)
  * \author Jean-Marc Lasgouttes
  * \author Angus Leeming
  * \author John Levon
@@ -188,9 +189,8 @@ public:
        FontList fontlist_;
 
        ///
-       unsigned int id_;
-       ///
-       static unsigned int paragraph_id;
+       int id_;
+
        ///
        ParagraphParameters params_;
 
@@ -207,17 +207,14 @@ public:
        ///
        TextContainer text_;
        
-       typedef std::set<docstring> Words;
+       typedef set<docstring> Words;
        ///
-       Words words_;
+       map<Language, Words> words_;
        ///
        Layout const * layout_;
 };
 
 
-// Initialization of the counter for the paragraph id's,
-unsigned int Paragraph::Private::paragraph_id = 0;
-
 namespace {
 
 struct special_phrase {
@@ -239,20 +236,26 @@ size_t const phrases_nr = sizeof(special_phrases)/sizeof(special_phrase);
 
 
 Paragraph::Private::Private(Paragraph * owner, Layout const & layout)
-       : owner_(owner), inset_owner_(0), begin_of_body_(0), layout_(&layout)
+       : owner_(owner), inset_owner_(0), id_(-1), begin_of_body_(0), layout_(&layout)
 {
-       id_ = paragraph_id++;
        text_.reserve(100);
 }
 
 
+// Initialization of the counter for the paragraph id's,
+//
+// FIXME: There should be a more intelligent way to generate and use the
+// paragraph ids per buffer instead a global static counter for all InsetText
+// in the running program.
+static int paragraph_id = -1;
+
 Paragraph::Private::Private(Private const & p, Paragraph * owner)
        : owner_(owner), inset_owner_(p.inset_owner_), fontlist_(p.fontlist_), 
          params_(p.params_), changes_(p.changes_), insetlist_(p.insetlist_),
          begin_of_body_(p.begin_of_body_), text_(p.text_), words_(p.words_),
          layout_(p.layout_)
 {
-       id_ = paragraph_id++;
+       id_ = ++paragraph_id;
 }
 
 
@@ -264,7 +267,7 @@ Paragraph::Private::Private(Private const & p, Paragraph * owner,
          begin_of_body_(p.begin_of_body_), words_(p.words_),
          layout_(p.layout_)
 {
-       id_ = paragraph_id++;
+       id_ = ++paragraph_id;
        if (beg >= pos_type(p.text_.size()))
                return;
        text_ = p.text_.substr(beg, end - beg);
@@ -1071,6 +1074,18 @@ bool Paragraph::Private::latexSpecialPhrase(odocstream & os, pos_type & i,
 
 void Paragraph::Private::validate(LaTeXFeatures & features) const
 {
+       if (layout_->inpreamble && inset_owner_) {
+               Buffer const & buf = inset_owner_->buffer();
+               BufferParams const & bp = buf.params();
+               Font f;
+               TexRow tr;
+               odocstringstream ods;
+               owner_->latex(bp, f, ods, tr, features.runparams());
+               docstring d = ods.str();
+               if (!d.empty())
+                       features.addPreambleSnippet(to_utf8(d));
+       }
+       
        // check the params.
        if (!params_.spacing().isDefault())
                features.require("setspace");
@@ -1945,6 +1960,9 @@ bool Paragraph::latex(BufferParams const & bparams,
 {
        LYXERR(Debug::LATEX, "Paragraph::latex...     " << this);
 
+       if (layout().inpreamble)
+               return true;
+
        bool return_value = false;
 
        bool const allowcust = allowParagraphCustomization();
@@ -2291,13 +2309,12 @@ pos_type Paragraph::firstWordLyXHTML(XHTMLStream & xs, OutputParams const & runp
        pos_type i;
        for (i = 0; i < size(); ++i) {
                if (Inset const * inset = getInset(i)) {
-// FIXME XHTMLStream
-//                     inset->xhtml(xs, runparams);
+                       inset->xhtml(xs, runparams);
                } else {
                        char_type c = d->text_[i];
                        if (c == ' ')
                                break;
-                       xs << html::escapeChar(c);
+                       xs << c;
                }
        }
        return i;
@@ -2379,17 +2396,23 @@ docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
                                    XHTMLStream & xs,
                                    OutputParams const & runparams,
                                    Font const & outerfont,
+                                               bool fortoc,
                                    pos_type initial) const
 {
        docstring retval;
 
-       // FIXME We really need to manage the tag nesting here.
-       // Probably in the same sort of way as in output_xhtml.
        bool emph_flag = false;
        bool bold_flag = false;
-       std::string closing_tag;
+       string closing_tag;
 
        Layout const & style = *d->layout_;
+
+       if (!fortoc) {
+               // generate a magic label for this paragraph
+               string const attr = "id='" + magicLabel() + "'";
+               xs << CompTag("a", attr);
+       }
+
        FontInfo font_old =
                style.labeltype == LABEL_MANUAL ? style.labelfont : style.font;
 
@@ -2417,16 +2440,19 @@ docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
                                bold_flag = false;
                        }
                }
-               // FIXME Other such tags? 
+               // FIXME XHTML
+               // Other such tags? What about the other text ranges?
 
                Inset const * inset = getInset(i);
                if (inset) {
+                       InsetCommand const * ic = inset->asInsetCommand();
                        InsetLayout const & il = inset->getLayout();
-                       OutputParams np = runparams;
-                       if (!il.htmlisblock())
-                               np.html_in_par = true;
-                       // FIXME XHTMLStream
-                       // retval += inset->xhtml(xs, np);
+                       if (!fortoc || il.isInToc() || (ic && ic->isInToc())) {
+                               OutputParams np = runparams;
+                               if (!il.htmlisblock())
+                                       np.html_in_par = true;
+                               retval += inset->xhtml(xs, np);
+                       }
                } else {
                        char_type c = d->text_[i];
 
@@ -2447,7 +2473,10 @@ docstring Paragraph::simpleLyXHTMLOnePar(Buffer const & buf,
                                }
                                else
                                        str += c;
-                               xs << str;
+                               // We don't want to escape the entities. Note that
+                               // it is safe to do this, since str can otherwise
+                               // only be "-". E.g., it can't be "<".
+                               xs << XHTMLStream::NextRaw() << str;
                        } else
                                xs << c;
                }
@@ -2627,6 +2656,12 @@ int Paragraph::id() const
 }
 
 
+void Paragraph::setId(int id)
+{
+       d->id_ = id;
+}
+
+
 Layout const & Paragraph::layout() const
 {
        return *d->layout_;
@@ -2995,10 +3030,13 @@ bool Paragraph::isSeparator(pos_type pos) const
 
 void Paragraph::deregisterWords()
 {
+       map<Language, Private::Words>::const_iterator itl;
        Private::Words::const_iterator it;
-       WordList & wl = theWordList();
-       for (it = d->words_.begin(); it != d->words_.end(); ++it)
-               wl.remove(*it);
+       for (itl = d->words_.begin(); itl != d->words_.end(); ++itl) {
+               WordList * wl = theWordList(itl->first);
+               for (it = (itl->second).begin(); it != (itl->second).end(); ++it)
+                       wl->remove(*it);
+       }
        d->words_.clear();
 }
 
@@ -3050,7 +3088,11 @@ void Paragraph::collectWords()
                locateWord(from, pos, WHOLE_WORD);
                if (pos - from >= 6) {
                        docstring word = asString(from, pos, AS_STR_NONE);
-                       d->words_.insert(word);
+                       FontList::const_iterator cit = d->fontlist_.fontIterator(pos);
+                       if (cit == d->fontlist_.end())
+                               return;
+                       Language const * lang = cit->font().language();
+                       d->words_[*lang].insert(word);
                }
        }
 }
@@ -3058,10 +3100,13 @@ void Paragraph::collectWords()
 
 void Paragraph::registerWords()
 {
+       map<Language, Private::Words>::const_iterator itl;
        Private::Words::const_iterator it;
-       WordList & wl = theWordList();
-       for (it = d->words_.begin(); it != d->words_.end(); ++it)
-               wl.insert(*it);
+       for (itl = d->words_.begin(); itl != d->words_.end(); ++itl) {
+               WordList * wl = theWordList(itl->first);
+               for (it = (itl->second).begin(); it != (itl->second).end(); ++it)
+                       wl->insert(*it);
+       }
 }
 
 
@@ -3121,4 +3166,12 @@ bool Paragraph::isMisspelled(pos_type pos) const
 }
 
 
+string Paragraph::magicLabel() const
+{
+       stringstream ss;
+       ss << "magicparlabel-" << id();
+       return ss.str();
+}
+
+
 } // namespace lyx