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), add_to_toc_(false), is_toc_caption_(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)
93 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
95 IL_FORCE_LOCAL_FONT_SWITCH,
125 IL_PARBREAKISNEWLINE,
137 LexerKeyword elementTags[] = {
138 { "addtotoc", IL_ADDTOTOC },
139 { "argument", IL_ARGUMENT },
140 { "babelpreamble", IL_BABELPREAMBLE },
141 { "bgcolor", IL_BGCOLOR },
142 { "contentaslabel", IL_CONTENTASLABEL },
143 { "copystyle", IL_COPYSTYLE },
144 { "counter", IL_COUNTER},
145 { "custompars", IL_CUSTOMPARS },
146 { "decoration", IL_DECORATION },
147 { "display", IL_DISPLAY },
149 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
151 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
152 { "forceltr", IL_FORCELTR },
153 { "forceownlines", IL_FORCEOWNLINES },
154 { "forceplain", IL_FORCEPLAIN },
155 { "freespacing", IL_FREESPACING },
156 { "htmlattr", IL_HTMLATTR },
157 { "htmlforcecss", IL_HTMLFORCECSS },
158 { "htmlinnerattr", IL_HTMLINNERATTR},
159 { "htmlinnertag", IL_HTMLINNERTAG},
160 { "htmlisblock", IL_HTMLISBLOCK},
161 { "htmllabel", IL_HTMLLABEL },
162 { "htmlpreamble", IL_HTMLPREAMBLE },
163 { "htmlstyle", IL_HTMLSTYLE },
164 { "htmltag", IL_HTMLTAG },
165 { "intoc", IL_INTOC },
166 { "istoccaption", IL_ISTOCCAPTION },
167 { "keepempty", IL_KEEPEMPTY },
168 { "labelfont", IL_LABELFONT },
169 { "labelstring", IL_LABELSTRING },
170 { "langpreamble", IL_LANGPREAMBLE },
171 { "latexname", IL_LATEXNAME },
172 { "latexparam", IL_LATEXPARAM },
173 { "latextype", IL_LATEXTYPE },
174 { "leftdelim", IL_LEFTDELIM },
175 { "lyxtype", IL_LYXTYPE },
176 { "multipar", IL_MULTIPAR },
177 { "needprotect", IL_NEEDPROTECT },
178 { "obsoletedby", IL_OBSOLETEDBY },
179 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
180 { "passthru", IL_PASSTHRU },
181 { "passthruchars", IL_PASSTHRU_CHARS },
182 { "preamble", IL_PREAMBLE },
183 { "refprefix", IL_REFPREFIX },
184 { "requires", IL_REQUIRES },
185 { "resetargs", IL_RESETARGS },
186 { "resetsfont", IL_RESETSFONT },
187 { "rightdelim", IL_RIGHTDELIM },
188 { "spellcheck", IL_SPELLCHECK }
191 lex.pushTable(elementTags);
193 labelfont_ = inherit_font;
194 bgcolor_ = Color_none;
196 // whether we've read the CustomPars or ForcePlain tag
197 // for issuing a warning in case MultiPars comes later
198 bool readCustomOrPlain = false;
201 while (!getout && lex.isOK()) {
204 case Lexer::LEX_UNDEF:
205 lex.printError("Unknown InsetLayout tag");
212 // Perhaps a more elegant way to deal with the next two would be the
213 // way this sort of thing is handled in Layout::read(), namely, by
216 // make sure that we have the right sort of name.
217 if (name_ != from_ascii("undefined")
218 && name_.substr(0,5) != from_ascii("Flex:")) {
219 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
220 "This one has the name `" << to_utf8(name_) << "'\n"
221 "Ignoring LyXType declaration.");
226 lyxtype_ = translateLyXType(lt);
227 if (lyxtype_ == NOLYXTYPE)
228 LYXERR0("Unknown LyXType `" << lt << "'.");
229 if (lyxtype_ == CHARSTYLE) {
230 // by default, charstyles force the plain layout
239 latextype_ = translateLaTeXType(lt);
240 if (latextype_ == ILT_ERROR)
241 LYXERR0("Unknown LaTeXType `" << lt << "'.");
249 decoration_ = translateDecoration(tmp);
256 latexparam_ = subst(tmp, """, "\"");
260 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
263 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
264 lex >> fixedwidthpreambleencoding_;
266 case IL_FORCE_LOCAL_FONT_SWITCH:
267 lex >> forcelocalfontswitch_;
271 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
275 labelfont_ = lyxRead(lex, inherit_font);
280 case IL_FORCEOWNLINES:
281 lex >> forceownlines_;
288 // the defaults for these depend upon multipar_
289 if (readCustomOrPlain)
290 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
291 "Previous value may be overwritten!");
292 readCustomOrPlain = false;
293 custompars_ = multipar_;
294 forceplain_ = !multipar_;
301 readCustomOrPlain = true;
305 readCustomOrPlain = true;
310 case IL_PASSTHRU_CHARS:
311 lex >> passthru_chars_;
313 case IL_PARBREAKISNEWLINE:
314 lex >> parbreakisnewline_;
325 case IL_CONTENTASLABEL:
326 lex >> contentaslabel_;
329 // initialize with a known style
332 style = subst(style, '_', ' ');
334 // We don't want to apply the algorithm in DocumentClass::insetLayout()
335 // here. So we do it the long way.
336 TextClass::InsetLayouts::const_iterator it =
337 tclass.insetLayouts().find(style);
338 if (it != tclass.insetLayouts().end()) {
339 docstring const tmpname = name_;
340 this->operator=(it->second);
343 LYXERR0("Cannot copy unknown InsetLayout `"
344 << style << "' to InsetLayout `"
346 << "All InsetLayouts so far:");
347 TextClass::InsetLayouts::const_iterator lit =
348 tclass.insetLayouts().begin();
349 TextClass::InsetLayouts::const_iterator len =
350 tclass.insetLayouts().end();
351 for (; lit != len; ++lit)
352 lyxerr << lit->second.name() << "\n";
356 case IL_OBSOLETEDBY: {
359 style = subst(style, '_', ' ');
361 // We don't want to apply the algorithm in DocumentClass::insetLayout()
362 // here. So we do it the long way.
363 TextClass::InsetLayouts::const_iterator it =
364 tclass.insetLayouts().find(style);
365 if (it != tclass.insetLayouts().end()) {
366 docstring const tmpname = name_;
367 this->operator=(it->second);
369 if (obsoleted_by().empty())
370 obsoleted_by_ = style;
372 LYXERR0("Cannot replace InsetLayout `"
374 << "' with unknown InsetLayout `"
376 << "All InsetLayouts so far:");
377 TextClass::InsetLayouts::const_iterator lit =
378 tclass.insetLayouts().begin();
379 TextClass::InsetLayouts::const_iterator len =
380 tclass.insetLayouts().end();
381 for (; lit != len; ++lit)
382 lyxerr << lit->second.name() << "\n";
388 font_ = lyxRead(lex, inherit_font);
389 // If you want to define labelfont, you need to do so after
399 postcommandargs_.clear();
407 bgcolor_ = lcolor.getFromLyXName(tmp);
410 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
412 case IL_BABELPREAMBLE:
413 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
415 case IL_LANGPREAMBLE:
416 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
427 case IL_HTMLFORCECSS:
428 lex >> htmlforcecss_;
430 case IL_HTMLINNERTAG:
431 lex >> htmlinnertag_;
433 case IL_HTMLINNERATTR:
434 lex >> htmlinnerattr_;
443 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
445 case IL_HTMLPREAMBLE:
446 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
450 vector<string> const req
451 = getVectorFromString(lex.getString(true));
452 requires_.insert(req.begin(), req.end());
466 add_to_toc_ = !toc_type_.empty();
468 case IL_ISTOCCAPTION:
469 lex >> is_toc_caption_;
477 // Here add element to list if getout == true
481 // The label font is generally used as-is without
482 // any realization against a given context.
483 labelfont_.realize(sane_font);
490 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
492 if (compare_ascii_no_case(str, "charstyle") == 0)
493 return InsetLayout::CHARSTYLE;
494 if (compare_ascii_no_case(str, "custom") == 0)
495 return InsetLayout::CUSTOM;
496 if (compare_ascii_no_case(str, "element") == 0)
497 return InsetLayout::ELEMENT;
498 if (compare_ascii_no_case(str, "end") == 0)
499 return InsetLayout::END;
500 if (compare_ascii_no_case(str, "standard") == 0)
501 return InsetLayout::STANDARD;
502 return InsetLayout::NOLYXTYPE;
506 string const & InsetLayout::htmltag() const
508 if (htmltag_.empty())
509 htmltag_ = multipar_ ? "div" : "span";
514 string const & InsetLayout::htmlattr() const
516 if (htmlattr_.empty())
517 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
522 string const & InsetLayout::htmlinnerattr() const
524 if (htmlinnerattr_.empty())
525 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
526 return htmlinnerattr_;
530 string InsetLayout::defaultCSSClass() const
532 if (!defaultcssclass_.empty())
533 return defaultcssclass_;
535 string n = to_utf8(name());
536 string::const_iterator it = n.begin();
537 string::const_iterator en = n.end();
538 for (; it != en; ++it) {
539 if (!isAlphaASCII(*it))
541 else if (isLower(*it))
546 // are there other characters we need to remove?
547 defaultcssclass_ = d;
548 return defaultcssclass_;
552 void InsetLayout::makeDefaultCSS() const
554 if (!htmldefaultstyle_.empty())
556 docstring const mainfontCSS = font_.asCSS();
557 if (!mainfontCSS.empty())
559 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
560 mainfontCSS + from_ascii("\n}\n");
564 docstring InsetLayout::htmlstyle() const
566 if (!htmlstyle_.empty() && !htmlforcecss_)
568 if (htmldefaultstyle_.empty())
570 docstring retval = htmldefaultstyle_;
571 if (!htmlstyle_.empty())
572 retval += '\n' + htmlstyle_ + '\n';
576 void InsetLayout::readArgument(Lexer & lex)
578 Layout::latexarg arg;
579 arg.mandatory = false;
580 arg.autoinsert = false;
581 arg.insertcotext = false;
583 bool finished = false;
584 arg.font = inherit_font;
585 arg.labelfont = inherit_font;
586 arg.is_toc_caption = false;
587 arg.passthru = PT_INHERITED;
590 bool const postcmd = prefixIs(nr, "post:");
591 while (!finished && lex.isOK() && !error) {
593 string const tok = ascii_lowercase(lex.getString());
597 } else if (tok == "endargument") {
599 } else if (tok == "labelstring") {
601 arg.labelstring = lex.getDocString();
602 } else if (tok == "menustring") {
604 arg.menustring = lex.getDocString();
605 } else if (tok == "mandatory") {
607 arg.mandatory = lex.getBool();
608 } else if (tok == "autoinsert") {
610 arg.autoinsert = lex.getBool();
611 } else if (tok == "insertcotext") {
613 arg.insertcotext = lex.getBool();
614 } else if (tok == "leftdelim") {
616 arg.ldelim = lex.getDocString();
617 arg.ldelim = subst(arg.ldelim,
618 from_ascii("<br/>"), from_ascii("\n"));
619 } else if (tok == "rightdelim") {
621 arg.rdelim = lex.getDocString();
622 arg.rdelim = subst(arg.rdelim,
623 from_ascii("<br/>"), from_ascii("\n"));
624 } else if (tok == "defaultarg") {
626 arg.defaultarg = lex.getDocString();
627 } else if (tok == "presetarg") {
629 arg.presetarg = lex.getDocString();
630 } else if (tok == "tooltip") {
632 arg.tooltip = lex.getDocString();
633 } else if (tok == "requires") {
635 arg.requires = lex.getString();
636 } else if (tok == "decoration") {
638 arg.decoration = lex.getString();
639 } else if (tok == "font") {
640 arg.font = lyxRead(lex, arg.font);
641 } else if (tok == "labelfont") {
642 arg.labelfont = lyxRead(lex, arg.labelfont);
643 } else if (tok == "passthruchars") {
645 arg.pass_thru_chars = lex.getDocString();
646 } else if (tok == "passthru") {
648 docstring value = lex.getDocString();
649 if (value == "true" || value == "1")
650 arg.passthru = PT_TRUE;
651 else if (value == "false" || value == "0")
652 arg.passthru = PT_FALSE;
654 arg.passthru = PT_INHERITED;
655 } else if (tok == "istoccaption") {
657 arg.is_toc_caption = lex.getBool();
659 lex.printError("Unknown tag");
663 if (arg.labelstring.empty())
664 LYXERR0("Incomplete Argument definition!");
666 postcommandargs_[nr] = arg;
668 latexargs_[nr] = arg;
672 Layout::LaTeXArgMap InsetLayout::args() const
674 Layout::LaTeXArgMap args = latexargs_;
675 if (!postcommandargs_.empty())
676 args.insert(postcommandargs_.begin(), postcommandargs_.end());
681 unsigned int InsetLayout::optArgs() const
684 Layout::LaTeXArgMap const args = InsetLayout::args();
685 Layout::LaTeXArgMap::const_iterator it = args.begin();
686 for (; it != args.end(); ++it) {
687 if (!(*it).second.mandatory)
694 unsigned int InsetLayout::requiredArgs() const
697 Layout::LaTeXArgMap const args = InsetLayout::args();
698 Layout::LaTeXArgMap::const_iterator it = args.begin();
699 for (; it != args.end(); ++it) {
700 if ((*it).second.mandatory)