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"
32 using namespace lyx::support;
36 InsetLayout::InsetLayout() :
37 name_(from_ascii("undefined")), lyxtype_(STANDARD),
38 labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
39 decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(inherit_font),
40 labelfont_(sane_font), bgcolor_(Color_error),
41 fixedwidthpreambleencoding_(false), htmlforcecss_ (false),
42 htmlisblock_(true), multipar_(true), custompars_(true),
43 forceplain_(false), passthru_(false), parbreakisnewline_(false),
44 freespacing_(false), keepempty_(false), forceltr_(false),
45 forceownlines_(false), needprotect_(false), intoc_(false),
46 spellcheck_(true), resetsfont_(false), display_(true),
47 forcelocalfontswitch_(false)
49 labelfont_.setColor(Color_error);
53 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
55 if (compare_ascii_no_case(str, "classic") == 0)
56 return InsetLayout::CLASSIC;
57 if (compare_ascii_no_case(str, "minimalistic") == 0)
58 return InsetLayout::MINIMALISTIC;
59 if (compare_ascii_no_case(str, "conglomerate") == 0)
60 return InsetLayout::CONGLOMERATE;
61 return InsetLayout::DEFAULT;
66 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
68 if (compare_ascii_no_case(str, "command") == 0)
69 return InsetLayout::COMMAND;
70 if (compare_ascii_no_case(str, "environment") == 0)
71 return InsetLayout::ENVIRONMENT;
72 if (compare_ascii_no_case(str, "none") == 0)
73 return InsetLayout::NOLATEXTYPE;
74 return InsetLayout::ILT_ERROR;
80 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
92 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
94 IL_FORCE_LOCAL_FONT_SWITCH,
123 IL_PARBREAKISNEWLINE,
135 LexerKeyword elementTags[] = {
136 { "argument", IL_ARGUMENT },
137 { "babelpreamble", IL_BABELPREAMBLE },
138 { "bgcolor", IL_BGCOLOR },
139 { "contentaslabel", IL_CONTENTASLABEL },
140 { "copystyle", IL_COPYSTYLE },
141 { "counter", IL_COUNTER},
142 { "custompars", IL_CUSTOMPARS },
143 { "decoration", IL_DECORATION },
144 { "display", IL_DISPLAY },
146 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
148 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
149 { "forceltr", IL_FORCELTR },
150 { "forceownlines", IL_FORCEOWNLINES },
151 { "forceplain", IL_FORCEPLAIN },
152 { "freespacing", IL_FREESPACING },
153 { "htmlattr", IL_HTMLATTR },
154 { "htmlforcecss", IL_HTMLFORCECSS },
155 { "htmlinnerattr", IL_HTMLINNERATTR},
156 { "htmlinnertag", IL_HTMLINNERTAG},
157 { "htmlisblock", IL_HTMLISBLOCK},
158 { "htmllabel", IL_HTMLLABEL },
159 { "htmlpreamble", IL_HTMLPREAMBLE },
160 { "htmlstyle", IL_HTMLSTYLE },
161 { "htmltag", IL_HTMLTAG },
162 { "intoc", IL_INTOC },
163 { "keepempty", IL_KEEPEMPTY },
164 { "labelfont", IL_LABELFONT },
165 { "labelstring", IL_LABELSTRING },
166 { "langpreamble", IL_LANGPREAMBLE },
167 { "latexname", IL_LATEXNAME },
168 { "latexparam", IL_LATEXPARAM },
169 { "latextype", IL_LATEXTYPE },
170 { "leftdelim", IL_LEFTDELIM },
171 { "lyxtype", IL_LYXTYPE },
172 { "multipar", IL_MULTIPAR },
173 { "needprotect", IL_NEEDPROTECT },
174 { "obsoletedby", IL_OBSOLETEDBY },
175 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
176 { "passthru", IL_PASSTHRU },
177 { "passthruchars", IL_PASSTHRU_CHARS },
178 { "preamble", IL_PREAMBLE },
179 { "refprefix", IL_REFPREFIX },
180 { "requires", IL_REQUIRES },
181 { "resetargs", IL_RESETARGS },
182 { "resetsfont", IL_RESETSFONT },
183 { "rightdelim", IL_RIGHTDELIM },
184 { "spellcheck", IL_SPELLCHECK }
187 lex.pushTable(elementTags);
189 labelfont_ = inherit_font;
190 bgcolor_ = Color_none;
192 // whether we've read the CustomPars or ForcePlain tag
193 // for issuing a warning in case MultiPars comes later
194 bool readCustomOrPlain = false;
197 while (!getout && lex.isOK()) {
200 case Lexer::LEX_UNDEF:
201 lex.printError("Unknown InsetLayout tag");
208 // Perhaps a more elegant way to deal with the next two would be the
209 // way this sort of thing is handled in Layout::read(), namely, by
212 // make sure that we have the right sort of name.
213 if (name_ != from_ascii("undefined")
214 && name_.substr(0,5) != from_ascii("Flex:")) {
215 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
216 "This one has the name `" << to_utf8(name_) << "'\n"
217 "Ignoring LyXType declaration.");
222 lyxtype_ = translateLyXType(lt);
223 if (lyxtype_ == NOLYXTYPE)
224 LYXERR0("Unknown LyXType `" << lt << "'.");
225 if (lyxtype_ == CHARSTYLE)
232 latextype_ = translateLaTeXType(lt);
233 if (latextype_ == ILT_ERROR)
234 LYXERR0("Unknown LaTeXType `" << lt << "'.");
242 decoration_ = translateDecoration(tmp);
249 latexparam_ = subst(tmp, """, "\"");
253 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
256 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
257 lex >> fixedwidthpreambleencoding_;
259 case IL_FORCE_LOCAL_FONT_SWITCH:
260 lex >> forcelocalfontswitch_;
264 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
268 labelfont_ = lyxRead(lex, inherit_font);
273 case IL_FORCEOWNLINES:
274 lex >> forceownlines_;
281 // the defaults for these depend upon multipar_
282 if (readCustomOrPlain)
283 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
284 "Previous value may be overwritten!");
285 readCustomOrPlain = false;
286 custompars_ = multipar_;
287 forceplain_ = !multipar_;
294 readCustomOrPlain = true;
298 readCustomOrPlain = true;
303 case IL_PASSTHRU_CHARS:
304 lex >> passthru_chars_;
306 case IL_PARBREAKISNEWLINE:
307 lex >> parbreakisnewline_;
318 case IL_CONTENTASLABEL:
319 lex >> contentaslabel_;
322 // initialize with a known style
325 style = subst(style, '_', ' ');
327 // We don't want to apply the algorithm in DocumentClass::insetLayout()
328 // here. So we do it the long way.
329 TextClass::InsetLayouts::const_iterator it =
330 tclass.insetLayouts().find(style);
331 if (it != tclass.insetLayouts().end()) {
332 docstring const tmpname = name_;
333 this->operator=(it->second);
336 LYXERR0("Cannot copy unknown InsetLayout `"
337 << style << "' to InsetLayout `"
339 << "All InsetLayouts so far:");
340 TextClass::InsetLayouts::const_iterator lit =
341 tclass.insetLayouts().begin();
342 TextClass::InsetLayouts::const_iterator len =
343 tclass.insetLayouts().end();
344 for (; lit != len; ++lit)
345 lyxerr << lit->second.name() << "\n";
349 case IL_OBSOLETEDBY: {
352 style = subst(style, '_', ' ');
354 // We don't want to apply the algorithm in DocumentClass::insetLayout()
355 // here. So we do it the long way.
356 TextClass::InsetLayouts::const_iterator it =
357 tclass.insetLayouts().find(style);
358 if (it != tclass.insetLayouts().end()) {
359 docstring const tmpname = name_;
360 this->operator=(it->second);
362 if (obsoleted_by().empty())
363 obsoleted_by_ = style;
365 LYXERR0("Cannot replace InsetLayout `"
367 << "' with unknown InsetLayout `"
369 << "All InsetLayouts so far:");
370 TextClass::InsetLayouts::const_iterator lit =
371 tclass.insetLayouts().begin();
372 TextClass::InsetLayouts::const_iterator len =
373 tclass.insetLayouts().end();
374 for (; lit != len; ++lit)
375 lyxerr << lit->second.name() << "\n";
381 font_ = lyxRead(lex, inherit_font);
382 // If you want to define labelfont, you need to do so after
392 postcommandargs_.clear();
400 bgcolor_ = lcolor.getFromLyXName(tmp);
403 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
405 case IL_BABELPREAMBLE:
406 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
408 case IL_LANGPREAMBLE:
409 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
420 case IL_HTMLFORCECSS:
421 lex >> htmlforcecss_;
423 case IL_HTMLINNERTAG:
424 lex >> htmlinnertag_;
426 case IL_HTMLINNERATTR:
427 lex >> htmlinnerattr_;
436 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
438 case IL_HTMLPREAMBLE:
439 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
443 vector<string> const req
444 = getVectorFromString(lex.getString());
445 requires_.insert(req.begin(), req.end());
463 // Here add element to list if getout == true
467 // The label font is generally used as-is without
468 // any realization against a given context.
469 labelfont_.realize(sane_font);
476 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
478 if (compare_ascii_no_case(str, "charstyle") == 0)
479 return InsetLayout::CHARSTYLE;
480 if (compare_ascii_no_case(str, "custom") == 0)
481 return InsetLayout::CUSTOM;
482 if (compare_ascii_no_case(str, "element") == 0)
483 return InsetLayout::ELEMENT;
484 if (compare_ascii_no_case(str, "end") == 0)
485 return InsetLayout::END;
486 if (compare_ascii_no_case(str, "standard") == 0)
487 return InsetLayout::STANDARD;
488 return InsetLayout::NOLYXTYPE;
492 string const & InsetLayout::htmltag() const
494 if (htmltag_.empty())
495 htmltag_ = multipar_ ? "div" : "span";
500 string const & InsetLayout::htmlattr() const
502 if (htmlattr_.empty())
503 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
508 string const & InsetLayout::htmlinnerattr() const
510 if (htmlinnerattr_.empty())
511 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
512 return htmlinnerattr_;
516 string InsetLayout::defaultCSSClass() const
518 if (!defaultcssclass_.empty())
519 return defaultcssclass_;
521 string n = to_utf8(name());
522 string::const_iterator it = n.begin();
523 string::const_iterator en = n.end();
524 for (; it != en; ++it) {
525 if (!isAlphaASCII(*it))
527 else if (isLower(*it))
532 // are there other characters we need to remove?
533 defaultcssclass_ = d;
534 return defaultcssclass_;
538 void InsetLayout::makeDefaultCSS() const
540 if (!htmldefaultstyle_.empty())
542 docstring const mainfontCSS = font_.asCSS();
543 if (!mainfontCSS.empty())
545 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
546 mainfontCSS + from_ascii("\n}\n");
550 docstring InsetLayout::htmlstyle() const
552 if (!htmlstyle_.empty() && !htmlforcecss_)
554 if (htmldefaultstyle_.empty())
556 docstring retval = htmldefaultstyle_;
557 if (!htmlstyle_.empty())
558 retval += '\n' + htmlstyle_ + '\n';
562 void InsetLayout::readArgument(Lexer & lex)
564 Layout::latexarg arg;
565 arg.mandatory = false;
566 arg.autoinsert = false;
567 arg.insertcotext = false;
569 bool finished = false;
570 arg.font = inherit_font;
571 arg.labelfont = inherit_font;
574 bool const postcmd = prefixIs(nr, "post:");
575 while (!finished && lex.isOK() && !error) {
577 string const tok = ascii_lowercase(lex.getString());
581 } else if (tok == "endargument") {
583 } else if (tok == "labelstring") {
585 arg.labelstring = lex.getDocString();
586 } else if (tok == "menustring") {
588 arg.menustring = lex.getDocString();
589 } else if (tok == "mandatory") {
591 arg.mandatory = lex.getBool();
592 } else if (tok == "autoinsert") {
594 arg.autoinsert = lex.getBool();
595 } else if (tok == "insertcotext") {
597 arg.insertcotext = lex.getBool();
598 } else if (tok == "leftdelim") {
600 arg.ldelim = lex.getDocString();
601 arg.ldelim = subst(arg.ldelim,
602 from_ascii("<br/>"), from_ascii("\n"));
603 } else if (tok == "rightdelim") {
605 arg.rdelim = lex.getDocString();
606 arg.rdelim = subst(arg.rdelim,
607 from_ascii("<br/>"), from_ascii("\n"));
608 } else if (tok == "defaultarg") {
610 arg.defaultarg = lex.getDocString();
611 } else if (tok == "presetarg") {
613 arg.presetarg = lex.getDocString();
614 } else if (tok == "tooltip") {
616 arg.tooltip = lex.getDocString();
617 } else if (tok == "requires") {
619 arg.requires = lex.getString();
620 } else if (tok == "decoration") {
622 arg.decoration = lex.getString();
623 } else if (tok == "font") {
624 arg.font = lyxRead(lex, arg.font);
625 } else if (tok == "labelfont") {
626 arg.labelfont = lyxRead(lex, arg.labelfont);
627 } else if (tok == "passthruchars") {
629 arg.pass_thru_chars = lex.getDocString();
631 lex.printError("Unknown tag");
635 if (arg.labelstring.empty())
636 LYXERR0("Incomplete Argument definition!");
638 postcommandargs_[nr] = arg;
640 latexargs_[nr] = arg;
644 Layout::LaTeXArgMap InsetLayout::args() const
646 Layout::LaTeXArgMap args = latexargs_;
647 if (!postcommandargs_.empty())
648 args.insert(postcommandargs_.begin(), postcommandargs_.end());
653 unsigned int InsetLayout::optArgs() const
656 Layout::LaTeXArgMap const args = InsetLayout::args();
657 Layout::LaTeXArgMap::const_iterator it = args.begin();
658 for (; it != args.end(); ++it) {
659 if (!(*it).second.mandatory)
666 unsigned int InsetLayout::requiredArgs() const
669 Layout::LaTeXArgMap const args = InsetLayout::args();
670 Layout::LaTeXArgMap::const_iterator it = args.begin();
671 for (; it != args.end(); ++it) {
672 if ((*it).second.mandatory)