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_(inherit_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
341 postcommandargs_.clear();
349 bgcolor_ = lcolor.getFromLyXName(tmp);
352 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
354 case IL_BABELPREAMBLE:
355 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
357 case IL_LANGPREAMBLE:
358 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
369 case IL_HTMLFORCECSS:
370 lex >> htmlforcecss_;
372 case IL_HTMLINNERTAG:
373 lex >> htmlinnertag_;
375 case IL_HTMLINNERATTR:
376 lex >> htmlinnerattr_;
385 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
387 case IL_HTMLPREAMBLE:
388 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
392 vector<string> const req
393 = support::getVectorFromString(lex.getString());
394 requires_.insert(req.begin(), req.end());
412 // Here add element to list if getout == true
416 // The label font is generally used as-is without
417 // any realization against a given context.
418 labelfont_.realize(sane_font);
425 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
428 if (support::compare_ascii_no_case(str, "charstyle") == 0)
429 return InsetLayout::CHARSTYLE;
430 if (support::compare_ascii_no_case(str, "custom") == 0)
431 return InsetLayout::CUSTOM;
432 if (support::compare_ascii_no_case(str, "element") == 0)
433 return InsetLayout::ELEMENT;
434 if (support::compare_ascii_no_case(str, "end") == 0)
435 return InsetLayout::END;
436 if (support::compare_ascii_no_case(str, "standard") == 0)
437 return InsetLayout::STANDARD;
438 return InsetLayout::NOLYXTYPE;
442 string const & InsetLayout::htmltag() const
444 if (htmltag_.empty())
445 htmltag_ = multipar_ ? "div" : "span";
450 string const & InsetLayout::htmlattr() const
452 if (htmlattr_.empty())
453 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
458 string const & InsetLayout::htmlinnerattr() const
460 if (htmlinnerattr_.empty())
461 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
462 return htmlinnerattr_;
466 string InsetLayout::defaultCSSClass() const
468 if (!defaultcssclass_.empty())
469 return defaultcssclass_;
471 string n = to_utf8(name());
472 string::const_iterator it = n.begin();
473 string::const_iterator en = n.end();
474 for (; it != en; ++it) {
475 if (!isAlphaASCII(*it))
477 else if (isLower(*it))
480 d += support::lowercase(*it);
482 // are there other characters we need to remove?
483 defaultcssclass_ = d;
484 return defaultcssclass_;
488 void InsetLayout::makeDefaultCSS() const
490 if (!htmldefaultstyle_.empty())
492 docstring const mainfontCSS = font_.asCSS();
493 if (!mainfontCSS.empty())
495 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
496 mainfontCSS + from_ascii("\n}\n");
500 docstring InsetLayout::htmlstyle() const
502 if (!htmlstyle_.empty() && !htmlforcecss_)
504 if (htmldefaultstyle_.empty())
506 docstring retval = htmldefaultstyle_;
507 if (!htmlstyle_.empty())
508 retval += '\n' + htmlstyle_ + '\n';
512 void InsetLayout::readArgument(Lexer & lex)
514 Layout::latexarg arg;
515 arg.mandatory = false;
516 arg.autoinsert = false;
518 bool finished = false;
519 arg.font = inherit_font;
520 arg.labelfont = inherit_font;
523 bool const postcmd = support::prefixIs(nr, "post:");
524 while (!finished && lex.isOK() && !error) {
526 string const tok = support::ascii_lowercase(lex.getString());
530 } else if (tok == "endargument") {
532 } else if (tok == "labelstring") {
534 arg.labelstring = lex.getDocString();
535 } else if (tok == "menustring") {
537 arg.menustring = lex.getDocString();
538 } else if (tok == "mandatory") {
540 arg.mandatory = lex.getBool();
541 } else if (tok == "autoinsert") {
543 arg.autoinsert = lex.getBool();
544 } else if (tok == "leftdelim") {
546 arg.ldelim = lex.getDocString();
547 arg.ldelim = support::subst(arg.ldelim,
548 from_ascii("<br/>"), from_ascii("\n"));
549 } else if (tok == "rightdelim") {
551 arg.rdelim = lex.getDocString();
552 arg.rdelim = support::subst(arg.rdelim,
553 from_ascii("<br/>"), from_ascii("\n"));
554 } else if (tok == "defaultarg") {
556 arg.defaultarg = lex.getDocString();
557 } else if (tok == "presetarg") {
559 arg.presetarg = lex.getDocString();
560 } else if (tok == "tooltip") {
562 arg.tooltip = lex.getDocString();
563 } else if (tok == "requires") {
565 arg.requires = lex.getString();
566 } else if (tok == "decoration") {
568 arg.decoration = lex.getString();
569 } else if (tok == "font") {
570 arg.font = lyxRead(lex, arg.font);
571 } else if (tok == "labelfont") {
572 arg.labelfont = lyxRead(lex, arg.labelfont);
574 lex.printError("Unknown tag");
578 if (arg.labelstring.empty())
579 LYXERR0("Incomplete Argument definition!");
581 postcommandargs_[nr] = arg;
583 latexargs_[nr] = arg;
587 Layout::LaTeXArgMap InsetLayout::args() const
589 Layout::LaTeXArgMap args = latexargs_;
590 if (!postcommandargs_.empty())
591 args.insert(postcommandargs_.begin(), postcommandargs_.end());
596 unsigned int InsetLayout::optArgs() const
599 Layout::LaTeXArgMap::const_iterator it = args().begin();
600 for (; it != args().end(); ++it) {
601 if (!(*it).second.mandatory)
608 unsigned int InsetLayout::requiredArgs() const
611 Layout::LaTeXArgMap::const_iterator it = args().begin();
612 for (; it != args().end(); ++it) {
613 if ((*it).second.mandatory)