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_(sane_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_(true), display_(true)
46 labelfont_.setColor(Color_error);
52 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
54 if (support::compare_ascii_no_case(str, "classic") == 0)
55 return InsetLayout::CLASSIC;
56 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
57 return InsetLayout::MINIMALISTIC;
58 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
59 return InsetLayout::CONGLOMERATE;
60 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)
115 IL_PARBREAKISNEWLINE,
126 LexerKeyword elementTags[] = {
127 { "argument", IL_ARGUMENT },
128 { "babelpreamble", IL_BABELPREAMBLE },
129 { "bgcolor", IL_BGCOLOR },
130 { "contentaslabel", IL_CONTENTASLABEL },
131 { "copystyle", IL_COPYSTYLE },
132 { "counter", IL_COUNTER},
133 { "custompars", IL_CUSTOMPARS },
134 { "decoration", IL_DECORATION },
135 { "display", IL_DISPLAY },
138 { "forceltr", IL_FORCELTR },
139 { "forceplain", IL_FORCEPLAIN },
140 { "freespacing", IL_FREESPACING },
141 { "htmlattr", IL_HTMLATTR },
142 { "htmlforcecss", IL_HTMLFORCECSS },
143 { "htmlinnerattr", IL_HTMLINNERATTR},
144 { "htmlinnertag", IL_HTMLINNERTAG},
145 { "htmlisblock", IL_HTMLISBLOCK},
146 { "htmllabel", IL_HTMLLABEL },
147 { "htmlpreamble", IL_HTMLPREAMBLE },
148 { "htmlstyle", IL_HTMLSTYLE },
149 { "htmltag", IL_HTMLTAG },
150 { "intoc", IL_INTOC },
151 { "keepempty", IL_KEEPEMPTY },
152 { "labelfont", IL_LABELFONT },
153 { "labelstring", IL_LABELSTRING },
154 { "langpreamble", IL_LANGPREAMBLE },
155 { "latexname", IL_LATEXNAME },
156 { "latexparam", IL_LATEXPARAM },
157 { "latextype", IL_LATEXTYPE },
158 { "leftdelim", IL_LEFTDELIM },
159 { "lyxtype", IL_LYXTYPE },
160 { "multipar", IL_MULTIPAR },
161 { "needprotect", IL_NEEDPROTECT },
162 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
163 { "passthru", IL_PASSTHRU },
164 { "preamble", IL_PREAMBLE },
165 { "refprefix", IL_REFPREFIX },
166 { "requires", IL_REQUIRES },
167 { "resetsfont", IL_RESETSFONT },
168 { "rightdelim", IL_RIGHTDELIM },
169 { "spellcheck", IL_SPELLCHECK }
172 lex.pushTable(elementTags);
174 labelfont_ = inherit_font;
175 bgcolor_ = Color_none;
177 // whether we've read the CustomPars or ForcePlain tag
178 // for issuing a warning in case MultiPars comes later
179 bool readCustomOrPlain = false;
182 while (!getout && lex.isOK()) {
185 case Lexer::LEX_UNDEF:
186 lex.printError("Unknown InsetLayout tag");
193 // Perhaps a more elegant way to deal with the next two would be the
194 // way this sort of thing is handled in Layout::read(), namely, by
197 // make sure that we have the right sort of name.
198 if (name_ != from_ascii("undefined")
199 && name_.substr(0,5) != from_ascii("Flex:")) {
200 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
201 "This one has the name `" << to_utf8(name_) << "'\n"
202 "Ignoring LyXType declaration.");
207 lyxtype_ = translateLyXType(lt);
208 if (lyxtype_ == NOLYXTYPE)
209 LYXERR0("Unknown LyXType `" << lt << "'.");
210 if (lyxtype_ == CHARSTYLE)
217 latextype_ = translateLaTeXType(lt);
218 if (latextype_ == ILT_ERROR)
219 LYXERR0("Unknown LaTeXType `" << lt << "'.");
227 decoration_ = translateDecoration(tmp);
234 latexparam_ = support::subst(tmp, """, "\"");
243 labelfont_ = lyxRead(lex, inherit_font);
253 // the defaults for these depend upon multipar_
254 if (readCustomOrPlain)
255 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
256 "Previous value may be overwritten!");
257 readCustomOrPlain = false;
258 custompars_ = multipar_;
259 forceplain_ = !multipar_;
266 readCustomOrPlain = true;
270 readCustomOrPlain = true;
275 case IL_PARBREAKISNEWLINE:
276 lex >> parbreakisnewline_;
287 case IL_CONTENTASLABEL:
288 lex >> contentaslabel_;
291 // initialize with a known style
294 style = support::subst(style, '_', ' ');
296 // We don't want to apply the algorithm in DocumentClass::insetLayout()
297 // here. So we do it the long way.
298 TextClass::InsetLayouts::const_iterator it =
299 tclass.insetLayouts().find(style);
300 if (it != tclass.insetLayouts().end()) {
301 docstring const tmpname = name_;
302 this->operator=(it->second);
305 LYXERR0("Cannot copy unknown InsetLayout `"
307 << "All InsetLayouts so far:");
308 TextClass::InsetLayouts::const_iterator lit =
309 tclass.insetLayouts().begin();
310 TextClass::InsetLayouts::const_iterator len =
311 tclass.insetLayouts().end();
312 for (; lit != len; ++lit)
313 lyxerr << lit->second.name() << "\n";
319 font_ = lyxRead(lex, inherit_font);
320 // If you want to define labelfont, you need to do so after
330 bgcolor_ = lcolor.getFromLyXName(tmp);
333 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
335 case IL_BABELPREAMBLE:
336 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
338 case IL_LANGPREAMBLE:
339 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
350 case IL_HTMLFORCECSS:
351 lex >> htmlforcecss_;
353 case IL_HTMLINNERTAG:
354 lex >> htmlinnertag_;
356 case IL_HTMLINNERATTR:
357 lex >> htmlinnerattr_;
366 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
368 case IL_HTMLPREAMBLE:
369 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
373 vector<string> const req
374 = support::getVectorFromString(lex.getString());
375 requires_.insert(req.begin(), req.end());
393 // Here add element to list if getout == true
397 // The label font is generally used as-is without
398 // any realization against a given context.
399 labelfont_.realize(sane_font);
406 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
409 if (support::compare_ascii_no_case(str, "charstyle") == 0)
410 return InsetLayout::CHARSTYLE;
411 if (support::compare_ascii_no_case(str, "custom") == 0)
412 return InsetLayout::CUSTOM;
413 if (support::compare_ascii_no_case(str, "element") == 0)
414 return InsetLayout::ELEMENT;
415 if (support::compare_ascii_no_case(str, "end") == 0)
416 return InsetLayout::END;
417 if (support::compare_ascii_no_case(str, "standard") == 0)
418 return InsetLayout::STANDARD;
419 return InsetLayout::NOLYXTYPE;
423 string const & InsetLayout::htmltag() const
425 if (htmltag_.empty())
426 htmltag_ = multipar_ ? "div" : "span";
431 string const & InsetLayout::htmlattr() const
433 if (htmlattr_.empty())
434 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
439 string const & InsetLayout::htmlinnerattr() const
441 if (htmlinnerattr_.empty())
442 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
443 return htmlinnerattr_;
447 string InsetLayout::defaultCSSClass() const
449 if (!defaultcssclass_.empty())
450 return defaultcssclass_;
452 string n = to_utf8(name());
453 string::const_iterator it = n.begin();
454 string::const_iterator en = n.end();
455 for (; it != en; ++it) {
456 if (!isAlphaASCII(*it))
458 else if (isLower(*it))
461 d += support::lowercase(*it);
463 // are there other characters we need to remove?
464 defaultcssclass_ = d;
465 return defaultcssclass_;
469 void InsetLayout::makeDefaultCSS() const
471 if (!htmldefaultstyle_.empty())
473 docstring const mainfontCSS = font_.asCSS();
474 if (!mainfontCSS.empty())
476 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
477 mainfontCSS + from_ascii("\n}\n");
481 docstring InsetLayout::htmlstyle() const
483 if (!htmlstyle_.empty() && !htmlforcecss_)
485 if (htmldefaultstyle_.empty())
487 docstring retval = htmldefaultstyle_;
488 if (!htmlstyle_.empty())
489 retval += '\n' + htmlstyle_ + '\n';
493 void InsetLayout::readArgument(Lexer & lex)
495 Layout::latexarg arg;
496 arg.mandatory = false;
498 bool finished = false;
501 while (!finished && lex.isOK() && !error) {
503 string const tok = support::ascii_lowercase(lex.getString());
507 } else if (tok == "endargument") {
509 } else if (tok == "labelstring") {
511 arg.labelstring = lex.getDocString();
512 } else if (tok == "mandatory") {
514 arg.mandatory = lex.getBool();
515 } else if (tok == "leftdelim") {
517 arg.ldelim = lex.getDocString();
518 } else if (tok == "rightdelim") {
520 arg.rdelim = lex.getDocString();
521 } else if (tok == "tooltip") {
523 arg.tooltip = lex.getDocString();
524 } else if (tok == "requires") {
526 arg.requires = lex.getString();
528 lex.printError("Unknown tag");
532 if (arg.labelstring.empty())
533 LYXERR0("Incomplete Argument definition!");
535 latexargs_[nr] = arg;
538 unsigned int InsetLayout::numOptArgs() const
541 Layout::LaTeXArgMap::const_iterator it = latexargs_.begin();
542 for (; it != latexargs_.end(); ++it) {
543 if (!(*it).second.mandatory)
550 unsigned int InsetLayout::numRequiredArgs() const
553 Layout::LaTeXArgMap::const_iterator it = latexargs_.begin();
554 for (; it != latexargs_.end(); ++it) {
555 if ((*it).second.mandatory)