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"
32 using namespace lyx::support;
36 InsetLayout::InsetLayout() :
37 name_(from_ascii("undefined")), lyxtype_(STANDARD),
38 labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
39 decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(inherit_font),
40 labelfont_(sane_font), bgcolor_(Color_error),
41 fixedwidthpreambleencoding_(false), htmlforcecss_ (false),
42 htmlisblock_(true), multipar_(true), custompars_(true),
43 forceplain_(false), passthru_(false), parbreakisnewline_(false),
44 freespacing_(false), keepempty_(false), forceltr_(false),
45 forceownlines_(false), needprotect_(false), needcprotect_(false),
46 intoc_(false), spellcheck_(true), resetsfont_(false), display_(true),
47 forcelocalfontswitch_(false), add_to_toc_(false), is_toc_caption_(false),
50 labelfont_.setColor(Color_error);
54 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
56 if (compare_ascii_no_case(str, "classic") == 0)
57 return InsetLayout::CLASSIC;
58 if (compare_ascii_no_case(str, "minimalistic") == 0)
59 return InsetLayout::MINIMALISTIC;
60 if (compare_ascii_no_case(str, "conglomerate") == 0)
61 return InsetLayout::CONGLOMERATE;
62 return InsetLayout::DEFAULT;
67 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
69 if (compare_ascii_no_case(str, "command") == 0)
70 return InsetLayout::COMMAND;
71 if (compare_ascii_no_case(str, "environment") == 0)
72 return InsetLayout::ENVIRONMENT;
73 if (compare_ascii_no_case(str, "none") == 0)
74 return InsetLayout::NOLATEXTYPE;
75 return InsetLayout::ILT_ERROR;
81 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
95 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
97 IL_FORCE_LOCAL_FONT_SWITCH,
128 IL_PARBREAKISNEWLINE,
140 LexerKeyword elementTags[] = {
141 { "addtotoc", IL_ADDTOTOC },
142 { "argument", IL_ARGUMENT },
143 { "babelpreamble", IL_BABELPREAMBLE },
144 { "bgcolor", IL_BGCOLOR },
145 { "contentaslabel", IL_CONTENTASLABEL },
146 { "copystyle", IL_COPYSTYLE },
147 { "counter", IL_COUNTER},
148 { "custompars", IL_CUSTOMPARS },
149 { "decoration", IL_DECORATION },
150 { "display", IL_DISPLAY },
151 { "editexternal", IL_EDITEXTERNAL },
153 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
155 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
156 { "forceltr", IL_FORCELTR },
157 { "forceownlines", IL_FORCEOWNLINES },
158 { "forceplain", IL_FORCEPLAIN },
159 { "freespacing", IL_FREESPACING },
160 { "htmlattr", IL_HTMLATTR },
161 { "htmlforcecss", IL_HTMLFORCECSS },
162 { "htmlinnerattr", IL_HTMLINNERATTR},
163 { "htmlinnertag", IL_HTMLINNERTAG},
164 { "htmlisblock", IL_HTMLISBLOCK},
165 { "htmllabel", IL_HTMLLABEL },
166 { "htmlpreamble", IL_HTMLPREAMBLE },
167 { "htmlstyle", IL_HTMLSTYLE },
168 { "htmltag", IL_HTMLTAG },
169 { "intoc", IL_INTOC },
170 { "istoccaption", IL_ISTOCCAPTION },
171 { "keepempty", IL_KEEPEMPTY },
172 { "labelfont", IL_LABELFONT },
173 { "labelstring", IL_LABELSTRING },
174 { "langpreamble", IL_LANGPREAMBLE },
175 { "latexname", IL_LATEXNAME },
176 { "latexparam", IL_LATEXPARAM },
177 { "latextype", IL_LATEXTYPE },
178 { "leftdelim", IL_LEFTDELIM },
179 { "lyxtype", IL_LYXTYPE },
180 { "multipar", IL_MULTIPAR },
181 { "needcprotect", IL_NEEDCPROTECT },
182 { "needprotect", IL_NEEDPROTECT },
183 { "obsoletedby", IL_OBSOLETEDBY },
184 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
185 { "passthru", IL_PASSTHRU },
186 { "passthruchars", IL_PASSTHRU_CHARS },
187 { "preamble", IL_PREAMBLE },
188 { "refprefix", IL_REFPREFIX },
189 { "requires", IL_REQUIRES },
190 { "resetargs", IL_RESETARGS },
191 { "resetsfont", IL_RESETSFONT },
192 { "rightdelim", IL_RIGHTDELIM },
193 { "spellcheck", IL_SPELLCHECK }
196 lex.pushTable(elementTags);
198 labelfont_ = inherit_font;
199 bgcolor_ = Color_none;
201 // whether we've read the CustomPars or ForcePlain tag
202 // for issuing a warning in case MultiPars comes later
203 bool readCustomOrPlain = false;
206 while (!getout && lex.isOK()) {
209 case Lexer::LEX_UNDEF:
210 lex.printError("Unknown InsetLayout tag");
217 // Perhaps a more elegant way to deal with the next two would be the
218 // way this sort of thing is handled in Layout::read(), namely, by
221 // make sure that we have the right sort of name.
222 if (name_ != from_ascii("undefined")
223 && name_.substr(0,5) != from_ascii("Flex:")) {
224 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
225 "This one has the name `" << to_utf8(name_) << "'\n"
226 "Ignoring LyXType declaration.");
231 lyxtype_ = translateLyXType(lt);
232 if (lyxtype_ == NOLYXTYPE)
233 LYXERR0("Unknown LyXType `" << lt << "'.");
234 if (lyxtype_ == CHARSTYLE) {
235 // by default, charstyles force the plain layout
244 latextype_ = translateLaTeXType(lt);
245 if (latextype_ == ILT_ERROR)
246 LYXERR0("Unknown LaTeXType `" << lt << "'.");
254 decoration_ = translateDecoration(tmp);
261 latexparam_ = subst(tmp, """, "\"");
265 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
268 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
269 lex >> fixedwidthpreambleencoding_;
271 case IL_FORCE_LOCAL_FONT_SWITCH:
272 lex >> forcelocalfontswitch_;
276 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
280 labelfont_ = lyxRead(lex, inherit_font);
285 case IL_FORCEOWNLINES:
286 lex >> forceownlines_;
293 // the defaults for these depend upon multipar_
294 if (readCustomOrPlain)
295 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
296 "Previous value may be overwritten!");
297 readCustomOrPlain = false;
298 custompars_ = multipar_;
299 forceplain_ = !multipar_;
306 readCustomOrPlain = true;
310 readCustomOrPlain = true;
315 case IL_PASSTHRU_CHARS:
316 lex >> passthru_chars_;
318 case IL_PARBREAKISNEWLINE:
319 lex >> parbreakisnewline_;
330 case IL_NEEDCPROTECT:
331 lex >> needcprotect_;
333 case IL_CONTENTASLABEL:
334 lex >> contentaslabel_;
337 // initialize with a known style
340 style = subst(style, '_', ' ');
342 // We don't want to apply the algorithm in DocumentClass::insetLayout()
343 // here. So we do it the long way.
344 TextClass::InsetLayouts::const_iterator it =
345 tclass.insetLayouts().find(style);
346 if (it != tclass.insetLayouts().end()) {
347 docstring const tmpname = name_;
348 this->operator=(it->second);
351 LYXERR0("Cannot copy unknown InsetLayout `"
352 << style << "' to InsetLayout `"
354 << "All InsetLayouts so far:");
355 TextClass::InsetLayouts::const_iterator lit =
356 tclass.insetLayouts().begin();
357 TextClass::InsetLayouts::const_iterator len =
358 tclass.insetLayouts().end();
359 for (; lit != len; ++lit)
360 lyxerr << lit->second.name() << "\n";
364 case IL_OBSOLETEDBY: {
367 style = subst(style, '_', ' ');
369 // We don't want to apply the algorithm in DocumentClass::insetLayout()
370 // here. So we do it the long way.
371 TextClass::InsetLayouts::const_iterator it =
372 tclass.insetLayouts().find(style);
373 if (it != tclass.insetLayouts().end()) {
374 docstring const tmpname = name_;
375 this->operator=(it->second);
377 if (obsoleted_by().empty())
378 obsoleted_by_ = style;
380 LYXERR0("Cannot replace InsetLayout `"
382 << "' with unknown InsetLayout `"
384 << "All InsetLayouts so far:");
385 TextClass::InsetLayouts::const_iterator lit =
386 tclass.insetLayouts().begin();
387 TextClass::InsetLayouts::const_iterator len =
388 tclass.insetLayouts().end();
389 for (; lit != len; ++lit)
390 lyxerr << lit->second.name() << "\n";
396 font_ = lyxRead(lex, inherit_font);
397 // If you want to define labelfont, you need to do so after
407 postcommandargs_.clear();
415 bgcolor_ = lcolor.getFromLyXName(tmp);
418 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
420 case IL_BABELPREAMBLE:
421 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
423 case IL_LANGPREAMBLE:
424 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
435 case IL_HTMLFORCECSS:
436 lex >> htmlforcecss_;
438 case IL_HTMLINNERTAG:
439 lex >> htmlinnertag_;
441 case IL_HTMLINNERATTR:
442 lex >> htmlinnerattr_;
451 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
453 case IL_HTMLPREAMBLE:
454 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
458 vector<string> const req
459 = getVectorFromString(lex.getString(true));
460 requires_.insert(req.begin(), req.end());
474 add_to_toc_ = !toc_type_.empty();
476 case IL_ISTOCCAPTION:
477 lex >> is_toc_caption_;
479 case IL_EDITEXTERNAL:
480 lex >> edit_external_;
488 // Here add element to list if getout == true
492 // The label font is generally used as-is without
493 // any realization against a given context.
494 labelfont_.realize(sane_font);
501 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
503 if (compare_ascii_no_case(str, "charstyle") == 0)
504 return InsetLayout::CHARSTYLE;
505 if (compare_ascii_no_case(str, "custom") == 0)
506 return InsetLayout::CUSTOM;
507 if (compare_ascii_no_case(str, "element") == 0)
508 return InsetLayout::ELEMENT;
509 if (compare_ascii_no_case(str, "end") == 0)
510 return InsetLayout::END;
511 if (compare_ascii_no_case(str, "standard") == 0)
512 return InsetLayout::STANDARD;
513 return InsetLayout::NOLYXTYPE;
517 string const & InsetLayout::htmltag() const
519 if (htmltag_.empty())
520 htmltag_ = multipar_ ? "div" : "span";
525 string const & InsetLayout::htmlattr() const
527 if (htmlattr_.empty())
528 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
533 string const & InsetLayout::htmlinnerattr() const
535 if (htmlinnerattr_.empty())
536 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
537 return htmlinnerattr_;
541 string InsetLayout::defaultCSSClass() const
543 if (!defaultcssclass_.empty())
544 return defaultcssclass_;
546 string n = to_utf8(name());
547 string::const_iterator it = n.begin();
548 string::const_iterator en = n.end();
549 for (; it != en; ++it) {
550 if (!isAlphaASCII(*it))
552 else if (isLower(*it))
557 // are there other characters we need to remove?
558 defaultcssclass_ = d;
559 return defaultcssclass_;
563 void InsetLayout::makeDefaultCSS() const
565 if (!htmldefaultstyle_.empty())
567 docstring const mainfontCSS = font_.asCSS();
568 if (!mainfontCSS.empty())
570 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
571 mainfontCSS + from_ascii("\n}\n");
575 docstring InsetLayout::htmlstyle() const
577 if (!htmlstyle_.empty() && !htmlforcecss_)
579 if (htmldefaultstyle_.empty())
581 docstring retval = htmldefaultstyle_;
582 if (!htmlstyle_.empty())
583 retval += '\n' + htmlstyle_ + '\n';
587 void InsetLayout::readArgument(Lexer & lex)
589 Layout::latexarg arg;
590 arg.mandatory = false;
591 arg.autoinsert = false;
592 arg.insertcotext = false;
594 bool finished = false;
595 arg.font = inherit_font;
596 arg.labelfont = inherit_font;
597 arg.is_toc_caption = false;
598 arg.passthru = PT_INHERITED;
599 arg.nodelims = false;
602 bool const postcmd = prefixIs(nr, "post:");
603 while (!finished && lex.isOK() && !error) {
605 string const tok = ascii_lowercase(lex.getString());
609 } else if (tok == "endargument") {
611 } else if (tok == "labelstring") {
613 arg.labelstring = lex.getDocString();
614 } else if (tok == "menustring") {
616 arg.menustring = lex.getDocString();
617 } else if (tok == "mandatory") {
619 arg.mandatory = lex.getBool();
620 } else if (tok == "autoinsert") {
622 arg.autoinsert = lex.getBool();
623 } else if (tok == "insertcotext") {
625 arg.insertcotext = lex.getBool();
626 } else if (tok == "leftdelim") {
628 arg.ldelim = lex.getDocString();
629 arg.ldelim = subst(arg.ldelim,
630 from_ascii("<br/>"), from_ascii("\n"));
631 } else if (tok == "rightdelim") {
633 arg.rdelim = lex.getDocString();
634 arg.rdelim = subst(arg.rdelim,
635 from_ascii("<br/>"), from_ascii("\n"));
636 } else if (tok == "defaultarg") {
638 arg.defaultarg = lex.getDocString();
639 } else if (tok == "presetarg") {
641 arg.presetarg = lex.getDocString();
642 } else if (tok == "tooltip") {
644 arg.tooltip = lex.getDocString();
645 } else if (tok == "requires") {
647 arg.requires = lex.getString();
648 } else if (tok == "decoration") {
650 arg.decoration = lex.getString();
651 } else if (tok == "font") {
652 arg.font = lyxRead(lex, arg.font);
653 } else if (tok == "labelfont") {
654 arg.labelfont = lyxRead(lex, arg.labelfont);
655 } else if (tok == "passthruchars") {
657 arg.pass_thru_chars = lex.getDocString();
658 } else if (tok == "passthru") {
660 docstring value = lex.getDocString();
661 if (value == "true" || value == "1")
662 arg.passthru = PT_TRUE;
663 else if (value == "false" || value == "0")
664 arg.passthru = PT_FALSE;
666 arg.passthru = PT_INHERITED;
667 } else if (tok == "istoccaption") {
669 arg.is_toc_caption = lex.getBool();
671 lex.printError("Unknown tag");
675 if (arg.labelstring.empty())
676 LYXERR0("Incomplete Argument definition!");
678 postcommandargs_[nr] = arg;
680 latexargs_[nr] = arg;
684 Layout::LaTeXArgMap InsetLayout::args() const
686 Layout::LaTeXArgMap args = latexargs_;
687 if (!postcommandargs_.empty())
688 args.insert(postcommandargs_.begin(), postcommandargs_.end());
693 unsigned int InsetLayout::optArgs() const
696 Layout::LaTeXArgMap const args = InsetLayout::args();
697 Layout::LaTeXArgMap::const_iterator it = args.begin();
698 for (; it != args.end(); ++it) {
699 if (!(*it).second.mandatory)
706 unsigned int InsetLayout::requiredArgs() const
709 Layout::LaTeXArgMap const args = InsetLayout::args();
710 Layout::LaTeXArgMap::const_iterator it = args.begin();
711 for (; it != args.end(); ++it) {
712 if ((*it).second.mandatory)