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), spellcheck_(true)
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)
114 LexerKeyword elementTags[] = {
115 { "bgcolor", IL_BGCOLOR },
116 { "contentaslabel", IL_CONTENTASLABEL },
117 { "copystyle", IL_COPYSTYLE },
118 { "counter", IL_COUNTER},
119 { "custompars", IL_CUSTOMPARS },
120 { "decoration", IL_DECORATION },
123 { "forceltr", IL_FORCELTR },
124 { "forceplain", IL_FORCEPLAIN },
125 { "freespacing", IL_FREESPACING },
126 { "htmlattr", IL_HTMLATTR },
127 { "htmlforcecss", IL_HTMLFORCECSS },
128 { "htmlinnerattr", IL_HTMLINNERATTR},
129 { "htmlinnertag", IL_HTMLINNERTAG},
130 { "htmlisblock", IL_HTMLISBLOCK},
131 { "htmllabel", IL_HTMLLABEL },
132 { "htmlpreamble", IL_HTMLPREAMBLE },
133 { "htmlstyle", IL_HTMLSTYLE },
134 { "htmltag", IL_HTMLTAG },
135 { "intoc", IL_INTOC },
136 { "keepempty", IL_KEEPEMPTY },
137 { "labelfont", IL_LABELFONT },
138 { "labelstring", IL_LABELSTRING },
139 { "latexname", IL_LATEXNAME },
140 { "latexparam", IL_LATEXPARAM },
141 { "latextype", IL_LATEXTYPE },
142 { "lyxtype", IL_LYXTYPE },
143 { "multipar", IL_MULTIPAR },
144 { "needprotect", IL_NEEDPROTECT },
145 { "passthru", IL_PASSTHRU },
146 { "preamble", IL_PREAMBLE },
147 { "refprefix", IL_REFPREFIX },
148 { "requires", IL_REQUIRES },
149 { "spellcheck", IL_SPELLCHECK }
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 << "'.");
183 if (lyxtype_ == CHARSTYLE)
190 latextype_ = translateLaTeXType(lt);
191 if (latextype_ == ILT_ERROR)
192 LYXERR0("Unknown LaTeXType `" << lt << "'.");
200 decoration_ = translateDecoration(tmp);
207 latexparam_ = support::subst(tmp, """, "\"");
210 labelfont_ = lyxRead(lex, inherit_font);
220 // the defaults for these depend upon multipar_
221 if (readCustomOrPlain)
222 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
223 "Previous value may be overwritten!");
224 readCustomOrPlain = false;
225 custompars_ = multipar_;
226 forceplain_ = !multipar_;
233 readCustomOrPlain = true;
240 readCustomOrPlain = true;
251 case IL_CONTENTASLABEL:
252 lex >> contentaslabel_;
254 case IL_COPYSTYLE: { // initialize with a known style
257 style = support::subst(style, '_', ' ');
259 // We don't want to apply the algorithm in DocumentClass::insetLayout()
260 // here. So we do it the long way.
261 TextClass::InsetLayouts::const_iterator it =
262 tclass.insetLayouts().find(style);
263 if (it != tclass.insetLayouts().end()) {
264 docstring const tmpname = name_;
265 this->operator=(it->second);
268 LYXERR0("Cannot copy unknown InsetLayout `"
270 << "All InsetLayouts so far:");
271 TextClass::InsetLayouts::const_iterator lit =
272 tclass.insetLayouts().begin();
273 TextClass::InsetLayouts::const_iterator len =
274 tclass.insetLayouts().end();
275 for (; lit != len; ++lit)
276 lyxerr << lit->second.name() << "\n";
282 font_ = lyxRead(lex, inherit_font);
283 // If you want to define labelfont, you need to do so after
290 bgcolor_ = lcolor.getFromLyXName(tmp);
293 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
304 case IL_HTMLFORCECSS:
305 lex >> htmlforcecss_;
307 case IL_HTMLINNERTAG:
308 lex >> htmlinnertag_;
310 case IL_HTMLINNERATTR:
311 lex >> htmlinnerattr_;
320 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
322 case IL_HTMLPREAMBLE:
323 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
327 vector<string> const req
328 = support::getVectorFromString(lex.getString());
329 requires_.insert(req.begin(), req.end());
341 // Here add element to list if getout == true
345 // The label font is generally used as-is without
346 // any realization against a given context.
347 labelfont_.realize(sane_font);
354 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
357 if (support::compare_ascii_no_case(str, "charstyle") == 0)
358 return InsetLayout::CHARSTYLE;
359 if (support::compare_ascii_no_case(str, "custom") == 0)
360 return InsetLayout::CUSTOM;
361 if (support::compare_ascii_no_case(str, "element") == 0)
362 return InsetLayout::ELEMENT;
363 if (support::compare_ascii_no_case(str, "end") == 0)
364 return InsetLayout::END;
365 if (support::compare_ascii_no_case(str, "standard") == 0)
366 return InsetLayout::STANDARD;
367 return InsetLayout::NOLYXTYPE;
371 string const & InsetLayout::htmltag() const
373 if (htmltag_.empty())
374 htmltag_ = multipar_ ? "div" : "span";
379 string const & InsetLayout::htmlattr() const
381 if (htmlattr_.empty())
382 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
387 string const & InsetLayout::htmlinnerattr() const
389 if (htmlinnerattr_.empty())
390 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
391 return htmlinnerattr_;
395 string InsetLayout::defaultCSSClass() const
397 if (!defaultcssclass_.empty())
398 return defaultcssclass_;
400 string n = to_utf8(name());
401 string::const_iterator it = n.begin();
402 string::const_iterator en = n.end();
403 for (; it != en; ++it) {
406 else if (islower(*it))
409 d += support::lowercase(*it);
411 // are there other characters we need to remove?
412 defaultcssclass_ = d;
413 return defaultcssclass_;
417 void InsetLayout::makeDefaultCSS() const
419 if (!htmldefaultstyle_.empty())
421 docstring const mainfontCSS = font_.asCSS();
422 if (!mainfontCSS.empty())
424 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
425 mainfontCSS + from_ascii("\n}\n");
429 docstring InsetLayout::htmlstyle() const
431 if (!htmlstyle_.empty() && !htmlforcecss_)
433 if (htmldefaultstyle_.empty())
435 docstring retval = htmldefaultstyle_;
436 if (!htmlstyle_.empty())
437 retval += '\n' + htmlstyle_ + '\n';