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 `"
325 << "All InsetLayouts so far:");
326 TextClass::InsetLayouts::const_iterator lit =
327 tclass.insetLayouts().begin();
328 TextClass::InsetLayouts::const_iterator len =
329 tclass.insetLayouts().end();
330 for (; lit != len; ++lit)
331 lyxerr << lit->second.name() << "\n";
335 case IL_OBSOLETEDBY: {
338 style = support::subst(style, '_', ' ');
340 // We don't want to apply the algorithm in DocumentClass::insetLayout()
341 // here. So we do it the long way.
342 TextClass::InsetLayouts::const_iterator it =
343 tclass.insetLayouts().find(style);
344 if (it != tclass.insetLayouts().end()) {
345 docstring const tmpname = name_;
346 this->operator=(it->second);
348 if (obsoleted_by().empty())
349 obsoleted_by_ = style;
351 LYXERR0("Cannot replace with unknown InsetLayout `"
358 font_ = lyxRead(lex, inherit_font);
359 // If you want to define labelfont, you need to do so after
369 postcommandargs_.clear();
377 bgcolor_ = lcolor.getFromLyXName(tmp);
380 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
382 case IL_BABELPREAMBLE:
383 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
385 case IL_LANGPREAMBLE:
386 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
397 case IL_HTMLFORCECSS:
398 lex >> htmlforcecss_;
400 case IL_HTMLINNERTAG:
401 lex >> htmlinnertag_;
403 case IL_HTMLINNERATTR:
404 lex >> htmlinnerattr_;
413 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
415 case IL_HTMLPREAMBLE:
416 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
420 vector<string> const req
421 = support::getVectorFromString(lex.getString());
422 requires_.insert(req.begin(), req.end());
440 // Here add element to list if getout == true
444 // The label font is generally used as-is without
445 // any realization against a given context.
446 labelfont_.realize(sane_font);
453 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
456 if (support::compare_ascii_no_case(str, "charstyle") == 0)
457 return InsetLayout::CHARSTYLE;
458 if (support::compare_ascii_no_case(str, "custom") == 0)
459 return InsetLayout::CUSTOM;
460 if (support::compare_ascii_no_case(str, "element") == 0)
461 return InsetLayout::ELEMENT;
462 if (support::compare_ascii_no_case(str, "end") == 0)
463 return InsetLayout::END;
464 if (support::compare_ascii_no_case(str, "standard") == 0)
465 return InsetLayout::STANDARD;
466 return InsetLayout::NOLYXTYPE;
470 string const & InsetLayout::htmltag() const
472 if (htmltag_.empty())
473 htmltag_ = multipar_ ? "div" : "span";
478 string const & InsetLayout::htmlattr() const
480 if (htmlattr_.empty())
481 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
486 string const & InsetLayout::htmlinnerattr() const
488 if (htmlinnerattr_.empty())
489 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
490 return htmlinnerattr_;
494 string InsetLayout::defaultCSSClass() const
496 if (!defaultcssclass_.empty())
497 return defaultcssclass_;
499 string n = to_utf8(name());
500 string::const_iterator it = n.begin();
501 string::const_iterator en = n.end();
502 for (; it != en; ++it) {
503 if (!isAlphaASCII(*it))
505 else if (isLower(*it))
508 d += support::lowercase(*it);
510 // are there other characters we need to remove?
511 defaultcssclass_ = d;
512 return defaultcssclass_;
516 void InsetLayout::makeDefaultCSS() const
518 if (!htmldefaultstyle_.empty())
520 docstring const mainfontCSS = font_.asCSS();
521 if (!mainfontCSS.empty())
523 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
524 mainfontCSS + from_ascii("\n}\n");
528 docstring InsetLayout::htmlstyle() const
530 if (!htmlstyle_.empty() && !htmlforcecss_)
532 if (htmldefaultstyle_.empty())
534 docstring retval = htmldefaultstyle_;
535 if (!htmlstyle_.empty())
536 retval += '\n' + htmlstyle_ + '\n';
540 void InsetLayout::readArgument(Lexer & lex)
542 Layout::latexarg arg;
543 arg.mandatory = false;
544 arg.autoinsert = false;
546 bool finished = false;
547 arg.font = inherit_font;
548 arg.labelfont = inherit_font;
551 bool const postcmd = support::prefixIs(nr, "post:");
552 while (!finished && lex.isOK() && !error) {
554 string const tok = support::ascii_lowercase(lex.getString());
558 } else if (tok == "endargument") {
560 } else if (tok == "labelstring") {
562 arg.labelstring = lex.getDocString();
563 } else if (tok == "menustring") {
565 arg.menustring = lex.getDocString();
566 } else if (tok == "mandatory") {
568 arg.mandatory = lex.getBool();
569 } else if (tok == "autoinsert") {
571 arg.autoinsert = lex.getBool();
572 } else if (tok == "leftdelim") {
574 arg.ldelim = lex.getDocString();
575 arg.ldelim = support::subst(arg.ldelim,
576 from_ascii("<br/>"), from_ascii("\n"));
577 } else if (tok == "rightdelim") {
579 arg.rdelim = lex.getDocString();
580 arg.rdelim = support::subst(arg.rdelim,
581 from_ascii("<br/>"), from_ascii("\n"));
582 } else if (tok == "defaultarg") {
584 arg.defaultarg = lex.getDocString();
585 } else if (tok == "presetarg") {
587 arg.presetarg = lex.getDocString();
588 } else if (tok == "tooltip") {
590 arg.tooltip = lex.getDocString();
591 } else if (tok == "requires") {
593 arg.requires = lex.getString();
594 } else if (tok == "decoration") {
596 arg.decoration = lex.getString();
597 } else if (tok == "font") {
598 arg.font = lyxRead(lex, arg.font);
599 } else if (tok == "labelfont") {
600 arg.labelfont = lyxRead(lex, arg.labelfont);
602 lex.printError("Unknown tag");
606 if (arg.labelstring.empty())
607 LYXERR0("Incomplete Argument definition!");
609 postcommandargs_[nr] = arg;
611 latexargs_[nr] = arg;
615 Layout::LaTeXArgMap InsetLayout::args() const
617 Layout::LaTeXArgMap args = latexargs_;
618 if (!postcommandargs_.empty())
619 args.insert(postcommandargs_.begin(), postcommandargs_.end());
624 unsigned int InsetLayout::optArgs() const
627 Layout::LaTeXArgMap const args = InsetLayout::args();
628 Layout::LaTeXArgMap::const_iterator it = args.begin();
629 for (; it != args.end(); ++it) {
630 if (!(*it).second.mandatory)
637 unsigned int InsetLayout::requiredArgs() const
640 Layout::LaTeXArgMap const args = InsetLayout::args();
641 Layout::LaTeXArgMap::const_iterator it = args.begin();
642 for (; it != args.end(); ++it) {
643 if ((*it).second.mandatory)