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 fixedwidthpreambleencoding_(false), htmlforcecss_ (false),
40 htmlisblock_(true), multipar_(true), custompars_(true),
41 forceplain_(false), passthru_(false), parbreakisnewline_(false),
42 freespacing_(false), keepempty_(false), forceltr_(false),
43 forceownlines_(false), needprotect_(false), intoc_(false),
44 spellcheck_(true), resetsfont_(false), display_(true),
45 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)
90 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
92 IL_FORCE_LOCAL_FONT_SWITCH,
120 IL_PARBREAKISNEWLINE,
132 LexerKeyword elementTags[] = {
133 { "argument", IL_ARGUMENT },
134 { "babelpreamble", IL_BABELPREAMBLE },
135 { "bgcolor", IL_BGCOLOR },
136 { "contentaslabel", IL_CONTENTASLABEL },
137 { "copystyle", IL_COPYSTYLE },
138 { "counter", IL_COUNTER},
139 { "custompars", IL_CUSTOMPARS },
140 { "decoration", IL_DECORATION },
141 { "display", IL_DISPLAY },
143 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
145 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
146 { "forceltr", IL_FORCELTR },
147 { "forceownlines", IL_FORCEOWNLINES },
148 { "forceplain", IL_FORCEPLAIN },
149 { "freespacing", IL_FREESPACING },
150 { "htmlattr", IL_HTMLATTR },
151 { "htmlforcecss", IL_HTMLFORCECSS },
152 { "htmlinnerattr", IL_HTMLINNERATTR},
153 { "htmlinnertag", IL_HTMLINNERTAG},
154 { "htmlisblock", IL_HTMLISBLOCK},
155 { "htmllabel", IL_HTMLLABEL },
156 { "htmlpreamble", IL_HTMLPREAMBLE },
157 { "htmlstyle", IL_HTMLSTYLE },
158 { "htmltag", IL_HTMLTAG },
159 { "intoc", IL_INTOC },
160 { "keepempty", IL_KEEPEMPTY },
161 { "labelfont", IL_LABELFONT },
162 { "labelstring", IL_LABELSTRING },
163 { "langpreamble", IL_LANGPREAMBLE },
164 { "latexname", IL_LATEXNAME },
165 { "latexparam", IL_LATEXPARAM },
166 { "latextype", IL_LATEXTYPE },
167 { "leftdelim", IL_LEFTDELIM },
168 { "lyxtype", IL_LYXTYPE },
169 { "multipar", IL_MULTIPAR },
170 { "needprotect", IL_NEEDPROTECT },
171 { "obsoletedby", IL_OBSOLETEDBY },
172 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
173 { "passthru", IL_PASSTHRU },
174 { "preamble", IL_PREAMBLE },
175 { "refprefix", IL_REFPREFIX },
176 { "requires", IL_REQUIRES },
177 { "resetargs", IL_RESETARGS },
178 { "resetsfont", IL_RESETSFONT },
179 { "rightdelim", IL_RIGHTDELIM },
180 { "spellcheck", IL_SPELLCHECK }
183 lex.pushTable(elementTags);
185 labelfont_ = inherit_font;
186 bgcolor_ = Color_none;
188 // whether we've read the CustomPars or ForcePlain tag
189 // for issuing a warning in case MultiPars comes later
190 bool readCustomOrPlain = false;
193 while (!getout && lex.isOK()) {
196 case Lexer::LEX_UNDEF:
197 lex.printError("Unknown InsetLayout tag");
204 // Perhaps a more elegant way to deal with the next two would be the
205 // way this sort of thing is handled in Layout::read(), namely, by
208 // make sure that we have the right sort of name.
209 if (name_ != from_ascii("undefined")
210 && name_.substr(0,5) != from_ascii("Flex:")) {
211 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
212 "This one has the name `" << to_utf8(name_) << "'\n"
213 "Ignoring LyXType declaration.");
218 lyxtype_ = translateLyXType(lt);
219 if (lyxtype_ == NOLYXTYPE)
220 LYXERR0("Unknown LyXType `" << lt << "'.");
221 if (lyxtype_ == CHARSTYLE)
228 latextype_ = translateLaTeXType(lt);
229 if (latextype_ == ILT_ERROR)
230 LYXERR0("Unknown LaTeXType `" << lt << "'.");
238 decoration_ = translateDecoration(tmp);
245 latexparam_ = support::subst(tmp, """, "\"");
249 leftdelim_ = support::subst(leftdelim_, from_ascii("<br/>"),
252 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
253 lex >> fixedwidthpreambleencoding_;
255 case IL_FORCE_LOCAL_FONT_SWITCH:
256 lex >> forcelocalfontswitch_;
260 rightdelim_ = support::subst(rightdelim_, from_ascii("<br/>"),
264 labelfont_ = lyxRead(lex, inherit_font);
269 case IL_FORCEOWNLINES:
270 lex >> forceownlines_;
277 // the defaults for these depend upon multipar_
278 if (readCustomOrPlain)
279 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
280 "Previous value may be overwritten!");
281 readCustomOrPlain = false;
282 custompars_ = multipar_;
283 forceplain_ = !multipar_;
290 readCustomOrPlain = true;
294 readCustomOrPlain = true;
299 case IL_PARBREAKISNEWLINE:
300 lex >> parbreakisnewline_;
311 case IL_CONTENTASLABEL:
312 lex >> contentaslabel_;
315 // initialize with a known style
318 style = support::subst(style, '_', ' ');
320 // We don't want to apply the algorithm in DocumentClass::insetLayout()
321 // here. So we do it the long way.
322 TextClass::InsetLayouts::const_iterator it =
323 tclass.insetLayouts().find(style);
324 if (it != tclass.insetLayouts().end()) {
325 docstring const tmpname = name_;
326 this->operator=(it->second);
329 LYXERR0("Cannot copy unknown InsetLayout `"
330 << style << "' to InsetLayout `"
332 << "All InsetLayouts so far:");
333 TextClass::InsetLayouts::const_iterator lit =
334 tclass.insetLayouts().begin();
335 TextClass::InsetLayouts::const_iterator len =
336 tclass.insetLayouts().end();
337 for (; lit != len; ++lit)
338 lyxerr << lit->second.name() << "\n";
342 case IL_OBSOLETEDBY: {
345 style = support::subst(style, '_', ' ');
347 // We don't want to apply the algorithm in DocumentClass::insetLayout()
348 // here. So we do it the long way.
349 TextClass::InsetLayouts::const_iterator it =
350 tclass.insetLayouts().find(style);
351 if (it != tclass.insetLayouts().end()) {
352 docstring const tmpname = name_;
353 this->operator=(it->second);
355 if (obsoleted_by().empty())
356 obsoleted_by_ = style;
358 LYXERR0("Cannot replace InsetLayout `"
360 << "' with unknown InsetLayout `"
362 << "All InsetLayouts so far:");
363 TextClass::InsetLayouts::const_iterator lit =
364 tclass.insetLayouts().begin();
365 TextClass::InsetLayouts::const_iterator len =
366 tclass.insetLayouts().end();
367 for (; lit != len; ++lit)
368 lyxerr << lit->second.name() << "\n";
374 font_ = lyxRead(lex, inherit_font);
375 // If you want to define labelfont, you need to do so after
385 postcommandargs_.clear();
393 bgcolor_ = lcolor.getFromLyXName(tmp);
396 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
398 case IL_BABELPREAMBLE:
399 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
401 case IL_LANGPREAMBLE:
402 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
413 case IL_HTMLFORCECSS:
414 lex >> htmlforcecss_;
416 case IL_HTMLINNERTAG:
417 lex >> htmlinnertag_;
419 case IL_HTMLINNERATTR:
420 lex >> htmlinnerattr_;
429 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
431 case IL_HTMLPREAMBLE:
432 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
436 vector<string> const req
437 = support::getVectorFromString(lex.getString());
438 requires_.insert(req.begin(), req.end());
456 // Here add element to list if getout == true
460 // The label font is generally used as-is without
461 // any realization against a given context.
462 labelfont_.realize(sane_font);
469 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
471 if (support::compare_ascii_no_case(str, "charstyle") == 0)
472 return InsetLayout::CHARSTYLE;
473 if (support::compare_ascii_no_case(str, "custom") == 0)
474 return InsetLayout::CUSTOM;
475 if (support::compare_ascii_no_case(str, "element") == 0)
476 return InsetLayout::ELEMENT;
477 if (support::compare_ascii_no_case(str, "end") == 0)
478 return InsetLayout::END;
479 if (support::compare_ascii_no_case(str, "standard") == 0)
480 return InsetLayout::STANDARD;
481 return InsetLayout::NOLYXTYPE;
485 string const & InsetLayout::htmltag() const
487 if (htmltag_.empty())
488 htmltag_ = multipar_ ? "div" : "span";
493 string const & InsetLayout::htmlattr() const
495 if (htmlattr_.empty())
496 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
501 string const & InsetLayout::htmlinnerattr() const
503 if (htmlinnerattr_.empty())
504 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
505 return htmlinnerattr_;
509 string InsetLayout::defaultCSSClass() const
511 if (!defaultcssclass_.empty())
512 return defaultcssclass_;
514 string n = to_utf8(name());
515 string::const_iterator it = n.begin();
516 string::const_iterator en = n.end();
517 for (; it != en; ++it) {
518 if (!isAlphaASCII(*it))
520 else if (isLower(*it))
523 d += support::lowercase(*it);
525 // are there other characters we need to remove?
526 defaultcssclass_ = d;
527 return defaultcssclass_;
531 void InsetLayout::makeDefaultCSS() const
533 if (!htmldefaultstyle_.empty())
535 docstring const mainfontCSS = font_.asCSS();
536 if (!mainfontCSS.empty())
538 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
539 mainfontCSS + from_ascii("\n}\n");
543 docstring InsetLayout::htmlstyle() const
545 if (!htmlstyle_.empty() && !htmlforcecss_)
547 if (htmldefaultstyle_.empty())
549 docstring retval = htmldefaultstyle_;
550 if (!htmlstyle_.empty())
551 retval += '\n' + htmlstyle_ + '\n';
555 void InsetLayout::readArgument(Lexer & lex)
557 Layout::latexarg arg;
558 arg.mandatory = false;
559 arg.autoinsert = false;
561 bool finished = false;
562 arg.font = inherit_font;
563 arg.labelfont = inherit_font;
566 bool const postcmd = support::prefixIs(nr, "post:");
567 while (!finished && lex.isOK() && !error) {
569 string const tok = support::ascii_lowercase(lex.getString());
573 } else if (tok == "endargument") {
575 } else if (tok == "labelstring") {
577 arg.labelstring = lex.getDocString();
578 } else if (tok == "menustring") {
580 arg.menustring = lex.getDocString();
581 } else if (tok == "mandatory") {
583 arg.mandatory = lex.getBool();
584 } else if (tok == "autoinsert") {
586 arg.autoinsert = lex.getBool();
587 } else if (tok == "leftdelim") {
589 arg.ldelim = lex.getDocString();
590 arg.ldelim = support::subst(arg.ldelim,
591 from_ascii("<br/>"), from_ascii("\n"));
592 } else if (tok == "rightdelim") {
594 arg.rdelim = lex.getDocString();
595 arg.rdelim = support::subst(arg.rdelim,
596 from_ascii("<br/>"), from_ascii("\n"));
597 } else if (tok == "defaultarg") {
599 arg.defaultarg = lex.getDocString();
600 } else if (tok == "presetarg") {
602 arg.presetarg = lex.getDocString();
603 } else if (tok == "tooltip") {
605 arg.tooltip = lex.getDocString();
606 } else if (tok == "requires") {
608 arg.requires = lex.getString();
609 } else if (tok == "decoration") {
611 arg.decoration = lex.getString();
612 } else if (tok == "font") {
613 arg.font = lyxRead(lex, arg.font);
614 } else if (tok == "labelfont") {
615 arg.labelfont = lyxRead(lex, arg.labelfont);
617 lex.printError("Unknown tag");
621 if (arg.labelstring.empty())
622 LYXERR0("Incomplete Argument definition!");
624 postcommandargs_[nr] = arg;
626 latexargs_[nr] = arg;
630 Layout::LaTeXArgMap InsetLayout::args() const
632 Layout::LaTeXArgMap args = latexargs_;
633 if (!postcommandargs_.empty())
634 args.insert(postcommandargs_.begin(), postcommandargs_.end());
639 unsigned int InsetLayout::optArgs() const
642 Layout::LaTeXArgMap const args = InsetLayout::args();
643 Layout::LaTeXArgMap::const_iterator it = args.begin();
644 for (; it != args.end(); ++it) {
645 if (!(*it).second.mandatory)
652 unsigned int InsetLayout::requiredArgs() const
655 Layout::LaTeXArgMap const args = InsetLayout::args();
656 Layout::LaTeXArgMap::const_iterator it = args.begin();
657 for (; it != args.end(); ++it) {
658 if ((*it).second.mandatory)