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),
47 labelfont_.setColor(Color_error);
51 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
53 if (support::compare_ascii_no_case(str, "classic") == 0)
54 return InsetLayout::CLASSIC;
55 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
56 return InsetLayout::MINIMALISTIC;
57 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
58 return InsetLayout::CONGLOMERATE;
59 return InsetLayout::DEFAULT;
64 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
66 if (support::compare_ascii_no_case(str, "command") == 0)
67 return InsetLayout::COMMAND;
68 if (support::compare_ascii_no_case(str, "environment") == 0)
69 return InsetLayout::ENVIRONMENT;
70 if (support::compare_ascii_no_case(str, "none") == 0)
71 return InsetLayout::NOLATEXTYPE;
72 return InsetLayout::ILT_ERROR;
78 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
91 IL_FORCE_LOCAL_FONT_SWITCH,
119 IL_PARBREAKISNEWLINE,
131 LexerKeyword elementTags[] = {
132 { "argument", IL_ARGUMENT },
133 { "babelpreamble", IL_BABELPREAMBLE },
134 { "bgcolor", IL_BGCOLOR },
135 { "contentaslabel", IL_CONTENTASLABEL },
136 { "copystyle", IL_COPYSTYLE },
137 { "counter", IL_COUNTER},
138 { "custompars", IL_CUSTOMPARS },
139 { "decoration", IL_DECORATION },
140 { "display", IL_DISPLAY },
143 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
144 { "forceltr", IL_FORCELTR },
145 { "forceownlines", IL_FORCEOWNLINES },
146 { "forceplain", IL_FORCEPLAIN },
147 { "freespacing", IL_FREESPACING },
148 { "htmlattr", IL_HTMLATTR },
149 { "htmlforcecss", IL_HTMLFORCECSS },
150 { "htmlinnerattr", IL_HTMLINNERATTR},
151 { "htmlinnertag", IL_HTMLINNERTAG},
152 { "htmlisblock", IL_HTMLISBLOCK},
153 { "htmllabel", IL_HTMLLABEL },
154 { "htmlpreamble", IL_HTMLPREAMBLE },
155 { "htmlstyle", IL_HTMLSTYLE },
156 { "htmltag", IL_HTMLTAG },
157 { "intoc", IL_INTOC },
158 { "keepempty", IL_KEEPEMPTY },
159 { "labelfont", IL_LABELFONT },
160 { "labelstring", IL_LABELSTRING },
161 { "langpreamble", IL_LANGPREAMBLE },
162 { "latexname", IL_LATEXNAME },
163 { "latexparam", IL_LATEXPARAM },
164 { "latextype", IL_LATEXTYPE },
165 { "leftdelim", IL_LEFTDELIM },
166 { "lyxtype", IL_LYXTYPE },
167 { "multipar", IL_MULTIPAR },
168 { "needprotect", IL_NEEDPROTECT },
169 { "obsoletedby", IL_OBSOLETEDBY },
170 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
171 { "passthru", IL_PASSTHRU },
172 { "preamble", IL_PREAMBLE },
173 { "refprefix", IL_REFPREFIX },
174 { "requires", IL_REQUIRES },
175 { "resetargs", IL_RESETARGS },
176 { "resetsfont", IL_RESETSFONT },
177 { "rightdelim", IL_RIGHTDELIM },
178 { "spellcheck", IL_SPELLCHECK }
181 lex.pushTable(elementTags);
183 labelfont_ = inherit_font;
184 bgcolor_ = Color_none;
186 // whether we've read the CustomPars or ForcePlain tag
187 // for issuing a warning in case MultiPars comes later
188 bool readCustomOrPlain = false;
191 while (!getout && lex.isOK()) {
194 case Lexer::LEX_UNDEF:
195 lex.printError("Unknown InsetLayout tag");
202 // Perhaps a more elegant way to deal with the next two would be the
203 // way this sort of thing is handled in Layout::read(), namely, by
206 // make sure that we have the right sort of name.
207 if (name_ != from_ascii("undefined")
208 && name_.substr(0,5) != from_ascii("Flex:")) {
209 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
210 "This one has the name `" << to_utf8(name_) << "'\n"
211 "Ignoring LyXType declaration.");
216 lyxtype_ = translateLyXType(lt);
217 if (lyxtype_ == NOLYXTYPE)
218 LYXERR0("Unknown LyXType `" << lt << "'.");
219 if (lyxtype_ == CHARSTYLE)
226 latextype_ = translateLaTeXType(lt);
227 if (latextype_ == ILT_ERROR)
228 LYXERR0("Unknown LaTeXType `" << lt << "'.");
236 decoration_ = translateDecoration(tmp);
243 latexparam_ = support::subst(tmp, """, "\"");
247 leftdelim_ = support::subst(leftdelim_, from_ascii("<br/>"),
250 case IL_FORCE_LOCAL_FONT_SWITCH:
251 lex >> forcelocalfontswitch_;
255 rightdelim_ = support::subst(rightdelim_, from_ascii("<br/>"),
259 labelfont_ = lyxRead(lex, inherit_font);
264 case IL_FORCEOWNLINES:
265 lex >> forceownlines_;
272 // the defaults for these depend upon multipar_
273 if (readCustomOrPlain)
274 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
275 "Previous value may be overwritten!");
276 readCustomOrPlain = false;
277 custompars_ = multipar_;
278 forceplain_ = !multipar_;
285 readCustomOrPlain = true;
289 readCustomOrPlain = true;
294 case IL_PARBREAKISNEWLINE:
295 lex >> parbreakisnewline_;
306 case IL_CONTENTASLABEL:
307 lex >> contentaslabel_;
310 // initialize with a known style
313 style = support::subst(style, '_', ' ');
315 // We don't want to apply the algorithm in DocumentClass::insetLayout()
316 // here. So we do it the long way.
317 TextClass::InsetLayouts::const_iterator it =
318 tclass.insetLayouts().find(style);
319 if (it != tclass.insetLayouts().end()) {
320 docstring const tmpname = name_;
321 this->operator=(it->second);
324 LYXERR0("Cannot copy unknown InsetLayout `"
325 << style << "' to InsetLayout `"
327 << "All InsetLayouts so far:");
328 TextClass::InsetLayouts::const_iterator lit =
329 tclass.insetLayouts().begin();
330 TextClass::InsetLayouts::const_iterator len =
331 tclass.insetLayouts().end();
332 for (; lit != len; ++lit)
333 lyxerr << lit->second.name() << "\n";
337 case IL_OBSOLETEDBY: {
340 style = support::subst(style, '_', ' ');
342 // We don't want to apply the algorithm in DocumentClass::insetLayout()
343 // here. So we do it the long way.
344 TextClass::InsetLayouts::const_iterator it =
345 tclass.insetLayouts().find(style);
346 if (it != tclass.insetLayouts().end()) {
347 docstring const tmpname = name_;
348 this->operator=(it->second);
350 if (obsoleted_by().empty())
351 obsoleted_by_ = style;
353 LYXERR0("Cannot replace InsetLayout `"
355 << "' with unknown InsetLayout `"
357 << "All InsetLayouts so far:");
358 TextClass::InsetLayouts::const_iterator lit =
359 tclass.insetLayouts().begin();
360 TextClass::InsetLayouts::const_iterator len =
361 tclass.insetLayouts().end();
362 for (; lit != len; ++lit)
363 lyxerr << lit->second.name() << "\n";
369 font_ = lyxRead(lex, inherit_font);
370 // If you want to define labelfont, you need to do so after
380 postcommandargs_.clear();
388 bgcolor_ = lcolor.getFromLyXName(tmp);
391 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
393 case IL_BABELPREAMBLE:
394 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
396 case IL_LANGPREAMBLE:
397 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
408 case IL_HTMLFORCECSS:
409 lex >> htmlforcecss_;
411 case IL_HTMLINNERTAG:
412 lex >> htmlinnertag_;
414 case IL_HTMLINNERATTR:
415 lex >> htmlinnerattr_;
424 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
426 case IL_HTMLPREAMBLE:
427 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
431 vector<string> const req
432 = support::getVectorFromString(lex.getString());
433 requires_.insert(req.begin(), req.end());
451 // Here add element to list if getout == true
455 // The label font is generally used as-is without
456 // any realization against a given context.
457 labelfont_.realize(sane_font);
464 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
467 if (support::compare_ascii_no_case(str, "charstyle") == 0)
468 return InsetLayout::CHARSTYLE;
469 if (support::compare_ascii_no_case(str, "custom") == 0)
470 return InsetLayout::CUSTOM;
471 if (support::compare_ascii_no_case(str, "element") == 0)
472 return InsetLayout::ELEMENT;
473 if (support::compare_ascii_no_case(str, "end") == 0)
474 return InsetLayout::END;
475 if (support::compare_ascii_no_case(str, "standard") == 0)
476 return InsetLayout::STANDARD;
477 return InsetLayout::NOLYXTYPE;
481 string const & InsetLayout::htmltag() const
483 if (htmltag_.empty())
484 htmltag_ = multipar_ ? "div" : "span";
489 string const & InsetLayout::htmlattr() const
491 if (htmlattr_.empty())
492 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
497 string const & InsetLayout::htmlinnerattr() const
499 if (htmlinnerattr_.empty())
500 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
501 return htmlinnerattr_;
505 string InsetLayout::defaultCSSClass() const
507 if (!defaultcssclass_.empty())
508 return defaultcssclass_;
510 string n = to_utf8(name());
511 string::const_iterator it = n.begin();
512 string::const_iterator en = n.end();
513 for (; it != en; ++it) {
514 if (!isAlphaASCII(*it))
516 else if (isLower(*it))
519 d += support::lowercase(*it);
521 // are there other characters we need to remove?
522 defaultcssclass_ = d;
523 return defaultcssclass_;
527 void InsetLayout::makeDefaultCSS() const
529 if (!htmldefaultstyle_.empty())
531 docstring const mainfontCSS = font_.asCSS();
532 if (!mainfontCSS.empty())
534 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
535 mainfontCSS + from_ascii("\n}\n");
539 docstring InsetLayout::htmlstyle() const
541 if (!htmlstyle_.empty() && !htmlforcecss_)
543 if (htmldefaultstyle_.empty())
545 docstring retval = htmldefaultstyle_;
546 if (!htmlstyle_.empty())
547 retval += '\n' + htmlstyle_ + '\n';
551 void InsetLayout::readArgument(Lexer & lex)
553 Layout::latexarg arg;
554 arg.mandatory = false;
555 arg.autoinsert = false;
557 bool finished = false;
558 arg.font = inherit_font;
559 arg.labelfont = inherit_font;
562 bool const postcmd = support::prefixIs(nr, "post:");
563 while (!finished && lex.isOK() && !error) {
565 string const tok = support::ascii_lowercase(lex.getString());
569 } else if (tok == "endargument") {
571 } else if (tok == "labelstring") {
573 arg.labelstring = lex.getDocString();
574 } else if (tok == "menustring") {
576 arg.menustring = lex.getDocString();
577 } else if (tok == "mandatory") {
579 arg.mandatory = lex.getBool();
580 } else if (tok == "autoinsert") {
582 arg.autoinsert = lex.getBool();
583 } else if (tok == "leftdelim") {
585 arg.ldelim = lex.getDocString();
586 arg.ldelim = support::subst(arg.ldelim,
587 from_ascii("<br/>"), from_ascii("\n"));
588 } else if (tok == "rightdelim") {
590 arg.rdelim = lex.getDocString();
591 arg.rdelim = support::subst(arg.rdelim,
592 from_ascii("<br/>"), from_ascii("\n"));
593 } else if (tok == "defaultarg") {
595 arg.defaultarg = lex.getDocString();
596 } else if (tok == "presetarg") {
598 arg.presetarg = lex.getDocString();
599 } else if (tok == "tooltip") {
601 arg.tooltip = lex.getDocString();
602 } else if (tok == "requires") {
604 arg.requires = lex.getString();
605 } else if (tok == "decoration") {
607 arg.decoration = lex.getString();
608 } else if (tok == "font") {
609 arg.font = lyxRead(lex, arg.font);
610 } else if (tok == "labelfont") {
611 arg.labelfont = lyxRead(lex, arg.labelfont);
613 lex.printError("Unknown tag");
617 if (arg.labelstring.empty())
618 LYXERR0("Incomplete Argument definition!");
620 postcommandargs_[nr] = arg;
622 latexargs_[nr] = arg;
626 Layout::LaTeXArgMap InsetLayout::args() const
628 Layout::LaTeXArgMap args = latexargs_;
629 if (!postcommandargs_.empty())
630 args.insert(postcommandargs_.begin(), postcommandargs_.end());
635 unsigned int InsetLayout::optArgs() const
638 Layout::LaTeXArgMap const args = InsetLayout::args();
639 Layout::LaTeXArgMap::const_iterator it = args.begin();
640 for (; it != args.end(); ++it) {
641 if (!(*it).second.mandatory)
648 unsigned int InsetLayout::requiredArgs() const
651 Layout::LaTeXArgMap const args = InsetLayout::args();
652 Layout::LaTeXArgMap::const_iterator it = args.begin();
653 for (; it != args.end(); ++it) {
654 if ((*it).second.mandatory)