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 << "'.");
184 latextype_ = translateLaTeXType(lt);
185 if (latextype_ == ILT_ERROR)
186 LYXERR0("Unknown LaTeXType `" << lt << "'.");
194 decoration_ = translateDecoration(tmp);
201 latexparam_ = support::subst(tmp, """, "\"");
204 labelfont_ = lyxRead(lex, inherit_font);
214 // the defaults for these depend upon multipar_
215 if (readCustomOrPlain)
216 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
217 "Previous value may be overwritten!");
218 readCustomOrPlain = false;
219 custompars_ = multipar_;
220 forceplain_ = !multipar_;
227 readCustomOrPlain = true;
234 readCustomOrPlain = true;
245 case IL_CONTENTASLABEL:
246 lex >> contentaslabel_;
248 case IL_COPYSTYLE: { // initialize with a known style
251 style = support::subst(style, '_', ' ');
253 // We don't want to apply the algorithm in DocumentClass::insetLayout()
254 // here. So we do it the long way.
255 TextClass::InsetLayouts::const_iterator it =
256 tclass.insetLayouts().find(style);
257 if (it != tclass.insetLayouts().end()) {
258 docstring const tmpname = name_;
259 this->operator=(it->second);
262 LYXERR0("Cannot copy unknown InsetLayout `"
264 << "All InsetLayouts so far:");
265 TextClass::InsetLayouts::const_iterator lit =
266 tclass.insetLayouts().begin();
267 TextClass::InsetLayouts::const_iterator len =
268 tclass.insetLayouts().end();
269 for (; lit != len; ++lit)
270 lyxerr << lit->second.name() << "\n";
276 font_ = lyxRead(lex, inherit_font);
277 // If you want to define labelfont, you need to do so after
284 bgcolor_ = lcolor.getFromLyXName(tmp);
287 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
295 case IL_HTMLFORCECSS:
296 lex >> htmlforcecss_;
298 case IL_HTMLINNERTAG:
299 lex >> htmlinnertag_;
301 case IL_HTMLINNERATTR:
302 lex >> htmlinnerattr_;
311 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
313 case IL_HTMLPREAMBLE:
314 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
318 vector<string> const req
319 = support::getVectorFromString(lex.getString());
320 requires_.insert(req.begin(), req.end());
329 // Here add element to list if getout == true
333 // The label font is generally used as-is without
334 // any realization against a given context.
335 labelfont_.realize(sane_font);
342 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
345 if (support::compare_ascii_no_case(str, "charstyle") == 0)
346 return InsetLayout::CHARSTYLE;
347 if (support::compare_ascii_no_case(str, "custom") == 0)
348 return InsetLayout::CUSTOM;
349 if (support::compare_ascii_no_case(str, "element") == 0)
350 return InsetLayout::ELEMENT;
351 if (support::compare_ascii_no_case(str, "end") == 0)
352 return InsetLayout::END;
353 if (support::compare_ascii_no_case(str, "standard") == 0)
354 return InsetLayout::STANDARD;
355 return InsetLayout::NOLYXTYPE;
359 string const & InsetLayout::htmltag() const
361 if (htmltag_.empty())
362 htmltag_ = multipar_ ? "div" : "span";
367 string const & InsetLayout::htmlattr() const
369 if (htmlattr_.empty())
370 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
375 string const & InsetLayout::htmlinnerattr() const
377 if (htmlinnerattr_.empty())
378 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
379 return htmlinnerattr_;
383 string InsetLayout::defaultCSSClass() const
385 if (!defaultcssclass_.empty())
386 return defaultcssclass_;
388 string n = to_utf8(name());
389 string::const_iterator it = n.begin();
390 string::const_iterator en = n.end();
391 for (; it != en; ++it) {
394 else if (islower(*it))
397 d += support::lowercase(*it);
399 // are there other characters we need to remove?
400 defaultcssclass_ = d;
401 return defaultcssclass_;
405 void InsetLayout::makeDefaultCSS() const
407 if (!htmldefaultstyle_.empty())
409 docstring const mainfontCSS = font_.asCSS();
410 if (!mainfontCSS.empty())
412 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
413 mainfontCSS + from_ascii("\n}\n");
417 docstring InsetLayout::htmlstyle() const
419 if (!htmlstyle_.empty() && !htmlforcecss_)
421 if (htmldefaultstyle_.empty())
423 docstring retval = htmldefaultstyle_;
424 if (!htmlstyle_.empty())
425 retval += '\n' + htmlstyle_ + '\n';