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)
43 labelfont_.setColor(Color_error);
49 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
51 if (support::compare_ascii_no_case(str, "classic") == 0)
52 return InsetLayout::CLASSIC;
53 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
54 return InsetLayout::MINIMALISTIC;
55 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
56 return InsetLayout::CONGLOMERATE;
57 return InsetLayout::DEFAULT;
60 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
62 if (support::compare_ascii_no_case(str, "command") == 0)
63 return InsetLayout::COMMAND;
64 if (support::compare_ascii_no_case(str, "environment") == 0)
65 return InsetLayout::ENVIRONMENT;
66 if (support::compare_ascii_no_case(str, "none") == 0)
67 return InsetLayout::NOLATEXTYPE;
68 return InsetLayout::ILT_ERROR;
74 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
107 IL_PARBREAKISNEWLINE,
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 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
148 { "passthru", IL_PASSTHRU },
149 { "preamble", IL_PREAMBLE },
150 { "refprefix", IL_REFPREFIX },
151 { "requires", IL_REQUIRES },
152 { "spellcheck", IL_SPELLCHECK }
155 lex.pushTable(elementTags);
157 FontInfo font = inherit_font;
158 labelfont_ = inherit_font;
159 bgcolor_ = Color_none;
161 // whether we've read the CustomPars or ForcePlain tag
162 // for issuing a warning in case MultiPars comes later
163 bool readCustomOrPlain = false;
166 while (!getout && lex.isOK()) {
169 case Lexer::LEX_UNDEF:
170 lex.printError("Unknown InsetLayout tag");
177 // Perhaps a more elegant way to deal with the next two would be the
178 // way this sort of thing is handled in Layout::read(), namely, by
183 lyxtype_ = translateLyXType(lt);
184 if (lyxtype_ == NOLYXTYPE)
185 LYXERR0("Unknown LyXType `" << lt << "'.");
186 if (lyxtype_ == CHARSTYLE)
193 latextype_ = translateLaTeXType(lt);
194 if (latextype_ == ILT_ERROR)
195 LYXERR0("Unknown LaTeXType `" << lt << "'.");
203 decoration_ = translateDecoration(tmp);
210 latexparam_ = support::subst(tmp, """, "\"");
213 labelfont_ = lyxRead(lex, inherit_font);
223 // the defaults for these depend upon multipar_
224 if (readCustomOrPlain)
225 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
226 "Previous value may be overwritten!");
227 readCustomOrPlain = false;
228 custompars_ = multipar_;
229 forceplain_ = !multipar_;
236 readCustomOrPlain = true;
240 readCustomOrPlain = true;
245 case IL_PARBREAKISNEWLINE:
246 lex >> parbreakisnewline_;
257 case IL_CONTENTASLABEL:
258 lex >> contentaslabel_;
260 case IL_COPYSTYLE: { // initialize with a known style
263 style = support::subst(style, '_', ' ');
265 // We don't want to apply the algorithm in DocumentClass::insetLayout()
266 // here. So we do it the long way.
267 TextClass::InsetLayouts::const_iterator it =
268 tclass.insetLayouts().find(style);
269 if (it != tclass.insetLayouts().end()) {
270 docstring const tmpname = name_;
271 this->operator=(it->second);
274 LYXERR0("Cannot copy unknown InsetLayout `"
276 << "All InsetLayouts so far:");
277 TextClass::InsetLayouts::const_iterator lit =
278 tclass.insetLayouts().begin();
279 TextClass::InsetLayouts::const_iterator len =
280 tclass.insetLayouts().end();
281 for (; lit != len; ++lit)
282 lyxerr << lit->second.name() << "\n";
288 font_ = lyxRead(lex, inherit_font);
289 // If you want to define labelfont, you need to do so after
296 bgcolor_ = lcolor.getFromLyXName(tmp);
299 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
310 case IL_HTMLFORCECSS:
311 lex >> htmlforcecss_;
313 case IL_HTMLINNERTAG:
314 lex >> htmlinnertag_;
316 case IL_HTMLINNERATTR:
317 lex >> htmlinnerattr_;
326 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
328 case IL_HTMLPREAMBLE:
329 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
333 vector<string> const req
334 = support::getVectorFromString(lex.getString());
335 requires_.insert(req.begin(), req.end());
347 // Here add element to list if getout == true
351 // The label font is generally used as-is without
352 // any realization against a given context.
353 labelfont_.realize(sane_font);
360 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
363 if (support::compare_ascii_no_case(str, "charstyle") == 0)
364 return InsetLayout::CHARSTYLE;
365 if (support::compare_ascii_no_case(str, "custom") == 0)
366 return InsetLayout::CUSTOM;
367 if (support::compare_ascii_no_case(str, "element") == 0)
368 return InsetLayout::ELEMENT;
369 if (support::compare_ascii_no_case(str, "end") == 0)
370 return InsetLayout::END;
371 if (support::compare_ascii_no_case(str, "standard") == 0)
372 return InsetLayout::STANDARD;
373 return InsetLayout::NOLYXTYPE;
377 string const & InsetLayout::htmltag() const
379 if (htmltag_.empty())
380 htmltag_ = multipar_ ? "div" : "span";
385 string const & InsetLayout::htmlattr() const
387 if (htmlattr_.empty())
388 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
393 string const & InsetLayout::htmlinnerattr() const
395 if (htmlinnerattr_.empty())
396 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
397 return htmlinnerattr_;
401 string InsetLayout::defaultCSSClass() const
403 if (!defaultcssclass_.empty())
404 return defaultcssclass_;
406 string n = to_utf8(name());
407 string::const_iterator it = n.begin();
408 string::const_iterator en = n.end();
409 for (; it != en; ++it) {
412 else if (islower(*it))
415 d += support::lowercase(*it);
417 // are there other characters we need to remove?
418 defaultcssclass_ = d;
419 return defaultcssclass_;
423 void InsetLayout::makeDefaultCSS() const
425 if (!htmldefaultstyle_.empty())
427 docstring const mainfontCSS = font_.asCSS();
428 if (!mainfontCSS.empty())
430 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
431 mainfontCSS + from_ascii("\n}\n");
435 docstring InsetLayout::htmlstyle() const
437 if (!htmlstyle_.empty() && !htmlforcecss_)
439 if (htmldefaultstyle_.empty())
441 docstring retval = htmldefaultstyle_;
442 if (!htmlstyle_.empty())
443 retval += '\n' + htmlstyle_ + '\n';