2002-08-09 00:42:12 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file sgml.C
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-08-09 00:42:12 +00:00
|
|
|
|
*
|
|
|
|
|
* \author Jos<EFBFBD> Matos
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* \author John Levon
|
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-08-09 00:42:12 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2002-10-21 16:21:56 +00:00
|
|
|
|
#include <config.h>
|
2002-11-04 02:12:42 +00:00
|
|
|
|
|
2004-10-24 20:55:22 +00:00
|
|
|
|
#include "sgml.h"
|
2002-11-04 02:12:42 +00:00
|
|
|
|
|
2004-10-24 20:55:22 +00:00
|
|
|
|
#include "buffer.h"
|
|
|
|
|
#include "bufferparams.h"
|
|
|
|
|
#include "counters.h"
|
|
|
|
|
#include "lyxtext.h"
|
2004-11-02 11:25:20 +00:00
|
|
|
|
#include "outputparams.h"
|
2002-08-09 00:42:12 +00:00
|
|
|
|
#include "paragraph.h"
|
2002-11-04 02:12:42 +00:00
|
|
|
|
|
2004-10-24 20:55:22 +00:00
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
#include "support/std_ostream.h"
|
|
|
|
|
#include "support/tostr.h"
|
|
|
|
|
|
2004-10-25 08:25:28 +00:00
|
|
|
|
#include <boost/tuple/tuple.hpp>
|
2004-10-25 08:30:51 +00:00
|
|
|
|
|
2004-10-29 23:08:04 +00:00
|
|
|
|
#include <map>
|
2004-10-25 00:26:05 +00:00
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2004-10-24 20:55:22 +00:00
|
|
|
|
using lyx::support::subst;
|
|
|
|
|
|
2002-08-09 00:42:12 +00:00
|
|
|
|
using std::make_pair;
|
2004-10-29 23:08:04 +00:00
|
|
|
|
using std::map;
|
2002-10-21 16:21:56 +00:00
|
|
|
|
using std::ostream;
|
2004-10-25 00:26:05 +00:00
|
|
|
|
using std::ostringstream;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
using std::pair;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
|
2002-08-09 00:42:12 +00:00
|
|
|
|
namespace sgml {
|
|
|
|
|
|
|
|
|
|
pair<bool, string> escapeChar(char c)
|
|
|
|
|
{
|
|
|
|
|
string str;
|
2002-11-04 02:12:42 +00:00
|
|
|
|
|
2002-08-09 00:42:12 +00:00
|
|
|
|
switch (c) {
|
|
|
|
|
case ' ':
|
|
|
|
|
return make_pair(true, string(" "));
|
|
|
|
|
break;
|
|
|
|
|
case '\0': // Ignore :-)
|
|
|
|
|
str.erase();
|
|
|
|
|
break;
|
|
|
|
|
case '&':
|
|
|
|
|
str = "&";
|
|
|
|
|
break;
|
|
|
|
|
case '<':
|
|
|
|
|
str = "<";
|
|
|
|
|
break;
|
|
|
|
|
case '>':
|
|
|
|
|
str = ">";
|
|
|
|
|
break;
|
2004-05-14 15:47:35 +00:00
|
|
|
|
#if 0
|
2002-08-09 00:42:12 +00:00
|
|
|
|
case '$':
|
|
|
|
|
str = "$";
|
|
|
|
|
break;
|
|
|
|
|
case '#':
|
|
|
|
|
str = "#";
|
|
|
|
|
break;
|
|
|
|
|
case '%':
|
|
|
|
|
str = "%";
|
|
|
|
|
break;
|
|
|
|
|
case '[':
|
|
|
|
|
str = "[";
|
|
|
|
|
break;
|
|
|
|
|
case ']':
|
|
|
|
|
str = "]";
|
|
|
|
|
break;
|
|
|
|
|
case '{':
|
|
|
|
|
str = "{";
|
|
|
|
|
break;
|
|
|
|
|
case '}':
|
|
|
|
|
str = "}";
|
|
|
|
|
break;
|
|
|
|
|
case '~':
|
|
|
|
|
str = "˜";
|
|
|
|
|
break;
|
|
|
|
|
case '"':
|
|
|
|
|
str = """;
|
|
|
|
|
break;
|
|
|
|
|
case '\\':
|
|
|
|
|
str = "\";
|
|
|
|
|
break;
|
2004-05-14 15:47:35 +00:00
|
|
|
|
#endif
|
2002-08-09 00:42:12 +00:00
|
|
|
|
default:
|
|
|
|
|
str = c;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return make_pair(false, str);
|
|
|
|
|
}
|
|
|
|
|
|
2002-11-04 02:12:42 +00:00
|
|
|
|
|
2004-10-25 00:26:05 +00:00
|
|
|
|
string escapeString(string const & raw)
|
|
|
|
|
{
|
|
|
|
|
ostringstream bin;
|
|
|
|
|
|
2004-10-25 08:30:51 +00:00
|
|
|
|
for(string::size_type i = 0; i < raw.size(); ++i) {
|
2004-10-25 00:26:05 +00:00
|
|
|
|
bool ws;
|
|
|
|
|
string str;
|
|
|
|
|
boost::tie(ws, str) = sgml::escapeChar(raw[i]);
|
|
|
|
|
bin << str;
|
|
|
|
|
}
|
|
|
|
|
return bin.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-10-29 23:08:04 +00:00
|
|
|
|
string const uniqueID(string const label)
|
|
|
|
|
{
|
|
|
|
|
static unsigned int seed = 1000;
|
2005-01-06 15:40:49 +00:00
|
|
|
|
return label + convert<string>(++seed);
|
2004-10-29 23:08:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-11-02 11:25:20 +00:00
|
|
|
|
string cleanID(Buffer const & buf, OutputParams const & runparams, std::string const & orig)
|
2004-10-29 23:08:04 +00:00
|
|
|
|
{
|
|
|
|
|
// 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.
|
2004-11-02 11:25:20 +00:00
|
|
|
|
LyXTextClass const & tclass = buf.params().getLyXTextClass();
|
|
|
|
|
string const allowed = runparams.flavor == OutputParams::XML? ".-_:":tclass.options();
|
|
|
|
|
|
|
|
|
|
if (allowed.empty())
|
|
|
|
|
return orig;
|
|
|
|
|
|
2004-10-29 23:08:04 +00:00
|
|
|
|
string::const_iterator it = orig.begin();
|
|
|
|
|
string::const_iterator end = orig.end();
|
|
|
|
|
|
|
|
|
|
string content;
|
|
|
|
|
|
|
|
|
|
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";
|
2004-11-26 14:52:54 +00:00
|
|
|
|
|
|
|
|
|
bool mangle = false;
|
2004-10-29 23:08:04 +00:00
|
|
|
|
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) {
|
2005-01-06 15:40:49 +00:00
|
|
|
|
content += "-" + convert<string>(mangleID++);
|
2004-10-29 23:08:04 +00:00
|
|
|
|
}
|
2005-01-06 15:40:49 +00:00
|
|
|
|
else if (isdigit(content[content.size() - 1])) {
|
2004-10-29 23:08:04 +00:00
|
|
|
|
content += ".";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mangledNames[orig] = content;
|
|
|
|
|
|
|
|
|
|
return content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-10-28 13:07:45 +00:00
|
|
|
|
void openTag(ostream & os, string const & name, string const & attribute)
|
2002-10-21 16:21:56 +00:00
|
|
|
|
{
|
2004-10-28 15:10:10 +00:00
|
|
|
|
// This should be fixed in layout files later.
|
|
|
|
|
string param = subst(attribute, "<", "\"");
|
|
|
|
|
param = subst(param, ">", "\"");
|
|
|
|
|
|
2004-10-24 20:55:22 +00:00
|
|
|
|
if (!name.empty() && name != "!-- --") {
|
|
|
|
|
os << '<' << name;
|
2004-10-28 15:10:10 +00:00
|
|
|
|
if (!param.empty())
|
|
|
|
|
os << " " << param;
|
2003-11-25 17:23:36 +00:00
|
|
|
|
os << '>';
|
2002-10-21 16:21:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-10-28 13:07:45 +00:00
|
|
|
|
void closeTag(ostream & os, string const & name)
|
2002-10-21 16:21:56 +00:00
|
|
|
|
{
|
2004-10-28 13:07:45 +00:00
|
|
|
|
if (!name.empty() && name != "!-- --")
|
2004-10-24 20:55:22 +00:00
|
|
|
|
os << "</" << name << '>';
|
2004-10-28 13:07:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-11-02 11:25:20 +00:00
|
|
|
|
void openTag(Buffer const & buf, ostream & os, OutputParams const & runparams, Paragraph const & par)
|
2004-10-28 13:07:45 +00:00
|
|
|
|
{
|
|
|
|
|
LyXLayout_ptr const & style = par.layout();
|
|
|
|
|
string const & name = style->latexname();
|
|
|
|
|
string param = style->latexparam();
|
|
|
|
|
Counters & counters = buf.params().getLyXTextClass().counters();
|
|
|
|
|
|
2004-11-02 11:25:20 +00:00
|
|
|
|
string id = par.getID(buf, runparams);
|
2004-10-28 13:07:45 +00:00
|
|
|
|
|
|
|
|
|
string attribute;
|
|
|
|
|
if(!id.empty()) {
|
|
|
|
|
if (param.find('#') != string::npos) {
|
|
|
|
|
string::size_type pos = param.find("id=<");
|
|
|
|
|
string::size_type end = param.find(">");
|
|
|
|
|
if( pos != string::npos and end != string::npos)
|
|
|
|
|
param.erase(pos, end-pos + 1);
|
|
|
|
|
}
|
|
|
|
|
attribute = id + ' ' + param;
|
|
|
|
|
} else {
|
|
|
|
|
if (param.find('#') != string::npos) {
|
|
|
|
|
if(!style->counter.empty())
|
|
|
|
|
counters.step(style->counter);
|
|
|
|
|
else
|
|
|
|
|
counters.step(style->latexname());
|
|
|
|
|
int i = counters.value(name);
|
2005-01-06 15:40:49 +00:00
|
|
|
|
attribute = subst(param, "#", convert<string>(i));
|
2004-10-28 13:07:45 +00:00
|
|
|
|
} else {
|
|
|
|
|
attribute = param;
|
|
|
|
|
}
|
2002-10-21 16:21:56 +00:00
|
|
|
|
}
|
2004-10-28 13:07:45 +00:00
|
|
|
|
openTag(os, name, attribute);
|
|
|
|
|
}
|
2002-10-21 16:21:56 +00:00
|
|
|
|
|
|
|
|
|
|
2004-10-28 13:07:45 +00:00
|
|
|
|
void closeTag(ostream & os, Paragraph const & par)
|
|
|
|
|
{
|
|
|
|
|
LyXLayout_ptr const & style = par.layout();
|
|
|
|
|
closeTag(os, style->latexname());
|
2002-10-21 16:21:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-09 00:42:12 +00:00
|
|
|
|
} // namespace sgml
|