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"
19 #include "TextClass.h"
21 #include "support/debug.h"
22 #include "support/lstrings.h"
23 #include "support/textutils.h"
31 using namespace lyx::support;
35 InsetLayout::InsetLayout() :
36 name_(from_ascii("undefined")), lyxtype_(STANDARD),
37 labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
38 decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(inherit_font),
39 labelfont_(sane_font), bgcolor_(Color_error),
40 fixedwidthpreambleencoding_(false), htmlforcecss_ (false),
41 htmlisblock_(true), docbooksection_(false), multipar_(true),
42 custompars_(true), forceplain_(false), passthru_(false),
43 parbreakisnewline_(false), parbreakignored_(false), freespacing_(false),
44 keepempty_(false), forceltr_(false), forceownlines_(false),
45 needprotect_(false), needcprotect_(false), needmboxprotect_(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,
96 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
98 IL_FORCE_LOCAL_FONT_SWITCH,
116 IL_DOCBOOKWRAPPERTAG,
117 IL_DOCBOOKWRAPPERTAGTYPE,
118 IL_DOCBOOKWRAPPERATTR,
140 IL_PARBREAKISNEWLINE,
152 LexerKeyword elementTags[] = {
153 { "addtotoc", IL_ADDTOTOC },
154 { "argument", IL_ARGUMENT },
155 { "babelpreamble", IL_BABELPREAMBLE },
156 { "bgcolor", IL_BGCOLOR },
157 { "contentaslabel", IL_CONTENTASLABEL },
158 { "copystyle", IL_COPYSTYLE },
159 { "counter", IL_COUNTER},
160 { "custompars", IL_CUSTOMPARS },
161 { "decoration", IL_DECORATION },
162 { "display", IL_DISPLAY },
163 { "docbookattr", IL_DOCBOOKATTR },
164 { "docbooksection", IL_DOCBOOKSECTION },
165 { "docbooktag", IL_DOCBOOKTAG },
166 { "docbooktagtype", IL_DOCBOOKTAGTYPE },
167 { "docbookwrapperattr", IL_DOCBOOKWRAPPERATTR },
168 { "docbookwrappertag", IL_DOCBOOKWRAPPERTAG },
169 { "docbookwrappertagtype", IL_DOCBOOKWRAPPERTAGTYPE },
170 { "editexternal", IL_EDITEXTERNAL },
172 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
174 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
175 { "forceltr", IL_FORCELTR },
176 { "forceownlines", IL_FORCEOWNLINES },
177 { "forceplain", IL_FORCEPLAIN },
178 { "freespacing", IL_FREESPACING },
179 { "htmlattr", IL_HTMLATTR },
180 { "htmlforcecss", IL_HTMLFORCECSS },
181 { "htmlinnerattr", IL_HTMLINNERATTR},
182 { "htmlinnertag", IL_HTMLINNERTAG},
183 { "htmlisblock", IL_HTMLISBLOCK},
184 { "htmllabel", IL_HTMLLABEL },
185 { "htmlpreamble", IL_HTMLPREAMBLE },
186 { "htmlstyle", IL_HTMLSTYLE },
187 { "htmltag", IL_HTMLTAG },
188 { "intoc", IL_INTOC },
189 { "istoccaption", IL_ISTOCCAPTION },
190 { "keepempty", IL_KEEPEMPTY },
191 { "labelfont", IL_LABELFONT },
192 { "labelstring", IL_LABELSTRING },
193 { "langpreamble", IL_LANGPREAMBLE },
194 { "latexname", IL_LATEXNAME },
195 { "latexparam", IL_LATEXPARAM },
196 { "latextype", IL_LATEXTYPE },
197 { "leftdelim", IL_LEFTDELIM },
198 { "lyxtype", IL_LYXTYPE },
199 { "menustring", IL_MENUSTRING },
200 { "multipar", IL_MULTIPAR },
201 { "needcprotect", IL_NEEDCPROTECT },
202 { "needmboxprotect", IL_NEEDMBOXPROTECT },
203 { "needprotect", IL_NEEDPROTECT },
204 { "newlinecmd", IL_NEWLINE_CMD },
205 { "obsoletedby", IL_OBSOLETEDBY },
206 { "parbreakignored", IL_PARBREAKIGNORED },
207 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
208 { "passthru", IL_PASSTHRU },
209 { "passthruchars", IL_PASSTHRU_CHARS },
210 { "preamble", IL_PREAMBLE },
211 { "refprefix", IL_REFPREFIX },
212 { "requires", IL_REQUIRES },
213 { "resetargs", IL_RESETARGS },
214 { "resetsfont", IL_RESETSFONT },
215 { "rightdelim", IL_RIGHTDELIM },
216 { "spellcheck", IL_SPELLCHECK }
219 lex.pushTable(elementTags);
221 labelfont_ = inherit_font;
222 bgcolor_ = Color_none;
224 // whether we've read the CustomPars or ForcePlain tag
225 // for issuing a warning in case MultiPars comes later
226 bool readCustomOrPlain = false;
229 while (!getout && lex.isOK()) {
232 case Lexer::LEX_UNDEF:
233 lex.printError("Unknown InsetLayout tag");
242 // Perhaps a more elegant way to deal with the next two would be the
243 // way this sort of thing is handled in Layout::read(), namely, by
246 // make sure that we have the right sort of name.
247 if (name_ != from_ascii("undefined")
248 && name_.substr(0,5) != from_ascii("Flex:")) {
249 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
250 "This one has the name `" << to_utf8(name_) << "'\n"
251 "Ignoring LyXType declaration.");
252 // this is not really a reason to abort
259 lyxtype_ = translateLyXType(lt);
260 if (lyxtype_ == NOLYXTYPE) {
261 LYXERR0("Unknown LyXType `" << lt << "'.");
262 // this is not really a reason to abort
266 if (lyxtype_ == CHARSTYLE) {
267 // by default, charstyles force the plain layout
276 latextype_ = translateLaTeXType(lt);
277 if (latextype_ == ILT_ERROR) {
278 LYXERR0("Unknown LaTeXType `" << lt << "'.");
279 // this is not really a reason to abort
293 decoration_ = translateDecoration(tmp);
300 latexparam_ = subst(tmp, """, "\"");
304 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
307 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
308 lex >> fixedwidthpreambleencoding_;
310 case IL_FORCE_LOCAL_FONT_SWITCH:
311 lex >> forcelocalfontswitch_;
315 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
319 labelfont_ = lyxRead(lex, inherit_font);
324 case IL_FORCEOWNLINES:
325 lex >> forceownlines_;
332 // the defaults for these depend upon multipar_
333 if (readCustomOrPlain)
334 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
335 "Previous value may be overwritten!");
336 readCustomOrPlain = false;
337 custompars_ = multipar_;
338 forceplain_ = !multipar_;
345 readCustomOrPlain = true;
349 readCustomOrPlain = true;
354 case IL_PASSTHRU_CHARS:
355 lex >> passthru_chars_;
360 case IL_PARBREAKIGNORED:
361 lex >> parbreakignored_;
363 case IL_PARBREAKISNEWLINE:
364 lex >> parbreakisnewline_;
375 case IL_NEEDCPROTECT:
376 lex >> needcprotect_;
378 case IL_NEEDMBOXPROTECT:
379 lex >> needmboxprotect_;
381 case IL_CONTENTASLABEL:
382 lex >> contentaslabel_;
385 // initialize with a known style
388 style = subst(style, '_', ' ');
390 // We don't want to apply the algorithm in DocumentClass::insetLayout()
391 // here. So we do it the long way.
392 TextClass::InsetLayouts::const_iterator it =
393 tclass.insetLayouts().find(style);
394 if (it != tclass.insetLayouts().end()) {
395 docstring const tmpname = name_;
396 this->operator=(it->second);
399 LYXERR0("Cannot copy unknown InsetLayout `"
400 << style << "' to InsetLayout `"
402 << "All InsetLayouts so far:");
403 TextClass::InsetLayouts::const_iterator lit =
404 tclass.insetLayouts().begin();
405 TextClass::InsetLayouts::const_iterator len =
406 tclass.insetLayouts().end();
407 for (; lit != len; ++lit)
408 lyxerr << lit->second.name() << "\n";
409 // this is not really a reason to abort
415 case IL_OBSOLETEDBY: {
418 style = subst(style, '_', ' ');
420 // We don't want to apply the algorithm in DocumentClass::insetLayout()
421 // here. So we do it the long way.
422 TextClass::InsetLayouts::const_iterator it =
423 tclass.insetLayouts().find(style);
424 if (it != tclass.insetLayouts().end()) {
425 docstring const tmpname = name_;
426 this->operator=(it->second);
428 if (obsoleted_by().empty())
429 obsoleted_by_ = style;
431 LYXERR0("Cannot replace InsetLayout `"
433 << "' with unknown InsetLayout `"
435 << "All InsetLayouts so far:");
436 TextClass::InsetLayouts::const_iterator lit =
437 tclass.insetLayouts().begin();
438 TextClass::InsetLayouts::const_iterator len =
439 tclass.insetLayouts().end();
440 for (; lit != len; ++lit)
441 lyxerr << lit->second.name() << "\n";
442 // this is not really a reason to abort
450 font_ = lyxRead(lex, inherit_font);
451 // If you want to define labelfont, you need to do so after
461 postcommandargs_.clear();
469 bgcolor_ = lcolor.getFromLyXName(tmp);
472 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
474 case IL_BABELPREAMBLE:
475 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
477 case IL_LANGPREAMBLE:
478 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
489 case IL_HTMLFORCECSS:
490 lex >> htmlforcecss_;
492 case IL_HTMLINNERTAG:
493 lex >> htmlinnertag_;
495 case IL_HTMLINNERATTR:
496 lex >> htmlinnerattr_;
505 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
507 case IL_HTMLPREAMBLE:
508 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
513 case IL_DOCBOOKTAGTYPE:
514 lex >> docbooktagtype_;
519 case IL_DOCBOOKSECTION:
520 lex >> docbooksection_;
522 case IL_DOCBOOKWRAPPERTAG:
523 lex >> docbookwrappertag_;
525 case IL_DOCBOOKWRAPPERTAGTYPE:
526 lex >> docbookwrappertagtype_;
528 case IL_DOCBOOKWRAPPERATTR:
529 lex >> docbookwrapperattr_;
533 vector<string> const req
534 = getVectorFromString(lex.getString(true));
535 required_.insert(req.begin(), req.end());
549 add_to_toc_ = !toc_type_.empty();
551 case IL_ISTOCCAPTION:
552 lex >> is_toc_caption_;
554 case IL_EDITEXTERNAL:
555 lex >> edit_external_;
563 // Here add element to list if getout == true
567 // The label font is generally used as-is without
568 // any realization against a given context.
569 labelfont_.realize(sane_font);
576 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
578 if (compare_ascii_no_case(str, "charstyle") == 0)
579 return InsetLayout::CHARSTYLE;
580 if (compare_ascii_no_case(str, "custom") == 0)
581 return InsetLayout::CUSTOM;
582 if (compare_ascii_no_case(str, "end") == 0)
583 return InsetLayout::END;
584 if (compare_ascii_no_case(str, "standard") == 0)
585 return InsetLayout::STANDARD;
586 return InsetLayout::NOLYXTYPE;
590 string const & InsetLayout::htmltag() const
592 if (htmltag_.empty())
593 htmltag_ = multipar_ ? "div" : "span";
598 string const & InsetLayout::htmlattr() const
600 if (htmlattr_.empty())
601 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
606 string const & InsetLayout::htmlinnerattr() const
608 if (htmlinnerattr_.empty())
609 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
610 return htmlinnerattr_;
614 string InsetLayout::defaultCSSClass() const
616 if (!defaultcssclass_.empty())
617 return defaultcssclass_;
619 string n = to_utf8(name());
620 string::const_iterator it = n.begin();
621 string::const_iterator en = n.end();
622 for (; it != en; ++it) {
623 if (!isAlphaASCII(*it))
625 else if (isLower(*it))
630 // are there other characters we need to remove?
631 defaultcssclass_ = d;
632 return defaultcssclass_;
636 void InsetLayout::makeDefaultCSS() const
638 if (!htmldefaultstyle_.empty())
640 docstring const mainfontCSS = font_.asCSS();
641 if (!mainfontCSS.empty())
643 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
644 mainfontCSS + from_ascii("\n}\n");
648 docstring InsetLayout::htmlstyle() const
650 if (!htmlstyle_.empty() && !htmlforcecss_)
652 if (htmldefaultstyle_.empty())
654 docstring retval = htmldefaultstyle_;
655 if (!htmlstyle_.empty())
656 retval += '\n' + htmlstyle_ + '\n';
660 void InsetLayout::readArgument(Lexer & lex)
662 Layout::latexarg arg;
663 arg.mandatory = false;
664 arg.autoinsert = false;
665 arg.insertcotext = false;
666 arg.insertonnewline = false;
668 bool finished = false;
669 arg.font = inherit_font;
670 arg.labelfont = inherit_font;
671 arg.is_toc_caption = false;
672 arg.free_spacing = false;
673 arg.passthru = PT_INHERITED;
674 arg.nodelims = false;
677 bool const postcmd = prefixIs(nr, "post:");
678 while (!finished && lex.isOK() && !error) {
680 string const tok = ascii_lowercase(lex.getString());
684 } else if (tok == "endargument") {
686 } else if (tok == "labelstring") {
688 arg.labelstring = lex.getDocString();
689 } else if (tok == "menustring") {
691 arg.menustring = lex.getDocString();
692 } else if (tok == "mandatory") {
694 arg.mandatory = lex.getBool();
695 } else if (tok == "autoinsert") {
697 arg.autoinsert = lex.getBool();
698 } else if (tok == "insertcotext") {
700 arg.insertcotext = lex.getBool();
701 } else if (tok == "insertonnewline") {
703 arg.insertonnewline = lex.getBool();
704 } else if (tok == "leftdelim") {
706 arg.ldelim = lex.getDocString();
707 arg.ldelim = subst(arg.ldelim,
708 from_ascii("<br/>"), from_ascii("\n"));
709 } else if (tok == "rightdelim") {
711 arg.rdelim = lex.getDocString();
712 arg.rdelim = subst(arg.rdelim,
713 from_ascii("<br/>"), from_ascii("\n"));
714 } else if (tok == "defaultarg") {
716 arg.defaultarg = lex.getDocString();
717 } else if (tok == "presetarg") {
719 arg.presetarg = lex.getDocString();
720 } else if (tok == "tooltip") {
722 arg.tooltip = lex.getDocString();
723 } else if (tok == "requires") {
725 arg.required = lex.getString();
726 } else if (tok == "decoration") {
728 arg.decoration = lex.getString();
729 } else if (tok == "newlinecmd") {
731 arg.newlinecmd = lex.getString();
732 } else if (tok == "font") {
733 arg.font = lyxRead(lex, arg.font);
734 } else if (tok == "labelfont") {
735 arg.labelfont = lyxRead(lex, arg.labelfont);
736 } else if (tok == "passthruchars") {
738 arg.pass_thru_chars = lex.getDocString();
739 } else if (tok == "passthru") {
741 docstring value = lex.getDocString();
742 if (value == "true" || value == "1")
743 arg.passthru = PT_TRUE;
744 else if (value == "false" || value == "0")
745 arg.passthru = PT_FALSE;
747 arg.passthru = PT_INHERITED;
748 } else if (tok == "istoccaption") {
750 arg.is_toc_caption = lex.getBool();
751 } else if (tok == "freespacing") {
753 arg.free_spacing = lex.getBool();
755 lex.printError("Unknown tag");
759 if (arg.labelstring.empty())
760 LYXERR0("Incomplete Argument definition!");
762 postcommandargs_[nr] = arg;
764 latexargs_[nr] = arg;
768 Layout::LaTeXArgMap InsetLayout::args() const
770 Layout::LaTeXArgMap args = latexargs_;
771 if (!postcommandargs_.empty())
772 args.insert(postcommandargs_.begin(), postcommandargs_.end());
777 int InsetLayout::optArgs() const
780 Layout::LaTeXArgMap const args = InsetLayout::args();
781 Layout::LaTeXArgMap::const_iterator it = args.begin();
782 for (; it != args.end(); ++it) {
783 if (!(*it).second.mandatory)
790 int InsetLayout::requiredArgs() const
793 Layout::LaTeXArgMap const args = InsetLayout::args();
794 Layout::LaTeXArgMap::const_iterator it = args.begin();
795 for (; it != args.end(); ++it) {
796 if ((*it).second.mandatory)