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 needmboxprotect_(false), intoc_(false), spellcheck_(true),
47 resetsfont_(false), display_(true), forcelocalfontswitch_(false),
48 add_to_toc_(false), is_toc_caption_(false), edit_external_(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,
130 IL_PARBREAKISNEWLINE,
142 LexerKeyword elementTags[] = {
143 { "addtotoc", IL_ADDTOTOC },
144 { "argument", IL_ARGUMENT },
145 { "babelpreamble", IL_BABELPREAMBLE },
146 { "bgcolor", IL_BGCOLOR },
147 { "contentaslabel", IL_CONTENTASLABEL },
148 { "copystyle", IL_COPYSTYLE },
149 { "counter", IL_COUNTER},
150 { "custompars", IL_CUSTOMPARS },
151 { "decoration", IL_DECORATION },
152 { "display", IL_DISPLAY },
153 { "editexternal", IL_EDITEXTERNAL },
155 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
157 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
158 { "forceltr", IL_FORCELTR },
159 { "forceownlines", IL_FORCEOWNLINES },
160 { "forceplain", IL_FORCEPLAIN },
161 { "freespacing", IL_FREESPACING },
162 { "htmlattr", IL_HTMLATTR },
163 { "htmlforcecss", IL_HTMLFORCECSS },
164 { "htmlinnerattr", IL_HTMLINNERATTR},
165 { "htmlinnertag", IL_HTMLINNERTAG},
166 { "htmlisblock", IL_HTMLISBLOCK},
167 { "htmllabel", IL_HTMLLABEL },
168 { "htmlpreamble", IL_HTMLPREAMBLE },
169 { "htmlstyle", IL_HTMLSTYLE },
170 { "htmltag", IL_HTMLTAG },
171 { "intoc", IL_INTOC },
172 { "istoccaption", IL_ISTOCCAPTION },
173 { "keepempty", IL_KEEPEMPTY },
174 { "labelfont", IL_LABELFONT },
175 { "labelstring", IL_LABELSTRING },
176 { "langpreamble", IL_LANGPREAMBLE },
177 { "latexname", IL_LATEXNAME },
178 { "latexparam", IL_LATEXPARAM },
179 { "latextype", IL_LATEXTYPE },
180 { "leftdelim", IL_LEFTDELIM },
181 { "lyxtype", IL_LYXTYPE },
182 { "menustring", IL_MENUSTRING },
183 { "multipar", IL_MULTIPAR },
184 { "needcprotect", IL_NEEDCPROTECT },
185 { "needmboxprotect", IL_NEEDMBOXPROTECT },
186 { "needprotect", IL_NEEDPROTECT },
187 { "obsoletedby", IL_OBSOLETEDBY },
188 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
189 { "passthru", IL_PASSTHRU },
190 { "passthruchars", IL_PASSTHRU_CHARS },
191 { "preamble", IL_PREAMBLE },
192 { "refprefix", IL_REFPREFIX },
193 { "requires", IL_REQUIRES },
194 { "resetargs", IL_RESETARGS },
195 { "resetsfont", IL_RESETSFONT },
196 { "rightdelim", IL_RIGHTDELIM },
197 { "spellcheck", IL_SPELLCHECK }
200 lex.pushTable(elementTags);
202 labelfont_ = inherit_font;
203 bgcolor_ = Color_none;
205 // whether we've read the CustomPars or ForcePlain tag
206 // for issuing a warning in case MultiPars comes later
207 bool readCustomOrPlain = false;
210 while (!getout && lex.isOK()) {
213 case Lexer::LEX_UNDEF:
214 lex.printError("Unknown InsetLayout tag");
221 // Perhaps a more elegant way to deal with the next two would be the
222 // way this sort of thing is handled in Layout::read(), namely, by
225 // make sure that we have the right sort of name.
226 if (name_ != from_ascii("undefined")
227 && name_.substr(0,5) != from_ascii("Flex:")) {
228 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
229 "This one has the name `" << to_utf8(name_) << "'\n"
230 "Ignoring LyXType declaration.");
235 lyxtype_ = translateLyXType(lt);
236 if (lyxtype_ == NOLYXTYPE)
237 LYXERR0("Unknown LyXType `" << lt << "'.");
238 if (lyxtype_ == CHARSTYLE) {
239 // by default, charstyles force the plain layout
248 latextype_ = translateLaTeXType(lt);
249 if (latextype_ == ILT_ERROR)
250 LYXERR0("Unknown LaTeXType `" << lt << "'.");
261 decoration_ = translateDecoration(tmp);
268 latexparam_ = subst(tmp, """, "\"");
272 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
275 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
276 lex >> fixedwidthpreambleencoding_;
278 case IL_FORCE_LOCAL_FONT_SWITCH:
279 lex >> forcelocalfontswitch_;
283 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
287 labelfont_ = lyxRead(lex, inherit_font);
292 case IL_FORCEOWNLINES:
293 lex >> forceownlines_;
300 // the defaults for these depend upon multipar_
301 if (readCustomOrPlain)
302 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
303 "Previous value may be overwritten!");
304 readCustomOrPlain = false;
305 custompars_ = multipar_;
306 forceplain_ = !multipar_;
313 readCustomOrPlain = true;
317 readCustomOrPlain = true;
322 case IL_PASSTHRU_CHARS:
323 lex >> passthru_chars_;
325 case IL_PARBREAKISNEWLINE:
326 lex >> parbreakisnewline_;
337 case IL_NEEDCPROTECT:
338 lex >> needcprotect_;
340 case IL_NEEDMBOXPROTECT:
341 lex >> needmboxprotect_;
343 case IL_CONTENTASLABEL:
344 lex >> contentaslabel_;
347 // initialize with a known style
350 style = subst(style, '_', ' ');
352 // We don't want to apply the algorithm in DocumentClass::insetLayout()
353 // here. So we do it the long way.
354 TextClass::InsetLayouts::const_iterator it =
355 tclass.insetLayouts().find(style);
356 if (it != tclass.insetLayouts().end()) {
357 docstring const tmpname = name_;
358 this->operator=(it->second);
361 LYXERR0("Cannot copy unknown InsetLayout `"
362 << style << "' to InsetLayout `"
364 << "All InsetLayouts so far:");
365 TextClass::InsetLayouts::const_iterator lit =
366 tclass.insetLayouts().begin();
367 TextClass::InsetLayouts::const_iterator len =
368 tclass.insetLayouts().end();
369 for (; lit != len; ++lit)
370 lyxerr << lit->second.name() << "\n";
374 case IL_OBSOLETEDBY: {
377 style = subst(style, '_', ' ');
379 // We don't want to apply the algorithm in DocumentClass::insetLayout()
380 // here. So we do it the long way.
381 TextClass::InsetLayouts::const_iterator it =
382 tclass.insetLayouts().find(style);
383 if (it != tclass.insetLayouts().end()) {
384 docstring const tmpname = name_;
385 this->operator=(it->second);
387 if (obsoleted_by().empty())
388 obsoleted_by_ = style;
390 LYXERR0("Cannot replace InsetLayout `"
392 << "' with unknown InsetLayout `"
394 << "All InsetLayouts so far:");
395 TextClass::InsetLayouts::const_iterator lit =
396 tclass.insetLayouts().begin();
397 TextClass::InsetLayouts::const_iterator len =
398 tclass.insetLayouts().end();
399 for (; lit != len; ++lit)
400 lyxerr << lit->second.name() << "\n";
406 font_ = lyxRead(lex, inherit_font);
407 // If you want to define labelfont, you need to do so after
417 postcommandargs_.clear();
425 bgcolor_ = lcolor.getFromLyXName(tmp);
428 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
430 case IL_BABELPREAMBLE:
431 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
433 case IL_LANGPREAMBLE:
434 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
445 case IL_HTMLFORCECSS:
446 lex >> htmlforcecss_;
448 case IL_HTMLINNERTAG:
449 lex >> htmlinnertag_;
451 case IL_HTMLINNERATTR:
452 lex >> htmlinnerattr_;
461 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
463 case IL_HTMLPREAMBLE:
464 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
468 vector<string> const req
469 = getVectorFromString(lex.getString(true));
470 requires_.insert(req.begin(), req.end());
484 add_to_toc_ = !toc_type_.empty();
486 case IL_ISTOCCAPTION:
487 lex >> is_toc_caption_;
489 case IL_EDITEXTERNAL:
490 lex >> edit_external_;
498 // Here add element to list if getout == true
502 // The label font is generally used as-is without
503 // any realization against a given context.
504 labelfont_.realize(sane_font);
511 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
513 if (compare_ascii_no_case(str, "charstyle") == 0)
514 return InsetLayout::CHARSTYLE;
515 if (compare_ascii_no_case(str, "custom") == 0)
516 return InsetLayout::CUSTOM;
517 if (compare_ascii_no_case(str, "element") == 0)
518 return InsetLayout::ELEMENT;
519 if (compare_ascii_no_case(str, "end") == 0)
520 return InsetLayout::END;
521 if (compare_ascii_no_case(str, "standard") == 0)
522 return InsetLayout::STANDARD;
523 return InsetLayout::NOLYXTYPE;
527 string const & InsetLayout::htmltag() const
529 if (htmltag_.empty())
530 htmltag_ = multipar_ ? "div" : "span";
535 string const & InsetLayout::htmlattr() const
537 if (htmlattr_.empty())
538 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
543 string const & InsetLayout::htmlinnerattr() const
545 if (htmlinnerattr_.empty())
546 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
547 return htmlinnerattr_;
551 string InsetLayout::defaultCSSClass() const
553 if (!defaultcssclass_.empty())
554 return defaultcssclass_;
556 string n = to_utf8(name());
557 string::const_iterator it = n.begin();
558 string::const_iterator en = n.end();
559 for (; it != en; ++it) {
560 if (!isAlphaASCII(*it))
562 else if (isLower(*it))
567 // are there other characters we need to remove?
568 defaultcssclass_ = d;
569 return defaultcssclass_;
573 void InsetLayout::makeDefaultCSS() const
575 if (!htmldefaultstyle_.empty())
577 docstring const mainfontCSS = font_.asCSS();
578 if (!mainfontCSS.empty())
580 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
581 mainfontCSS + from_ascii("\n}\n");
585 docstring InsetLayout::htmlstyle() const
587 if (!htmlstyle_.empty() && !htmlforcecss_)
589 if (htmldefaultstyle_.empty())
591 docstring retval = htmldefaultstyle_;
592 if (!htmlstyle_.empty())
593 retval += '\n' + htmlstyle_ + '\n';
597 void InsetLayout::readArgument(Lexer & lex)
599 Layout::latexarg arg;
600 arg.mandatory = false;
601 arg.autoinsert = false;
602 arg.insertcotext = false;
604 bool finished = false;
605 arg.font = inherit_font;
606 arg.labelfont = inherit_font;
607 arg.is_toc_caption = false;
608 arg.passthru = PT_INHERITED;
609 arg.nodelims = false;
612 bool const postcmd = prefixIs(nr, "post:");
613 while (!finished && lex.isOK() && !error) {
615 string const tok = ascii_lowercase(lex.getString());
619 } else if (tok == "endargument") {
621 } else if (tok == "labelstring") {
623 arg.labelstring = lex.getDocString();
624 } else if (tok == "menustring") {
626 arg.menustring = lex.getDocString();
627 } else if (tok == "mandatory") {
629 arg.mandatory = lex.getBool();
630 } else if (tok == "autoinsert") {
632 arg.autoinsert = lex.getBool();
633 } else if (tok == "insertcotext") {
635 arg.insertcotext = lex.getBool();
636 } else if (tok == "leftdelim") {
638 arg.ldelim = lex.getDocString();
639 arg.ldelim = subst(arg.ldelim,
640 from_ascii("<br/>"), from_ascii("\n"));
641 } else if (tok == "rightdelim") {
643 arg.rdelim = lex.getDocString();
644 arg.rdelim = subst(arg.rdelim,
645 from_ascii("<br/>"), from_ascii("\n"));
646 } else if (tok == "defaultarg") {
648 arg.defaultarg = lex.getDocString();
649 } else if (tok == "presetarg") {
651 arg.presetarg = lex.getDocString();
652 } else if (tok == "tooltip") {
654 arg.tooltip = lex.getDocString();
655 } else if (tok == "requires") {
657 arg.requires = lex.getString();
658 } else if (tok == "decoration") {
660 arg.decoration = lex.getString();
661 } else if (tok == "font") {
662 arg.font = lyxRead(lex, arg.font);
663 } else if (tok == "labelfont") {
664 arg.labelfont = lyxRead(lex, arg.labelfont);
665 } else if (tok == "passthruchars") {
667 arg.pass_thru_chars = lex.getDocString();
668 } else if (tok == "passthru") {
670 docstring value = lex.getDocString();
671 if (value == "true" || value == "1")
672 arg.passthru = PT_TRUE;
673 else if (value == "false" || value == "0")
674 arg.passthru = PT_FALSE;
676 arg.passthru = PT_INHERITED;
677 } else if (tok == "istoccaption") {
679 arg.is_toc_caption = lex.getBool();
681 lex.printError("Unknown tag");
685 if (arg.labelstring.empty())
686 LYXERR0("Incomplete Argument definition!");
688 postcommandargs_[nr] = arg;
690 latexargs_[nr] = arg;
694 Layout::LaTeXArgMap InsetLayout::args() const
696 Layout::LaTeXArgMap args = latexargs_;
697 if (!postcommandargs_.empty())
698 args.insert(postcommandargs_.begin(), postcommandargs_.end());
703 unsigned int InsetLayout::optArgs() const
706 Layout::LaTeXArgMap const args = InsetLayout::args();
707 Layout::LaTeXArgMap::const_iterator it = args.begin();
708 for (; it != args.end(); ++it) {
709 if (!(*it).second.mandatory)
716 unsigned int InsetLayout::requiredArgs() const
719 Layout::LaTeXArgMap const args = InsetLayout::args();
720 Layout::LaTeXArgMap::const_iterator it = args.begin();
721 for (; it != args.end(); ++it) {
722 if ((*it).second.mandatory)