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 parbreakignored_(false), 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,
96 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
98 IL_FORCE_LOCAL_FONT_SWITCH,
133 IL_PARBREAKISNEWLINE,
145 LexerKeyword elementTags[] = {
146 { "addtotoc", IL_ADDTOTOC },
147 { "argument", IL_ARGUMENT },
148 { "babelpreamble", IL_BABELPREAMBLE },
149 { "bgcolor", IL_BGCOLOR },
150 { "contentaslabel", IL_CONTENTASLABEL },
151 { "copystyle", IL_COPYSTYLE },
152 { "counter", IL_COUNTER},
153 { "custompars", IL_CUSTOMPARS },
154 { "decoration", IL_DECORATION },
155 { "display", IL_DISPLAY },
156 { "editexternal", IL_EDITEXTERNAL },
158 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
160 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
161 { "forceltr", IL_FORCELTR },
162 { "forceownlines", IL_FORCEOWNLINES },
163 { "forceplain", IL_FORCEPLAIN },
164 { "freespacing", IL_FREESPACING },
165 { "htmlattr", IL_HTMLATTR },
166 { "htmlforcecss", IL_HTMLFORCECSS },
167 { "htmlinnerattr", IL_HTMLINNERATTR},
168 { "htmlinnertag", IL_HTMLINNERTAG},
169 { "htmlisblock", IL_HTMLISBLOCK},
170 { "htmllabel", IL_HTMLLABEL },
171 { "htmlpreamble", IL_HTMLPREAMBLE },
172 { "htmlstyle", IL_HTMLSTYLE },
173 { "htmltag", IL_HTMLTAG },
174 { "intoc", IL_INTOC },
175 { "istoccaption", IL_ISTOCCAPTION },
176 { "keepempty", IL_KEEPEMPTY },
177 { "labelfont", IL_LABELFONT },
178 { "labelstring", IL_LABELSTRING },
179 { "langpreamble", IL_LANGPREAMBLE },
180 { "latexname", IL_LATEXNAME },
181 { "latexparam", IL_LATEXPARAM },
182 { "latextype", IL_LATEXTYPE },
183 { "leftdelim", IL_LEFTDELIM },
184 { "lyxtype", IL_LYXTYPE },
185 { "menustring", IL_MENUSTRING },
186 { "multipar", IL_MULTIPAR },
187 { "needcprotect", IL_NEEDCPROTECT },
188 { "needmboxprotect", IL_NEEDMBOXPROTECT },
189 { "needprotect", IL_NEEDPROTECT },
190 { "newlinecmd", IL_NEWLINE_CMD },
191 { "obsoletedby", IL_OBSOLETEDBY },
192 { "parbreakignored", IL_PARBREAKIGNORED },
193 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
194 { "passthru", IL_PASSTHRU },
195 { "passthruchars", IL_PASSTHRU_CHARS },
196 { "preamble", IL_PREAMBLE },
197 { "refprefix", IL_REFPREFIX },
198 { "requires", IL_REQUIRES },
199 { "resetargs", IL_RESETARGS },
200 { "resetsfont", IL_RESETSFONT },
201 { "rightdelim", IL_RIGHTDELIM },
202 { "spellcheck", IL_SPELLCHECK }
205 lex.pushTable(elementTags);
207 labelfont_ = inherit_font;
208 bgcolor_ = Color_none;
210 // whether we've read the CustomPars or ForcePlain tag
211 // for issuing a warning in case MultiPars comes later
212 bool readCustomOrPlain = false;
215 while (!getout && lex.isOK()) {
218 case Lexer::LEX_UNDEF:
219 lex.printError("Unknown InsetLayout tag");
228 // Perhaps a more elegant way to deal with the next two would be the
229 // way this sort of thing is handled in Layout::read(), namely, by
232 // make sure that we have the right sort of name.
233 if (name_ != from_ascii("undefined")
234 && name_.substr(0,5) != from_ascii("Flex:")) {
235 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
236 "This one has the name `" << to_utf8(name_) << "'\n"
237 "Ignoring LyXType declaration.");
238 // this is not really a reason to abort
245 lyxtype_ = translateLyXType(lt);
246 if (lyxtype_ == NOLYXTYPE) {
247 LYXERR0("Unknown LyXType `" << lt << "'.");
248 // this is not really a reason to abort
252 if (lyxtype_ == CHARSTYLE) {
253 // by default, charstyles force the plain layout
262 latextype_ = translateLaTeXType(lt);
263 if (latextype_ == ILT_ERROR) {
264 LYXERR0("Unknown LaTeXType `" << lt << "'.");
265 // this is not really a reason to abort
279 decoration_ = translateDecoration(tmp);
286 latexparam_ = subst(tmp, """, "\"");
290 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
293 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
294 lex >> fixedwidthpreambleencoding_;
296 case IL_FORCE_LOCAL_FONT_SWITCH:
297 lex >> forcelocalfontswitch_;
301 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
305 labelfont_ = lyxRead(lex, inherit_font);
310 case IL_FORCEOWNLINES:
311 lex >> forceownlines_;
318 // the defaults for these depend upon multipar_
319 if (readCustomOrPlain)
320 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
321 "Previous value may be overwritten!");
322 readCustomOrPlain = false;
323 custompars_ = multipar_;
324 forceplain_ = !multipar_;
331 readCustomOrPlain = true;
335 readCustomOrPlain = true;
340 case IL_PASSTHRU_CHARS:
341 lex >> passthru_chars_;
346 case IL_PARBREAKIGNORED:
347 lex >> parbreakignored_;
349 case IL_PARBREAKISNEWLINE:
350 lex >> parbreakisnewline_;
361 case IL_NEEDCPROTECT:
362 lex >> needcprotect_;
364 case IL_NEEDMBOXPROTECT:
365 lex >> needmboxprotect_;
367 case IL_CONTENTASLABEL:
368 lex >> contentaslabel_;
371 // initialize with a known style
374 style = subst(style, '_', ' ');
376 // We don't want to apply the algorithm in DocumentClass::insetLayout()
377 // here. So we do it the long way.
378 TextClass::InsetLayouts::const_iterator it =
379 tclass.insetLayouts().find(style);
380 if (it != tclass.insetLayouts().end()) {
381 docstring const tmpname = name_;
382 this->operator=(it->second);
385 LYXERR0("Cannot copy unknown InsetLayout `"
386 << style << "' to InsetLayout `"
388 << "All InsetLayouts so far:");
389 TextClass::InsetLayouts::const_iterator lit =
390 tclass.insetLayouts().begin();
391 TextClass::InsetLayouts::const_iterator len =
392 tclass.insetLayouts().end();
393 for (; lit != len; ++lit)
394 lyxerr << lit->second.name() << "\n";
395 // this is not really a reason to abort
401 case IL_OBSOLETEDBY: {
404 style = subst(style, '_', ' ');
406 // We don't want to apply the algorithm in DocumentClass::insetLayout()
407 // here. So we do it the long way.
408 TextClass::InsetLayouts::const_iterator it =
409 tclass.insetLayouts().find(style);
410 if (it != tclass.insetLayouts().end()) {
411 docstring const tmpname = name_;
412 this->operator=(it->second);
414 if (obsoleted_by().empty())
415 obsoleted_by_ = style;
417 LYXERR0("Cannot replace InsetLayout `"
419 << "' with unknown InsetLayout `"
421 << "All InsetLayouts so far:");
422 TextClass::InsetLayouts::const_iterator lit =
423 tclass.insetLayouts().begin();
424 TextClass::InsetLayouts::const_iterator len =
425 tclass.insetLayouts().end();
426 for (; lit != len; ++lit)
427 lyxerr << lit->second.name() << "\n";
428 // this is not really a reason to abort
436 font_ = lyxRead(lex, inherit_font);
437 // If you want to define labelfont, you need to do so after
447 postcommandargs_.clear();
455 bgcolor_ = lcolor.getFromLyXName(tmp);
458 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
460 case IL_BABELPREAMBLE:
461 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
463 case IL_LANGPREAMBLE:
464 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
475 case IL_HTMLFORCECSS:
476 lex >> htmlforcecss_;
478 case IL_HTMLINNERTAG:
479 lex >> htmlinnertag_;
481 case IL_HTMLINNERATTR:
482 lex >> htmlinnerattr_;
491 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
493 case IL_HTMLPREAMBLE:
494 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
498 vector<string> const req
499 = getVectorFromString(lex.getString(true));
500 required_.insert(req.begin(), req.end());
514 add_to_toc_ = !toc_type_.empty();
516 case IL_ISTOCCAPTION:
517 lex >> is_toc_caption_;
519 case IL_EDITEXTERNAL:
520 lex >> edit_external_;
528 // Here add element to list if getout == true
532 // The label font is generally used as-is without
533 // any realization against a given context.
534 labelfont_.realize(sane_font);
541 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
543 if (compare_ascii_no_case(str, "charstyle") == 0)
544 return InsetLayout::CHARSTYLE;
545 if (compare_ascii_no_case(str, "custom") == 0)
546 return InsetLayout::CUSTOM;
547 if (compare_ascii_no_case(str, "element") == 0)
548 return InsetLayout::ELEMENT;
549 if (compare_ascii_no_case(str, "end") == 0)
550 return InsetLayout::END;
551 if (compare_ascii_no_case(str, "standard") == 0)
552 return InsetLayout::STANDARD;
553 return InsetLayout::NOLYXTYPE;
557 string const & InsetLayout::htmltag() const
559 if (htmltag_.empty())
560 htmltag_ = multipar_ ? "div" : "span";
565 string const & InsetLayout::htmlattr() const
567 if (htmlattr_.empty())
568 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
573 string const & InsetLayout::htmlinnerattr() const
575 if (htmlinnerattr_.empty())
576 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
577 return htmlinnerattr_;
581 string InsetLayout::defaultCSSClass() const
583 if (!defaultcssclass_.empty())
584 return defaultcssclass_;
586 string n = to_utf8(name());
587 string::const_iterator it = n.begin();
588 string::const_iterator en = n.end();
589 for (; it != en; ++it) {
590 if (!isAlphaASCII(*it))
592 else if (isLower(*it))
597 // are there other characters we need to remove?
598 defaultcssclass_ = d;
599 return defaultcssclass_;
603 void InsetLayout::makeDefaultCSS() const
605 if (!htmldefaultstyle_.empty())
607 docstring const mainfontCSS = font_.asCSS();
608 if (!mainfontCSS.empty())
610 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
611 mainfontCSS + from_ascii("\n}\n");
615 docstring InsetLayout::htmlstyle() const
617 if (!htmlstyle_.empty() && !htmlforcecss_)
619 if (htmldefaultstyle_.empty())
621 docstring retval = htmldefaultstyle_;
622 if (!htmlstyle_.empty())
623 retval += '\n' + htmlstyle_ + '\n';
627 void InsetLayout::readArgument(Lexer & lex)
629 Layout::latexarg arg;
630 arg.mandatory = false;
631 arg.autoinsert = false;
632 arg.insertcotext = false;
633 arg.insertonnewline = false;
635 bool finished = false;
636 arg.font = inherit_font;
637 arg.labelfont = inherit_font;
638 arg.is_toc_caption = false;
639 arg.free_spacing = false;
640 arg.passthru = PT_INHERITED;
641 arg.nodelims = false;
644 bool const postcmd = prefixIs(nr, "post:");
645 while (!finished && lex.isOK() && !error) {
647 string const tok = ascii_lowercase(lex.getString());
651 } else if (tok == "endargument") {
653 } else if (tok == "labelstring") {
655 arg.labelstring = lex.getDocString();
656 } else if (tok == "menustring") {
658 arg.menustring = lex.getDocString();
659 } else if (tok == "mandatory") {
661 arg.mandatory = lex.getBool();
662 } else if (tok == "autoinsert") {
664 arg.autoinsert = lex.getBool();
665 } else if (tok == "insertcotext") {
667 arg.insertcotext = lex.getBool();
668 } else if (tok == "insertonnewline") {
670 arg.insertonnewline = lex.getBool();
671 } else if (tok == "leftdelim") {
673 arg.ldelim = lex.getDocString();
674 arg.ldelim = subst(arg.ldelim,
675 from_ascii("<br/>"), from_ascii("\n"));
676 } else if (tok == "rightdelim") {
678 arg.rdelim = lex.getDocString();
679 arg.rdelim = subst(arg.rdelim,
680 from_ascii("<br/>"), from_ascii("\n"));
681 } else if (tok == "defaultarg") {
683 arg.defaultarg = lex.getDocString();
684 } else if (tok == "presetarg") {
686 arg.presetarg = lex.getDocString();
687 } else if (tok == "tooltip") {
689 arg.tooltip = lex.getDocString();
690 } else if (tok == "requires") {
692 arg.required = lex.getString();
693 } else if (tok == "decoration") {
695 arg.decoration = lex.getString();
696 } else if (tok == "newlinecmd") {
698 arg.newlinecmd = lex.getString();
699 } else if (tok == "font") {
700 arg.font = lyxRead(lex, arg.font);
701 } else if (tok == "labelfont") {
702 arg.labelfont = lyxRead(lex, arg.labelfont);
703 } else if (tok == "passthruchars") {
705 arg.pass_thru_chars = lex.getDocString();
706 } else if (tok == "passthru") {
708 docstring value = lex.getDocString();
709 if (value == "true" || value == "1")
710 arg.passthru = PT_TRUE;
711 else if (value == "false" || value == "0")
712 arg.passthru = PT_FALSE;
714 arg.passthru = PT_INHERITED;
715 } else if (tok == "istoccaption") {
717 arg.is_toc_caption = lex.getBool();
718 } else if (tok == "freespacing") {
720 arg.free_spacing = lex.getBool();
722 lex.printError("Unknown tag");
726 if (arg.labelstring.empty())
727 LYXERR0("Incomplete Argument definition!");
729 postcommandargs_[nr] = arg;
731 latexargs_[nr] = arg;
735 Layout::LaTeXArgMap InsetLayout::args() const
737 Layout::LaTeXArgMap args = latexargs_;
738 if (!postcommandargs_.empty())
739 args.insert(postcommandargs_.begin(), postcommandargs_.end());
744 unsigned int InsetLayout::optArgs() const
747 Layout::LaTeXArgMap const args = InsetLayout::args();
748 Layout::LaTeXArgMap::const_iterator it = args.begin();
749 for (; it != args.end(); ++it) {
750 if (!(*it).second.mandatory)
757 unsigned int InsetLayout::requiredArgs() const
760 Layout::LaTeXArgMap const args = InsetLayout::args();
761 Layout::LaTeXArgMap::const_iterator it = args.begin();
762 for (; it != args.end(); ++it) {
763 if ((*it).second.mandatory)