3 * \file InsetLayout.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Martin Vermeer
10 * Full author contact details are available in file CREDITS.
15 #include "InsetLayout.h"
19 #include "TextClass.h"
21 #include "support/debug.h"
22 #include "support/lstrings.h"
32 InsetLayout::InsetLayout() :
33 name_(from_ascii("undefined")), lyxtype_(STANDARD),
34 labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
35 decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(sane_font),
36 labelfont_(sane_font), bgcolor_(Color_error),
37 htmlforcecss_ (false), htmlisblock_(true),
38 multipar_(true), custompars_(true), forceplain_(false),
39 passthru_(false), needprotect_(false), freespacing_(false),
40 keepempty_(false), forceltr_(false), intoc_(false)
42 labelfont_.setColor(Color_error);
48 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
50 if (support::compare_ascii_no_case(str, "classic") == 0)
51 return InsetLayout::CLASSIC;
52 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
53 return InsetLayout::MINIMALISTIC;
54 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
55 return InsetLayout::CONGLOMERATE;
56 return InsetLayout::DEFAULT;
59 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
61 if (support::compare_ascii_no_case(str, "command") == 0)
62 return InsetLayout::COMMAND;
63 if (support::compare_ascii_no_case(str, "environment") == 0)
64 return InsetLayout::ENVIRONMENT;
65 if (support::compare_ascii_no_case(str, "none") == 0)
66 return InsetLayout::NOLATEXTYPE;
67 return InsetLayout::ILT_ERROR;
73 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
112 LexerKeyword elementTags[] = {
113 { "bgcolor", IL_BGCOLOR },
114 { "contentaslabel", IL_CONTENTASLABEL },
115 { "copystyle", IL_COPYSTYLE },
116 { "counter", IL_COUNTER},
117 { "custompars", IL_CUSTOMPARS },
118 { "decoration", IL_DECORATION },
121 { "forceltr", IL_FORCELTR },
122 { "forceplain", IL_FORCEPLAIN },
123 { "freespacing", IL_FREESPACING },
124 { "htmlattr", IL_HTMLATTR },
125 { "htmlforcecss", IL_HTMLFORCECSS },
126 { "htmlinnerattr", IL_HTMLINNERATTR},
127 { "htmlinnertag", IL_HTMLINNERTAG},
128 { "htmlisblock", IL_HTMLISBLOCK},
129 { "htmllabel", IL_HTMLLABEL },
130 { "htmlpreamble", IL_HTMLPREAMBLE },
131 { "htmlstyle", IL_HTMLSTYLE },
132 { "htmltag", IL_HTMLTAG },
133 { "intoc", IL_INTOC },
134 { "keepempty", IL_KEEPEMPTY },
135 { "labelfont", IL_LABELFONT },
136 { "labelstring", IL_LABELSTRING },
137 { "latexname", IL_LATEXNAME },
138 { "latexparam", IL_LATEXPARAM },
139 { "latextype", IL_LATEXTYPE },
140 { "lyxtype", IL_LYXTYPE },
141 { "multipar", IL_MULTIPAR },
142 { "needprotect", IL_NEEDPROTECT },
143 { "passthru", IL_PASSTHRU },
144 { "preamble", IL_PREAMBLE },
145 { "requires", IL_REQUIRES }
148 lex.pushTable(elementTags);
150 FontInfo font = inherit_font;
151 labelfont_ = inherit_font;
152 bgcolor_ = Color_none;
154 // whether we've read the CustomPars or ForcePlain tag
155 // for issuing a warning in case MultiPars comes later
156 bool readCustomOrPlain = false;
159 while (!getout && lex.isOK()) {
162 case Lexer::LEX_UNDEF:
163 lex.printError("Unknown InsetLayout tag");
170 // Perhaps a more elegant way to deal with the next two would be the
171 // way this sort of thing is handled in Layout::read(), namely, by
176 lyxtype_ = translateLyXType(lt);
177 if (lyxtype_ == NOLYXTYPE)
178 LYXERR0("Unknown LyXType `" << lt << "'.");
179 if (lyxtype_ == CHARSTYLE)
186 latextype_ = translateLaTeXType(lt);
187 if (latextype_ == ILT_ERROR)
188 LYXERR0("Unknown LaTeXType `" << lt << "'.");
196 decoration_ = translateDecoration(tmp);
203 latexparam_ = support::subst(tmp, """, "\"");
206 labelfont_ = lyxRead(lex, inherit_font);
216 // the defaults for these depend upon multipar_
217 if (readCustomOrPlain)
218 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
219 "Previous value may be overwritten!");
220 readCustomOrPlain = false;
221 custompars_ = multipar_;
222 forceplain_ = !multipar_;
229 readCustomOrPlain = true;
236 readCustomOrPlain = true;
247 case IL_CONTENTASLABEL:
248 lex >> contentaslabel_;
250 case IL_COPYSTYLE: { // initialize with a known style
253 style = support::subst(style, '_', ' ');
255 // We don't want to apply the algorithm in DocumentClass::insetLayout()
256 // here. So we do it the long way.
257 TextClass::InsetLayouts::const_iterator it =
258 tclass.insetLayouts().find(style);
259 if (it != tclass.insetLayouts().end()) {
260 docstring const tmpname = name_;
261 this->operator=(it->second);
264 LYXERR0("Cannot copy unknown InsetLayout `"
266 << "All InsetLayouts so far:");
267 TextClass::InsetLayouts::const_iterator lit =
268 tclass.insetLayouts().begin();
269 TextClass::InsetLayouts::const_iterator len =
270 tclass.insetLayouts().end();
271 for (; lit != len; ++lit)
272 lyxerr << lit->second.name() << "\n";
278 font_ = lyxRead(lex, inherit_font);
279 // If you want to define labelfont, you need to do so after
286 bgcolor_ = lcolor.getFromLyXName(tmp);
289 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
297 case IL_HTMLFORCECSS:
298 lex >> htmlforcecss_;
300 case IL_HTMLINNERTAG:
301 lex >> htmlinnertag_;
303 case IL_HTMLINNERATTR:
304 lex >> htmlinnerattr_;
313 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
315 case IL_HTMLPREAMBLE:
316 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
320 vector<string> const req
321 = support::getVectorFromString(lex.getString());
322 requires_.insert(req.begin(), req.end());
331 // Here add element to list if getout == true
335 // The label font is generally used as-is without
336 // any realization against a given context.
337 labelfont_.realize(sane_font);
344 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
347 if (support::compare_ascii_no_case(str, "charstyle") == 0)
348 return InsetLayout::CHARSTYLE;
349 if (support::compare_ascii_no_case(str, "custom") == 0)
350 return InsetLayout::CUSTOM;
351 if (support::compare_ascii_no_case(str, "element") == 0)
352 return InsetLayout::ELEMENT;
353 if (support::compare_ascii_no_case(str, "end") == 0)
354 return InsetLayout::END;
355 if (support::compare_ascii_no_case(str, "standard") == 0)
356 return InsetLayout::STANDARD;
357 return InsetLayout::NOLYXTYPE;
361 string const & InsetLayout::htmltag() const
363 if (htmltag_.empty())
364 htmltag_ = multipar_ ? "div" : "span";
369 string const & InsetLayout::htmlattr() const
371 if (htmlattr_.empty())
372 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
377 string const & InsetLayout::htmlinnerattr() const
379 if (htmlinnerattr_.empty())
380 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
381 return htmlinnerattr_;
385 string InsetLayout::defaultCSSClass() const
387 if (!defaultcssclass_.empty())
388 return defaultcssclass_;
390 string n = to_utf8(name());
391 string::const_iterator it = n.begin();
392 string::const_iterator en = n.end();
393 for (; it != en; ++it) {
396 else if (islower(*it))
399 d += support::lowercase(*it);
401 // are there other characters we need to remove?
402 defaultcssclass_ = d;
403 return defaultcssclass_;
407 void InsetLayout::makeDefaultCSS() const
409 if (!htmldefaultstyle_.empty())
411 docstring const mainfontCSS = font_.asCSS();
412 if (!mainfontCSS.empty())
414 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
415 mainfontCSS + from_ascii("\n}\n");
419 docstring InsetLayout::htmlstyle() const
421 if (!htmlstyle_.empty() && !htmlforcecss_)
423 if (htmldefaultstyle_.empty())
425 docstring retval = htmldefaultstyle_;
426 if (!htmlstyle_.empty())
427 retval += '\n' + htmlstyle_ + '\n';