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,
115 IL_DOCBOOKWRAPPERTAG,
116 IL_DOCBOOKWRAPPERTAGTYPE,
117 IL_DOCBOOKWRAPPERATTR,
139 IL_PARBREAKISNEWLINE,
151 LexerKeyword elementTags[] = {
152 { "addtotoc", IL_ADDTOTOC },
153 { "argument", IL_ARGUMENT },
154 { "babelpreamble", IL_BABELPREAMBLE },
155 { "bgcolor", IL_BGCOLOR },
156 { "contentaslabel", IL_CONTENTASLABEL },
157 { "copystyle", IL_COPYSTYLE },
158 { "counter", IL_COUNTER},
159 { "custompars", IL_CUSTOMPARS },
160 { "decoration", IL_DECORATION },
161 { "display", IL_DISPLAY },
162 { "docbookattr", IL_DOCBOOKATTR },
163 { "docbooktag", IL_DOCBOOKTAG },
164 { "docbooktagtype", IL_DOCBOOKTAGTYPE },
165 { "docbookwrapperattr", IL_DOCBOOKWRAPPERATTR },
166 { "docbookwrappertag", IL_DOCBOOKWRAPPERTAG },
167 { "docbookwrappertagtype", IL_DOCBOOKWRAPPERTAGTYPE },
168 { "editexternal", IL_EDITEXTERNAL },
170 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
172 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
173 { "forceltr", IL_FORCELTR },
174 { "forceownlines", IL_FORCEOWNLINES },
175 { "forceplain", IL_FORCEPLAIN },
176 { "freespacing", IL_FREESPACING },
177 { "htmlattr", IL_HTMLATTR },
178 { "htmlforcecss", IL_HTMLFORCECSS },
179 { "htmlinnerattr", IL_HTMLINNERATTR},
180 { "htmlinnertag", IL_HTMLINNERTAG},
181 { "htmlisblock", IL_HTMLISBLOCK},
182 { "htmllabel", IL_HTMLLABEL },
183 { "htmlpreamble", IL_HTMLPREAMBLE },
184 { "htmlstyle", IL_HTMLSTYLE },
185 { "htmltag", IL_HTMLTAG },
186 { "intoc", IL_INTOC },
187 { "istoccaption", IL_ISTOCCAPTION },
188 { "keepempty", IL_KEEPEMPTY },
189 { "labelfont", IL_LABELFONT },
190 { "labelstring", IL_LABELSTRING },
191 { "langpreamble", IL_LANGPREAMBLE },
192 { "latexname", IL_LATEXNAME },
193 { "latexparam", IL_LATEXPARAM },
194 { "latextype", IL_LATEXTYPE },
195 { "leftdelim", IL_LEFTDELIM },
196 { "lyxtype", IL_LYXTYPE },
197 { "menustring", IL_MENUSTRING },
198 { "multipar", IL_MULTIPAR },
199 { "needcprotect", IL_NEEDCPROTECT },
200 { "needmboxprotect", IL_NEEDMBOXPROTECT },
201 { "needprotect", IL_NEEDPROTECT },
202 { "newlinecmd", IL_NEWLINE_CMD },
203 { "obsoletedby", IL_OBSOLETEDBY },
204 { "parbreakignored", IL_PARBREAKIGNORED },
205 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
206 { "passthru", IL_PASSTHRU },
207 { "passthruchars", IL_PASSTHRU_CHARS },
208 { "preamble", IL_PREAMBLE },
209 { "refprefix", IL_REFPREFIX },
210 { "requires", IL_REQUIRES },
211 { "resetargs", IL_RESETARGS },
212 { "resetsfont", IL_RESETSFONT },
213 { "rightdelim", IL_RIGHTDELIM },
214 { "spellcheck", IL_SPELLCHECK }
217 lex.pushTable(elementTags);
219 labelfont_ = inherit_font;
220 bgcolor_ = Color_none;
222 // whether we've read the CustomPars or ForcePlain tag
223 // for issuing a warning in case MultiPars comes later
224 bool readCustomOrPlain = false;
227 while (!getout && lex.isOK()) {
230 case Lexer::LEX_UNDEF:
231 lex.printError("Unknown InsetLayout tag");
240 // Perhaps a more elegant way to deal with the next two would be the
241 // way this sort of thing is handled in Layout::read(), namely, by
244 // make sure that we have the right sort of name.
245 if (name_ != from_ascii("undefined")
246 && name_.substr(0,5) != from_ascii("Flex:")) {
247 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
248 "This one has the name `" << to_utf8(name_) << "'\n"
249 "Ignoring LyXType declaration.");
250 // this is not really a reason to abort
257 lyxtype_ = translateLyXType(lt);
258 if (lyxtype_ == NOLYXTYPE) {
259 LYXERR0("Unknown LyXType `" << lt << "'.");
260 // this is not really a reason to abort
264 if (lyxtype_ == CHARSTYLE) {
265 // by default, charstyles force the plain layout
274 latextype_ = translateLaTeXType(lt);
275 if (latextype_ == ILT_ERROR) {
276 LYXERR0("Unknown LaTeXType `" << lt << "'.");
277 // this is not really a reason to abort
291 decoration_ = translateDecoration(tmp);
298 latexparam_ = subst(tmp, """, "\"");
302 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
305 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
306 lex >> fixedwidthpreambleencoding_;
308 case IL_FORCE_LOCAL_FONT_SWITCH:
309 lex >> forcelocalfontswitch_;
313 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
317 labelfont_ = lyxRead(lex, inherit_font);
322 case IL_FORCEOWNLINES:
323 lex >> forceownlines_;
330 // the defaults for these depend upon multipar_
331 if (readCustomOrPlain)
332 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
333 "Previous value may be overwritten!");
334 readCustomOrPlain = false;
335 custompars_ = multipar_;
336 forceplain_ = !multipar_;
343 readCustomOrPlain = true;
347 readCustomOrPlain = true;
352 case IL_PASSTHRU_CHARS:
353 lex >> passthru_chars_;
358 case IL_PARBREAKIGNORED:
359 lex >> parbreakignored_;
361 case IL_PARBREAKISNEWLINE:
362 lex >> parbreakisnewline_;
373 case IL_NEEDCPROTECT:
374 lex >> needcprotect_;
376 case IL_NEEDMBOXPROTECT:
377 lex >> needmboxprotect_;
379 case IL_CONTENTASLABEL:
380 lex >> contentaslabel_;
383 // initialize with a known style
386 style = subst(style, '_', ' ');
388 // We don't want to apply the algorithm in DocumentClass::insetLayout()
389 // here. So we do it the long way.
390 TextClass::InsetLayouts::const_iterator it =
391 tclass.insetLayouts().find(style);
392 if (it != tclass.insetLayouts().end()) {
393 docstring const tmpname = name_;
394 this->operator=(it->second);
397 LYXERR0("Cannot copy unknown InsetLayout `"
398 << style << "' to InsetLayout `"
400 << "All InsetLayouts so far:");
401 TextClass::InsetLayouts::const_iterator lit =
402 tclass.insetLayouts().begin();
403 TextClass::InsetLayouts::const_iterator len =
404 tclass.insetLayouts().end();
405 for (; lit != len; ++lit)
406 lyxerr << lit->second.name() << "\n";
407 // this is not really a reason to abort
413 case IL_OBSOLETEDBY: {
416 style = subst(style, '_', ' ');
418 // We don't want to apply the algorithm in DocumentClass::insetLayout()
419 // here. So we do it the long way.
420 TextClass::InsetLayouts::const_iterator it =
421 tclass.insetLayouts().find(style);
422 if (it != tclass.insetLayouts().end()) {
423 docstring const tmpname = name_;
424 this->operator=(it->second);
426 if (obsoleted_by().empty())
427 obsoleted_by_ = style;
429 LYXERR0("Cannot replace InsetLayout `"
431 << "' with unknown InsetLayout `"
433 << "All InsetLayouts so far:");
434 TextClass::InsetLayouts::const_iterator lit =
435 tclass.insetLayouts().begin();
436 TextClass::InsetLayouts::const_iterator len =
437 tclass.insetLayouts().end();
438 for (; lit != len; ++lit)
439 lyxerr << lit->second.name() << "\n";
440 // this is not really a reason to abort
448 font_ = lyxRead(lex, inherit_font);
449 // If you want to define labelfont, you need to do so after
459 postcommandargs_.clear();
467 bgcolor_ = lcolor.getFromLyXName(tmp);
470 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
472 case IL_BABELPREAMBLE:
473 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
475 case IL_LANGPREAMBLE:
476 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
487 case IL_HTMLFORCECSS:
488 lex >> htmlforcecss_;
490 case IL_HTMLINNERTAG:
491 lex >> htmlinnertag_;
493 case IL_HTMLINNERATTR:
494 lex >> htmlinnerattr_;
503 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
505 case IL_HTMLPREAMBLE:
506 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
511 case IL_DOCBOOKTAGTYPE:
512 lex >> docbooktagtype_;
517 case IL_DOCBOOKWRAPPERTAG:
518 lex >> docbookwrappertag_;
520 case IL_DOCBOOKWRAPPERTAGTYPE:
521 lex >> docbookwrappertagtype_;
523 case IL_DOCBOOKWRAPPERATTR:
524 lex >> docbookwrapperattr_;
528 vector<string> const req
529 = getVectorFromString(lex.getString(true));
530 required_.insert(req.begin(), req.end());
544 add_to_toc_ = !toc_type_.empty();
546 case IL_ISTOCCAPTION:
547 lex >> is_toc_caption_;
549 case IL_EDITEXTERNAL:
550 lex >> edit_external_;
558 // Here add element to list if getout == true
562 // The label font is generally used as-is without
563 // any realization against a given context.
564 labelfont_.realize(sane_font);
571 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
573 if (compare_ascii_no_case(str, "charstyle") == 0)
574 return InsetLayout::CHARSTYLE;
575 if (compare_ascii_no_case(str, "custom") == 0)
576 return InsetLayout::CUSTOM;
577 if (compare_ascii_no_case(str, "end") == 0)
578 return InsetLayout::END;
579 if (compare_ascii_no_case(str, "standard") == 0)
580 return InsetLayout::STANDARD;
581 return InsetLayout::NOLYXTYPE;
585 string const & InsetLayout::htmltag() const
587 if (htmltag_.empty())
588 htmltag_ = multipar_ ? "div" : "span";
593 string const & InsetLayout::htmlattr() const
595 if (htmlattr_.empty())
596 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
601 string const & InsetLayout::htmlinnerattr() const
603 if (htmlinnerattr_.empty())
604 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
605 return htmlinnerattr_;
609 string InsetLayout::defaultCSSClass() const
611 if (!defaultcssclass_.empty())
612 return defaultcssclass_;
614 string n = to_utf8(name());
615 string::const_iterator it = n.begin();
616 string::const_iterator en = n.end();
617 for (; it != en; ++it) {
618 if (!isAlphaASCII(*it))
620 else if (isLower(*it))
625 // are there other characters we need to remove?
626 defaultcssclass_ = d;
627 return defaultcssclass_;
631 void InsetLayout::makeDefaultCSS() const
633 if (!htmldefaultstyle_.empty())
635 docstring const mainfontCSS = font_.asCSS();
636 if (!mainfontCSS.empty())
638 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
639 mainfontCSS + from_ascii("\n}\n");
643 docstring InsetLayout::htmlstyle() const
645 if (!htmlstyle_.empty() && !htmlforcecss_)
647 if (htmldefaultstyle_.empty())
649 docstring retval = htmldefaultstyle_;
650 if (!htmlstyle_.empty())
651 retval += '\n' + htmlstyle_ + '\n';
655 void InsetLayout::readArgument(Lexer & lex)
657 Layout::latexarg arg;
658 arg.mandatory = false;
659 arg.autoinsert = false;
660 arg.insertcotext = false;
661 arg.insertonnewline = false;
663 bool finished = false;
664 arg.font = inherit_font;
665 arg.labelfont = inherit_font;
666 arg.is_toc_caption = false;
667 arg.free_spacing = false;
668 arg.passthru = PT_INHERITED;
669 arg.nodelims = false;
672 bool const postcmd = prefixIs(nr, "post:");
673 while (!finished && lex.isOK() && !error) {
675 string const tok = ascii_lowercase(lex.getString());
679 } else if (tok == "endargument") {
681 } else if (tok == "labelstring") {
683 arg.labelstring = lex.getDocString();
684 } else if (tok == "menustring") {
686 arg.menustring = lex.getDocString();
687 } else if (tok == "mandatory") {
689 arg.mandatory = lex.getBool();
690 } else if (tok == "autoinsert") {
692 arg.autoinsert = lex.getBool();
693 } else if (tok == "insertcotext") {
695 arg.insertcotext = lex.getBool();
696 } else if (tok == "insertonnewline") {
698 arg.insertonnewline = lex.getBool();
699 } else if (tok == "leftdelim") {
701 arg.ldelim = lex.getDocString();
702 arg.ldelim = subst(arg.ldelim,
703 from_ascii("<br/>"), from_ascii("\n"));
704 } else if (tok == "rightdelim") {
706 arg.rdelim = lex.getDocString();
707 arg.rdelim = subst(arg.rdelim,
708 from_ascii("<br/>"), from_ascii("\n"));
709 } else if (tok == "defaultarg") {
711 arg.defaultarg = lex.getDocString();
712 } else if (tok == "presetarg") {
714 arg.presetarg = lex.getDocString();
715 } else if (tok == "tooltip") {
717 arg.tooltip = lex.getDocString();
718 } else if (tok == "requires") {
720 arg.required = lex.getString();
721 } else if (tok == "decoration") {
723 arg.decoration = lex.getString();
724 } else if (tok == "newlinecmd") {
726 arg.newlinecmd = lex.getString();
727 } else if (tok == "font") {
728 arg.font = lyxRead(lex, arg.font);
729 } else if (tok == "labelfont") {
730 arg.labelfont = lyxRead(lex, arg.labelfont);
731 } else if (tok == "passthruchars") {
733 arg.pass_thru_chars = lex.getDocString();
734 } else if (tok == "passthru") {
736 docstring value = lex.getDocString();
737 if (value == "true" || value == "1")
738 arg.passthru = PT_TRUE;
739 else if (value == "false" || value == "0")
740 arg.passthru = PT_FALSE;
742 arg.passthru = PT_INHERITED;
743 } else if (tok == "istoccaption") {
745 arg.is_toc_caption = lex.getBool();
746 } else if (tok == "freespacing") {
748 arg.free_spacing = lex.getBool();
750 lex.printError("Unknown tag");
754 if (arg.labelstring.empty())
755 LYXERR0("Incomplete Argument definition!");
757 postcommandargs_[nr] = arg;
759 latexargs_[nr] = arg;
763 Layout::LaTeXArgMap InsetLayout::args() const
765 Layout::LaTeXArgMap args = latexargs_;
766 if (!postcommandargs_.empty())
767 args.insert(postcommandargs_.begin(), postcommandargs_.end());
772 int InsetLayout::optArgs() const
775 Layout::LaTeXArgMap const args = InsetLayout::args();
776 Layout::LaTeXArgMap::const_iterator it = args.begin();
777 for (; it != args.end(); ++it) {
778 if (!(*it).second.mandatory)
785 int InsetLayout::requiredArgs() const
788 Layout::LaTeXArgMap const args = InsetLayout::args();
789 Layout::LaTeXArgMap::const_iterator it = args.begin();
790 for (; it != args.end(); ++it) {
791 if ((*it).second.mandatory)