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,
135 IL_PARBREAKISNEWLINE,
147 LexerKeyword elementTags[] = {
148 { "addtotoc", IL_ADDTOTOC },
149 { "argument", IL_ARGUMENT },
150 { "babelpreamble", IL_BABELPREAMBLE },
151 { "bgcolor", IL_BGCOLOR },
152 { "contentaslabel", IL_CONTENTASLABEL },
153 { "copystyle", IL_COPYSTYLE },
154 { "counter", IL_COUNTER},
155 { "custompars", IL_CUSTOMPARS },
156 { "decoration", IL_DECORATION },
157 { "display", IL_DISPLAY },
158 { "docbookattr", IL_DOCBOOKATTR },
159 { "docbooktag", IL_DOCBOOKTAG },
160 { "editexternal", IL_EDITEXTERNAL },
162 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
164 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
165 { "forceltr", IL_FORCELTR },
166 { "forceownlines", IL_FORCEOWNLINES },
167 { "forceplain", IL_FORCEPLAIN },
168 { "freespacing", IL_FREESPACING },
169 { "htmlattr", IL_HTMLATTR },
170 { "htmlforcecss", IL_HTMLFORCECSS },
171 { "htmlinnerattr", IL_HTMLINNERATTR},
172 { "htmlinnertag", IL_HTMLINNERTAG},
173 { "htmlisblock", IL_HTMLISBLOCK},
174 { "htmllabel", IL_HTMLLABEL },
175 { "htmlpreamble", IL_HTMLPREAMBLE },
176 { "htmlstyle", IL_HTMLSTYLE },
177 { "htmltag", IL_HTMLTAG },
178 { "intoc", IL_INTOC },
179 { "istoccaption", IL_ISTOCCAPTION },
180 { "keepempty", IL_KEEPEMPTY },
181 { "labelfont", IL_LABELFONT },
182 { "labelstring", IL_LABELSTRING },
183 { "langpreamble", IL_LANGPREAMBLE },
184 { "latexname", IL_LATEXNAME },
185 { "latexparam", IL_LATEXPARAM },
186 { "latextype", IL_LATEXTYPE },
187 { "leftdelim", IL_LEFTDELIM },
188 { "lyxtype", IL_LYXTYPE },
189 { "menustring", IL_MENUSTRING },
190 { "multipar", IL_MULTIPAR },
191 { "needcprotect", IL_NEEDCPROTECT },
192 { "needmboxprotect", IL_NEEDMBOXPROTECT },
193 { "needprotect", IL_NEEDPROTECT },
194 { "newlinecmd", IL_NEWLINE_CMD },
195 { "obsoletedby", IL_OBSOLETEDBY },
196 { "parbreakignored", IL_PARBREAKIGNORED },
197 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
198 { "passthru", IL_PASSTHRU },
199 { "passthruchars", IL_PASSTHRU_CHARS },
200 { "preamble", IL_PREAMBLE },
201 { "refprefix", IL_REFPREFIX },
202 { "requires", IL_REQUIRES },
203 { "resetargs", IL_RESETARGS },
204 { "resetsfont", IL_RESETSFONT },
205 { "rightdelim", IL_RIGHTDELIM },
206 { "spellcheck", IL_SPELLCHECK }
209 lex.pushTable(elementTags);
211 labelfont_ = inherit_font;
212 bgcolor_ = Color_none;
214 // whether we've read the CustomPars or ForcePlain tag
215 // for issuing a warning in case MultiPars comes later
216 bool readCustomOrPlain = false;
219 while (!getout && lex.isOK()) {
222 case Lexer::LEX_UNDEF:
223 lex.printError("Unknown InsetLayout tag");
232 // Perhaps a more elegant way to deal with the next two would be the
233 // way this sort of thing is handled in Layout::read(), namely, by
236 // make sure that we have the right sort of name.
237 if (name_ != from_ascii("undefined")
238 && name_.substr(0,5) != from_ascii("Flex:")) {
239 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
240 "This one has the name `" << to_utf8(name_) << "'\n"
241 "Ignoring LyXType declaration.");
242 // this is not really a reason to abort
249 lyxtype_ = translateLyXType(lt);
250 if (lyxtype_ == NOLYXTYPE) {
251 LYXERR0("Unknown LyXType `" << lt << "'.");
252 // this is not really a reason to abort
256 if (lyxtype_ == CHARSTYLE) {
257 // by default, charstyles force the plain layout
266 latextype_ = translateLaTeXType(lt);
267 if (latextype_ == ILT_ERROR) {
268 LYXERR0("Unknown LaTeXType `" << lt << "'.");
269 // this is not really a reason to abort
283 decoration_ = translateDecoration(tmp);
290 latexparam_ = subst(tmp, """, "\"");
294 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
297 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
298 lex >> fixedwidthpreambleencoding_;
300 case IL_FORCE_LOCAL_FONT_SWITCH:
301 lex >> forcelocalfontswitch_;
305 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
309 labelfont_ = lyxRead(lex, inherit_font);
314 case IL_FORCEOWNLINES:
315 lex >> forceownlines_;
322 // the defaults for these depend upon multipar_
323 if (readCustomOrPlain)
324 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
325 "Previous value may be overwritten!");
326 readCustomOrPlain = false;
327 custompars_ = multipar_;
328 forceplain_ = !multipar_;
335 readCustomOrPlain = true;
339 readCustomOrPlain = true;
344 case IL_PASSTHRU_CHARS:
345 lex >> passthru_chars_;
350 case IL_PARBREAKIGNORED:
351 lex >> parbreakignored_;
353 case IL_PARBREAKISNEWLINE:
354 lex >> parbreakisnewline_;
365 case IL_NEEDCPROTECT:
366 lex >> needcprotect_;
368 case IL_NEEDMBOXPROTECT:
369 lex >> needmboxprotect_;
371 case IL_CONTENTASLABEL:
372 lex >> contentaslabel_;
375 // initialize with a known style
378 style = subst(style, '_', ' ');
380 // We don't want to apply the algorithm in DocumentClass::insetLayout()
381 // here. So we do it the long way.
382 TextClass::InsetLayouts::const_iterator it =
383 tclass.insetLayouts().find(style);
384 if (it != tclass.insetLayouts().end()) {
385 docstring const tmpname = name_;
386 this->operator=(it->second);
389 LYXERR0("Cannot copy unknown InsetLayout `"
390 << style << "' to InsetLayout `"
392 << "All InsetLayouts so far:");
393 TextClass::InsetLayouts::const_iterator lit =
394 tclass.insetLayouts().begin();
395 TextClass::InsetLayouts::const_iterator len =
396 tclass.insetLayouts().end();
397 for (; lit != len; ++lit)
398 lyxerr << lit->second.name() << "\n";
399 // this is not really a reason to abort
405 case IL_OBSOLETEDBY: {
408 style = subst(style, '_', ' ');
410 // We don't want to apply the algorithm in DocumentClass::insetLayout()
411 // here. So we do it the long way.
412 TextClass::InsetLayouts::const_iterator it =
413 tclass.insetLayouts().find(style);
414 if (it != tclass.insetLayouts().end()) {
415 docstring const tmpname = name_;
416 this->operator=(it->second);
418 if (obsoleted_by().empty())
419 obsoleted_by_ = style;
421 LYXERR0("Cannot replace InsetLayout `"
423 << "' with unknown InsetLayout `"
425 << "All InsetLayouts so far:");
426 TextClass::InsetLayouts::const_iterator lit =
427 tclass.insetLayouts().begin();
428 TextClass::InsetLayouts::const_iterator len =
429 tclass.insetLayouts().end();
430 for (; lit != len; ++lit)
431 lyxerr << lit->second.name() << "\n";
432 // this is not really a reason to abort
440 font_ = lyxRead(lex, inherit_font);
441 // If you want to define labelfont, you need to do so after
451 postcommandargs_.clear();
459 bgcolor_ = lcolor.getFromLyXName(tmp);
462 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
464 case IL_BABELPREAMBLE:
465 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
467 case IL_LANGPREAMBLE:
468 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
479 case IL_HTMLFORCECSS:
480 lex >> htmlforcecss_;
482 case IL_HTMLINNERTAG:
483 lex >> htmlinnertag_;
485 case IL_HTMLINNERATTR:
486 lex >> htmlinnerattr_;
495 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
497 case IL_HTMLPREAMBLE:
498 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
508 vector<string> const req
509 = getVectorFromString(lex.getString(true));
510 required_.insert(req.begin(), req.end());
524 add_to_toc_ = !toc_type_.empty();
526 case IL_ISTOCCAPTION:
527 lex >> is_toc_caption_;
529 case IL_EDITEXTERNAL:
530 lex >> edit_external_;
538 // Here add element to list if getout == true
542 // The label font is generally used as-is without
543 // any realization against a given context.
544 labelfont_.realize(sane_font);
551 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
553 if (compare_ascii_no_case(str, "charstyle") == 0)
554 return InsetLayout::CHARSTYLE;
555 if (compare_ascii_no_case(str, "custom") == 0)
556 return InsetLayout::CUSTOM;
557 if (compare_ascii_no_case(str, "end") == 0)
558 return InsetLayout::END;
559 if (compare_ascii_no_case(str, "standard") == 0)
560 return InsetLayout::STANDARD;
561 return InsetLayout::NOLYXTYPE;
565 string const & InsetLayout::htmltag() const
567 if (htmltag_.empty())
568 htmltag_ = multipar_ ? "div" : "span";
573 string const & InsetLayout::htmlattr() const
575 if (htmlattr_.empty())
576 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
581 string const & InsetLayout::htmlinnerattr() const
583 if (htmlinnerattr_.empty())
584 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
585 return htmlinnerattr_;
589 string InsetLayout::defaultCSSClass() const
591 if (!defaultcssclass_.empty())
592 return defaultcssclass_;
594 string n = to_utf8(name());
595 string::const_iterator it = n.begin();
596 string::const_iterator en = n.end();
597 for (; it != en; ++it) {
598 if (!isAlphaASCII(*it))
600 else if (isLower(*it))
605 // are there other characters we need to remove?
606 defaultcssclass_ = d;
607 return defaultcssclass_;
611 void InsetLayout::makeDefaultCSS() const
613 if (!htmldefaultstyle_.empty())
615 docstring const mainfontCSS = font_.asCSS();
616 if (!mainfontCSS.empty())
618 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
619 mainfontCSS + from_ascii("\n}\n");
623 docstring InsetLayout::htmlstyle() const
625 if (!htmlstyle_.empty() && !htmlforcecss_)
627 if (htmldefaultstyle_.empty())
629 docstring retval = htmldefaultstyle_;
630 if (!htmlstyle_.empty())
631 retval += '\n' + htmlstyle_ + '\n';
635 void InsetLayout::readArgument(Lexer & lex)
637 Layout::latexarg arg;
638 arg.mandatory = false;
639 arg.autoinsert = false;
640 arg.insertcotext = false;
641 arg.insertonnewline = false;
643 bool finished = false;
644 arg.font = inherit_font;
645 arg.labelfont = inherit_font;
646 arg.is_toc_caption = false;
647 arg.free_spacing = false;
648 arg.passthru = PT_INHERITED;
649 arg.nodelims = false;
652 bool const postcmd = prefixIs(nr, "post:");
653 while (!finished && lex.isOK() && !error) {
655 string const tok = ascii_lowercase(lex.getString());
659 } else if (tok == "endargument") {
661 } else if (tok == "labelstring") {
663 arg.labelstring = lex.getDocString();
664 } else if (tok == "menustring") {
666 arg.menustring = lex.getDocString();
667 } else if (tok == "mandatory") {
669 arg.mandatory = lex.getBool();
670 } else if (tok == "autoinsert") {
672 arg.autoinsert = lex.getBool();
673 } else if (tok == "insertcotext") {
675 arg.insertcotext = lex.getBool();
676 } else if (tok == "insertonnewline") {
678 arg.insertonnewline = lex.getBool();
679 } else if (tok == "leftdelim") {
681 arg.ldelim = lex.getDocString();
682 arg.ldelim = subst(arg.ldelim,
683 from_ascii("<br/>"), from_ascii("\n"));
684 } else if (tok == "rightdelim") {
686 arg.rdelim = lex.getDocString();
687 arg.rdelim = subst(arg.rdelim,
688 from_ascii("<br/>"), from_ascii("\n"));
689 } else if (tok == "defaultarg") {
691 arg.defaultarg = lex.getDocString();
692 } else if (tok == "presetarg") {
694 arg.presetarg = lex.getDocString();
695 } else if (tok == "tooltip") {
697 arg.tooltip = lex.getDocString();
698 } else if (tok == "requires") {
700 arg.required = lex.getString();
701 } else if (tok == "decoration") {
703 arg.decoration = lex.getString();
704 } else if (tok == "newlinecmd") {
706 arg.newlinecmd = lex.getString();
707 } else if (tok == "font") {
708 arg.font = lyxRead(lex, arg.font);
709 } else if (tok == "labelfont") {
710 arg.labelfont = lyxRead(lex, arg.labelfont);
711 } else if (tok == "passthruchars") {
713 arg.pass_thru_chars = lex.getDocString();
714 } else if (tok == "passthru") {
716 docstring value = lex.getDocString();
717 if (value == "true" || value == "1")
718 arg.passthru = PT_TRUE;
719 else if (value == "false" || value == "0")
720 arg.passthru = PT_FALSE;
722 arg.passthru = PT_INHERITED;
723 } else if (tok == "istoccaption") {
725 arg.is_toc_caption = lex.getBool();
726 } else if (tok == "freespacing") {
728 arg.free_spacing = lex.getBool();
730 lex.printError("Unknown tag");
734 if (arg.labelstring.empty())
735 LYXERR0("Incomplete Argument definition!");
737 postcommandargs_[nr] = arg;
739 latexargs_[nr] = arg;
743 Layout::LaTeXArgMap InsetLayout::args() const
745 Layout::LaTeXArgMap args = latexargs_;
746 if (!postcommandargs_.empty())
747 args.insert(postcommandargs_.begin(), postcommandargs_.end());
752 unsigned int InsetLayout::optArgs() const
755 Layout::LaTeXArgMap const args = InsetLayout::args();
756 Layout::LaTeXArgMap::const_iterator it = args.begin();
757 for (; it != args.end(); ++it) {
758 if (!(*it).second.mandatory)
765 unsigned int InsetLayout::requiredArgs() const
768 Layout::LaTeXArgMap const args = InsetLayout::args();
769 Layout::LaTeXArgMap::const_iterator it = args.begin();
770 for (; it != args.end(); ++it) {
771 if ((*it).second.mandatory)