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), forcelocalfontswitch_(false)
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)
90 IL_FORCE_LOCAL_FONT_SWITCH,
116 IL_PARBREAKISNEWLINE,
127 LexerKeyword elementTags[] = {
128 { "argument", IL_ARGUMENT },
129 { "babelpreamble", IL_BABELPREAMBLE },
130 { "bgcolor", IL_BGCOLOR },
131 { "contentaslabel", IL_CONTENTASLABEL },
132 { "copystyle", IL_COPYSTYLE },
133 { "counter", IL_COUNTER},
134 { "custompars", IL_CUSTOMPARS },
135 { "decoration", IL_DECORATION },
136 { "display", IL_DISPLAY },
139 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
140 { "forceltr", IL_FORCELTR },
141 { "forceplain", IL_FORCEPLAIN },
142 { "freespacing", IL_FREESPACING },
143 { "htmlattr", IL_HTMLATTR },
144 { "htmlforcecss", IL_HTMLFORCECSS },
145 { "htmlinnerattr", IL_HTMLINNERATTR},
146 { "htmlinnertag", IL_HTMLINNERTAG},
147 { "htmlisblock", IL_HTMLISBLOCK},
148 { "htmllabel", IL_HTMLLABEL },
149 { "htmlpreamble", IL_HTMLPREAMBLE },
150 { "htmlstyle", IL_HTMLSTYLE },
151 { "htmltag", IL_HTMLTAG },
152 { "intoc", IL_INTOC },
153 { "keepempty", IL_KEEPEMPTY },
154 { "labelfont", IL_LABELFONT },
155 { "labelstring", IL_LABELSTRING },
156 { "langpreamble", IL_LANGPREAMBLE },
157 { "latexname", IL_LATEXNAME },
158 { "latexparam", IL_LATEXPARAM },
159 { "latextype", IL_LATEXTYPE },
160 { "leftdelim", IL_LEFTDELIM },
161 { "lyxtype", IL_LYXTYPE },
162 { "multipar", IL_MULTIPAR },
163 { "needprotect", IL_NEEDPROTECT },
164 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
165 { "passthru", IL_PASSTHRU },
166 { "preamble", IL_PREAMBLE },
167 { "refprefix", IL_REFPREFIX },
168 { "requires", IL_REQUIRES },
169 { "resetsfont", IL_RESETSFONT },
170 { "rightdelim", IL_RIGHTDELIM },
171 { "spellcheck", IL_SPELLCHECK }
174 lex.pushTable(elementTags);
176 labelfont_ = inherit_font;
177 bgcolor_ = Color_none;
179 // whether we've read the CustomPars or ForcePlain tag
180 // for issuing a warning in case MultiPars comes later
181 bool readCustomOrPlain = false;
184 while (!getout && lex.isOK()) {
187 case Lexer::LEX_UNDEF:
188 lex.printError("Unknown InsetLayout tag");
195 // Perhaps a more elegant way to deal with the next two would be the
196 // way this sort of thing is handled in Layout::read(), namely, by
199 // make sure that we have the right sort of name.
200 if (name_ != from_ascii("undefined")
201 && name_.substr(0,5) != from_ascii("Flex:")) {
202 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
203 "This one has the name `" << to_utf8(name_) << "'\n"
204 "Ignoring LyXType declaration.");
209 lyxtype_ = translateLyXType(lt);
210 if (lyxtype_ == NOLYXTYPE)
211 LYXERR0("Unknown LyXType `" << lt << "'.");
212 if (lyxtype_ == CHARSTYLE)
219 latextype_ = translateLaTeXType(lt);
220 if (latextype_ == ILT_ERROR)
221 LYXERR0("Unknown LaTeXType `" << lt << "'.");
229 decoration_ = translateDecoration(tmp);
236 latexparam_ = support::subst(tmp, """, "\"");
241 case IL_FORCE_LOCAL_FONT_SWITCH:
242 lex >> forcelocalfontswitch_;
248 labelfont_ = lyxRead(lex, inherit_font);
258 // the defaults for these depend upon multipar_
259 if (readCustomOrPlain)
260 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
261 "Previous value may be overwritten!");
262 readCustomOrPlain = false;
263 custompars_ = multipar_;
264 forceplain_ = !multipar_;
271 readCustomOrPlain = true;
275 readCustomOrPlain = true;
280 case IL_PARBREAKISNEWLINE:
281 lex >> parbreakisnewline_;
292 case IL_CONTENTASLABEL:
293 lex >> contentaslabel_;
296 // initialize with a known style
299 style = support::subst(style, '_', ' ');
301 // We don't want to apply the algorithm in DocumentClass::insetLayout()
302 // here. So we do it the long way.
303 TextClass::InsetLayouts::const_iterator it =
304 tclass.insetLayouts().find(style);
305 if (it != tclass.insetLayouts().end()) {
306 docstring const tmpname = name_;
307 this->operator=(it->second);
310 LYXERR0("Cannot copy unknown InsetLayout `"
312 << "All InsetLayouts so far:");
313 TextClass::InsetLayouts::const_iterator lit =
314 tclass.insetLayouts().begin();
315 TextClass::InsetLayouts::const_iterator len =
316 tclass.insetLayouts().end();
317 for (; lit != len; ++lit)
318 lyxerr << lit->second.name() << "\n";
324 font_ = lyxRead(lex, inherit_font);
325 // If you want to define labelfont, you need to do so after
335 bgcolor_ = lcolor.getFromLyXName(tmp);
338 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
340 case IL_BABELPREAMBLE:
341 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
343 case IL_LANGPREAMBLE:
344 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
355 case IL_HTMLFORCECSS:
356 lex >> htmlforcecss_;
358 case IL_HTMLINNERTAG:
359 lex >> htmlinnertag_;
361 case IL_HTMLINNERATTR:
362 lex >> htmlinnerattr_;
371 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
373 case IL_HTMLPREAMBLE:
374 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
378 vector<string> const req
379 = support::getVectorFromString(lex.getString());
380 requires_.insert(req.begin(), req.end());
398 // Here add element to list if getout == true
402 // The label font is generally used as-is without
403 // any realization against a given context.
404 labelfont_.realize(sane_font);
411 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
414 if (support::compare_ascii_no_case(str, "charstyle") == 0)
415 return InsetLayout::CHARSTYLE;
416 if (support::compare_ascii_no_case(str, "custom") == 0)
417 return InsetLayout::CUSTOM;
418 if (support::compare_ascii_no_case(str, "element") == 0)
419 return InsetLayout::ELEMENT;
420 if (support::compare_ascii_no_case(str, "end") == 0)
421 return InsetLayout::END;
422 if (support::compare_ascii_no_case(str, "standard") == 0)
423 return InsetLayout::STANDARD;
424 return InsetLayout::NOLYXTYPE;
428 string const & InsetLayout::htmltag() const
430 if (htmltag_.empty())
431 htmltag_ = multipar_ ? "div" : "span";
436 string const & InsetLayout::htmlattr() const
438 if (htmlattr_.empty())
439 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
444 string const & InsetLayout::htmlinnerattr() const
446 if (htmlinnerattr_.empty())
447 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
448 return htmlinnerattr_;
452 string InsetLayout::defaultCSSClass() const
454 if (!defaultcssclass_.empty())
455 return defaultcssclass_;
457 string n = to_utf8(name());
458 string::const_iterator it = n.begin();
459 string::const_iterator en = n.end();
460 for (; it != en; ++it) {
461 if (!isAlphaASCII(*it))
463 else if (isLower(*it))
466 d += support::lowercase(*it);
468 // are there other characters we need to remove?
469 defaultcssclass_ = d;
470 return defaultcssclass_;
474 void InsetLayout::makeDefaultCSS() const
476 if (!htmldefaultstyle_.empty())
478 docstring const mainfontCSS = font_.asCSS();
479 if (!mainfontCSS.empty())
481 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
482 mainfontCSS + from_ascii("\n}\n");
486 docstring InsetLayout::htmlstyle() const
488 if (!htmlstyle_.empty() && !htmlforcecss_)
490 if (htmldefaultstyle_.empty())
492 docstring retval = htmldefaultstyle_;
493 if (!htmlstyle_.empty())
494 retval += '\n' + htmlstyle_ + '\n';
498 void InsetLayout::readArgument(Lexer & lex)
500 Layout::latexarg arg;
501 arg.mandatory = false;
503 bool finished = false;
504 arg.font = inherit_font;
505 arg.labelfont = inherit_font;
508 while (!finished && lex.isOK() && !error) {
510 string const tok = support::ascii_lowercase(lex.getString());
514 } else if (tok == "endargument") {
516 } else if (tok == "labelstring") {
518 arg.labelstring = lex.getDocString();
519 } else if (tok == "mandatory") {
521 arg.mandatory = lex.getBool();
522 } else if (tok == "leftdelim") {
524 arg.ldelim = lex.getDocString();
525 } else if (tok == "rightdelim") {
527 arg.rdelim = lex.getDocString();
528 } else if (tok == "tooltip") {
530 arg.tooltip = lex.getDocString();
531 } else if (tok == "requires") {
533 arg.requires = lex.getString();
534 } else if (tok == "decoration") {
536 arg.decoration = lex.getString();
537 } else if (tok == "font") {
538 arg.font = lyxRead(lex, arg.font);
539 } else if (tok == "labelfont") {
540 arg.labelfont = lyxRead(lex, arg.labelfont);
542 lex.printError("Unknown tag");
546 if (arg.labelstring.empty())
547 LYXERR0("Incomplete Argument definition!");
549 latexargs_[nr] = arg;
552 unsigned int InsetLayout::optArgs() const
555 Layout::LaTeXArgMap::const_iterator it = latexargs_.begin();
556 for (; it != latexargs_.end(); ++it) {
557 if (!(*it).second.mandatory)
564 unsigned int InsetLayout::requiredArgs() const
567 Layout::LaTeXArgMap::const_iterator it = latexargs_.begin();
568 for (; it != latexargs_.end(); ++it) {
569 if ((*it).second.mandatory)