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
|
|
|
|
|
2006-10-21 11:38:43 +00:00
|
|
|
|
#include "support/docstring.h"
|
2004-10-24 20:55:22 +00:00
|
|
|
|
#include "support/lstrings.h"
|
|
|
|
|
#include "support/std_ostream.h"
|
2005-01-06 16:39:35 +00:00
|
|
|
|
#include "support/convert.h"
|
2004-10-24 20:55:22 +00:00
|
|
|
|
|
2004-10-29 23:08:04 +00:00
|
|
|
|
#include <map>
|
2004-10-25 00:26:05 +00:00
|
|
|
|
#include <sstream>
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
|
using support::subst;
|
2004-10-24 20:55:22 +00:00
|
|
|
|
|
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-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
|
2006-10-21 11:38:43 +00:00
|
|
|
|
docstring sgml::escapeChar(char_type c)
|
2002-08-09 00:42:12 +00:00
|
|
|
|
{
|
2006-10-21 11:38:43 +00:00
|
|
|
|
docstring str;
|
2002-08-09 00:42:12 +00:00
|
|
|
|
switch (c) {
|
|
|
|
|
case ' ':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += " ";
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
|
|
|
|
case '&':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += "&";
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
|
|
|
|
case '<':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += "<";
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
|
|
|
|
case '>':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += ">";
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
2004-05-14 15:47:35 +00:00
|
|
|
|
#if 0
|
2002-08-09 00:42:12 +00:00
|
|
|
|
case '$':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += "$";
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
|
|
|
|
case '#':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += "#";
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
|
|
|
|
case '%':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += "%";
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
|
|
|
|
case '[':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += "[";
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
|
|
|
|
case ']':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += "]";
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
|
|
|
|
case '{':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += "{";
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
|
|
|
|
case '}':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += "}";
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
|
|
|
|
case '~':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += "˜";
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
|
|
|
|
case '"':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += """;
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
|
|
|
|
case '\\':
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += "\";
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
2004-05-14 15:47:35 +00:00
|
|
|
|
#endif
|
2002-08-09 00:42:12 +00:00
|
|
|
|
default:
|
2006-10-21 11:38:43 +00:00
|
|
|
|
str += c;
|
2002-08-09 00:42:12 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2006-10-21 11:38:43 +00:00
|
|
|
|
return str;
|
2002-08-09 00:42:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-11-04 02:12:42 +00:00
|
|
|
|
|
2006-10-21 11:38:43 +00:00
|
|
|
|
docstring sgml::escapeString(docstring const & raw)
|
2004-10-25 00:26:05 +00:00
|
|
|
|
{
|
2006-10-21 11:38:43 +00:00
|
|
|
|
odocstringstream bin;
|
2004-10-25 00:26:05 +00:00
|
|
|
|
|
2006-10-21 11:38:43 +00:00
|
|
|
|
for(docstring::size_type i = 0; i < raw.size(); ++i) {
|
|
|
|
|
bin << sgml::escapeChar(raw[i]);
|
2004-10-25 00:26:05 +00:00
|
|
|
|
}
|
|
|
|
|
return bin.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-19 21:00:33 +00:00
|
|
|
|
string const sgml::uniqueID(string const label)
|
2004-10-29 23:08:04 +00:00
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-19 21:00:33 +00:00
|
|
|
|
string sgml::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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-19 21:00:33 +00:00
|
|
|
|
void sgml::openTag(odocstream & os, string const & name, string const & attribute)
|
2002-10-21 16:21:56 +00:00
|
|
|
|
{
|
2006-10-19 21:00:33 +00:00
|
|
|
|
// FIXME UNICODE
|
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 != "!-- --") {
|
2006-10-21 00:16:43 +00:00
|
|
|
|
os << '<' << from_ascii(name);
|
2004-10-28 15:10:10 +00:00
|
|
|
|
if (!param.empty())
|
2006-10-21 00:16:43 +00:00
|
|
|
|
os << ' ' << from_ascii(param);
|
2003-11-25 17:23:36 +00:00
|
|
|
|
os << '>';
|
2002-10-21 16:21:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-19 21:00:33 +00:00
|
|
|
|
void sgml::closeTag(odocstream & os, string const & name)
|
2002-10-21 16:21:56 +00:00
|
|
|
|
{
|
2006-10-19 21:00:33 +00:00
|
|
|
|
// FIXME UNICODE
|
2004-10-28 13:07:45 +00:00
|
|
|
|
if (!name.empty() && name != "!-- --")
|
2006-10-21 00:16:43 +00:00
|
|
|
|
os << "</" << from_ascii(name) << '>';
|
2004-10-28 13:07:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-19 21:00:33 +00:00
|
|
|
|
void sgml::openTag(Buffer const & buf, odocstream & 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(">");
|
2005-01-18 14:15:57 +00:00
|
|
|
|
if( pos != string::npos && end != string::npos)
|
2004-10-28 13:07:45 +00:00
|
|
|
|
param.erase(pos, end-pos + 1);
|
|
|
|
|
}
|
|
|
|
|
attribute = id + ' ' + param;
|
|
|
|
|
} else {
|
|
|
|
|
if (param.find('#') != string::npos) {
|
2006-10-20 20:30:00 +00:00
|
|
|
|
// FIXME UNICODE
|
2004-10-28 13:07:45 +00:00
|
|
|
|
if(!style->counter.empty())
|
2006-10-20 22:17:16 +00:00
|
|
|
|
counters.step(style->counter);
|
2004-10-28 13:07:45 +00:00
|
|
|
|
else
|
2006-10-21 00:16:43 +00:00
|
|
|
|
counters.step(from_ascii(name));
|
|
|
|
|
int i = counters.value(from_ascii(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
|
|
|
|
|
|
|
|
|
|
2006-10-19 21:00:33 +00:00
|
|
|
|
void sgml::closeTag(odocstream & os, Paragraph const & par)
|
2004-10-28 13:07:45 +00:00
|
|
|
|
{
|
|
|
|
|
LyXLayout_ptr const & style = par.layout();
|
|
|
|
|
closeTag(os, style->latexname());
|
2002-10-21 16:21:56 +00:00
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|