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)
95 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
97 IL_FORCE_LOCAL_FONT_SWITCH,
132 IL_PARBREAKISNEWLINE,
144 LexerKeyword elementTags[] = {
145 { "addtotoc", IL_ADDTOTOC },
146 { "argument", IL_ARGUMENT },
147 { "babelpreamble", IL_BABELPREAMBLE },
148 { "bgcolor", IL_BGCOLOR },
149 { "contentaslabel", IL_CONTENTASLABEL },
150 { "copystyle", IL_COPYSTYLE },
151 { "counter", IL_COUNTER},
152 { "custompars", IL_CUSTOMPARS },
153 { "decoration", IL_DECORATION },
154 { "display", IL_DISPLAY },
155 { "editexternal", IL_EDITEXTERNAL },
157 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
159 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
160 { "forceltr", IL_FORCELTR },
161 { "forceownlines", IL_FORCEOWNLINES },
162 { "forceplain", IL_FORCEPLAIN },
163 { "freespacing", IL_FREESPACING },
164 { "htmlattr", IL_HTMLATTR },
165 { "htmlforcecss", IL_HTMLFORCECSS },
166 { "htmlinnerattr", IL_HTMLINNERATTR},
167 { "htmlinnertag", IL_HTMLINNERTAG},
168 { "htmlisblock", IL_HTMLISBLOCK},
169 { "htmllabel", IL_HTMLLABEL },
170 { "htmlpreamble", IL_HTMLPREAMBLE },
171 { "htmlstyle", IL_HTMLSTYLE },
172 { "htmltag", IL_HTMLTAG },
173 { "intoc", IL_INTOC },
174 { "istoccaption", IL_ISTOCCAPTION },
175 { "keepempty", IL_KEEPEMPTY },
176 { "labelfont", IL_LABELFONT },
177 { "labelstring", IL_LABELSTRING },
178 { "langpreamble", IL_LANGPREAMBLE },
179 { "latexname", IL_LATEXNAME },
180 { "latexparam", IL_LATEXPARAM },
181 { "latextype", IL_LATEXTYPE },
182 { "leftdelim", IL_LEFTDELIM },
183 { "lyxtype", IL_LYXTYPE },
184 { "menustring", IL_MENUSTRING },
185 { "multipar", IL_MULTIPAR },
186 { "needcprotect", IL_NEEDCPROTECT },
187 { "needmboxprotect", IL_NEEDMBOXPROTECT },
188 { "needprotect", IL_NEEDPROTECT },
189 { "newlinecmd", IL_NEWLINE_CMD },
190 { "obsoletedby", IL_OBSOLETEDBY },
191 { "parbreakignored", IL_PARBREAKIGNORED },
192 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
193 { "passthru", IL_PASSTHRU },
194 { "passthruchars", IL_PASSTHRU_CHARS },
195 { "preamble", IL_PREAMBLE },
196 { "refprefix", IL_REFPREFIX },
197 { "requires", IL_REQUIRES },
198 { "resetargs", IL_RESETARGS },
199 { "resetsfont", IL_RESETSFONT },
200 { "rightdelim", IL_RIGHTDELIM },
201 { "spellcheck", IL_SPELLCHECK }
204 lex.pushTable(elementTags);
206 labelfont_ = inherit_font;
207 bgcolor_ = Color_none;
209 // whether we've read the CustomPars or ForcePlain tag
210 // for issuing a warning in case MultiPars comes later
211 bool readCustomOrPlain = false;
214 while (!getout && lex.isOK()) {
217 case Lexer::LEX_UNDEF:
218 lex.printError("Unknown InsetLayout tag");
225 // Perhaps a more elegant way to deal with the next two would be the
226 // way this sort of thing is handled in Layout::read(), namely, by
229 // make sure that we have the right sort of name.
230 if (name_ != from_ascii("undefined")
231 && name_.substr(0,5) != from_ascii("Flex:")) {
232 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
233 "This one has the name `" << to_utf8(name_) << "'\n"
234 "Ignoring LyXType declaration.");
239 lyxtype_ = translateLyXType(lt);
240 if (lyxtype_ == NOLYXTYPE)
241 LYXERR0("Unknown LyXType `" << lt << "'.");
242 if (lyxtype_ == CHARSTYLE) {
243 // by default, charstyles force the plain layout
252 latextype_ = translateLaTeXType(lt);
253 if (latextype_ == ILT_ERROR)
254 LYXERR0("Unknown LaTeXType `" << lt << "'.");
265 decoration_ = translateDecoration(tmp);
272 latexparam_ = subst(tmp, """, "\"");
276 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
279 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
280 lex >> fixedwidthpreambleencoding_;
282 case IL_FORCE_LOCAL_FONT_SWITCH:
283 lex >> forcelocalfontswitch_;
287 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
291 labelfont_ = lyxRead(lex, inherit_font);
296 case IL_FORCEOWNLINES:
297 lex >> forceownlines_;
304 // the defaults for these depend upon multipar_
305 if (readCustomOrPlain)
306 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
307 "Previous value may be overwritten!");
308 readCustomOrPlain = false;
309 custompars_ = multipar_;
310 forceplain_ = !multipar_;
317 readCustomOrPlain = true;
321 readCustomOrPlain = true;
326 case IL_PASSTHRU_CHARS:
327 lex >> passthru_chars_;
332 case IL_PARBREAKIGNORED:
333 lex >> parbreakignored_;
335 case IL_PARBREAKISNEWLINE:
336 lex >> parbreakisnewline_;
347 case IL_NEEDCPROTECT:
348 lex >> needcprotect_;
350 case IL_NEEDMBOXPROTECT:
351 lex >> needmboxprotect_;
353 case IL_CONTENTASLABEL:
354 lex >> contentaslabel_;
357 // initialize with a known style
360 style = subst(style, '_', ' ');
362 // We don't want to apply the algorithm in DocumentClass::insetLayout()
363 // here. So we do it the long way.
364 TextClass::InsetLayouts::const_iterator it =
365 tclass.insetLayouts().find(style);
366 if (it != tclass.insetLayouts().end()) {
367 docstring const tmpname = name_;
368 this->operator=(it->second);
371 LYXERR0("Cannot copy unknown InsetLayout `"
372 << style << "' to InsetLayout `"
374 << "All InsetLayouts so far:");
375 TextClass::InsetLayouts::const_iterator lit =
376 tclass.insetLayouts().begin();
377 TextClass::InsetLayouts::const_iterator len =
378 tclass.insetLayouts().end();
379 for (; lit != len; ++lit)
380 lyxerr << lit->second.name() << "\n";
384 case IL_OBSOLETEDBY: {
387 style = subst(style, '_', ' ');
389 // We don't want to apply the algorithm in DocumentClass::insetLayout()
390 // here. So we do it the long way.
391 TextClass::InsetLayouts::const_iterator it =
392 tclass.insetLayouts().find(style);
393 if (it != tclass.insetLayouts().end()) {
394 docstring const tmpname = name_;
395 this->operator=(it->second);
397 if (obsoleted_by().empty())
398 obsoleted_by_ = style;
400 LYXERR0("Cannot replace InsetLayout `"
402 << "' with unknown InsetLayout `"
404 << "All InsetLayouts so far:");
405 TextClass::InsetLayouts::const_iterator lit =
406 tclass.insetLayouts().begin();
407 TextClass::InsetLayouts::const_iterator len =
408 tclass.insetLayouts().end();
409 for (; lit != len; ++lit)
410 lyxerr << lit->second.name() << "\n";
416 font_ = lyxRead(lex, inherit_font);
417 // If you want to define labelfont, you need to do so after
427 postcommandargs_.clear();
435 bgcolor_ = lcolor.getFromLyXName(tmp);
438 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
440 case IL_BABELPREAMBLE:
441 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
443 case IL_LANGPREAMBLE:
444 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
455 case IL_HTMLFORCECSS:
456 lex >> htmlforcecss_;
458 case IL_HTMLINNERTAG:
459 lex >> htmlinnertag_;
461 case IL_HTMLINNERATTR:
462 lex >> htmlinnerattr_;
471 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
473 case IL_HTMLPREAMBLE:
474 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
478 vector<string> const req
479 = getVectorFromString(lex.getString(true));
480 requires_.insert(req.begin(), req.end());
494 add_to_toc_ = !toc_type_.empty();
496 case IL_ISTOCCAPTION:
497 lex >> is_toc_caption_;
499 case IL_EDITEXTERNAL:
500 lex >> edit_external_;
508 // Here add element to list if getout == true
512 // The label font is generally used as-is without
513 // any realization against a given context.
514 labelfont_.realize(sane_font);
521 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
523 if (compare_ascii_no_case(str, "charstyle") == 0)
524 return InsetLayout::CHARSTYLE;
525 if (compare_ascii_no_case(str, "custom") == 0)
526 return InsetLayout::CUSTOM;
527 if (compare_ascii_no_case(str, "element") == 0)
528 return InsetLayout::ELEMENT;
529 if (compare_ascii_no_case(str, "end") == 0)
530 return InsetLayout::END;
531 if (compare_ascii_no_case(str, "standard") == 0)
532 return InsetLayout::STANDARD;
533 return InsetLayout::NOLYXTYPE;
537 string const & InsetLayout::htmltag() const
539 if (htmltag_.empty())
540 htmltag_ = multipar_ ? "div" : "span";
545 string const & InsetLayout::htmlattr() const
547 if (htmlattr_.empty())
548 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
553 string const & InsetLayout::htmlinnerattr() const
555 if (htmlinnerattr_.empty())
556 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
557 return htmlinnerattr_;
561 string InsetLayout::defaultCSSClass() const
563 if (!defaultcssclass_.empty())
564 return defaultcssclass_;
566 string n = to_utf8(name());
567 string::const_iterator it = n.begin();
568 string::const_iterator en = n.end();
569 for (; it != en; ++it) {
570 if (!isAlphaASCII(*it))
572 else if (isLower(*it))
577 // are there other characters we need to remove?
578 defaultcssclass_ = d;
579 return defaultcssclass_;
583 void InsetLayout::makeDefaultCSS() const
585 if (!htmldefaultstyle_.empty())
587 docstring const mainfontCSS = font_.asCSS();
588 if (!mainfontCSS.empty())
590 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
591 mainfontCSS + from_ascii("\n}\n");
595 docstring InsetLayout::htmlstyle() const
597 if (!htmlstyle_.empty() && !htmlforcecss_)
599 if (htmldefaultstyle_.empty())
601 docstring retval = htmldefaultstyle_;
602 if (!htmlstyle_.empty())
603 retval += '\n' + htmlstyle_ + '\n';
607 void InsetLayout::readArgument(Lexer & lex)
609 Layout::latexarg arg;
610 arg.mandatory = false;
611 arg.autoinsert = false;
612 arg.insertcotext = false;
613 arg.insertonnewline = false;
615 bool finished = false;
616 arg.font = inherit_font;
617 arg.labelfont = inherit_font;
618 arg.is_toc_caption = false;
619 arg.free_spacing = false;
620 arg.passthru = PT_INHERITED;
621 arg.nodelims = false;
624 bool const postcmd = prefixIs(nr, "post:");
625 while (!finished && lex.isOK() && !error) {
627 string const tok = ascii_lowercase(lex.getString());
631 } else if (tok == "endargument") {
633 } else if (tok == "labelstring") {
635 arg.labelstring = lex.getDocString();
636 } else if (tok == "menustring") {
638 arg.menustring = lex.getDocString();
639 } else if (tok == "mandatory") {
641 arg.mandatory = lex.getBool();
642 } else if (tok == "autoinsert") {
644 arg.autoinsert = lex.getBool();
645 } else if (tok == "insertcotext") {
647 arg.insertcotext = lex.getBool();
648 } else if (tok == "insertonnewline") {
650 arg.insertonnewline = lex.getBool();
651 } else if (tok == "leftdelim") {
653 arg.ldelim = lex.getDocString();
654 arg.ldelim = subst(arg.ldelim,
655 from_ascii("<br/>"), from_ascii("\n"));
656 } else if (tok == "rightdelim") {
658 arg.rdelim = lex.getDocString();
659 arg.rdelim = subst(arg.rdelim,
660 from_ascii("<br/>"), from_ascii("\n"));
661 } else if (tok == "defaultarg") {
663 arg.defaultarg = lex.getDocString();
664 } else if (tok == "presetarg") {
666 arg.presetarg = lex.getDocString();
667 } else if (tok == "tooltip") {
669 arg.tooltip = lex.getDocString();
670 } else if (tok == "requires") {
672 arg.requires = lex.getString();
673 } else if (tok == "decoration") {
675 arg.decoration = lex.getString();
676 } else if (tok == "newlinecmd") {
678 arg.newlinecmd = lex.getString();
679 } else if (tok == "font") {
680 arg.font = lyxRead(lex, arg.font);
681 } else if (tok == "labelfont") {
682 arg.labelfont = lyxRead(lex, arg.labelfont);
683 } else if (tok == "passthruchars") {
685 arg.pass_thru_chars = lex.getDocString();
686 } else if (tok == "passthru") {
688 docstring value = lex.getDocString();
689 if (value == "true" || value == "1")
690 arg.passthru = PT_TRUE;
691 else if (value == "false" || value == "0")
692 arg.passthru = PT_FALSE;
694 arg.passthru = PT_INHERITED;
695 } else if (tok == "istoccaption") {
697 arg.is_toc_caption = lex.getBool();
698 } else if (tok == "freespacing") {
700 arg.free_spacing = lex.getBool();
702 lex.printError("Unknown tag");
706 if (arg.labelstring.empty())
707 LYXERR0("Incomplete Argument definition!");
709 postcommandargs_[nr] = arg;
711 latexargs_[nr] = arg;
715 Layout::LaTeXArgMap InsetLayout::args() const
717 Layout::LaTeXArgMap args = latexargs_;
718 if (!postcommandargs_.empty())
719 args.insert(postcommandargs_.begin(), postcommandargs_.end());
724 unsigned int InsetLayout::optArgs() const
727 Layout::LaTeXArgMap const args = InsetLayout::args();
728 Layout::LaTeXArgMap::const_iterator it = args.begin();
729 for (; it != args.end(); ++it) {
730 if (!(*it).second.mandatory)
737 unsigned int InsetLayout::requiredArgs() const
740 Layout::LaTeXArgMap const args = InsetLayout::args();
741 Layout::LaTeXArgMap::const_iterator it = args.begin();
742 for (; it != args.end(); ++it) {
743 if ((*it).second.mandatory)