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),
42 resetsfont_(true), display_(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)
111 IL_PARBREAKISNEWLINE,
121 LexerKeyword elementTags[] = {
122 { "babelpreamble", IL_BABELPREAMBLE },
123 { "bgcolor", IL_BGCOLOR },
124 { "contentaslabel", IL_CONTENTASLABEL },
125 { "copystyle", IL_COPYSTYLE },
126 { "counter", IL_COUNTER},
127 { "custompars", IL_CUSTOMPARS },
128 { "decoration", IL_DECORATION },
129 { "display", IL_DISPLAY },
132 { "forceltr", IL_FORCELTR },
133 { "forceplain", IL_FORCEPLAIN },
134 { "freespacing", IL_FREESPACING },
135 { "htmlattr", IL_HTMLATTR },
136 { "htmlforcecss", IL_HTMLFORCECSS },
137 { "htmlinnerattr", IL_HTMLINNERATTR},
138 { "htmlinnertag", IL_HTMLINNERTAG},
139 { "htmlisblock", IL_HTMLISBLOCK},
140 { "htmllabel", IL_HTMLLABEL },
141 { "htmlpreamble", IL_HTMLPREAMBLE },
142 { "htmlstyle", IL_HTMLSTYLE },
143 { "htmltag", IL_HTMLTAG },
144 { "intoc", IL_INTOC },
145 { "keepempty", IL_KEEPEMPTY },
146 { "labelfont", IL_LABELFONT },
147 { "labelstring", IL_LABELSTRING },
148 { "langpreamble", IL_LANGPREAMBLE },
149 { "latexname", IL_LATEXNAME },
150 { "latexparam", IL_LATEXPARAM },
151 { "latextype", IL_LATEXTYPE },
152 { "lyxtype", IL_LYXTYPE },
153 { "multipar", IL_MULTIPAR },
154 { "needprotect", IL_NEEDPROTECT },
155 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
156 { "passthru", IL_PASSTHRU },
157 { "preamble", IL_PREAMBLE },
158 { "refprefix", IL_REFPREFIX },
159 { "requires", IL_REQUIRES },
160 { "resetsfont", IL_RESETSFONT },
161 { "spellcheck", IL_SPELLCHECK }
164 lex.pushTable(elementTags);
166 labelfont_ = inherit_font;
167 bgcolor_ = Color_none;
169 // whether we've read the CustomPars or ForcePlain tag
170 // for issuing a warning in case MultiPars comes later
171 bool readCustomOrPlain = false;
174 while (!getout && lex.isOK()) {
177 case Lexer::LEX_UNDEF:
178 lex.printError("Unknown InsetLayout tag");
185 // Perhaps a more elegant way to deal with the next two would be the
186 // way this sort of thing is handled in Layout::read(), namely, by
189 // make sure that we have the right sort of name.
190 if (name_ != from_ascii("undefined")
191 && name_.substr(0,5) != from_ascii("Flex:")) {
192 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
193 "This one has the name `" << to_utf8(name_) << "'\n"
194 "Ignoring LyXType declaration.");
199 lyxtype_ = translateLyXType(lt);
200 if (lyxtype_ == NOLYXTYPE)
201 LYXERR0("Unknown LyXType `" << lt << "'.");
202 if (lyxtype_ == CHARSTYLE)
209 latextype_ = translateLaTeXType(lt);
210 if (latextype_ == ILT_ERROR)
211 LYXERR0("Unknown LaTeXType `" << lt << "'.");
219 decoration_ = translateDecoration(tmp);
226 latexparam_ = support::subst(tmp, """, "\"");
229 labelfont_ = lyxRead(lex, inherit_font);
239 // the defaults for these depend upon multipar_
240 if (readCustomOrPlain)
241 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
242 "Previous value may be overwritten!");
243 readCustomOrPlain = false;
244 custompars_ = multipar_;
245 forceplain_ = !multipar_;
252 readCustomOrPlain = true;
256 readCustomOrPlain = true;
261 case IL_PARBREAKISNEWLINE:
262 lex >> parbreakisnewline_;
273 case IL_CONTENTASLABEL:
274 lex >> contentaslabel_;
277 // initialize with a known style
280 style = support::subst(style, '_', ' ');
282 // We don't want to apply the algorithm in DocumentClass::insetLayout()
283 // here. So we do it the long way.
284 TextClass::InsetLayouts::const_iterator it =
285 tclass.insetLayouts().find(style);
286 if (it != tclass.insetLayouts().end()) {
287 docstring const tmpname = name_;
288 this->operator=(it->second);
291 LYXERR0("Cannot copy unknown InsetLayout `"
293 << "All InsetLayouts so far:");
294 TextClass::InsetLayouts::const_iterator lit =
295 tclass.insetLayouts().begin();
296 TextClass::InsetLayouts::const_iterator len =
297 tclass.insetLayouts().end();
298 for (; lit != len; ++lit)
299 lyxerr << lit->second.name() << "\n";
305 font_ = lyxRead(lex, inherit_font);
306 // If you want to define labelfont, you need to do so after
313 bgcolor_ = lcolor.getFromLyXName(tmp);
316 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
318 case IL_BABELPREAMBLE:
319 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
321 case IL_LANGPREAMBLE:
322 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
333 case IL_HTMLFORCECSS:
334 lex >> htmlforcecss_;
336 case IL_HTMLINNERTAG:
337 lex >> htmlinnertag_;
339 case IL_HTMLINNERATTR:
340 lex >> htmlinnerattr_;
349 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
351 case IL_HTMLPREAMBLE:
352 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
356 vector<string> const req
357 = support::getVectorFromString(lex.getString());
358 requires_.insert(req.begin(), req.end());
376 // Here add element to list if getout == true
380 // The label font is generally used as-is without
381 // any realization against a given context.
382 labelfont_.realize(sane_font);
389 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
392 if (support::compare_ascii_no_case(str, "charstyle") == 0)
393 return InsetLayout::CHARSTYLE;
394 if (support::compare_ascii_no_case(str, "custom") == 0)
395 return InsetLayout::CUSTOM;
396 if (support::compare_ascii_no_case(str, "element") == 0)
397 return InsetLayout::ELEMENT;
398 if (support::compare_ascii_no_case(str, "end") == 0)
399 return InsetLayout::END;
400 if (support::compare_ascii_no_case(str, "standard") == 0)
401 return InsetLayout::STANDARD;
402 return InsetLayout::NOLYXTYPE;
406 string const & InsetLayout::htmltag() const
408 if (htmltag_.empty())
409 htmltag_ = multipar_ ? "div" : "span";
414 string const & InsetLayout::htmlattr() const
416 if (htmlattr_.empty())
417 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
422 string const & InsetLayout::htmlinnerattr() const
424 if (htmlinnerattr_.empty())
425 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
426 return htmlinnerattr_;
430 string InsetLayout::defaultCSSClass() const
432 if (!defaultcssclass_.empty())
433 return defaultcssclass_;
435 string n = to_utf8(name());
436 string::const_iterator it = n.begin();
437 string::const_iterator en = n.end();
438 for (; it != en; ++it) {
441 else if (islower(*it))
444 d += support::lowercase(*it);
446 // are there other characters we need to remove?
447 defaultcssclass_ = d;
448 return defaultcssclass_;
452 void InsetLayout::makeDefaultCSS() const
454 if (!htmldefaultstyle_.empty())
456 docstring const mainfontCSS = font_.asCSS();
457 if (!mainfontCSS.empty())
459 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
460 mainfontCSS + from_ascii("\n}\n");
464 docstring InsetLayout::htmlstyle() const
466 if (!htmlstyle_.empty() && !htmlforcecss_)
468 if (htmldefaultstyle_.empty())
470 docstring retval = htmldefaultstyle_;
471 if (!htmlstyle_.empty())
472 retval += '\n' + htmlstyle_ + '\n';