]> git.lyx.org Git - features.git/commitdiff
Sanitize ids for SGML/XML.
authorJosé Matox <jamatos@lyx.org>
Fri, 29 Oct 2004 23:08:04 +0000 (23:08 +0000)
committerJosé Matox <jamatos@lyx.org>
Fri, 29 Oct 2004 23:08:04 +0000 (23:08 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@9142 a592a061-630c-0410-9148-cb99ea01b6c8

13 files changed:
src/ChangeLog
src/insets/ChangeLog
src/insets/insetgraphics.C
src/insets/insetlabel.C
src/insets/insetref.C
src/mathed/ChangeLog
src/mathed/math_hullinset.C
src/mathed/ref_inset.C
src/output_docbook.C
src/paragraph.C
src/paragraph.h
src/sgml.C
src/sgml.h

index 72e7e17643e52f63353560f5c47f937ce072f3f3..0955a935158fd7de776aa689d63a4132a8fef133 100644 (file)
@@ -1,3 +1,14 @@
+2004-10-29  José Matos  <jamatos@lyx.org>
+
+       * output_docbook.C (makeEnvironment):
+       * sgml.C (openTag):
+       * paragraph.[Ch] (getID): rename function, and return it enclosed in id="...".
+
+2004-10-29 Andreas Vox  <vox@isp.uni-luebeck.de>
+
+       * sgml.[Ch] (uniqueID): returns a unique id for a given label.
+       (cleanID): sanitize any id.
+
 2004-10-29  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
 
        * buffer.C, lyxlex_pimpl.C:
index 0bd9e7c3d0972a46e56e8f2bbb65b39de856f348..1d3430135ce63d98a5eb4208c7270f111b6711f2 100644 (file)
@@ -1,3 +1,12 @@
+2004-10-29  José Matos  <jamatos@lyx.org>
+
+       * insetgraphics.C (uniqueID): transfered to sgml.C
+
+2004-10-29 Andreas Vox  <vox@isp.uni-luebeck.de>
+
+       * insetlabel.C (docbook, linuxdoc):
+       * insetref.C (docbook, linuxdoc) sanitize id.
+
 2004-10-29  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
 
        * ExternalSupport.C: s/getExtFromContents/getFormatFromContents/
index 956819d79856df56eedf2617aedffd83c0b966ab..71f4a3d531edeaa72e11e8bad9961cab9f684450 100644 (file)
@@ -69,6 +69,7 @@ TODO
 #include "metricsinfo.h"
 #include "mover.h"
 #include "outputparams.h"
+#include "sgml.h"
 
 #include "frontends/Alert.h"
 #include "frontends/LyXView.h"
@@ -78,7 +79,6 @@ TODO
 #include "support/lyxlib.h" // float_equal
 #include "support/os.h"
 #include "support/systemcall.h"
-#include "support/tostr.h"
 
 #include <boost/bind.hpp>
 #include <boost/tuple/tuple.hpp>
@@ -126,13 +126,6 @@ string const RemoveExtension(string const & filename)
 }
 
 
-string const uniqueID()
-{
-       static unsigned int seed = 1000;
-       return "graph" + tostr(++seed);
-}
-
-
 string findTargetFormat(string const & format, OutputParams const & runparams)
 {
        // Are we using latex or pdflatex?
@@ -160,7 +153,7 @@ string findTargetFormat(string const & format, OutputParams const & runparams)
 
 
 InsetGraphics::InsetGraphics()
-       : graphic_label(uniqueID()),
+       : graphic_label(sgml::uniqueID("graph")),
          graphic_(new RenderGraphic(this))
 {}
 
@@ -168,7 +161,7 @@ InsetGraphics::InsetGraphics()
 InsetGraphics::InsetGraphics(InsetGraphics const & ig)
        : InsetOld(ig),
          boost::signals::trackable(),
-         graphic_label(uniqueID()),
+         graphic_label(sgml::uniqueID("graph")),
          graphic_(new RenderGraphic(*ig.graphic_, this))
 {
        setParams(ig.params());
index 816821f9c792737bda6ab0d8bbc8b53e90eaabc4..c3643f5b95a33fc3c36420ef5a8c00a4fa0559dc 100644 (file)
@@ -20,6 +20,7 @@
 #include "lyxtext.h"
 #include "paragraph.h"
 #include "pariterator.h"
+#include "sgml.h"
 
 #include "frontends/LyXView.h"
 
@@ -135,7 +136,7 @@ int InsetLabel::plaintext(Buffer const &, ostream & os,
 int InsetLabel::linuxdoc(Buffer const &, ostream & os,
                         OutputParams const &) const
 {
-       os << "<label id=\"" << getContents() << "\" >";
+       os << "<label id=\"" << sgml::cleanID(getContents()) << "\" >";
        return 0;
 }
 
@@ -143,6 +144,6 @@ int InsetLabel::linuxdoc(Buffer const &, ostream & os,
 int InsetLabel::docbook(Buffer const &, ostream & os,
                        OutputParams const &) const
 {
-       os << "<!-- anchor id=\"" << getContents() << "\" -->";
+       os << "<!-- anchor id=\"" << sgml::cleanID(getContents()) << "\" -->";
        return 0;
 }
index f30f0710229166f024ca9323c9d89f235612dad5..b5f82e6748fdac2277cb6ae2bde1eb2c8e585bfe 100644 (file)
@@ -19,6 +19,7 @@
 #include "gettext.h"
 #include "LaTeXFeatures.h"
 #include "outputparams.h"
+#include "sgml.h"
 
 #include "frontends/LyXView.h"
 
@@ -104,7 +105,7 @@ int InsetRef::plaintext(Buffer const &, ostream & os,
 int InsetRef::linuxdoc(Buffer const &, ostream & os,
                       OutputParams const &) const
 {
-       os << "<ref id=\"" << getContents()
+       os << "<ref id=\"" << sgml::cleanID(getContents())
           << "\" name=\"" << getOptions() << "\" >";
        return 0;
 }
@@ -114,11 +115,11 @@ int InsetRef::docbook(Buffer const &, ostream & os,
                      OutputParams const & runparams) const
 {
        if (getOptions().empty() && runparams.flavor == OutputParams::XML) {
-               os << "<xref linkend=\"" << getContents() << "\" />";
+               os << "<xref linkend=\"" << sgml::cleanID(getContents()) << "\" />";
        } else if (getOptions().empty()) {
-               os << "<xref linkend=\"" << getContents() << "\">";
+               os << "<xref linkend=\"" << sgml::cleanID(getContents()) << "\">";
        } else {
-               os << "<link linkend=\"" << getContents()
+               os << "<link linkend=\"" << sgml::cleanID(getContents())
                   << "\">" << getOptions() << "</link>";
        }
 
index 09d22478e3dbcedd78c2d2f2587a43fea28874f0..63dc990baff498276c37b62a8a0cac0220ed1d63 100644 (file)
@@ -1,3 +1,8 @@
+2004-10-29  Andreas Vox  <vox@isp.uni-luebeck.de>
+
+       * math_hullinset.C (docbook):
+       * ref_inset.C (docbook): sanitize ids, clean exported docbook.
+
 2004-10-15  Georg Baum  <Georg.Baum@post.rwth-aachen.de>
 
        * math_hullinset.C (mutate): fix endless loop for unknown types
index 5d068841c49bb2748a4e655fcd2bafca497191d1..4248ad291d132565fb624f24dbdb3ef61f19f131 100644 (file)
@@ -30,6 +30,7 @@
 #include "lyx_main.h"
 #include "lyxrc.h"
 #include "outputparams.h"
+#include "sgml.h"
 #include "textpainter.h"
 #include "undo.h"
 
 #include "graphics/PreviewImage.h"
 #include "graphics/PreviewLoader.h"
 
+#include "support/lstrings.h"
+
 #include <boost/bind.hpp>
 
 #include <sstream>
 
 using lyx::cap::grabAndEraseSelection;
+using lyx::support::subst;
 
 using std::endl;
 using std::max;
@@ -1122,7 +1126,6 @@ bool MathHullInset::getStatus(LCursor & cur, FuncRequest const & cmd,
 #include "frontends/LyXView.h"
 #include "frontends/Dialogs.h"
 
-#include "support/lstrings.h"
 #include "support/lyxlib.h"
 
 
@@ -1342,25 +1345,44 @@ int MathHullInset::docbook(Buffer const & buf, ostream & os,
                name = "informalequation";
 
        string bname = name;
-       if (! label(0).empty()) bname += " id=\"" + label(0)+ "\"";
+       if (!label(0).empty()) 
+               bname += " id=\"" + sgml::cleanID(label(0)) + "\"";
        ms << MTag(bname.c_str());
 
+       ostringstream ls;
        if (runparams.flavor == OutputParams::XML) {
-               ms <<   MTag("math");
-               MathGridInset::mathmlize(ms);
-               ms <<   ETag("math");
-               ms <<   MTag("alt role=\"tex\" ");
-               ostringstream ls;
+               ms << MTag("alt role=\"tex\" ");
+               // Workaround for db2latex: db2latex always includes equations with
+               // \ensuremath{} or \begin{display}\end{display}
+               // so we strip LyX' math environment
                WriteStream wi(ls, false, false);
                MathGridInset::write(wi);
-               ms << ls.str();
-               ms <<   ETag("alt");
+               ms << subst(subst(ls.str(), "&", "&amp;"), "<", "&lt;");
+               ms << ETag("alt");
+               ms << MTag("math");
+               MathGridInset::mathmlize(ms);
+               ms << ETag("math");
        } else {
-               ms <<   MTag("alt role=\"tex\" ");
-               res = latex(buf, ms.os(), runparams);
-               ms <<   ETag("alt");
+               ms << MTag("alt role=\"tex\"");         
+               res = latex(buf, ls, runparams);
+               ms << subst(subst(ls.str(), "&", "&amp;"), "<", "&lt;");
+               ms << ETag("alt");
        }
-
+       
+       ms <<  "<graphic fileref=\"eqn/";
+       if ( !label(0).empty()) 
+               ms << sgml::cleanID(label(0));
+       else {
+               // Some arbitrary unique number for this os. 
+               // Note that each call of math_hullinset::docbook()
+               // will increase the os position by at least 60 chars or more
+               ms << sgml::uniqueID("anon");
+       }
+       if (runparams.flavor == OutputParams::XML) 
+               ms << "\"/>";
+       else 
+               ms << "\">";
+               
        ms << ETag(name.c_str());
        return ms.line() + res;
 }
index 2706f76bbc70b431b0ae3c4de99cfb8394b6a932..0aac3bbe28d5e3047eefee9cd0820b1935be3927 100644 (file)
@@ -21,6 +21,8 @@
 #include "math_data.h"
 #include "math_factory.h"
 #include "math_support.h"
+#include "outputparams.h"
+#include "sgml.h"
 
 #include "frontends/LyXView.h"
 #include "frontends/Dialogs.h"
@@ -131,17 +133,21 @@ int RefInset::plaintext(std::ostream & os, OutputParams const &) const
 int RefInset::linuxdoc(std::ostream & os, OutputParams const &) const
 {
        os << "<ref id=\"" << asString(cell(0))
-          << "\" name=\"" << asString(cell(1)) << "\" >";
+          << "\" name=\"" << asString(cell(1)) << "\">";
        return 0;
 }
 
 
-int RefInset::docbook(std::ostream & os, OutputParams const &) const
+int RefInset::docbook(std::ostream & os, OutputParams const & runparams) const
 {
        if (cell(1).empty()) {
-               os << "<xref linkend=\"" << asString(cell(0)) << "\">";
+               os << "<xref linkend=\"" << sgml::cleanID(asString(cell(0)));
+               if (runparams.flavor == OutputParams::XML) 
+                       os << "\"/>";
+               else
+                       os << "\">";
        } else {
-               os << "<link linkend=\"" << asString(cell(0))
+               os << "<link linkend=\"" << sgml::cleanID(asString(cell(0)))
                   << "\">" << asString(cell(1)) << "</link>";
        }
 
index 593071c11adb4a34f9dea44a603dba53c1d0ec87..69e04977d02917473ddbfda7e6da037f8ab91a83 100644 (file)
@@ -133,8 +133,7 @@ ParagraphList::const_iterator makeEnvironment(Buffer const & buf,
        while (par != pend) {
                LyXLayout_ptr const & style = par->layout();
                ParagraphList::const_iterator send;
-               string id = par->getDocbookId();
-               id = id.empty()? "" : " id = \"" + id + "\"";
+               string id = par->getID();
                string wrapper = "";
                pos_type sep = 0;
 
index 72d73583033b0ce91ed46b8099d425093533fa38..95d8bbfb9777f8145fb5eaa8e6bb269f634c7074 100644 (file)
@@ -1331,14 +1331,15 @@ void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
 }
 
 
-string Paragraph::getDocbookId() const
+string Paragraph::getID() const
 {
        for (pos_type i = 0; i < size(); ++i) {
                if (isInset(i)) {
                        InsetBase const * inset = getInset(i);
                        InsetBase::Code lyx_code = inset->lyxCode();
                        if (lyx_code == InsetBase::LABEL_CODE) {
-                               return static_cast<InsetCommand const *>(inset)->getContents();
+                               string const id = static_cast<InsetCommand const *>(inset)->getContents();
+                               return "id=\"" + sgml::cleanID(id) + "\"";
                        }
                }
 
index f937fdbd07c438df08d2aec2145370c234b52b5b..a204661bc6ef8040b239bad914db3e04353ebf4f 100644 (file)
@@ -129,8 +129,8 @@ public:
                                  OutputParams const & runparams,
                                  lyx::depth_type depth) const;
 
-       /// Get the id of the paragraph, usefull for docbook
-       std::string getDocbookId() const;
+       /// Get the id of the paragraph, usefull for docbook and linuxdoc
+       std::string getID() const;
 
        // Get the first word of a paragraph, return the position where it left
        lyx::pos_type getFirstWord(Buffer const & buf,
index 12f045193ac550475b1b7e2abe8f594f6c10a68a..12f3ae53e374117c499202236635028575ec2e4a 100644 (file)
 
 #include <boost/tuple/tuple.hpp>
 
+#include <map>
 #include <sstream>
 
 using lyx::support::subst;
 
 using std::make_pair;
-
+using std::map;
 using std::ostream;
 using std::ostringstream;
 using std::pair;
@@ -112,6 +113,75 @@ string escapeString(string const & raw)
 }
 
 
+string const uniqueID(string const label)
+{
+       static unsigned int seed = 1000;
+       return label + tostr(++seed);
+}
+
+
+string cleanID(std::string const & orig, std::string const & allowed)
+{
+       // The standard DocBook SGML declaration only allows letters,
+       // digits, '-' and '.' in a name.
+       // Since users might change that declaration one has to cater
+       // for additional allowed characters.
+       // This routine replaces illegal characters by '-' or '.'
+       // and adds a number for uniqueness.
+       // If you know what you are doing, you can set allowed==""
+       // to disable this mangling.
+       
+       string::const_iterator it  = orig.begin();
+       string::const_iterator end = orig.end();
+
+       string content;
+
+       if (allowed.empty()) {
+               return orig;
+       }
+
+       typedef map<string, string> MangledMap;
+       static MangledMap mangledNames;
+       static int mangleID = 1;
+
+       MangledMap::const_iterator const known = mangledNames.find(orig);
+       if (known != mangledNames.end())
+               return (*known).second;
+
+       // make sure it starts with a letter
+       if (!isalpha(*it) && allowed.find(*it) >= allowed.size())
+               content += "x";
+       
+       bool mangle = false;    
+       for (; it != end; ++it) {
+               char c = *it;
+               if (isalpha(c) || isdigit(c) || c == '-' || c == '.' || allowed.find(c) < allowed.size())
+                       content += c;
+               else if (c == '_' || c == ' ') {
+                       mangle = true;
+                       content += "-";
+               }
+               else if (c == ':' || c == ',' || c == ';' || c == '!') {
+                       mangle = true;
+                       content += ".";
+               }
+               else {
+                       mangle = true;
+               }
+       }
+       if (mangle) {
+               content += "-" + tostr(mangleID++);
+       }
+       else if (isdigit(content[content.size()-1])) {
+               content += ".";
+       }
+
+       mangledNames[orig] = content;
+
+       return content;
+}
+
+
 void openTag(ostream & os, string const & name, string const & attribute)
 {
        // This should be fixed in layout files later.
@@ -141,8 +211,7 @@ void openTag(Buffer const & buf, ostream & os, Paragraph const & par)
        string param = style->latexparam();
        Counters & counters = buf.params().getLyXTextClass().counters();
 
-       string id = par.getDocbookId();
-       id = id.empty()? "" : " id = \"" + id + "\"";
+       string id = par.getID();
 
        string attribute;
        if(!id.empty()) {
index ff495d7882ea5ce99498a3b159ee813eafa91efa..e6b2e4e487a47036d2f2ec2ec31ca49f7352e590 100644 (file)
@@ -34,6 +34,12 @@ std::pair<bool, std::string> escapeChar(char c);
 /// Escape a word instead of a single character
 std::string escapeString(std::string const & raw);
 
+/// replaces illegal chars like ':' or '_' from SGML ID attributes
+std::string cleanID(std::string const & orig, std::string const & allowed = std::string());
+
+/// returns a uniq numeric id
+std::string const uniqueID(std::string const label);
+
 /// Opens tag
 void openTag(std::ostream & os, std::string const & name,
            std::string const & attribute = std::string());