]> git.lyx.org Git - lyx.git/blobdiff - src/xml.cpp
Update Win installer for new dictionary links. Untested.
[lyx.git] / src / xml.cpp
index 0daa9d84e2581c805216dc09e5a92d5fc2eb55b5..5b81024336481a9c612e639bd221e435d8f29717 100644 (file)
 #include "BufferParams.h"
 #include "Counters.h"
 #include "Layout.h"
-#include "OutputParams.h"
 #include "Paragraph.h"
 #include "Text.h"
 #include "TextClass.h"
 
 #include "support/convert.h"
+#include "support/debug.h"
 #include "support/docstream.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
@@ -108,12 +108,9 @@ docstring StartTag::writeTag() const
 {
        docstring output = '<' + tag_;
        if (!attr_.empty()) {
-               docstring attributes = xml::escapeString(attr_, XMLStream::ESCAPE_NONE);
-               attributes.erase(attributes.begin(), std::find_if(attributes.begin(), attributes.end(),
-                                                          [](int c) {return !std::isspace(c);}));
-               if (!attributes.empty()) {
+               docstring attributes = xml::trimLeft(xml::escapeString(attr_, XMLStream::ESCAPE_NONE));
+               if (!attributes.empty())
                        output += ' ' + attributes;
-               }
        }
        output += ">";
        return output;
@@ -126,12 +123,6 @@ docstring StartTag::writeEndTag() const
 }
 
 
-bool StartTag::operator==(FontTag const &rhs) const
-{
-       return rhs == *this;
-}
-
-
 docstring EndTag::writeEndTag() const
 {
        return from_utf8("</") + tag_ + from_utf8(">");
@@ -140,13 +131,13 @@ docstring EndTag::writeEndTag() const
 
 docstring CompTag::writeTag() const
 {
-       docstring output = '<' + from_utf8(tag_);
+       docstring output = '<' + tag_;
        if (!attr_.empty()) {
                // Erase the beginning of the attributes if it contains space characters: this function deals with that
                // automatically.
-               docstring attributes = escapeString(from_utf8(attr_), XMLStream::ESCAPE_NONE);
+               docstring attributes = escapeString(attr_, XMLStream::ESCAPE_NONE);
                attributes.erase(attributes.begin(), std::find_if(attributes.begin(), attributes.end(),
-                                                          [](int c) {return !std::isspace(c);}));
+                                                          [](char_type c) {return !isSpace(c);}));
                if (!attributes.empty()) {
                        output += ' ' + attributes;
                }
@@ -155,21 +146,12 @@ docstring CompTag::writeTag() const
        return output;
 }
 
-
-bool FontTag::operator==(StartTag const & tag) const
-{
-       FontTag const * const ftag = tag.asFontTag();
-       if (!ftag)
-               return false;
-       return (font_type_ == ftag->font_type_);
-}
-
 } // namespace xml
 
 
 void XMLStream::writeError(std::string const &s)
 {
-       LYXERR0(s);
+       LYXERR(Debug::OUTFILE, s);
        *this << ESCAPE_NONE << from_utf8("<!-- Output Error: " + s + " -->");
        *this << xml::CR();
 }
@@ -177,7 +159,7 @@ void XMLStream::writeError(std::string const &s)
 
 void XMLStream::writeError(docstring const &s)
 {
-       LYXERR0(s);
+       LYXERR(Debug::OUTFILE, s);
        *this << ESCAPE_NONE << from_utf8("<!-- Output Error: ");
        *this << s;
        *this << ESCAPE_NONE << from_utf8(" -->");
@@ -208,9 +190,6 @@ bool XMLStream::closeFontTags()
                if (**curtag != xml::parsep_tag)
                        os_ << (*curtag)->writeEndTag();
                tag_stack_.pop_back();
-               // this shouldn't happen, since then the font tags
-               // weren't in any other tag.
-               LASSERT(!tag_stack_.empty(), return true);
                if (tag_stack_.empty())
                        return true;
                curtag = &tag_stack_.back();
@@ -308,6 +287,16 @@ XMLStream &XMLStream::operator<<(docstring const &d)
 }
 
 
+XMLStream &XMLStream::operator<<(xml::NullTag const &)
+{
+       is_last_tag_cr_ = false;
+       clearTagDeque();
+       // Don't output anything to os_, by definition of a NullTag (as opposed to text output).
+       escape_ = ESCAPE_ALL;
+       return *this;
+}
+
+
 XMLStream &XMLStream::operator<<(const char *s)
 {
        is_last_tag_cr_ = false;
@@ -483,7 +472,8 @@ XMLStream &XMLStream::operator<<(xml::EndTag const &etag)
                                                   + "' when other tags were pending. Last pending tag is `"
                                                   + to_utf8(pending_tags_.back()->writeTag())
                                                   + "'. Tag discarded.");
-                               pending_tags_.erase(dit);
+                               if (!pending_tags_.empty())
+                                       pending_tags_.erase(dit);
                                return *this;
                        }
                }
@@ -604,6 +594,28 @@ docstring xml::uniqueID(docstring const & label)
 }
 
 
+bool xml::isNotOnlySpace(docstring const & str)
+{
+       for (auto const & c: str) {
+               if (c != ' ' && c != '\t' && c != '\n' && c != '\v' && c != '\f' && c != '\r')
+               return true;
+       }
+       return false;
+}
+
+
+docstring xml::trimLeft(docstring const & str)
+{
+       size_t i = 0;
+       for (auto const & c: str) {
+               if (c != ' ' && c != '\t' && c != '\n' && c != '\v' && c != '\f' && c != '\r')
+                       return str.substr(i, docstring::npos);
+               i++;
+       }
+       return str;
+}
+
+
 docstring xml::cleanID(docstring const & orig)
 {
        // The standard xml:id only allows letters, digits, '-' and '.' in a name.
@@ -651,7 +663,8 @@ docstring xml::cleanID(docstring const & orig)
        // as both of them would be transformed as "a.b". With this procedure, one will become "a.b" and the other "a.b-1".
        if (mangle && mangledNames.find(content) != mangledNames.end()) {
                int & mangleID = tMangleID.localData();
-               content += "-" + convert<docstring>(mangleID);
+               if (mangleID > 0)
+                       content += "-" + convert<docstring>(mangleID);
                mangleID += 1;
        }
 
@@ -661,6 +674,44 @@ docstring xml::cleanID(docstring const & orig)
 }
 
 
+bool operator==(xml::StartTag const & lhs, xml::StartTag const & rhs)
+{
+       xml::FontTag const * const lhs_ft = lhs.asFontTag();
+       xml::FontTag const * const rhs_ft = rhs.asFontTag();
+
+       if ((!lhs_ft && rhs_ft) || (lhs_ft && !rhs_ft))
+               return false;
+       if (!lhs_ft && !rhs_ft)
+               return lhs.tag_ == rhs.tag_;
+       return lhs_ft->tag_ == rhs_ft->tag_ && lhs_ft->font_type_ == rhs_ft->font_type_;
+}
+
+
+bool operator==(xml::EndTag const & lhs, xml::StartTag const & rhs)
+{
+       xml::EndFontTag const * const lhs_ft = lhs.asFontTag();
+       xml::FontTag const * const rhs_ft = rhs.asFontTag();
+
+       if ((!lhs_ft && rhs_ft) || (lhs_ft && !rhs_ft))
+               return false;
+       if (!lhs_ft && !rhs_ft)
+               return lhs.tag_ == rhs.tag_;
+       return lhs_ft->tag_ == rhs_ft->tag_ && lhs_ft->font_type_ == rhs_ft->font_type_;
+}
+
+
+bool operator!=(xml::EndTag const & lhs, xml::StartTag const & rhs)
+{
+       return !(lhs == rhs);
+}
+
+
+bool operator!=(xml::StartTag const & lhs, xml::StartTag const & rhs)
+{
+       return !(lhs == rhs);
+}
+
+
 void xml::openTag(odocstream & os, string const & name, string const & attribute)
 {
     // FIXME UNICODE
@@ -668,9 +719,9 @@ void xml::openTag(odocstream & os, string const & name, string const & attribute
     string param = subst(attribute, "<", "\"");
     param = subst(param, ">", "\"");
 
-    // Note: we ignore the name if it empty or if it is a comment "<!-- -->" or
+    // Note: we ignore the name if it is empty or if it is a comment "<!-- -->" or
     // if the name is *dummy*.
-    // We ignore dummy because dummy is not a valid docbook element and it is
+    // We ignore dummy because dummy is not a valid DocBook element and it is
     // the internal name given to single paragraphs in the latex output.
     // This allow us to simplify the code a lot and is a reasonable compromise.
     if (!name.empty() && name != "!-- --" && name != "dummy") {
@@ -735,4 +786,147 @@ void xml::closeTag(odocstream & os, Paragraph const & par)
 }
 
 
+void openInlineTag(XMLStream & xs, const docstring & tag, const docstring & attr)
+{
+       xs << xml::StartTag(tag, attr);
+}
+
+
+void closeInlineTag(XMLStream & xs, const docstring & tag)
+{
+       xs << xml::EndTag(tag);
+}
+
+
+void openParTag(XMLStream & xs, const docstring & tag, const docstring & attr)
+{
+       if (!xs.isLastTagCR())
+               xs << xml::CR();
+       xs << xml::StartTag(tag, attr);
+}
+
+
+void closeParTag(XMLStream & xs, const docstring & tag)
+{
+       xs << xml::EndTag(tag);
+       xs << xml::CR();
+}
+
+
+void openBlockTag(XMLStream & xs, const docstring & tag, const docstring & attr)
+{
+       if (!xs.isLastTagCR())
+               xs << xml::CR();
+       xs << xml::StartTag(tag, attr);
+       xs << xml::CR();
+}
+
+
+void closeBlockTag(XMLStream & xs, const docstring & tag)
+{
+       if (!xs.isLastTagCR())
+               xs << xml::CR();
+       xs << xml::EndTag(tag);
+       xs << xml::CR();
+}
+
+
+void xml::openTag(XMLStream & xs, const docstring & tag, const docstring & attr, const std::string & tagtype)
+{
+       if (tag.empty() || tag == from_ascii("NONE")) // Common check to be performed elsewhere, if it was not here.
+               return;
+
+       if (tag == from_ascii("para") || tagtype == "paragraph") // Special case for <para>: always considered as a paragraph.
+               openParTag(xs, tag, attr);
+       else if (tagtype == "block")
+               openBlockTag(xs, tag, attr);
+       else if (tagtype == "inline")
+               openInlineTag(xs, tag, attr);
+       else if (tagtype == "none")
+               xs << xml::StartTag(tag, attr);
+       else
+               xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + to_utf8(tag) + (attr.empty() ? "" : " ") +
+                               to_utf8(attr) + "'");
+}
+
+
+void xml::openTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype)
+{
+       xml::openTag(xs, from_utf8(tag), from_utf8(attr), tagtype);
+}
+
+
+void xml::openTag(XMLStream & xs, const docstring & tag, const std::string & attr, const std::string & tagtype)
+{
+       xml::openTag(xs, tag, from_utf8(attr), tagtype);
+}
+
+
+void xml::openTag(XMLStream & xs, const std::string & tag, const docstring & attr, const std::string & tagtype)
+{
+       xml::openTag(xs, from_utf8(tag), attr, tagtype);
+}
+
+
+void xml::closeTag(XMLStream & xs, const docstring & tag, const std::string & tagtype)
+{
+       if (tag.empty() || tag == "NONE" || tag == "IGNORE")
+               return;
+
+       if (tag == "para" || tagtype == "paragraph") // Special case for <para>: always considered as a paragraph.
+               closeParTag(xs, tag);
+       else if (tagtype == "block")
+               closeBlockTag(xs, tag);
+       else if (tagtype == "inline")
+               closeInlineTag(xs, tag);
+       else if (tagtype == "none")
+               xs << xml::EndTag(tag);
+       else
+               xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + to_utf8(tag) + "'");
+}
+
+
+void xml::closeTag(XMLStream & xs, const std::string & tag, const std::string & tagtype)
+{
+       xml::closeTag(xs, from_utf8(tag), tagtype);
+}
+
+
+void xml::compTag(XMLStream & xs, const docstring & tag, const docstring & attr, const std::string & tagtype)
+{
+       if (tag.empty() || tag == from_ascii("NONE"))
+               return;
+
+       // Special case for <para>: always considered as a paragraph.
+       if (tag == from_ascii("para") || tagtype == "paragraph" || tagtype == "block") {
+               if (!xs.isLastTagCR())
+                       xs << xml::CR();
+               xs << xml::CompTag(tag, attr);
+               xs << xml::CR();
+       } else if (tagtype == "inline") {
+               xs << xml::CompTag(tag, attr);
+       } else {
+               xs.writeError("Unrecognised tag type '" + tagtype + "' for '" + to_utf8(tag) + "'");
+       }
+}
+
+
+void xml::compTag(XMLStream & xs, const std::string & tag, const std::string & attr, const std::string & tagtype)
+{
+       xml::compTag(xs, from_utf8(tag), from_utf8(attr), tagtype);
+}
+
+
+void xml::compTag(XMLStream & xs, const docstring & tag, const std::string & attr, const std::string & tagtype)
+{
+       xml::compTag(xs, tag, from_utf8(attr), tagtype);
+}
+
+
+void xml::compTag(XMLStream & xs, const std::string & tag, const docstring & attr, const std::string & tagtype)
+{
+       xml::compTag(xs, from_utf8(tag), attr, tagtype);
+}
+
+
 } // namespace lyx