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)
114 IL_PARBREAKISNEWLINE,
124 LexerKeyword elementTags[] = {
125 { "argument", IL_ARGUMENT },
126 { "babelpreamble", IL_BABELPREAMBLE },
127 { "bgcolor", IL_BGCOLOR },
128 { "contentaslabel", IL_CONTENTASLABEL },
129 { "copystyle", IL_COPYSTYLE },
130 { "counter", IL_COUNTER},
131 { "custompars", IL_CUSTOMPARS },
132 { "decoration", IL_DECORATION },
133 { "display", IL_DISPLAY },
136 { "forceltr", IL_FORCELTR },
137 { "forceplain", IL_FORCEPLAIN },
138 { "freespacing", IL_FREESPACING },
139 { "htmlattr", IL_HTMLATTR },
140 { "htmlforcecss", IL_HTMLFORCECSS },
141 { "htmlinnerattr", IL_HTMLINNERATTR},
142 { "htmlinnertag", IL_HTMLINNERTAG},
143 { "htmlisblock", IL_HTMLISBLOCK},
144 { "htmllabel", IL_HTMLLABEL },
145 { "htmlpreamble", IL_HTMLPREAMBLE },
146 { "htmlstyle", IL_HTMLSTYLE },
147 { "htmltag", IL_HTMLTAG },
148 { "intoc", IL_INTOC },
149 { "keepempty", IL_KEEPEMPTY },
150 { "labelfont", IL_LABELFONT },
151 { "labelstring", IL_LABELSTRING },
152 { "langpreamble", IL_LANGPREAMBLE },
153 { "latexname", IL_LATEXNAME },
154 { "latexparam", IL_LATEXPARAM },
155 { "latextype", IL_LATEXTYPE },
156 { "lyxtype", IL_LYXTYPE },
157 { "multipar", IL_MULTIPAR },
158 { "needprotect", IL_NEEDPROTECT },
159 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
160 { "passthru", IL_PASSTHRU },
161 { "preamble", IL_PREAMBLE },
162 { "refprefix", IL_REFPREFIX },
163 { "requires", IL_REQUIRES },
164 { "resetsfont", IL_RESETSFONT },
165 { "spellcheck", IL_SPELLCHECK }
168 lex.pushTable(elementTags);
170 labelfont_ = inherit_font;
171 bgcolor_ = Color_none;
173 // whether we've read the CustomPars or ForcePlain tag
174 // for issuing a warning in case MultiPars comes later
175 bool readCustomOrPlain = false;
178 while (!getout && lex.isOK()) {
181 case Lexer::LEX_UNDEF:
182 lex.printError("Unknown InsetLayout tag");
189 // Perhaps a more elegant way to deal with the next two would be the
190 // way this sort of thing is handled in Layout::read(), namely, by
193 // make sure that we have the right sort of name.
194 if (name_ != from_ascii("undefined")
195 && name_.substr(0,5) != from_ascii("Flex:")) {
196 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
197 "This one has the name `" << to_utf8(name_) << "'\n"
198 "Ignoring LyXType declaration.");
203 lyxtype_ = translateLyXType(lt);
204 if (lyxtype_ == NOLYXTYPE)
205 LYXERR0("Unknown LyXType `" << lt << "'.");
206 if (lyxtype_ == CHARSTYLE)
213 latextype_ = translateLaTeXType(lt);
214 if (latextype_ == ILT_ERROR)
215 LYXERR0("Unknown LaTeXType `" << lt << "'.");
223 decoration_ = translateDecoration(tmp);
230 latexparam_ = support::subst(tmp, """, "\"");
233 labelfont_ = lyxRead(lex, inherit_font);
243 // the defaults for these depend upon multipar_
244 if (readCustomOrPlain)
245 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
246 "Previous value may be overwritten!");
247 readCustomOrPlain = false;
248 custompars_ = multipar_;
249 forceplain_ = !multipar_;
256 readCustomOrPlain = true;
260 readCustomOrPlain = true;
265 case IL_PARBREAKISNEWLINE:
266 lex >> parbreakisnewline_;
277 case IL_CONTENTASLABEL:
278 lex >> contentaslabel_;
281 // initialize with a known style
284 style = support::subst(style, '_', ' ');
286 // We don't want to apply the algorithm in DocumentClass::insetLayout()
287 // here. So we do it the long way.
288 TextClass::InsetLayouts::const_iterator it =
289 tclass.insetLayouts().find(style);
290 if (it != tclass.insetLayouts().end()) {
291 docstring const tmpname = name_;
292 this->operator=(it->second);
295 LYXERR0("Cannot copy unknown InsetLayout `"
297 << "All InsetLayouts so far:");
298 TextClass::InsetLayouts::const_iterator lit =
299 tclass.insetLayouts().begin();
300 TextClass::InsetLayouts::const_iterator len =
301 tclass.insetLayouts().end();
302 for (; lit != len; ++lit)
303 lyxerr << lit->second.name() << "\n";
309 font_ = lyxRead(lex, inherit_font);
310 // If you want to define labelfont, you need to do so after
320 bgcolor_ = lcolor.getFromLyXName(tmp);
323 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
325 case IL_BABELPREAMBLE:
326 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
328 case IL_LANGPREAMBLE:
329 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
340 case IL_HTMLFORCECSS:
341 lex >> htmlforcecss_;
343 case IL_HTMLINNERTAG:
344 lex >> htmlinnertag_;
346 case IL_HTMLINNERATTR:
347 lex >> htmlinnerattr_;
356 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
358 case IL_HTMLPREAMBLE:
359 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
363 vector<string> const req
364 = support::getVectorFromString(lex.getString());
365 requires_.insert(req.begin(), req.end());
383 // Here add element to list if getout == true
387 // The label font is generally used as-is without
388 // any realization against a given context.
389 labelfont_.realize(sane_font);
396 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
399 if (support::compare_ascii_no_case(str, "charstyle") == 0)
400 return InsetLayout::CHARSTYLE;
401 if (support::compare_ascii_no_case(str, "custom") == 0)
402 return InsetLayout::CUSTOM;
403 if (support::compare_ascii_no_case(str, "element") == 0)
404 return InsetLayout::ELEMENT;
405 if (support::compare_ascii_no_case(str, "end") == 0)
406 return InsetLayout::END;
407 if (support::compare_ascii_no_case(str, "standard") == 0)
408 return InsetLayout::STANDARD;
409 return InsetLayout::NOLYXTYPE;
413 string const & InsetLayout::htmltag() const
415 if (htmltag_.empty())
416 htmltag_ = multipar_ ? "div" : "span";
421 string const & InsetLayout::htmlattr() const
423 if (htmlattr_.empty())
424 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
429 string const & InsetLayout::htmlinnerattr() const
431 if (htmlinnerattr_.empty())
432 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
433 return htmlinnerattr_;
437 string InsetLayout::defaultCSSClass() const
439 if (!defaultcssclass_.empty())
440 return defaultcssclass_;
442 string n = to_utf8(name());
443 string::const_iterator it = n.begin();
444 string::const_iterator en = n.end();
445 for (; it != en; ++it) {
446 if (!isAlphaASCII(*it))
448 else if (isLower(*it))
451 d += support::lowercase(*it);
453 // are there other characters we need to remove?
454 defaultcssclass_ = d;
455 return defaultcssclass_;
459 void InsetLayout::makeDefaultCSS() const
461 if (!htmldefaultstyle_.empty())
463 docstring const mainfontCSS = font_.asCSS();
464 if (!mainfontCSS.empty())
466 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
467 mainfontCSS + from_ascii("\n}\n");
471 docstring InsetLayout::htmlstyle() const
473 if (!htmlstyle_.empty() && !htmlforcecss_)
475 if (htmldefaultstyle_.empty())
477 docstring retval = htmldefaultstyle_;
478 if (!htmlstyle_.empty())
479 retval += '\n' + htmlstyle_ + '\n';
483 void InsetLayout::readArgument(Lexer & lex)
485 Layout::latexarg arg;
486 arg.mandatory = false;
488 bool finished = false;
491 while (!finished && lex.isOK() && !error) {
493 string const tok = support::ascii_lowercase(lex.getString());
497 } else if (tok == "endargument") {
499 } else if (tok == "labelstring") {
501 arg.labelstring = lex.getDocString();
502 } else if (tok == "mandatory") {
504 arg.mandatory = lex.getBool();
505 } else if (tok == "leftdelim") {
507 arg.ldelim = lex.getDocString();
508 } else if (tok == "rightdelim") {
510 arg.rdelim = lex.getDocString();
511 } else if (tok == "tooltip") {
513 arg.tooltip = lex.getDocString();
514 } else if (tok == "shortcut") {
516 arg.shortcut = lex.getString();
517 } else if (tok == "requires") {
519 arg.requires = lex.getString();
521 lex.printError("Unknown tag");
525 if (arg.labelstring.empty())
526 LYXERR0("Incomplete Argument definition!");
528 latexargs_[nr] = arg;
531 int InsetLayout::optArgs() const
534 Layout::LaTeXArgMap::const_iterator it = latexargs_.begin();
535 for (; it != latexargs_.end(); ++it) {
536 if (!(*it).second.mandatory)
543 int InsetLayout::requiredArgs() const
546 Layout::LaTeXArgMap::const_iterator it = latexargs_.begin();
547 for (; it != latexargs_.end(); ++it) {
548 if ((*it).second.mandatory)