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,
117 IL_PARBREAKISNEWLINE,
129 LexerKeyword elementTags[] = {
130 { "argument", IL_ARGUMENT },
131 { "babelpreamble", IL_BABELPREAMBLE },
132 { "bgcolor", IL_BGCOLOR },
133 { "contentaslabel", IL_CONTENTASLABEL },
134 { "copystyle", IL_COPYSTYLE },
135 { "counter", IL_COUNTER},
136 { "custompars", IL_CUSTOMPARS },
137 { "decoration", IL_DECORATION },
138 { "display", IL_DISPLAY },
141 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
142 { "forceltr", IL_FORCELTR },
143 { "forceownlines", IL_FORCEOWNLINES },
144 { "forceplain", IL_FORCEPLAIN },
145 { "freespacing", IL_FREESPACING },
146 { "htmlattr", IL_HTMLATTR },
147 { "htmlforcecss", IL_HTMLFORCECSS },
148 { "htmlinnerattr", IL_HTMLINNERATTR},
149 { "htmlinnertag", IL_HTMLINNERTAG},
150 { "htmlisblock", IL_HTMLISBLOCK},
151 { "htmllabel", IL_HTMLLABEL },
152 { "htmlpreamble", IL_HTMLPREAMBLE },
153 { "htmlstyle", IL_HTMLSTYLE },
154 { "htmltag", IL_HTMLTAG },
155 { "intoc", IL_INTOC },
156 { "keepempty", IL_KEEPEMPTY },
157 { "labelfont", IL_LABELFONT },
158 { "labelstring", IL_LABELSTRING },
159 { "langpreamble", IL_LANGPREAMBLE },
160 { "latexname", IL_LATEXNAME },
161 { "latexparam", IL_LATEXPARAM },
162 { "latextype", IL_LATEXTYPE },
163 { "leftdelim", IL_LEFTDELIM },
164 { "lyxtype", IL_LYXTYPE },
165 { "multipar", IL_MULTIPAR },
166 { "needprotect", IL_NEEDPROTECT },
167 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
168 { "passthru", IL_PASSTHRU },
169 { "preamble", IL_PREAMBLE },
170 { "refprefix", IL_REFPREFIX },
171 { "requires", IL_REQUIRES },
172 { "resetargs", IL_RESETARGS },
173 { "resetsfont", IL_RESETSFONT },
174 { "rightdelim", IL_RIGHTDELIM },
175 { "spellcheck", IL_SPELLCHECK }
178 lex.pushTable(elementTags);
180 labelfont_ = inherit_font;
181 bgcolor_ = Color_none;
183 // whether we've read the CustomPars or ForcePlain tag
184 // for issuing a warning in case MultiPars comes later
185 bool readCustomOrPlain = false;
188 while (!getout && lex.isOK()) {
191 case Lexer::LEX_UNDEF:
192 lex.printError("Unknown InsetLayout tag");
199 // Perhaps a more elegant way to deal with the next two would be the
200 // way this sort of thing is handled in Layout::read(), namely, by
203 // make sure that we have the right sort of name.
204 if (name_ != from_ascii("undefined")
205 && name_.substr(0,5) != from_ascii("Flex:")) {
206 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
207 "This one has the name `" << to_utf8(name_) << "'\n"
208 "Ignoring LyXType declaration.");
213 lyxtype_ = translateLyXType(lt);
214 if (lyxtype_ == NOLYXTYPE)
215 LYXERR0("Unknown LyXType `" << lt << "'.");
216 if (lyxtype_ == CHARSTYLE)
223 latextype_ = translateLaTeXType(lt);
224 if (latextype_ == ILT_ERROR)
225 LYXERR0("Unknown LaTeXType `" << lt << "'.");
233 decoration_ = translateDecoration(tmp);
240 latexparam_ = support::subst(tmp, """, "\"");
244 leftdelim_ = support::subst(leftdelim_, from_ascii("<br/>"),
247 case IL_FORCE_LOCAL_FONT_SWITCH:
248 lex >> forcelocalfontswitch_;
252 rightdelim_ = support::subst(rightdelim_, from_ascii("<br/>"),
256 labelfont_ = lyxRead(lex, inherit_font);
261 case IL_FORCEOWNLINES:
262 lex >> forceownlines_;
269 // the defaults for these depend upon multipar_
270 if (readCustomOrPlain)
271 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
272 "Previous value may be overwritten!");
273 readCustomOrPlain = false;
274 custompars_ = multipar_;
275 forceplain_ = !multipar_;
282 readCustomOrPlain = true;
286 readCustomOrPlain = true;
291 case IL_PARBREAKISNEWLINE:
292 lex >> parbreakisnewline_;
303 case IL_CONTENTASLABEL:
304 lex >> contentaslabel_;
307 // initialize with a known style
310 style = support::subst(style, '_', ' ');
312 // We don't want to apply the algorithm in DocumentClass::insetLayout()
313 // here. So we do it the long way.
314 TextClass::InsetLayouts::const_iterator it =
315 tclass.insetLayouts().find(style);
316 if (it != tclass.insetLayouts().end()) {
317 docstring const tmpname = name_;
318 this->operator=(it->second);
321 LYXERR0("Cannot copy unknown InsetLayout `"
323 << "All InsetLayouts so far:");
324 TextClass::InsetLayouts::const_iterator lit =
325 tclass.insetLayouts().begin();
326 TextClass::InsetLayouts::const_iterator len =
327 tclass.insetLayouts().end();
328 for (; lit != len; ++lit)
329 lyxerr << lit->second.name() << "\n";
335 font_ = lyxRead(lex, inherit_font);
336 // If you want to define labelfont, you need to do so after
346 postcommandargs_.clear();
354 bgcolor_ = lcolor.getFromLyXName(tmp);
357 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
359 case IL_BABELPREAMBLE:
360 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
362 case IL_LANGPREAMBLE:
363 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
374 case IL_HTMLFORCECSS:
375 lex >> htmlforcecss_;
377 case IL_HTMLINNERTAG:
378 lex >> htmlinnertag_;
380 case IL_HTMLINNERATTR:
381 lex >> htmlinnerattr_;
390 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
392 case IL_HTMLPREAMBLE:
393 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
397 vector<string> const req
398 = support::getVectorFromString(lex.getString());
399 requires_.insert(req.begin(), req.end());
417 // Here add element to list if getout == true
421 // The label font is generally used as-is without
422 // any realization against a given context.
423 labelfont_.realize(sane_font);
430 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
433 if (support::compare_ascii_no_case(str, "charstyle") == 0)
434 return InsetLayout::CHARSTYLE;
435 if (support::compare_ascii_no_case(str, "custom") == 0)
436 return InsetLayout::CUSTOM;
437 if (support::compare_ascii_no_case(str, "element") == 0)
438 return InsetLayout::ELEMENT;
439 if (support::compare_ascii_no_case(str, "end") == 0)
440 return InsetLayout::END;
441 if (support::compare_ascii_no_case(str, "standard") == 0)
442 return InsetLayout::STANDARD;
443 return InsetLayout::NOLYXTYPE;
447 string const & InsetLayout::htmltag() const
449 if (htmltag_.empty())
450 htmltag_ = multipar_ ? "div" : "span";
455 string const & InsetLayout::htmlattr() const
457 if (htmlattr_.empty())
458 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
463 string const & InsetLayout::htmlinnerattr() const
465 if (htmlinnerattr_.empty())
466 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
467 return htmlinnerattr_;
471 string InsetLayout::defaultCSSClass() const
473 if (!defaultcssclass_.empty())
474 return defaultcssclass_;
476 string n = to_utf8(name());
477 string::const_iterator it = n.begin();
478 string::const_iterator en = n.end();
479 for (; it != en; ++it) {
480 if (!isAlphaASCII(*it))
482 else if (isLower(*it))
485 d += support::lowercase(*it);
487 // are there other characters we need to remove?
488 defaultcssclass_ = d;
489 return defaultcssclass_;
493 void InsetLayout::makeDefaultCSS() const
495 if (!htmldefaultstyle_.empty())
497 docstring const mainfontCSS = font_.asCSS();
498 if (!mainfontCSS.empty())
500 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
501 mainfontCSS + from_ascii("\n}\n");
505 docstring InsetLayout::htmlstyle() const
507 if (!htmlstyle_.empty() && !htmlforcecss_)
509 if (htmldefaultstyle_.empty())
511 docstring retval = htmldefaultstyle_;
512 if (!htmlstyle_.empty())
513 retval += '\n' + htmlstyle_ + '\n';
517 void InsetLayout::readArgument(Lexer & lex)
519 Layout::latexarg arg;
520 arg.mandatory = false;
521 arg.autoinsert = false;
523 bool finished = false;
524 arg.font = inherit_font;
525 arg.labelfont = inherit_font;
528 bool const postcmd = support::prefixIs(nr, "post:");
529 while (!finished && lex.isOK() && !error) {
531 string const tok = support::ascii_lowercase(lex.getString());
535 } else if (tok == "endargument") {
537 } else if (tok == "labelstring") {
539 arg.labelstring = lex.getDocString();
540 } else if (tok == "menustring") {
542 arg.menustring = lex.getDocString();
543 } else if (tok == "mandatory") {
545 arg.mandatory = lex.getBool();
546 } else if (tok == "autoinsert") {
548 arg.autoinsert = lex.getBool();
549 } else if (tok == "leftdelim") {
551 arg.ldelim = lex.getDocString();
552 arg.ldelim = support::subst(arg.ldelim,
553 from_ascii("<br/>"), from_ascii("\n"));
554 } else if (tok == "rightdelim") {
556 arg.rdelim = lex.getDocString();
557 arg.rdelim = support::subst(arg.rdelim,
558 from_ascii("<br/>"), from_ascii("\n"));
559 } else if (tok == "defaultarg") {
561 arg.defaultarg = lex.getDocString();
562 } else if (tok == "presetarg") {
564 arg.presetarg = lex.getDocString();
565 } else if (tok == "tooltip") {
567 arg.tooltip = lex.getDocString();
568 } else if (tok == "requires") {
570 arg.requires = lex.getString();
571 } else if (tok == "decoration") {
573 arg.decoration = lex.getString();
574 } else if (tok == "font") {
575 arg.font = lyxRead(lex, arg.font);
576 } else if (tok == "labelfont") {
577 arg.labelfont = lyxRead(lex, arg.labelfont);
579 lex.printError("Unknown tag");
583 if (arg.labelstring.empty())
584 LYXERR0("Incomplete Argument definition!");
586 postcommandargs_[nr] = arg;
588 latexargs_[nr] = arg;
592 Layout::LaTeXArgMap InsetLayout::args() const
594 Layout::LaTeXArgMap args = latexargs_;
595 if (!postcommandargs_.empty())
596 args.insert(postcommandargs_.begin(), postcommandargs_.end());
601 unsigned int InsetLayout::optArgs() const
604 Layout::LaTeXArgMap const args = InsetLayout::args();
605 Layout::LaTeXArgMap::const_iterator it = args.begin();
606 for (; it != args.end(); ++it) {
607 if (!(*it).second.mandatory)
614 unsigned int InsetLayout::requiredArgs() const
617 Layout::LaTeXArgMap const args = InsetLayout::args();
618 Layout::LaTeXArgMap::const_iterator it = args.begin();
619 for (; it != args.end(); ++it) {
620 if ((*it).second.mandatory)