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,
121 IL_PARBREAKISNEWLINE,
133 LexerKeyword elementTags[] = {
134 { "argument", IL_ARGUMENT },
135 { "babelpreamble", IL_BABELPREAMBLE },
136 { "bgcolor", IL_BGCOLOR },
137 { "contentaslabel", IL_CONTENTASLABEL },
138 { "copystyle", IL_COPYSTYLE },
139 { "counter", IL_COUNTER},
140 { "custompars", IL_CUSTOMPARS },
141 { "decoration", IL_DECORATION },
142 { "display", IL_DISPLAY },
144 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
146 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
147 { "forceltr", IL_FORCELTR },
148 { "forceownlines", IL_FORCEOWNLINES },
149 { "forceplain", IL_FORCEPLAIN },
150 { "freespacing", IL_FREESPACING },
151 { "htmlattr", IL_HTMLATTR },
152 { "htmlforcecss", IL_HTMLFORCECSS },
153 { "htmlinnerattr", IL_HTMLINNERATTR},
154 { "htmlinnertag", IL_HTMLINNERTAG},
155 { "htmlisblock", IL_HTMLISBLOCK},
156 { "htmllabel", IL_HTMLLABEL },
157 { "htmlpreamble", IL_HTMLPREAMBLE },
158 { "htmlstyle", IL_HTMLSTYLE },
159 { "htmltag", IL_HTMLTAG },
160 { "intoc", IL_INTOC },
161 { "keepempty", IL_KEEPEMPTY },
162 { "labelfont", IL_LABELFONT },
163 { "labelstring", IL_LABELSTRING },
164 { "langpreamble", IL_LANGPREAMBLE },
165 { "latexname", IL_LATEXNAME },
166 { "latexparam", IL_LATEXPARAM },
167 { "latextype", IL_LATEXTYPE },
168 { "leftdelim", IL_LEFTDELIM },
169 { "lyxtype", IL_LYXTYPE },
170 { "multipar", IL_MULTIPAR },
171 { "needprotect", IL_NEEDPROTECT },
172 { "obsoletedby", IL_OBSOLETEDBY },
173 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
174 { "passthru", IL_PASSTHRU },
175 { "passthruchars", IL_PASSTHRU_CHARS },
176 { "preamble", IL_PREAMBLE },
177 { "refprefix", IL_REFPREFIX },
178 { "requires", IL_REQUIRES },
179 { "resetargs", IL_RESETARGS },
180 { "resetsfont", IL_RESETSFONT },
181 { "rightdelim", IL_RIGHTDELIM },
182 { "spellcheck", IL_SPELLCHECK }
185 lex.pushTable(elementTags);
187 labelfont_ = inherit_font;
188 bgcolor_ = Color_none;
190 // whether we've read the CustomPars or ForcePlain tag
191 // for issuing a warning in case MultiPars comes later
192 bool readCustomOrPlain = false;
195 while (!getout && lex.isOK()) {
198 case Lexer::LEX_UNDEF:
199 lex.printError("Unknown InsetLayout tag");
206 // Perhaps a more elegant way to deal with the next two would be the
207 // way this sort of thing is handled in Layout::read(), namely, by
210 // make sure that we have the right sort of name.
211 if (name_ != from_ascii("undefined")
212 && name_.substr(0,5) != from_ascii("Flex:")) {
213 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
214 "This one has the name `" << to_utf8(name_) << "'\n"
215 "Ignoring LyXType declaration.");
220 lyxtype_ = translateLyXType(lt);
221 if (lyxtype_ == NOLYXTYPE)
222 LYXERR0("Unknown LyXType `" << lt << "'.");
223 if (lyxtype_ == CHARSTYLE)
230 latextype_ = translateLaTeXType(lt);
231 if (latextype_ == ILT_ERROR)
232 LYXERR0("Unknown LaTeXType `" << lt << "'.");
240 decoration_ = translateDecoration(tmp);
247 latexparam_ = support::subst(tmp, """, "\"");
251 leftdelim_ = support::subst(leftdelim_, from_ascii("<br/>"),
254 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
255 lex >> fixedwidthpreambleencoding_;
257 case IL_FORCE_LOCAL_FONT_SWITCH:
258 lex >> forcelocalfontswitch_;
262 rightdelim_ = support::subst(rightdelim_, from_ascii("<br/>"),
266 labelfont_ = lyxRead(lex, inherit_font);
271 case IL_FORCEOWNLINES:
272 lex >> forceownlines_;
279 // the defaults for these depend upon multipar_
280 if (readCustomOrPlain)
281 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
282 "Previous value may be overwritten!");
283 readCustomOrPlain = false;
284 custompars_ = multipar_;
285 forceplain_ = !multipar_;
292 readCustomOrPlain = true;
296 readCustomOrPlain = true;
301 case IL_PASSTHRU_CHARS:
302 lex >> passthru_chars_;
304 case IL_PARBREAKISNEWLINE:
305 lex >> parbreakisnewline_;
316 case IL_CONTENTASLABEL:
317 lex >> contentaslabel_;
320 // initialize with a known style
323 style = support::subst(style, '_', ' ');
325 // We don't want to apply the algorithm in DocumentClass::insetLayout()
326 // here. So we do it the long way.
327 TextClass::InsetLayouts::const_iterator it =
328 tclass.insetLayouts().find(style);
329 if (it != tclass.insetLayouts().end()) {
330 docstring const tmpname = name_;
331 this->operator=(it->second);
334 LYXERR0("Cannot copy unknown InsetLayout `"
335 << style << "' to InsetLayout `"
337 << "All InsetLayouts so far:");
338 TextClass::InsetLayouts::const_iterator lit =
339 tclass.insetLayouts().begin();
340 TextClass::InsetLayouts::const_iterator len =
341 tclass.insetLayouts().end();
342 for (; lit != len; ++lit)
343 lyxerr << lit->second.name() << "\n";
347 case IL_OBSOLETEDBY: {
350 style = support::subst(style, '_', ' ');
352 // We don't want to apply the algorithm in DocumentClass::insetLayout()
353 // here. So we do it the long way.
354 TextClass::InsetLayouts::const_iterator it =
355 tclass.insetLayouts().find(style);
356 if (it != tclass.insetLayouts().end()) {
357 docstring const tmpname = name_;
358 this->operator=(it->second);
360 if (obsoleted_by().empty())
361 obsoleted_by_ = style;
363 LYXERR0("Cannot replace InsetLayout `"
365 << "' with unknown InsetLayout `"
367 << "All InsetLayouts so far:");
368 TextClass::InsetLayouts::const_iterator lit =
369 tclass.insetLayouts().begin();
370 TextClass::InsetLayouts::const_iterator len =
371 tclass.insetLayouts().end();
372 for (; lit != len; ++lit)
373 lyxerr << lit->second.name() << "\n";
379 font_ = lyxRead(lex, inherit_font);
380 // If you want to define labelfont, you need to do so after
390 postcommandargs_.clear();
398 bgcolor_ = lcolor.getFromLyXName(tmp);
401 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
403 case IL_BABELPREAMBLE:
404 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
406 case IL_LANGPREAMBLE:
407 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
418 case IL_HTMLFORCECSS:
419 lex >> htmlforcecss_;
421 case IL_HTMLINNERTAG:
422 lex >> htmlinnertag_;
424 case IL_HTMLINNERATTR:
425 lex >> htmlinnerattr_;
434 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
436 case IL_HTMLPREAMBLE:
437 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
441 vector<string> const req
442 = support::getVectorFromString(lex.getString());
443 requires_.insert(req.begin(), req.end());
461 // Here add element to list if getout == true
465 // The label font is generally used as-is without
466 // any realization against a given context.
467 labelfont_.realize(sane_font);
474 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
476 if (support::compare_ascii_no_case(str, "charstyle") == 0)
477 return InsetLayout::CHARSTYLE;
478 if (support::compare_ascii_no_case(str, "custom") == 0)
479 return InsetLayout::CUSTOM;
480 if (support::compare_ascii_no_case(str, "element") == 0)
481 return InsetLayout::ELEMENT;
482 if (support::compare_ascii_no_case(str, "end") == 0)
483 return InsetLayout::END;
484 if (support::compare_ascii_no_case(str, "standard") == 0)
485 return InsetLayout::STANDARD;
486 return InsetLayout::NOLYXTYPE;
490 string const & InsetLayout::htmltag() const
492 if (htmltag_.empty())
493 htmltag_ = multipar_ ? "div" : "span";
498 string const & InsetLayout::htmlattr() const
500 if (htmlattr_.empty())
501 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
506 string const & InsetLayout::htmlinnerattr() const
508 if (htmlinnerattr_.empty())
509 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
510 return htmlinnerattr_;
514 string InsetLayout::defaultCSSClass() const
516 if (!defaultcssclass_.empty())
517 return defaultcssclass_;
519 string n = to_utf8(name());
520 string::const_iterator it = n.begin();
521 string::const_iterator en = n.end();
522 for (; it != en; ++it) {
523 if (!isAlphaASCII(*it))
525 else if (isLower(*it))
528 d += support::lowercase(*it);
530 // are there other characters we need to remove?
531 defaultcssclass_ = d;
532 return defaultcssclass_;
536 void InsetLayout::makeDefaultCSS() const
538 if (!htmldefaultstyle_.empty())
540 docstring const mainfontCSS = font_.asCSS();
541 if (!mainfontCSS.empty())
543 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
544 mainfontCSS + from_ascii("\n}\n");
548 docstring InsetLayout::htmlstyle() const
550 if (!htmlstyle_.empty() && !htmlforcecss_)
552 if (htmldefaultstyle_.empty())
554 docstring retval = htmldefaultstyle_;
555 if (!htmlstyle_.empty())
556 retval += '\n' + htmlstyle_ + '\n';
560 void InsetLayout::readArgument(Lexer & lex)
562 Layout::latexarg arg;
563 arg.mandatory = false;
564 arg.autoinsert = false;
566 bool finished = false;
567 arg.font = inherit_font;
568 arg.labelfont = inherit_font;
571 bool const postcmd = support::prefixIs(nr, "post:");
572 while (!finished && lex.isOK() && !error) {
574 string const tok = support::ascii_lowercase(lex.getString());
578 } else if (tok == "endargument") {
580 } else if (tok == "labelstring") {
582 arg.labelstring = lex.getDocString();
583 } else if (tok == "menustring") {
585 arg.menustring = lex.getDocString();
586 } else if (tok == "mandatory") {
588 arg.mandatory = lex.getBool();
589 } else if (tok == "autoinsert") {
591 arg.autoinsert = lex.getBool();
592 } else if (tok == "leftdelim") {
594 arg.ldelim = lex.getDocString();
595 arg.ldelim = support::subst(arg.ldelim,
596 from_ascii("<br/>"), from_ascii("\n"));
597 } else if (tok == "rightdelim") {
599 arg.rdelim = lex.getDocString();
600 arg.rdelim = support::subst(arg.rdelim,
601 from_ascii("<br/>"), from_ascii("\n"));
602 } else if (tok == "defaultarg") {
604 arg.defaultarg = lex.getDocString();
605 } else if (tok == "presetarg") {
607 arg.presetarg = lex.getDocString();
608 } else if (tok == "tooltip") {
610 arg.tooltip = lex.getDocString();
611 } else if (tok == "requires") {
613 arg.requires = lex.getString();
614 } else if (tok == "decoration") {
616 arg.decoration = lex.getString();
617 } else if (tok == "font") {
618 arg.font = lyxRead(lex, arg.font);
619 } else if (tok == "labelfont") {
620 arg.labelfont = lyxRead(lex, arg.labelfont);
622 lex.printError("Unknown tag");
626 if (arg.labelstring.empty())
627 LYXERR0("Incomplete Argument definition!");
629 postcommandargs_[nr] = arg;
631 latexargs_[nr] = arg;
635 Layout::LaTeXArgMap InsetLayout::args() const
637 Layout::LaTeXArgMap args = latexargs_;
638 if (!postcommandargs_.empty())
639 args.insert(postcommandargs_.begin(), postcommandargs_.end());
644 unsigned int InsetLayout::optArgs() const
647 Layout::LaTeXArgMap const args = InsetLayout::args();
648 Layout::LaTeXArgMap::const_iterator it = args.begin();
649 for (; it != args.end(); ++it) {
650 if (!(*it).second.mandatory)
657 unsigned int InsetLayout::requiredArgs() const
660 Layout::LaTeXArgMap const args = InsetLayout::args();
661 Layout::LaTeXArgMap::const_iterator it = args.begin();
662 for (; it != args.end(); ++it) {
663 if ((*it).second.mandatory)