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);
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;
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,
128 LexerKeyword elementTags[] = {
129 { "argument", IL_ARGUMENT },
130 { "babelpreamble", IL_BABELPREAMBLE },
131 { "bgcolor", IL_BGCOLOR },
132 { "contentaslabel", IL_CONTENTASLABEL },
133 { "copystyle", IL_COPYSTYLE },
134 { "counter", IL_COUNTER},
135 { "custompars", IL_CUSTOMPARS },
136 { "decoration", IL_DECORATION },
137 { "display", IL_DISPLAY },
140 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
141 { "forceltr", IL_FORCELTR },
142 { "forceplain", IL_FORCEPLAIN },
143 { "freespacing", IL_FREESPACING },
144 { "htmlattr", IL_HTMLATTR },
145 { "htmlforcecss", IL_HTMLFORCECSS },
146 { "htmlinnerattr", IL_HTMLINNERATTR},
147 { "htmlinnertag", IL_HTMLINNERTAG},
148 { "htmlisblock", IL_HTMLISBLOCK},
149 { "htmllabel", IL_HTMLLABEL },
150 { "htmlpreamble", IL_HTMLPREAMBLE },
151 { "htmlstyle", IL_HTMLSTYLE },
152 { "htmltag", IL_HTMLTAG },
153 { "intoc", IL_INTOC },
154 { "keepempty", IL_KEEPEMPTY },
155 { "labelfont", IL_LABELFONT },
156 { "labelstring", IL_LABELSTRING },
157 { "langpreamble", IL_LANGPREAMBLE },
158 { "latexname", IL_LATEXNAME },
159 { "latexparam", IL_LATEXPARAM },
160 { "latextype", IL_LATEXTYPE },
161 { "leftdelim", IL_LEFTDELIM },
162 { "lyxtype", IL_LYXTYPE },
163 { "multipar", IL_MULTIPAR },
164 { "needprotect", IL_NEEDPROTECT },
165 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
166 { "passthru", IL_PASSTHRU },
167 { "preamble", IL_PREAMBLE },
168 { "refprefix", IL_REFPREFIX },
169 { "requires", IL_REQUIRES },
170 { "resetargs", IL_RESETARGS },
171 { "resetsfont", IL_RESETSFONT },
172 { "rightdelim", IL_RIGHTDELIM },
173 { "spellcheck", IL_SPELLCHECK }
176 lex.pushTable(elementTags);
178 labelfont_ = inherit_font;
179 bgcolor_ = Color_none;
181 // whether we've read the CustomPars or ForcePlain tag
182 // for issuing a warning in case MultiPars comes later
183 bool readCustomOrPlain = false;
186 while (!getout && lex.isOK()) {
189 case Lexer::LEX_UNDEF:
190 lex.printError("Unknown InsetLayout tag");
197 // Perhaps a more elegant way to deal with the next two would be the
198 // way this sort of thing is handled in Layout::read(), namely, by
201 // make sure that we have the right sort of name.
202 if (name_ != from_ascii("undefined")
203 && name_.substr(0,5) != from_ascii("Flex:")) {
204 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
205 "This one has the name `" << to_utf8(name_) << "'\n"
206 "Ignoring LyXType declaration.");
211 lyxtype_ = translateLyXType(lt);
212 if (lyxtype_ == NOLYXTYPE)
213 LYXERR0("Unknown LyXType `" << lt << "'.");
214 if (lyxtype_ == CHARSTYLE)
221 latextype_ = translateLaTeXType(lt);
222 if (latextype_ == ILT_ERROR)
223 LYXERR0("Unknown LaTeXType `" << lt << "'.");
231 decoration_ = translateDecoration(tmp);
238 latexparam_ = support::subst(tmp, """, "\"");
242 leftdelim_ = support::subst(leftdelim_, from_ascii("<br/>"),
245 case IL_FORCE_LOCAL_FONT_SWITCH:
246 lex >> forcelocalfontswitch_;
250 rightdelim_ = support::subst(rightdelim_, from_ascii("<br/>"),
254 labelfont_ = lyxRead(lex, inherit_font);
264 // the defaults for these depend upon multipar_
265 if (readCustomOrPlain)
266 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
267 "Previous value may be overwritten!");
268 readCustomOrPlain = false;
269 custompars_ = multipar_;
270 forceplain_ = !multipar_;
277 readCustomOrPlain = true;
281 readCustomOrPlain = true;
286 case IL_PARBREAKISNEWLINE:
287 lex >> parbreakisnewline_;
298 case IL_CONTENTASLABEL:
299 lex >> contentaslabel_;
302 // initialize with a known style
305 style = support::subst(style, '_', ' ');
307 // We don't want to apply the algorithm in DocumentClass::insetLayout()
308 // here. So we do it the long way.
309 TextClass::InsetLayouts::const_iterator it =
310 tclass.insetLayouts().find(style);
311 if (it != tclass.insetLayouts().end()) {
312 docstring const tmpname = name_;
313 this->operator=(it->second);
316 LYXERR0("Cannot copy unknown InsetLayout `"
318 << "All InsetLayouts so far:");
319 TextClass::InsetLayouts::const_iterator lit =
320 tclass.insetLayouts().begin();
321 TextClass::InsetLayouts::const_iterator len =
322 tclass.insetLayouts().end();
323 for (; lit != len; ++lit)
324 lyxerr << lit->second.name() << "\n";
330 font_ = lyxRead(lex, inherit_font);
331 // If you want to define labelfont, you need to do so after
347 bgcolor_ = lcolor.getFromLyXName(tmp);
350 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
352 case IL_BABELPREAMBLE:
353 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
355 case IL_LANGPREAMBLE:
356 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
367 case IL_HTMLFORCECSS:
368 lex >> htmlforcecss_;
370 case IL_HTMLINNERTAG:
371 lex >> htmlinnertag_;
373 case IL_HTMLINNERATTR:
374 lex >> htmlinnerattr_;
383 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
385 case IL_HTMLPREAMBLE:
386 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
390 vector<string> const req
391 = support::getVectorFromString(lex.getString());
392 requires_.insert(req.begin(), req.end());
410 // Here add element to list if getout == true
414 // The label font is generally used as-is without
415 // any realization against a given context.
416 labelfont_.realize(sane_font);
423 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
426 if (support::compare_ascii_no_case(str, "charstyle") == 0)
427 return InsetLayout::CHARSTYLE;
428 if (support::compare_ascii_no_case(str, "custom") == 0)
429 return InsetLayout::CUSTOM;
430 if (support::compare_ascii_no_case(str, "element") == 0)
431 return InsetLayout::ELEMENT;
432 if (support::compare_ascii_no_case(str, "end") == 0)
433 return InsetLayout::END;
434 if (support::compare_ascii_no_case(str, "standard") == 0)
435 return InsetLayout::STANDARD;
436 return InsetLayout::NOLYXTYPE;
440 string const & InsetLayout::htmltag() const
442 if (htmltag_.empty())
443 htmltag_ = multipar_ ? "div" : "span";
448 string const & InsetLayout::htmlattr() const
450 if (htmlattr_.empty())
451 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
456 string const & InsetLayout::htmlinnerattr() const
458 if (htmlinnerattr_.empty())
459 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
460 return htmlinnerattr_;
464 string InsetLayout::defaultCSSClass() const
466 if (!defaultcssclass_.empty())
467 return defaultcssclass_;
469 string n = to_utf8(name());
470 string::const_iterator it = n.begin();
471 string::const_iterator en = n.end();
472 for (; it != en; ++it) {
473 if (!isAlphaASCII(*it))
475 else if (isLower(*it))
478 d += support::lowercase(*it);
480 // are there other characters we need to remove?
481 defaultcssclass_ = d;
482 return defaultcssclass_;
486 void InsetLayout::makeDefaultCSS() const
488 if (!htmldefaultstyle_.empty())
490 docstring const mainfontCSS = font_.asCSS();
491 if (!mainfontCSS.empty())
493 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
494 mainfontCSS + from_ascii("\n}\n");
498 docstring InsetLayout::htmlstyle() const
500 if (!htmlstyle_.empty() && !htmlforcecss_)
502 if (htmldefaultstyle_.empty())
504 docstring retval = htmldefaultstyle_;
505 if (!htmlstyle_.empty())
506 retval += '\n' + htmlstyle_ + '\n';
510 void InsetLayout::readArgument(Lexer & lex)
512 Layout::latexarg arg;
513 arg.mandatory = false;
514 arg.autoinsert = false;
516 bool finished = false;
517 arg.font = inherit_font;
518 arg.labelfont = inherit_font;
521 while (!finished && lex.isOK() && !error) {
523 string const tok = support::ascii_lowercase(lex.getString());
527 } else if (tok == "endargument") {
529 } else if (tok == "labelstring") {
531 arg.labelstring = lex.getDocString();
532 } else if (tok == "menustring") {
534 arg.menustring = lex.getDocString();
535 } else if (tok == "mandatory") {
537 arg.mandatory = lex.getBool();
538 } else if (tok == "autoinsert") {
540 arg.autoinsert = lex.getBool();
541 } else if (tok == "leftdelim") {
543 arg.ldelim = lex.getDocString();
544 arg.ldelim = support::subst(arg.ldelim,
545 from_ascii("<br/>"), from_ascii("\n"));
546 } else if (tok == "rightdelim") {
548 arg.rdelim = lex.getDocString();
549 arg.rdelim = support::subst(arg.rdelim,
550 from_ascii("<br/>"), from_ascii("\n"));
551 } else if (tok == "presetarg") {
553 arg.presetarg = lex.getDocString();
554 } else if (tok == "tooltip") {
556 arg.tooltip = lex.getDocString();
557 } else if (tok == "requires") {
559 arg.requires = lex.getString();
560 } else if (tok == "decoration") {
562 arg.decoration = lex.getString();
563 } else if (tok == "font") {
564 arg.font = lyxRead(lex, arg.font);
565 } else if (tok == "labelfont") {
566 arg.labelfont = lyxRead(lex, arg.labelfont);
568 lex.printError("Unknown tag");
572 if (arg.labelstring.empty())
573 LYXERR0("Incomplete Argument definition!");
575 latexargs_[nr] = arg;
578 unsigned int InsetLayout::optArgs() const
581 Layout::LaTeXArgMap::const_iterator it = latexargs_.begin();
582 for (; it != latexargs_.end(); ++it) {
583 if (!(*it).second.mandatory)
590 unsigned int InsetLayout::requiredArgs() const
593 Layout::LaTeXArgMap::const_iterator it = latexargs_.begin();
594 for (; it != latexargs_.end(); ++it) {
595 if ((*it).second.mandatory)