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"
20 #include "TextClass.h"
22 #include "support/debug.h"
23 #include "support/lstrings.h"
24 #include "support/textutils.h"
34 InsetLayout::InsetLayout() :
35 name_(from_ascii("undefined")), lyxtype_(STANDARD),
36 labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
37 decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(sane_font),
38 labelfont_(sane_font), bgcolor_(Color_error),
39 htmlforcecss_ (false), htmlisblock_(true),
40 multipar_(true), custompars_(true), forceplain_(false),
41 passthru_(false), parbreakisnewline_(false), freespacing_(false),
42 keepempty_(false), forceltr_(false),
43 needprotect_(false), intoc_(false), spellcheck_(true),
44 resetsfont_(true), display_(true), forcelocalfontswitch_(false)
46 labelfont_.setColor(Color_error);
52 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
54 if (support::compare_ascii_no_case(str, "classic") == 0)
55 return InsetLayout::CLASSIC;
56 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
57 return InsetLayout::MINIMALISTIC;
58 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
59 return InsetLayout::CONGLOMERATE;
60 return InsetLayout::DEFAULT;
63 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
65 if (support::compare_ascii_no_case(str, "command") == 0)
66 return InsetLayout::COMMAND;
67 if (support::compare_ascii_no_case(str, "environment") == 0)
68 return InsetLayout::ENVIRONMENT;
69 if (support::compare_ascii_no_case(str, "none") == 0)
70 return InsetLayout::NOLATEXTYPE;
71 return InsetLayout::ILT_ERROR;
77 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
90 IL_FORCE_LOCAL_FONT_SWITCH,
116 IL_PARBREAKISNEWLINE,
127 LexerKeyword elementTags[] = {
128 { "argument", IL_ARGUMENT },
129 { "babelpreamble", IL_BABELPREAMBLE },
130 { "bgcolor", IL_BGCOLOR },
131 { "contentaslabel", IL_CONTENTASLABEL },
132 { "copystyle", IL_COPYSTYLE },
133 { "counter", IL_COUNTER},
134 { "custompars", IL_CUSTOMPARS },
135 { "decoration", IL_DECORATION },
136 { "display", IL_DISPLAY },
139 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
140 { "forceltr", IL_FORCELTR },
141 { "forceplain", IL_FORCEPLAIN },
142 { "freespacing", IL_FREESPACING },
143 { "htmlattr", IL_HTMLATTR },
144 { "htmlforcecss", IL_HTMLFORCECSS },
145 { "htmlinnerattr", IL_HTMLINNERATTR},
146 { "htmlinnertag", IL_HTMLINNERTAG},
147 { "htmlisblock", IL_HTMLISBLOCK},
148 { "htmllabel", IL_HTMLLABEL },
149 { "htmlpreamble", IL_HTMLPREAMBLE },
150 { "htmlstyle", IL_HTMLSTYLE },
151 { "htmltag", IL_HTMLTAG },
152 { "intoc", IL_INTOC },
153 { "keepempty", IL_KEEPEMPTY },
154 { "labelfont", IL_LABELFONT },
155 { "labelstring", IL_LABELSTRING },
156 { "langpreamble", IL_LANGPREAMBLE },
157 { "latexname", IL_LATEXNAME },
158 { "latexparam", IL_LATEXPARAM },
159 { "latextype", IL_LATEXTYPE },
160 { "leftdelim", IL_LEFTDELIM },
161 { "lyxtype", IL_LYXTYPE },
162 { "multipar", IL_MULTIPAR },
163 { "needprotect", IL_NEEDPROTECT },
164 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
165 { "passthru", IL_PASSTHRU },
166 { "preamble", IL_PREAMBLE },
167 { "refprefix", IL_REFPREFIX },
168 { "requires", IL_REQUIRES },
169 { "resetsfont", IL_RESETSFONT },
170 { "rightdelim", IL_RIGHTDELIM },
171 { "spellcheck", IL_SPELLCHECK }
174 lex.pushTable(elementTags);
176 labelfont_ = inherit_font;
177 bgcolor_ = Color_none;
179 // whether we've read the CustomPars or ForcePlain tag
180 // for issuing a warning in case MultiPars comes later
181 bool readCustomOrPlain = false;
184 while (!getout && lex.isOK()) {
187 case Lexer::LEX_UNDEF:
188 lex.printError("Unknown InsetLayout tag");
195 // Perhaps a more elegant way to deal with the next two would be the
196 // way this sort of thing is handled in Layout::read(), namely, by
199 // make sure that we have the right sort of name.
200 if (name_ != from_ascii("undefined")
201 && name_.substr(0,5) != from_ascii("Flex:")) {
202 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
203 "This one has the name `" << to_utf8(name_) << "'\n"
204 "Ignoring LyXType declaration.");
209 lyxtype_ = translateLyXType(lt);
210 if (lyxtype_ == NOLYXTYPE)
211 LYXERR0("Unknown LyXType `" << lt << "'.");
212 if (lyxtype_ == CHARSTYLE)
219 latextype_ = translateLaTeXType(lt);
220 if (latextype_ == ILT_ERROR)
221 LYXERR0("Unknown LaTeXType `" << lt << "'.");
229 decoration_ = translateDecoration(tmp);
236 latexparam_ = support::subst(tmp, """, "\"");
240 leftdelim_ = support::subst(leftdelim_, from_ascii("<br/>"),
243 case IL_FORCE_LOCAL_FONT_SWITCH:
244 lex >> forcelocalfontswitch_;
248 rightdelim_ = support::subst(rightdelim_, from_ascii("<br/>"),
252 labelfont_ = lyxRead(lex, inherit_font);
262 // the defaults for these depend upon multipar_
263 if (readCustomOrPlain)
264 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
265 "Previous value may be overwritten!");
266 readCustomOrPlain = false;
267 custompars_ = multipar_;
268 forceplain_ = !multipar_;
275 readCustomOrPlain = true;
279 readCustomOrPlain = true;
284 case IL_PARBREAKISNEWLINE:
285 lex >> parbreakisnewline_;
296 case IL_CONTENTASLABEL:
297 lex >> contentaslabel_;
300 // initialize with a known style
303 style = support::subst(style, '_', ' ');
305 // We don't want to apply the algorithm in DocumentClass::insetLayout()
306 // here. So we do it the long way.
307 TextClass::InsetLayouts::const_iterator it =
308 tclass.insetLayouts().find(style);
309 if (it != tclass.insetLayouts().end()) {
310 docstring const tmpname = name_;
311 this->operator=(it->second);
314 LYXERR0("Cannot copy unknown InsetLayout `"
316 << "All InsetLayouts so far:");
317 TextClass::InsetLayouts::const_iterator lit =
318 tclass.insetLayouts().begin();
319 TextClass::InsetLayouts::const_iterator len =
320 tclass.insetLayouts().end();
321 for (; lit != len; ++lit)
322 lyxerr << lit->second.name() << "\n";
328 font_ = lyxRead(lex, inherit_font);
329 // If you want to define labelfont, you need to do so after
339 bgcolor_ = lcolor.getFromLyXName(tmp);
342 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
344 case IL_BABELPREAMBLE:
345 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
347 case IL_LANGPREAMBLE:
348 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
359 case IL_HTMLFORCECSS:
360 lex >> htmlforcecss_;
362 case IL_HTMLINNERTAG:
363 lex >> htmlinnertag_;
365 case IL_HTMLINNERATTR:
366 lex >> htmlinnerattr_;
375 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
377 case IL_HTMLPREAMBLE:
378 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
382 vector<string> const req
383 = support::getVectorFromString(lex.getString());
384 requires_.insert(req.begin(), req.end());
402 // Here add element to list if getout == true
406 // The label font is generally used as-is without
407 // any realization against a given context.
408 labelfont_.realize(sane_font);
415 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
418 if (support::compare_ascii_no_case(str, "charstyle") == 0)
419 return InsetLayout::CHARSTYLE;
420 if (support::compare_ascii_no_case(str, "custom") == 0)
421 return InsetLayout::CUSTOM;
422 if (support::compare_ascii_no_case(str, "element") == 0)
423 return InsetLayout::ELEMENT;
424 if (support::compare_ascii_no_case(str, "end") == 0)
425 return InsetLayout::END;
426 if (support::compare_ascii_no_case(str, "standard") == 0)
427 return InsetLayout::STANDARD;
428 return InsetLayout::NOLYXTYPE;
432 string const & InsetLayout::htmltag() const
434 if (htmltag_.empty())
435 htmltag_ = multipar_ ? "div" : "span";
440 string const & InsetLayout::htmlattr() const
442 if (htmlattr_.empty())
443 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
448 string const & InsetLayout::htmlinnerattr() const
450 if (htmlinnerattr_.empty())
451 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
452 return htmlinnerattr_;
456 string InsetLayout::defaultCSSClass() const
458 if (!defaultcssclass_.empty())
459 return defaultcssclass_;
461 string n = to_utf8(name());
462 string::const_iterator it = n.begin();
463 string::const_iterator en = n.end();
464 for (; it != en; ++it) {
465 if (!isAlphaASCII(*it))
467 else if (isLower(*it))
470 d += support::lowercase(*it);
472 // are there other characters we need to remove?
473 defaultcssclass_ = d;
474 return defaultcssclass_;
478 void InsetLayout::makeDefaultCSS() const
480 if (!htmldefaultstyle_.empty())
482 docstring const mainfontCSS = font_.asCSS();
483 if (!mainfontCSS.empty())
485 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
486 mainfontCSS + from_ascii("\n}\n");
490 docstring InsetLayout::htmlstyle() const
492 if (!htmlstyle_.empty() && !htmlforcecss_)
494 if (htmldefaultstyle_.empty())
496 docstring retval = htmldefaultstyle_;
497 if (!htmlstyle_.empty())
498 retval += '\n' + htmlstyle_ + '\n';
502 void InsetLayout::readArgument(Lexer & lex)
504 Layout::latexarg arg;
505 arg.mandatory = false;
507 bool finished = false;
508 arg.font = inherit_font;
509 arg.labelfont = inherit_font;
512 while (!finished && lex.isOK() && !error) {
514 string const tok = support::ascii_lowercase(lex.getString());
518 } else if (tok == "endargument") {
520 } else if (tok == "labelstring") {
522 arg.labelstring = lex.getDocString();
523 } else if (tok == "mandatory") {
525 arg.mandatory = lex.getBool();
526 } else if (tok == "leftdelim") {
528 arg.ldelim = lex.getDocString();
529 arg.ldelim = support::subst(arg.ldelim,
530 from_ascii("<br/>"), from_ascii("\n"));
531 } else if (tok == "rightdelim") {
533 arg.rdelim = lex.getDocString();
534 arg.rdelim = support::subst(arg.rdelim,
535 from_ascii("<br/>"), from_ascii("\n"));
536 } else if (tok == "tooltip") {
538 arg.tooltip = lex.getDocString();
539 } else if (tok == "requires") {
541 arg.requires = lex.getString();
542 } else if (tok == "decoration") {
544 arg.decoration = lex.getString();
545 } else if (tok == "font") {
546 arg.font = lyxRead(lex, arg.font);
547 } else if (tok == "labelfont") {
548 arg.labelfont = lyxRead(lex, arg.labelfont);
550 lex.printError("Unknown tag");
554 if (arg.labelstring.empty())
555 LYXERR0("Incomplete Argument definition!");
557 latexargs_[nr] = arg;
560 unsigned int InsetLayout::optArgs() const
563 Layout::LaTeXArgMap::const_iterator it = latexargs_.begin();
564 for (; it != latexargs_.end(); ++it) {
565 if (!(*it).second.mandatory)
572 unsigned int InsetLayout::requiredArgs() const
575 Layout::LaTeXArgMap::const_iterator it = latexargs_.begin();
576 for (; it != latexargs_.end(); ++it) {
577 if ((*it).second.mandatory)