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 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,
131 IL_PARBREAKISNEWLINE,
143 LexerKeyword elementTags[] = {
144 { "addtotoc", IL_ADDTOTOC },
145 { "argument", IL_ARGUMENT },
146 { "babelpreamble", IL_BABELPREAMBLE },
147 { "bgcolor", IL_BGCOLOR },
148 { "contentaslabel", IL_CONTENTASLABEL },
149 { "copystyle", IL_COPYSTYLE },
150 { "counter", IL_COUNTER},
151 { "custompars", IL_CUSTOMPARS },
152 { "decoration", IL_DECORATION },
153 { "display", IL_DISPLAY },
154 { "editexternal", IL_EDITEXTERNAL },
156 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
158 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
159 { "forceltr", IL_FORCELTR },
160 { "forceownlines", IL_FORCEOWNLINES },
161 { "forceplain", IL_FORCEPLAIN },
162 { "freespacing", IL_FREESPACING },
163 { "htmlattr", IL_HTMLATTR },
164 { "htmlforcecss", IL_HTMLFORCECSS },
165 { "htmlinnerattr", IL_HTMLINNERATTR},
166 { "htmlinnertag", IL_HTMLINNERTAG},
167 { "htmlisblock", IL_HTMLISBLOCK},
168 { "htmllabel", IL_HTMLLABEL },
169 { "htmlpreamble", IL_HTMLPREAMBLE },
170 { "htmlstyle", IL_HTMLSTYLE },
171 { "htmltag", IL_HTMLTAG },
172 { "intoc", IL_INTOC },
173 { "istoccaption", IL_ISTOCCAPTION },
174 { "keepempty", IL_KEEPEMPTY },
175 { "labelfont", IL_LABELFONT },
176 { "labelstring", IL_LABELSTRING },
177 { "langpreamble", IL_LANGPREAMBLE },
178 { "latexname", IL_LATEXNAME },
179 { "latexparam", IL_LATEXPARAM },
180 { "latextype", IL_LATEXTYPE },
181 { "leftdelim", IL_LEFTDELIM },
182 { "lyxtype", IL_LYXTYPE },
183 { "menustring", IL_MENUSTRING },
184 { "multipar", IL_MULTIPAR },
185 { "needcprotect", IL_NEEDCPROTECT },
186 { "needmboxprotect", IL_NEEDMBOXPROTECT },
187 { "needprotect", IL_NEEDPROTECT },
188 { "newlinecmd", IL_NEWLINE_CMD },
189 { "obsoletedby", IL_OBSOLETEDBY },
190 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
191 { "passthru", IL_PASSTHRU },
192 { "passthruchars", IL_PASSTHRU_CHARS },
193 { "preamble", IL_PREAMBLE },
194 { "refprefix", IL_REFPREFIX },
195 { "requires", IL_REQUIRES },
196 { "resetargs", IL_RESETARGS },
197 { "resetsfont", IL_RESETSFONT },
198 { "rightdelim", IL_RIGHTDELIM },
199 { "spellcheck", IL_SPELLCHECK }
202 lex.pushTable(elementTags);
204 labelfont_ = inherit_font;
205 bgcolor_ = Color_none;
207 // whether we've read the CustomPars or ForcePlain tag
208 // for issuing a warning in case MultiPars comes later
209 bool readCustomOrPlain = false;
212 while (!getout && lex.isOK()) {
215 case Lexer::LEX_UNDEF:
216 lex.printError("Unknown InsetLayout tag");
223 // Perhaps a more elegant way to deal with the next two would be the
224 // way this sort of thing is handled in Layout::read(), namely, by
227 // make sure that we have the right sort of name.
228 if (name_ != from_ascii("undefined")
229 && name_.substr(0,5) != from_ascii("Flex:")) {
230 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
231 "This one has the name `" << to_utf8(name_) << "'\n"
232 "Ignoring LyXType declaration.");
237 lyxtype_ = translateLyXType(lt);
238 if (lyxtype_ == NOLYXTYPE)
239 LYXERR0("Unknown LyXType `" << lt << "'.");
240 if (lyxtype_ == CHARSTYLE) {
241 // by default, charstyles force the plain layout
250 latextype_ = translateLaTeXType(lt);
251 if (latextype_ == ILT_ERROR)
252 LYXERR0("Unknown LaTeXType `" << lt << "'.");
263 decoration_ = translateDecoration(tmp);
270 latexparam_ = subst(tmp, """, "\"");
274 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
277 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
278 lex >> fixedwidthpreambleencoding_;
280 case IL_FORCE_LOCAL_FONT_SWITCH:
281 lex >> forcelocalfontswitch_;
285 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
289 labelfont_ = lyxRead(lex, inherit_font);
294 case IL_FORCEOWNLINES:
295 lex >> forceownlines_;
302 // the defaults for these depend upon multipar_
303 if (readCustomOrPlain)
304 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
305 "Previous value may be overwritten!");
306 readCustomOrPlain = false;
307 custompars_ = multipar_;
308 forceplain_ = !multipar_;
315 readCustomOrPlain = true;
319 readCustomOrPlain = true;
324 case IL_PASSTHRU_CHARS:
325 lex >> passthru_chars_;
330 case IL_PARBREAKISNEWLINE:
331 lex >> parbreakisnewline_;
342 case IL_NEEDCPROTECT:
343 lex >> needcprotect_;
345 case IL_NEEDMBOXPROTECT:
346 lex >> needmboxprotect_;
348 case IL_CONTENTASLABEL:
349 lex >> contentaslabel_;
352 // initialize with a known style
355 style = subst(style, '_', ' ');
357 // We don't want to apply the algorithm in DocumentClass::insetLayout()
358 // here. So we do it the long way.
359 TextClass::InsetLayouts::const_iterator it =
360 tclass.insetLayouts().find(style);
361 if (it != tclass.insetLayouts().end()) {
362 docstring const tmpname = name_;
363 this->operator=(it->second);
366 LYXERR0("Cannot copy unknown InsetLayout `"
367 << style << "' to InsetLayout `"
369 << "All InsetLayouts so far:");
370 TextClass::InsetLayouts::const_iterator lit =
371 tclass.insetLayouts().begin();
372 TextClass::InsetLayouts::const_iterator len =
373 tclass.insetLayouts().end();
374 for (; lit != len; ++lit)
375 lyxerr << lit->second.name() << "\n";
379 case IL_OBSOLETEDBY: {
382 style = subst(style, '_', ' ');
384 // We don't want to apply the algorithm in DocumentClass::insetLayout()
385 // here. So we do it the long way.
386 TextClass::InsetLayouts::const_iterator it =
387 tclass.insetLayouts().find(style);
388 if (it != tclass.insetLayouts().end()) {
389 docstring const tmpname = name_;
390 this->operator=(it->second);
392 if (obsoleted_by().empty())
393 obsoleted_by_ = style;
395 LYXERR0("Cannot replace InsetLayout `"
397 << "' with unknown InsetLayout `"
399 << "All InsetLayouts so far:");
400 TextClass::InsetLayouts::const_iterator lit =
401 tclass.insetLayouts().begin();
402 TextClass::InsetLayouts::const_iterator len =
403 tclass.insetLayouts().end();
404 for (; lit != len; ++lit)
405 lyxerr << lit->second.name() << "\n";
411 font_ = lyxRead(lex, inherit_font);
412 // If you want to define labelfont, you need to do so after
422 postcommandargs_.clear();
430 bgcolor_ = lcolor.getFromLyXName(tmp);
433 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
435 case IL_BABELPREAMBLE:
436 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
438 case IL_LANGPREAMBLE:
439 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
450 case IL_HTMLFORCECSS:
451 lex >> htmlforcecss_;
453 case IL_HTMLINNERTAG:
454 lex >> htmlinnertag_;
456 case IL_HTMLINNERATTR:
457 lex >> htmlinnerattr_;
466 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
468 case IL_HTMLPREAMBLE:
469 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
473 vector<string> const req
474 = getVectorFromString(lex.getString(true));
475 requires_.insert(req.begin(), req.end());
489 add_to_toc_ = !toc_type_.empty();
491 case IL_ISTOCCAPTION:
492 lex >> is_toc_caption_;
494 case IL_EDITEXTERNAL:
495 lex >> edit_external_;
503 // Here add element to list if getout == true
507 // The label font is generally used as-is without
508 // any realization against a given context.
509 labelfont_.realize(sane_font);
516 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
518 if (compare_ascii_no_case(str, "charstyle") == 0)
519 return InsetLayout::CHARSTYLE;
520 if (compare_ascii_no_case(str, "custom") == 0)
521 return InsetLayout::CUSTOM;
522 if (compare_ascii_no_case(str, "element") == 0)
523 return InsetLayout::ELEMENT;
524 if (compare_ascii_no_case(str, "end") == 0)
525 return InsetLayout::END;
526 if (compare_ascii_no_case(str, "standard") == 0)
527 return InsetLayout::STANDARD;
528 return InsetLayout::NOLYXTYPE;
532 string const & InsetLayout::htmltag() const
534 if (htmltag_.empty())
535 htmltag_ = multipar_ ? "div" : "span";
540 string const & InsetLayout::htmlattr() const
542 if (htmlattr_.empty())
543 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
548 string const & InsetLayout::htmlinnerattr() const
550 if (htmlinnerattr_.empty())
551 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
552 return htmlinnerattr_;
556 string InsetLayout::defaultCSSClass() const
558 if (!defaultcssclass_.empty())
559 return defaultcssclass_;
561 string n = to_utf8(name());
562 string::const_iterator it = n.begin();
563 string::const_iterator en = n.end();
564 for (; it != en; ++it) {
565 if (!isAlphaASCII(*it))
567 else if (isLower(*it))
572 // are there other characters we need to remove?
573 defaultcssclass_ = d;
574 return defaultcssclass_;
578 void InsetLayout::makeDefaultCSS() const
580 if (!htmldefaultstyle_.empty())
582 docstring const mainfontCSS = font_.asCSS();
583 if (!mainfontCSS.empty())
585 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
586 mainfontCSS + from_ascii("\n}\n");
590 docstring InsetLayout::htmlstyle() const
592 if (!htmlstyle_.empty() && !htmlforcecss_)
594 if (htmldefaultstyle_.empty())
596 docstring retval = htmldefaultstyle_;
597 if (!htmlstyle_.empty())
598 retval += '\n' + htmlstyle_ + '\n';
602 void InsetLayout::readArgument(Lexer & lex)
604 Layout::latexarg arg;
605 arg.mandatory = false;
606 arg.autoinsert = false;
607 arg.insertcotext = false;
609 bool finished = false;
610 arg.font = inherit_font;
611 arg.labelfont = inherit_font;
612 arg.is_toc_caption = false;
613 arg.free_spacing = false;
614 arg.passthru = PT_INHERITED;
615 arg.nodelims = false;
618 bool const postcmd = prefixIs(nr, "post:");
619 while (!finished && lex.isOK() && !error) {
621 string const tok = ascii_lowercase(lex.getString());
625 } else if (tok == "endargument") {
627 } else if (tok == "labelstring") {
629 arg.labelstring = lex.getDocString();
630 } else if (tok == "menustring") {
632 arg.menustring = lex.getDocString();
633 } else if (tok == "mandatory") {
635 arg.mandatory = lex.getBool();
636 } else if (tok == "autoinsert") {
638 arg.autoinsert = lex.getBool();
639 } else if (tok == "insertcotext") {
641 arg.insertcotext = lex.getBool();
642 } else if (tok == "leftdelim") {
644 arg.ldelim = lex.getDocString();
645 arg.ldelim = subst(arg.ldelim,
646 from_ascii("<br/>"), from_ascii("\n"));
647 } else if (tok == "rightdelim") {
649 arg.rdelim = lex.getDocString();
650 arg.rdelim = subst(arg.rdelim,
651 from_ascii("<br/>"), from_ascii("\n"));
652 } else if (tok == "defaultarg") {
654 arg.defaultarg = lex.getDocString();
655 } else if (tok == "presetarg") {
657 arg.presetarg = lex.getDocString();
658 } else if (tok == "tooltip") {
660 arg.tooltip = lex.getDocString();
661 } else if (tok == "requires") {
663 arg.requires = lex.getString();
664 } else if (tok == "decoration") {
666 arg.decoration = lex.getString();
667 } else if (tok == "newlinecmd") {
669 arg.newlinecmd = lex.getString();
670 } else if (tok == "font") {
671 arg.font = lyxRead(lex, arg.font);
672 } else if (tok == "labelfont") {
673 arg.labelfont = lyxRead(lex, arg.labelfont);
674 } else if (tok == "passthruchars") {
676 arg.pass_thru_chars = lex.getDocString();
677 } else if (tok == "passthru") {
679 docstring value = lex.getDocString();
680 if (value == "true" || value == "1")
681 arg.passthru = PT_TRUE;
682 else if (value == "false" || value == "0")
683 arg.passthru = PT_FALSE;
685 arg.passthru = PT_INHERITED;
686 } else if (tok == "istoccaption") {
688 arg.is_toc_caption = lex.getBool();
689 } else if (tok == "freespacing") {
691 arg.free_spacing = lex.getBool();
693 lex.printError("Unknown tag");
697 if (arg.labelstring.empty())
698 LYXERR0("Incomplete Argument definition!");
700 postcommandargs_[nr] = arg;
702 latexargs_[nr] = arg;
706 Layout::LaTeXArgMap InsetLayout::args() const
708 Layout::LaTeXArgMap args = latexargs_;
709 if (!postcommandargs_.empty())
710 args.insert(postcommandargs_.begin(), postcommandargs_.end());
715 unsigned int InsetLayout::optArgs() const
718 Layout::LaTeXArgMap const args = InsetLayout::args();
719 Layout::LaTeXArgMap::const_iterator it = args.begin();
720 for (; it != args.end(); ++it) {
721 if (!(*it).second.mandatory)
728 unsigned int InsetLayout::requiredArgs() const
731 Layout::LaTeXArgMap const args = InsetLayout::args();
732 Layout::LaTeXArgMap::const_iterator it = args.begin();
733 for (; it != args.end(); ++it) {
734 if ((*it).second.mandatory)