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), parbreakisnewline_(false), freespacing_(false),
40 keepempty_(false), forceltr_(false),
41 needprotect_(false), intoc_(false), spellcheck_(true),
44 labelfont_.setColor(Color_error);
50 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
52 if (support::compare_ascii_no_case(str, "classic") == 0)
53 return InsetLayout::CLASSIC;
54 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
55 return InsetLayout::MINIMALISTIC;
56 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
57 return InsetLayout::CONGLOMERATE;
58 return InsetLayout::DEFAULT;
61 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
63 if (support::compare_ascii_no_case(str, "command") == 0)
64 return InsetLayout::COMMAND;
65 if (support::compare_ascii_no_case(str, "environment") == 0)
66 return InsetLayout::ENVIRONMENT;
67 if (support::compare_ascii_no_case(str, "none") == 0)
68 return InsetLayout::NOLATEXTYPE;
69 return InsetLayout::ILT_ERROR;
75 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
108 IL_PARBREAKISNEWLINE,
118 LexerKeyword elementTags[] = {
119 { "bgcolor", IL_BGCOLOR },
120 { "contentaslabel", IL_CONTENTASLABEL },
121 { "copystyle", IL_COPYSTYLE },
122 { "counter", IL_COUNTER},
123 { "custompars", IL_CUSTOMPARS },
124 { "decoration", IL_DECORATION },
127 { "forceltr", IL_FORCELTR },
128 { "forceplain", IL_FORCEPLAIN },
129 { "freespacing", IL_FREESPACING },
130 { "htmlattr", IL_HTMLATTR },
131 { "htmlforcecss", IL_HTMLFORCECSS },
132 { "htmlinnerattr", IL_HTMLINNERATTR},
133 { "htmlinnertag", IL_HTMLINNERTAG},
134 { "htmlisblock", IL_HTMLISBLOCK},
135 { "htmllabel", IL_HTMLLABEL },
136 { "htmlpreamble", IL_HTMLPREAMBLE },
137 { "htmlstyle", IL_HTMLSTYLE },
138 { "htmltag", IL_HTMLTAG },
139 { "intoc", IL_INTOC },
140 { "keepempty", IL_KEEPEMPTY },
141 { "labelfont", IL_LABELFONT },
142 { "labelstring", IL_LABELSTRING },
143 { "latexname", IL_LATEXNAME },
144 { "latexparam", IL_LATEXPARAM },
145 { "latextype", IL_LATEXTYPE },
146 { "lyxtype", IL_LYXTYPE },
147 { "multipar", IL_MULTIPAR },
148 { "needprotect", IL_NEEDPROTECT },
149 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
150 { "passthru", IL_PASSTHRU },
151 { "preamble", IL_PREAMBLE },
152 { "refprefix", IL_REFPREFIX },
153 { "requires", IL_REQUIRES },
154 { "resetsfont", IL_RESETSFONT },
155 { "spellcheck", IL_SPELLCHECK }
158 lex.pushTable(elementTags);
160 FontInfo font = inherit_font;
161 labelfont_ = inherit_font;
162 bgcolor_ = Color_none;
164 // whether we've read the CustomPars or ForcePlain tag
165 // for issuing a warning in case MultiPars comes later
166 bool readCustomOrPlain = false;
169 while (!getout && lex.isOK()) {
172 case Lexer::LEX_UNDEF:
173 lex.printError("Unknown InsetLayout tag");
180 // Perhaps a more elegant way to deal with the next two would be the
181 // way this sort of thing is handled in Layout::read(), namely, by
186 lyxtype_ = translateLyXType(lt);
187 if (lyxtype_ == NOLYXTYPE)
188 LYXERR0("Unknown LyXType `" << lt << "'.");
189 if (lyxtype_ == CHARSTYLE)
196 latextype_ = translateLaTeXType(lt);
197 if (latextype_ == ILT_ERROR)
198 LYXERR0("Unknown LaTeXType `" << lt << "'.");
206 decoration_ = translateDecoration(tmp);
213 latexparam_ = support::subst(tmp, """, "\"");
216 labelfont_ = lyxRead(lex, inherit_font);
226 // the defaults for these depend upon multipar_
227 if (readCustomOrPlain)
228 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
229 "Previous value may be overwritten!");
230 readCustomOrPlain = false;
231 custompars_ = multipar_;
232 forceplain_ = !multipar_;
239 readCustomOrPlain = true;
243 readCustomOrPlain = true;
248 case IL_PARBREAKISNEWLINE:
249 lex >> parbreakisnewline_;
260 case IL_CONTENTASLABEL:
261 lex >> contentaslabel_;
264 // initialize with a known style
267 style = support::subst(style, '_', ' ');
269 // We don't want to apply the algorithm in DocumentClass::insetLayout()
270 // here. So we do it the long way.
271 TextClass::InsetLayouts::const_iterator it =
272 tclass.insetLayouts().find(style);
273 if (it != tclass.insetLayouts().end()) {
274 docstring const tmpname = name_;
275 this->operator=(it->second);
278 LYXERR0("Cannot copy unknown InsetLayout `"
280 << "All InsetLayouts so far:");
281 TextClass::InsetLayouts::const_iterator lit =
282 tclass.insetLayouts().begin();
283 TextClass::InsetLayouts::const_iterator len =
284 tclass.insetLayouts().end();
285 for (; lit != len; ++lit)
286 lyxerr << lit->second.name() << "\n";
292 font_ = lyxRead(lex, inherit_font);
293 // If you want to define labelfont, you need to do so after
300 bgcolor_ = lcolor.getFromLyXName(tmp);
303 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
314 case IL_HTMLFORCECSS:
315 lex >> htmlforcecss_;
317 case IL_HTMLINNERTAG:
318 lex >> htmlinnertag_;
320 case IL_HTMLINNERATTR:
321 lex >> htmlinnerattr_;
330 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
332 case IL_HTMLPREAMBLE:
333 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
337 vector<string> const req
338 = support::getVectorFromString(lex.getString());
339 requires_.insert(req.begin(), req.end());
354 // Here add element to list if getout == true
358 // The label font is generally used as-is without
359 // any realization against a given context.
360 labelfont_.realize(sane_font);
367 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
370 if (support::compare_ascii_no_case(str, "charstyle") == 0)
371 return InsetLayout::CHARSTYLE;
372 if (support::compare_ascii_no_case(str, "custom") == 0)
373 return InsetLayout::CUSTOM;
374 if (support::compare_ascii_no_case(str, "element") == 0)
375 return InsetLayout::ELEMENT;
376 if (support::compare_ascii_no_case(str, "end") == 0)
377 return InsetLayout::END;
378 if (support::compare_ascii_no_case(str, "standard") == 0)
379 return InsetLayout::STANDARD;
380 return InsetLayout::NOLYXTYPE;
384 string const & InsetLayout::htmltag() const
386 if (htmltag_.empty())
387 htmltag_ = multipar_ ? "div" : "span";
392 string const & InsetLayout::htmlattr() const
394 if (htmlattr_.empty())
395 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
400 string const & InsetLayout::htmlinnerattr() const
402 if (htmlinnerattr_.empty())
403 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
404 return htmlinnerattr_;
408 string InsetLayout::defaultCSSClass() const
410 if (!defaultcssclass_.empty())
411 return defaultcssclass_;
413 string n = to_utf8(name());
414 string::const_iterator it = n.begin();
415 string::const_iterator en = n.end();
416 for (; it != en; ++it) {
419 else if (islower(*it))
422 d += support::lowercase(*it);
424 // are there other characters we need to remove?
425 defaultcssclass_ = d;
426 return defaultcssclass_;
430 void InsetLayout::makeDefaultCSS() const
432 if (!htmldefaultstyle_.empty())
434 docstring const mainfontCSS = font_.asCSS();
435 if (!mainfontCSS.empty())
437 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
438 mainfontCSS + from_ascii("\n}\n");
442 docstring InsetLayout::htmlstyle() const
444 if (!htmlstyle_.empty() && !htmlforcecss_)
446 if (htmldefaultstyle_.empty())
448 docstring retval = htmldefaultstyle_;
449 if (!htmlstyle_.empty())
450 retval += '\n' + htmlstyle_ + '\n';