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()
36 InsetLayout::InsetLayout(docstring const & name) :
37 name_(name), lyxtype_(STANDARD),
38 labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
39 decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(sane_font),
40 labelfont_(sane_font), bgcolor_(Color_error),
41 htmlforcecss_ (false), htmlisblock_(true),
42 multipar_(true), custompars_(true), forceplain_(false),
43 passthru_(false), needprotect_(false), freespacing_(false),
44 keepempty_(false), forceltr_(false), intoc_(false)
46 labelfont_.setColor(Color_error);
52 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
54 if (support::compare_ascii_no_case(str, "classic") == 0)
55 return InsetLayout::CLASSIC;
56 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
57 return InsetLayout::MINIMALISTIC;
58 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
59 return InsetLayout::CONGLOMERATE;
60 return InsetLayout::DEFAULT;
63 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
65 if (support::compare_ascii_no_case(str, "command") == 0)
66 return InsetLayout::COMMAND;
67 if (support::compare_ascii_no_case(str, "environment") == 0)
68 return InsetLayout::ENVIRONMENT;
69 if (support::compare_ascii_no_case(str, "none") == 0)
70 return InsetLayout::NOLATEXTYPE;
71 return InsetLayout::ILT_ERROR;
77 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
116 LexerKeyword elementTags[] = {
117 { "bgcolor", IL_BGCOLOR },
118 { "contentaslabel", IL_CONTENTASLABEL },
119 { "copystyle", IL_COPYSTYLE },
120 { "counter", IL_COUNTER},
121 { "custompars", IL_CUSTOMPARS },
122 { "decoration", IL_DECORATION },
125 { "forceltr", IL_FORCELTR },
126 { "forceplain", IL_FORCEPLAIN },
127 { "freespacing", IL_FREESPACING },
128 { "htmlattr", IL_HTMLATTR },
129 { "htmlforcecss", IL_HTMLFORCECSS },
130 { "htmlinnerattr", IL_HTMLINNERATTR},
131 { "htmlinnertag", IL_HTMLINNERTAG},
132 { "htmlisblock", IL_HTMLISBLOCK},
133 { "htmllabel", IL_HTMLLABEL },
134 { "htmlpreamble", IL_HTMLPREAMBLE },
135 { "htmlstyle", IL_HTMLSTYLE },
136 { "htmltag", IL_HTMLTAG },
137 { "intoc", IL_INTOC },
138 { "keepempty", IL_KEEPEMPTY },
139 { "labelfont", IL_LABELFONT },
140 { "labelstring", IL_LABELSTRING },
141 { "latexname", IL_LATEXNAME },
142 { "latexparam", IL_LATEXPARAM },
143 { "latextype", IL_LATEXTYPE },
144 { "lyxtype", IL_LYXTYPE },
145 { "multipar", IL_MULTIPAR },
146 { "needprotect", IL_NEEDPROTECT },
147 { "passthru", IL_PASSTHRU },
148 { "preamble", IL_PREAMBLE },
149 { "requires", IL_REQUIRES }
152 lex.pushTable(elementTags);
154 FontInfo font = inherit_font;
155 labelfont_ = inherit_font;
156 bgcolor_ = Color_none;
158 // whether we've read the CustomPars or ForcePlain tag
159 // for issuing a warning in case MultiPars comes later
160 bool readCustomOrPlain = false;
163 while (!getout && lex.isOK()) {
166 case Lexer::LEX_UNDEF:
167 lex.printError("Unknown InsetLayout tag");
174 // Perhaps a more elegant way to deal with the next two would be the
175 // way this sort of thing is handled in Layout::read(), namely, by
180 lyxtype_ = translateLyXType(lt);
181 if (lyxtype_ == NOLYXTYPE)
182 LYXERR0("Unknown LyXType `" << lt << "'.");
188 latextype_ = translateLaTeXType(lt);
189 if (latextype_ == ILT_ERROR)
190 LYXERR0("Unknown LaTeXType `" << lt << "'.");
198 decoration_ = translateDecoration(tmp);
205 latexparam_ = support::subst(tmp, """, "\"");
208 labelfont_ = lyxRead(lex, inherit_font);
218 // the defaults for these depend upon multipar_
219 if (readCustomOrPlain)
220 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
221 "Previous value may be overwritten!");
222 readCustomOrPlain = false;
223 custompars_ = multipar_;
224 forceplain_ = !multipar_;
231 readCustomOrPlain = true;
238 readCustomOrPlain = true;
249 case IL_CONTENTASLABEL:
250 lex >> contentaslabel_;
252 case IL_COPYSTYLE: { // initialize with a known style
255 style = support::subst(style, '_', ' ');
257 // We don't want to apply the algorithm in DocumentClass::insetLayout()
258 // here. So we do it the long way.
259 TextClass::InsetLayouts::const_iterator it =
260 tclass.insetLayouts().find(style);
261 if (it != tclass.insetLayouts().end()) {
262 docstring const tmpname = name_;
263 this->operator=(it->second);
266 LYXERR0("Cannot copy unknown InsetLayout `"
268 << "All InsetLayouts so far:");
269 TextClass::InsetLayouts::const_iterator lit =
270 tclass.insetLayouts().begin();
271 TextClass::InsetLayouts::const_iterator len =
272 tclass.insetLayouts().end();
273 for (; lit != len; ++lit)
274 lyxerr << lit->second.name() << "\n";
280 font_ = lyxRead(lex, inherit_font);
281 // If you want to define labelfont, you need to do so after
288 bgcolor_ = lcolor.getFromLyXName(tmp);
291 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
299 case IL_HTMLFORCECSS:
300 lex >> htmlforcecss_;
302 case IL_HTMLINNERTAG:
303 lex >> htmlinnertag_;
305 case IL_HTMLINNERATTR:
306 lex >> htmlinnerattr_;
315 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
317 case IL_HTMLPREAMBLE:
318 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
322 vector<string> const req
323 = support::getVectorFromString(lex.getString());
324 requires_.insert(req.begin(), req.end());
333 // Here add element to list if getout == true
337 // The label font is generally used as-is without
338 // any realization against a given context.
339 labelfont_.realize(sane_font);
346 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
349 if (support::compare_ascii_no_case(str, "charstyle") == 0)
350 return InsetLayout::CHARSTYLE;
351 if (support::compare_ascii_no_case(str, "custom") == 0)
352 return InsetLayout::CUSTOM;
353 if (support::compare_ascii_no_case(str, "element") == 0)
354 return InsetLayout::ELEMENT;
355 if (support::compare_ascii_no_case(str, "end") == 0)
356 return InsetLayout::END;
357 if (support::compare_ascii_no_case(str, "standard") == 0)
358 return InsetLayout::STANDARD;
359 return InsetLayout::NOLYXTYPE;
363 string const & InsetLayout::htmltag() const
365 if (htmltag_.empty())
366 htmltag_ = multipar_ ? "div" : "span";
371 string const & InsetLayout::htmlattr() const
373 if (htmlattr_.empty())
374 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
379 string const & InsetLayout::htmlinnerattr() const
381 if (htmlinnerattr_.empty())
382 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
383 return htmlinnerattr_;
387 string InsetLayout::defaultCSSClass() const
389 if (!defaultcssclass_.empty())
390 return defaultcssclass_;
392 string n = to_utf8(name());
393 string::const_iterator it = n.begin();
394 string::const_iterator en = n.end();
395 for (; it != en; ++it) {
398 else if (islower(*it))
401 d += support::lowercase(*it);
403 // are there other characters we need to remove?
404 defaultcssclass_ = d;
405 return defaultcssclass_;
409 void InsetLayout::makeDefaultCSS() const
411 if (!htmldefaultstyle_.empty())
413 docstring const mainfontCSS = font_.asCSS();
414 if (!mainfontCSS.empty())
416 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
417 mainfontCSS + from_ascii("\n}\n");
421 docstring InsetLayout::htmlstyle() const
423 if (!htmlstyle_.empty() && !htmlforcecss_)
425 if (htmldefaultstyle_.empty())
427 docstring retval = htmldefaultstyle_;
428 if (!htmlstyle_.empty())
429 retval += '\n' + htmlstyle_ + '\n';