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"
34 InsetLayout::InsetLayout() :
35 name_(from_ascii("undefined")), lyxtype_(STANDARD),
36 labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
37 decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(inherit_font),
38 labelfont_(sane_font), bgcolor_(Color_error),
39 htmlforcecss_ (false), htmlisblock_(true),
40 multipar_(true), custompars_(true), forceplain_(false),
41 passthru_(false), parbreakisnewline_(false), freespacing_(false),
42 keepempty_(false), forceltr_(false),
43 needprotect_(false), intoc_(false), spellcheck_(true),
44 resetsfont_(false), display_(true), forcelocalfontswitch_(false)
46 labelfont_.setColor(Color_error);
50 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
52 if (support::compare_ascii_no_case(str, "classic") == 0)
53 return InsetLayout::CLASSIC;
54 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
55 return InsetLayout::MINIMALISTIC;
56 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
57 return InsetLayout::CONGLOMERATE;
58 return InsetLayout::DEFAULT;
63 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
65 if (support::compare_ascii_no_case(str, "command") == 0)
66 return InsetLayout::COMMAND;
67 if (support::compare_ascii_no_case(str, "environment") == 0)
68 return InsetLayout::ENVIRONMENT;
69 if (support::compare_ascii_no_case(str, "none") == 0)
70 return InsetLayout::NOLATEXTYPE;
71 return InsetLayout::ILT_ERROR;
77 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
90 IL_FORCE_LOCAL_FONT_SWITCH,
118 IL_PARBREAKISNEWLINE,
130 LexerKeyword elementTags[] = {
131 { "argument", IL_ARGUMENT },
132 { "babelpreamble", IL_BABELPREAMBLE },
133 { "bgcolor", IL_BGCOLOR },
134 { "contentaslabel", IL_CONTENTASLABEL },
135 { "copystyle", IL_COPYSTYLE },
136 { "counter", IL_COUNTER},
137 { "custompars", IL_CUSTOMPARS },
138 { "decoration", IL_DECORATION },
139 { "display", IL_DISPLAY },
142 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
143 { "forceltr", IL_FORCELTR },
144 { "forceownlines", IL_FORCEOWNLINES },
145 { "forceplain", IL_FORCEPLAIN },
146 { "freespacing", IL_FREESPACING },
147 { "htmlattr", IL_HTMLATTR },
148 { "htmlforcecss", IL_HTMLFORCECSS },
149 { "htmlinnerattr", IL_HTMLINNERATTR},
150 { "htmlinnertag", IL_HTMLINNERTAG},
151 { "htmlisblock", IL_HTMLISBLOCK},
152 { "htmllabel", IL_HTMLLABEL },
153 { "htmlpreamble", IL_HTMLPREAMBLE },
154 { "htmlstyle", IL_HTMLSTYLE },
155 { "htmltag", IL_HTMLTAG },
156 { "intoc", IL_INTOC },
157 { "keepempty", IL_KEEPEMPTY },
158 { "labelfont", IL_LABELFONT },
159 { "labelstring", IL_LABELSTRING },
160 { "langpreamble", IL_LANGPREAMBLE },
161 { "latexname", IL_LATEXNAME },
162 { "latexparam", IL_LATEXPARAM },
163 { "latextype", IL_LATEXTYPE },
164 { "leftdelim", IL_LEFTDELIM },
165 { "lyxtype", IL_LYXTYPE },
166 { "multipar", IL_MULTIPAR },
167 { "needprotect", IL_NEEDPROTECT },
168 { "obsoletedby", IL_OBSOLETEDBY },
169 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
170 { "passthru", IL_PASSTHRU },
171 { "preamble", IL_PREAMBLE },
172 { "refprefix", IL_REFPREFIX },
173 { "requires", IL_REQUIRES },
174 { "resetargs", IL_RESETARGS },
175 { "resetsfont", IL_RESETSFONT },
176 { "rightdelim", IL_RIGHTDELIM },
177 { "spellcheck", IL_SPELLCHECK }
180 lex.pushTable(elementTags);
182 labelfont_ = inherit_font;
183 bgcolor_ = Color_none;
185 // whether we've read the CustomPars or ForcePlain tag
186 // for issuing a warning in case MultiPars comes later
187 bool readCustomOrPlain = false;
190 while (!getout && lex.isOK()) {
193 case Lexer::LEX_UNDEF:
194 lex.printError("Unknown InsetLayout tag");
201 // Perhaps a more elegant way to deal with the next two would be the
202 // way this sort of thing is handled in Layout::read(), namely, by
205 // make sure that we have the right sort of name.
206 if (name_ != from_ascii("undefined")
207 && name_.substr(0,5) != from_ascii("Flex:")) {
208 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
209 "This one has the name `" << to_utf8(name_) << "'\n"
210 "Ignoring LyXType declaration.");
215 lyxtype_ = translateLyXType(lt);
216 if (lyxtype_ == NOLYXTYPE)
217 LYXERR0("Unknown LyXType `" << lt << "'.");
218 if (lyxtype_ == CHARSTYLE)
225 latextype_ = translateLaTeXType(lt);
226 if (latextype_ == ILT_ERROR)
227 LYXERR0("Unknown LaTeXType `" << lt << "'.");
235 decoration_ = translateDecoration(tmp);
242 latexparam_ = support::subst(tmp, """, "\"");
246 leftdelim_ = support::subst(leftdelim_, from_ascii("<br/>"),
249 case IL_FORCE_LOCAL_FONT_SWITCH:
250 lex >> forcelocalfontswitch_;
254 rightdelim_ = support::subst(rightdelim_, from_ascii("<br/>"),
258 labelfont_ = lyxRead(lex, inherit_font);
263 case IL_FORCEOWNLINES:
264 lex >> forceownlines_;
271 // the defaults for these depend upon multipar_
272 if (readCustomOrPlain)
273 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
274 "Previous value may be overwritten!");
275 readCustomOrPlain = false;
276 custompars_ = multipar_;
277 forceplain_ = !multipar_;
284 readCustomOrPlain = true;
288 readCustomOrPlain = true;
293 case IL_PARBREAKISNEWLINE:
294 lex >> parbreakisnewline_;
305 case IL_CONTENTASLABEL:
306 lex >> contentaslabel_;
309 // initialize with a known style
312 style = support::subst(style, '_', ' ');
314 // We don't want to apply the algorithm in DocumentClass::insetLayout()
315 // here. So we do it the long way.
316 TextClass::InsetLayouts::const_iterator it =
317 tclass.insetLayouts().find(style);
318 if (it != tclass.insetLayouts().end()) {
319 docstring const tmpname = name_;
320 this->operator=(it->second);
323 LYXERR0("Cannot copy unknown InsetLayout `"
324 << style << "' to InsetLayout `"
326 << "All InsetLayouts so far:");
327 TextClass::InsetLayouts::const_iterator lit =
328 tclass.insetLayouts().begin();
329 TextClass::InsetLayouts::const_iterator len =
330 tclass.insetLayouts().end();
331 for (; lit != len; ++lit)
332 lyxerr << lit->second.name() << "\n";
336 case IL_OBSOLETEDBY: {
339 style = support::subst(style, '_', ' ');
341 // We don't want to apply the algorithm in DocumentClass::insetLayout()
342 // here. So we do it the long way.
343 TextClass::InsetLayouts::const_iterator it =
344 tclass.insetLayouts().find(style);
345 if (it != tclass.insetLayouts().end()) {
346 docstring const tmpname = name_;
347 this->operator=(it->second);
349 if (obsoleted_by().empty())
350 obsoleted_by_ = style;
352 LYXERR0("Cannot replace InsetLayout `"
354 << "' with unknown InsetLayout `"
356 << "All InsetLayouts so far:");
357 TextClass::InsetLayouts::const_iterator lit =
358 tclass.insetLayouts().begin();
359 TextClass::InsetLayouts::const_iterator len =
360 tclass.insetLayouts().end();
361 for (; lit != len; ++lit)
362 lyxerr << lit->second.name() << "\n";
368 font_ = lyxRead(lex, inherit_font);
369 // If you want to define labelfont, you need to do so after
379 postcommandargs_.clear();
387 bgcolor_ = lcolor.getFromLyXName(tmp);
390 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
392 case IL_BABELPREAMBLE:
393 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
395 case IL_LANGPREAMBLE:
396 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
407 case IL_HTMLFORCECSS:
408 lex >> htmlforcecss_;
410 case IL_HTMLINNERTAG:
411 lex >> htmlinnertag_;
413 case IL_HTMLINNERATTR:
414 lex >> htmlinnerattr_;
423 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
425 case IL_HTMLPREAMBLE:
426 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
430 vector<string> const req
431 = support::getVectorFromString(lex.getString());
432 requires_.insert(req.begin(), req.end());
450 // Here add element to list if getout == true
454 // The label font is generally used as-is without
455 // any realization against a given context.
456 labelfont_.realize(sane_font);
463 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
466 if (support::compare_ascii_no_case(str, "charstyle") == 0)
467 return InsetLayout::CHARSTYLE;
468 if (support::compare_ascii_no_case(str, "custom") == 0)
469 return InsetLayout::CUSTOM;
470 if (support::compare_ascii_no_case(str, "element") == 0)
471 return InsetLayout::ELEMENT;
472 if (support::compare_ascii_no_case(str, "end") == 0)
473 return InsetLayout::END;
474 if (support::compare_ascii_no_case(str, "standard") == 0)
475 return InsetLayout::STANDARD;
476 return InsetLayout::NOLYXTYPE;
480 string const & InsetLayout::htmltag() const
482 if (htmltag_.empty())
483 htmltag_ = multipar_ ? "div" : "span";
488 string const & InsetLayout::htmlattr() const
490 if (htmlattr_.empty())
491 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
496 string const & InsetLayout::htmlinnerattr() const
498 if (htmlinnerattr_.empty())
499 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
500 return htmlinnerattr_;
504 string InsetLayout::defaultCSSClass() const
506 if (!defaultcssclass_.empty())
507 return defaultcssclass_;
509 string n = to_utf8(name());
510 string::const_iterator it = n.begin();
511 string::const_iterator en = n.end();
512 for (; it != en; ++it) {
513 if (!isAlphaASCII(*it))
515 else if (isLower(*it))
518 d += support::lowercase(*it);
520 // are there other characters we need to remove?
521 defaultcssclass_ = d;
522 return defaultcssclass_;
526 void InsetLayout::makeDefaultCSS() const
528 if (!htmldefaultstyle_.empty())
530 docstring const mainfontCSS = font_.asCSS();
531 if (!mainfontCSS.empty())
533 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
534 mainfontCSS + from_ascii("\n}\n");
538 docstring InsetLayout::htmlstyle() const
540 if (!htmlstyle_.empty() && !htmlforcecss_)
542 if (htmldefaultstyle_.empty())
544 docstring retval = htmldefaultstyle_;
545 if (!htmlstyle_.empty())
546 retval += '\n' + htmlstyle_ + '\n';
550 void InsetLayout::readArgument(Lexer & lex)
552 Layout::latexarg arg;
553 arg.mandatory = false;
554 arg.autoinsert = false;
556 bool finished = false;
557 arg.font = inherit_font;
558 arg.labelfont = inherit_font;
561 bool const postcmd = support::prefixIs(nr, "post:");
562 while (!finished && lex.isOK() && !error) {
564 string const tok = support::ascii_lowercase(lex.getString());
568 } else if (tok == "endargument") {
570 } else if (tok == "labelstring") {
572 arg.labelstring = lex.getDocString();
573 } else if (tok == "menustring") {
575 arg.menustring = lex.getDocString();
576 } else if (tok == "mandatory") {
578 arg.mandatory = lex.getBool();
579 } else if (tok == "autoinsert") {
581 arg.autoinsert = lex.getBool();
582 } else if (tok == "leftdelim") {
584 arg.ldelim = lex.getDocString();
585 arg.ldelim = support::subst(arg.ldelim,
586 from_ascii("<br/>"), from_ascii("\n"));
587 } else if (tok == "rightdelim") {
589 arg.rdelim = lex.getDocString();
590 arg.rdelim = support::subst(arg.rdelim,
591 from_ascii("<br/>"), from_ascii("\n"));
592 } else if (tok == "defaultarg") {
594 arg.defaultarg = lex.getDocString();
595 } else if (tok == "presetarg") {
597 arg.presetarg = lex.getDocString();
598 } else if (tok == "tooltip") {
600 arg.tooltip = lex.getDocString();
601 } else if (tok == "requires") {
603 arg.requires = lex.getString();
604 } else if (tok == "decoration") {
606 arg.decoration = lex.getString();
607 } else if (tok == "font") {
608 arg.font = lyxRead(lex, arg.font);
609 } else if (tok == "labelfont") {
610 arg.labelfont = lyxRead(lex, arg.labelfont);
612 lex.printError("Unknown tag");
616 if (arg.labelstring.empty())
617 LYXERR0("Incomplete Argument definition!");
619 postcommandargs_[nr] = arg;
621 latexargs_[nr] = arg;
625 Layout::LaTeXArgMap InsetLayout::args() const
627 Layout::LaTeXArgMap args = latexargs_;
628 if (!postcommandargs_.empty())
629 args.insert(postcommandargs_.begin(), postcommandargs_.end());
634 unsigned int InsetLayout::optArgs() const
637 Layout::LaTeXArgMap const args = InsetLayout::args();
638 Layout::LaTeXArgMap::const_iterator it = args.begin();
639 for (; it != args.end(); ++it) {
640 if (!(*it).second.mandatory)
647 unsigned int InsetLayout::requiredArgs() const
650 Layout::LaTeXArgMap const args = InsetLayout::args();
651 Layout::LaTeXArgMap::const_iterator it = args.begin();
652 for (; it != args.end(); ++it) {
653 if ((*it).second.mandatory)