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"
23 #include "support/textutils.h"
33 InsetLayout::InsetLayout() :
34 name_(from_ascii("undefined")), lyxtype_(STANDARD),
35 labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
36 decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(sane_font),
37 labelfont_(sane_font), bgcolor_(Color_error),
38 htmlforcecss_ (false), htmlisblock_(true),
39 multipar_(true), custompars_(true), forceplain_(false),
40 passthru_(false), parbreakisnewline_(false), freespacing_(false),
41 keepempty_(false), forceltr_(false),
42 needprotect_(false), intoc_(false), spellcheck_(true),
43 resetsfont_(true), display_(true)
45 labelfont_.setColor(Color_error);
51 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
53 if (support::compare_ascii_no_case(str, "classic") == 0)
54 return InsetLayout::CLASSIC;
55 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
56 return InsetLayout::MINIMALISTIC;
57 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
58 return InsetLayout::CONGLOMERATE;
59 return InsetLayout::DEFAULT;
62 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
64 if (support::compare_ascii_no_case(str, "command") == 0)
65 return InsetLayout::COMMAND;
66 if (support::compare_ascii_no_case(str, "environment") == 0)
67 return InsetLayout::ENVIRONMENT;
68 if (support::compare_ascii_no_case(str, "none") == 0)
69 return InsetLayout::NOLATEXTYPE;
70 return InsetLayout::ILT_ERROR;
76 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
112 IL_PARBREAKISNEWLINE,
122 LexerKeyword elementTags[] = {
123 { "babelpreamble", IL_BABELPREAMBLE },
124 { "bgcolor", IL_BGCOLOR },
125 { "contentaslabel", IL_CONTENTASLABEL },
126 { "copystyle", IL_COPYSTYLE },
127 { "counter", IL_COUNTER},
128 { "custompars", IL_CUSTOMPARS },
129 { "decoration", IL_DECORATION },
130 { "display", IL_DISPLAY },
133 { "forceltr", IL_FORCELTR },
134 { "forceplain", IL_FORCEPLAIN },
135 { "freespacing", IL_FREESPACING },
136 { "htmlattr", IL_HTMLATTR },
137 { "htmlforcecss", IL_HTMLFORCECSS },
138 { "htmlinnerattr", IL_HTMLINNERATTR},
139 { "htmlinnertag", IL_HTMLINNERTAG},
140 { "htmlisblock", IL_HTMLISBLOCK},
141 { "htmllabel", IL_HTMLLABEL },
142 { "htmlpreamble", IL_HTMLPREAMBLE },
143 { "htmlstyle", IL_HTMLSTYLE },
144 { "htmltag", IL_HTMLTAG },
145 { "intoc", IL_INTOC },
146 { "keepempty", IL_KEEPEMPTY },
147 { "labelfont", IL_LABELFONT },
148 { "labelstring", IL_LABELSTRING },
149 { "langpreamble", IL_LANGPREAMBLE },
150 { "latexname", IL_LATEXNAME },
151 { "latexparam", IL_LATEXPARAM },
152 { "latextype", IL_LATEXTYPE },
153 { "lyxtype", IL_LYXTYPE },
154 { "multipar", IL_MULTIPAR },
155 { "needprotect", IL_NEEDPROTECT },
156 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
157 { "passthru", IL_PASSTHRU },
158 { "preamble", IL_PREAMBLE },
159 { "refprefix", IL_REFPREFIX },
160 { "requires", IL_REQUIRES },
161 { "resetsfont", IL_RESETSFONT },
162 { "spellcheck", IL_SPELLCHECK }
165 lex.pushTable(elementTags);
167 labelfont_ = inherit_font;
168 bgcolor_ = Color_none;
170 // whether we've read the CustomPars or ForcePlain tag
171 // for issuing a warning in case MultiPars comes later
172 bool readCustomOrPlain = false;
175 while (!getout && lex.isOK()) {
178 case Lexer::LEX_UNDEF:
179 lex.printError("Unknown InsetLayout tag");
186 // Perhaps a more elegant way to deal with the next two would be the
187 // way this sort of thing is handled in Layout::read(), namely, by
190 // make sure that we have the right sort of name.
191 if (name_ != from_ascii("undefined")
192 && name_.substr(0,5) != from_ascii("Flex:")) {
193 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
194 "This one has the name `" << to_utf8(name_) << "'\n"
195 "Ignoring LyXType declaration.");
200 lyxtype_ = translateLyXType(lt);
201 if (lyxtype_ == NOLYXTYPE)
202 LYXERR0("Unknown LyXType `" << lt << "'.");
203 if (lyxtype_ == CHARSTYLE)
210 latextype_ = translateLaTeXType(lt);
211 if (latextype_ == ILT_ERROR)
212 LYXERR0("Unknown LaTeXType `" << lt << "'.");
220 decoration_ = translateDecoration(tmp);
227 latexparam_ = support::subst(tmp, """, "\"");
230 labelfont_ = lyxRead(lex, inherit_font);
240 // the defaults for these depend upon multipar_
241 if (readCustomOrPlain)
242 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
243 "Previous value may be overwritten!");
244 readCustomOrPlain = false;
245 custompars_ = multipar_;
246 forceplain_ = !multipar_;
253 readCustomOrPlain = true;
257 readCustomOrPlain = true;
262 case IL_PARBREAKISNEWLINE:
263 lex >> parbreakisnewline_;
274 case IL_CONTENTASLABEL:
275 lex >> contentaslabel_;
278 // initialize with a known style
281 style = support::subst(style, '_', ' ');
283 // We don't want to apply the algorithm in DocumentClass::insetLayout()
284 // here. So we do it the long way.
285 TextClass::InsetLayouts::const_iterator it =
286 tclass.insetLayouts().find(style);
287 if (it != tclass.insetLayouts().end()) {
288 docstring const tmpname = name_;
289 this->operator=(it->second);
292 LYXERR0("Cannot copy unknown InsetLayout `"
294 << "All InsetLayouts so far:");
295 TextClass::InsetLayouts::const_iterator lit =
296 tclass.insetLayouts().begin();
297 TextClass::InsetLayouts::const_iterator len =
298 tclass.insetLayouts().end();
299 for (; lit != len; ++lit)
300 lyxerr << lit->second.name() << "\n";
306 font_ = lyxRead(lex, inherit_font);
307 // If you want to define labelfont, you need to do so after
314 bgcolor_ = lcolor.getFromLyXName(tmp);
317 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
319 case IL_BABELPREAMBLE:
320 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
322 case IL_LANGPREAMBLE:
323 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
334 case IL_HTMLFORCECSS:
335 lex >> htmlforcecss_;
337 case IL_HTMLINNERTAG:
338 lex >> htmlinnertag_;
340 case IL_HTMLINNERATTR:
341 lex >> htmlinnerattr_;
350 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
352 case IL_HTMLPREAMBLE:
353 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
357 vector<string> const req
358 = support::getVectorFromString(lex.getString());
359 requires_.insert(req.begin(), req.end());
377 // Here add element to list if getout == true
381 // The label font is generally used as-is without
382 // any realization against a given context.
383 labelfont_.realize(sane_font);
390 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
393 if (support::compare_ascii_no_case(str, "charstyle") == 0)
394 return InsetLayout::CHARSTYLE;
395 if (support::compare_ascii_no_case(str, "custom") == 0)
396 return InsetLayout::CUSTOM;
397 if (support::compare_ascii_no_case(str, "element") == 0)
398 return InsetLayout::ELEMENT;
399 if (support::compare_ascii_no_case(str, "end") == 0)
400 return InsetLayout::END;
401 if (support::compare_ascii_no_case(str, "standard") == 0)
402 return InsetLayout::STANDARD;
403 return InsetLayout::NOLYXTYPE;
407 string const & InsetLayout::htmltag() const
409 if (htmltag_.empty())
410 htmltag_ = multipar_ ? "div" : "span";
415 string const & InsetLayout::htmlattr() const
417 if (htmlattr_.empty())
418 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
423 string const & InsetLayout::htmlinnerattr() const
425 if (htmlinnerattr_.empty())
426 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
427 return htmlinnerattr_;
431 string InsetLayout::defaultCSSClass() const
433 if (!defaultcssclass_.empty())
434 return defaultcssclass_;
436 string n = to_utf8(name());
437 string::const_iterator it = n.begin();
438 string::const_iterator en = n.end();
439 for (; it != en; ++it) {
440 if (!isAlphaASCII(*it))
442 else if (isLower(*it))
445 d += support::lowercase(*it);
447 // are there other characters we need to remove?
448 defaultcssclass_ = d;
449 return defaultcssclass_;
453 void InsetLayout::makeDefaultCSS() const
455 if (!htmldefaultstyle_.empty())
457 docstring const mainfontCSS = font_.asCSS();
458 if (!mainfontCSS.empty())
460 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
461 mainfontCSS + from_ascii("\n}\n");
465 docstring InsetLayout::htmlstyle() const
467 if (!htmlstyle_.empty() && !htmlforcecss_)
469 if (htmldefaultstyle_.empty())
471 docstring retval = htmldefaultstyle_;
472 if (!htmlstyle_.empty())
473 retval += '\n' + htmlstyle_ + '\n';