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), needcprotect_(false),
46 intoc_(false), 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,
126 IL_PARBREAKISNEWLINE,
138 LexerKeyword elementTags[] = {
139 { "addtotoc", IL_ADDTOTOC },
140 { "argument", IL_ARGUMENT },
141 { "babelpreamble", IL_BABELPREAMBLE },
142 { "bgcolor", IL_BGCOLOR },
143 { "contentaslabel", IL_CONTENTASLABEL },
144 { "copystyle", IL_COPYSTYLE },
145 { "counter", IL_COUNTER},
146 { "custompars", IL_CUSTOMPARS },
147 { "decoration", IL_DECORATION },
148 { "display", IL_DISPLAY },
150 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
152 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
153 { "forceltr", IL_FORCELTR },
154 { "forceownlines", IL_FORCEOWNLINES },
155 { "forceplain", IL_FORCEPLAIN },
156 { "freespacing", IL_FREESPACING },
157 { "htmlattr", IL_HTMLATTR },
158 { "htmlforcecss", IL_HTMLFORCECSS },
159 { "htmlinnerattr", IL_HTMLINNERATTR},
160 { "htmlinnertag", IL_HTMLINNERTAG},
161 { "htmlisblock", IL_HTMLISBLOCK},
162 { "htmllabel", IL_HTMLLABEL },
163 { "htmlpreamble", IL_HTMLPREAMBLE },
164 { "htmlstyle", IL_HTMLSTYLE },
165 { "htmltag", IL_HTMLTAG },
166 { "intoc", IL_INTOC },
167 { "istoccaption", IL_ISTOCCAPTION },
168 { "keepempty", IL_KEEPEMPTY },
169 { "labelfont", IL_LABELFONT },
170 { "labelstring", IL_LABELSTRING },
171 { "langpreamble", IL_LANGPREAMBLE },
172 { "latexname", IL_LATEXNAME },
173 { "latexparam", IL_LATEXPARAM },
174 { "latextype", IL_LATEXTYPE },
175 { "leftdelim", IL_LEFTDELIM },
176 { "lyxtype", IL_LYXTYPE },
177 { "multipar", IL_MULTIPAR },
178 { "needcprotect", IL_NEEDCPROTECT },
179 { "needprotect", IL_NEEDPROTECT },
180 { "obsoletedby", IL_OBSOLETEDBY },
181 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
182 { "passthru", IL_PASSTHRU },
183 { "passthruchars", IL_PASSTHRU_CHARS },
184 { "preamble", IL_PREAMBLE },
185 { "refprefix", IL_REFPREFIX },
186 { "requires", IL_REQUIRES },
187 { "resetargs", IL_RESETARGS },
188 { "resetsfont", IL_RESETSFONT },
189 { "rightdelim", IL_RIGHTDELIM },
190 { "spellcheck", IL_SPELLCHECK }
193 lex.pushTable(elementTags);
195 labelfont_ = inherit_font;
196 bgcolor_ = Color_none;
198 // whether we've read the CustomPars or ForcePlain tag
199 // for issuing a warning in case MultiPars comes later
200 bool readCustomOrPlain = false;
203 while (!getout && lex.isOK()) {
206 case Lexer::LEX_UNDEF:
207 lex.printError("Unknown InsetLayout tag");
214 // Perhaps a more elegant way to deal with the next two would be the
215 // way this sort of thing is handled in Layout::read(), namely, by
218 // make sure that we have the right sort of name.
219 if (name_ != from_ascii("undefined")
220 && name_.substr(0,5) != from_ascii("Flex:")) {
221 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
222 "This one has the name `" << to_utf8(name_) << "'\n"
223 "Ignoring LyXType declaration.");
228 lyxtype_ = translateLyXType(lt);
229 if (lyxtype_ == NOLYXTYPE)
230 LYXERR0("Unknown LyXType `" << lt << "'.");
231 if (lyxtype_ == CHARSTYLE) {
232 // by default, charstyles force the plain layout
241 latextype_ = translateLaTeXType(lt);
242 if (latextype_ == ILT_ERROR)
243 LYXERR0("Unknown LaTeXType `" << lt << "'.");
251 decoration_ = translateDecoration(tmp);
258 latexparam_ = subst(tmp, """, "\"");
262 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
265 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
266 lex >> fixedwidthpreambleencoding_;
268 case IL_FORCE_LOCAL_FONT_SWITCH:
269 lex >> forcelocalfontswitch_;
273 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
277 labelfont_ = lyxRead(lex, inherit_font);
282 case IL_FORCEOWNLINES:
283 lex >> forceownlines_;
290 // the defaults for these depend upon multipar_
291 if (readCustomOrPlain)
292 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
293 "Previous value may be overwritten!");
294 readCustomOrPlain = false;
295 custompars_ = multipar_;
296 forceplain_ = !multipar_;
303 readCustomOrPlain = true;
307 readCustomOrPlain = true;
312 case IL_PASSTHRU_CHARS:
313 lex >> passthru_chars_;
315 case IL_PARBREAKISNEWLINE:
316 lex >> parbreakisnewline_;
327 case IL_NEEDCPROTECT:
328 lex >> needcprotect_;
330 case IL_CONTENTASLABEL:
331 lex >> contentaslabel_;
334 // initialize with a known style
337 style = subst(style, '_', ' ');
339 // We don't want to apply the algorithm in DocumentClass::insetLayout()
340 // here. So we do it the long way.
341 TextClass::InsetLayouts::const_iterator it =
342 tclass.insetLayouts().find(style);
343 if (it != tclass.insetLayouts().end()) {
344 docstring const tmpname = name_;
345 this->operator=(it->second);
348 LYXERR0("Cannot copy unknown InsetLayout `"
349 << style << "' to InsetLayout `"
351 << "All InsetLayouts so far:");
352 TextClass::InsetLayouts::const_iterator lit =
353 tclass.insetLayouts().begin();
354 TextClass::InsetLayouts::const_iterator len =
355 tclass.insetLayouts().end();
356 for (; lit != len; ++lit)
357 lyxerr << lit->second.name() << "\n";
361 case IL_OBSOLETEDBY: {
364 style = subst(style, '_', ' ');
366 // We don't want to apply the algorithm in DocumentClass::insetLayout()
367 // here. So we do it the long way.
368 TextClass::InsetLayouts::const_iterator it =
369 tclass.insetLayouts().find(style);
370 if (it != tclass.insetLayouts().end()) {
371 docstring const tmpname = name_;
372 this->operator=(it->second);
374 if (obsoleted_by().empty())
375 obsoleted_by_ = style;
377 LYXERR0("Cannot replace InsetLayout `"
379 << "' with unknown InsetLayout `"
381 << "All InsetLayouts so far:");
382 TextClass::InsetLayouts::const_iterator lit =
383 tclass.insetLayouts().begin();
384 TextClass::InsetLayouts::const_iterator len =
385 tclass.insetLayouts().end();
386 for (; lit != len; ++lit)
387 lyxerr << lit->second.name() << "\n";
393 font_ = lyxRead(lex, inherit_font);
394 // If you want to define labelfont, you need to do so after
404 postcommandargs_.clear();
412 bgcolor_ = lcolor.getFromLyXName(tmp);
415 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
417 case IL_BABELPREAMBLE:
418 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
420 case IL_LANGPREAMBLE:
421 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
432 case IL_HTMLFORCECSS:
433 lex >> htmlforcecss_;
435 case IL_HTMLINNERTAG:
436 lex >> htmlinnertag_;
438 case IL_HTMLINNERATTR:
439 lex >> htmlinnerattr_;
448 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
450 case IL_HTMLPREAMBLE:
451 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
455 vector<string> const req
456 = getVectorFromString(lex.getString(true));
457 requires_.insert(req.begin(), req.end());
471 add_to_toc_ = !toc_type_.empty();
473 case IL_ISTOCCAPTION:
474 lex >> is_toc_caption_;
482 // Here add element to list if getout == true
486 // The label font is generally used as-is without
487 // any realization against a given context.
488 labelfont_.realize(sane_font);
495 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
497 if (compare_ascii_no_case(str, "charstyle") == 0)
498 return InsetLayout::CHARSTYLE;
499 if (compare_ascii_no_case(str, "custom") == 0)
500 return InsetLayout::CUSTOM;
501 if (compare_ascii_no_case(str, "element") == 0)
502 return InsetLayout::ELEMENT;
503 if (compare_ascii_no_case(str, "end") == 0)
504 return InsetLayout::END;
505 if (compare_ascii_no_case(str, "standard") == 0)
506 return InsetLayout::STANDARD;
507 return InsetLayout::NOLYXTYPE;
511 string const & InsetLayout::htmltag() const
513 if (htmltag_.empty())
514 htmltag_ = multipar_ ? "div" : "span";
519 string const & InsetLayout::htmlattr() const
521 if (htmlattr_.empty())
522 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
527 string const & InsetLayout::htmlinnerattr() const
529 if (htmlinnerattr_.empty())
530 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
531 return htmlinnerattr_;
535 string InsetLayout::defaultCSSClass() const
537 if (!defaultcssclass_.empty())
538 return defaultcssclass_;
540 string n = to_utf8(name());
541 string::const_iterator it = n.begin();
542 string::const_iterator en = n.end();
543 for (; it != en; ++it) {
544 if (!isAlphaASCII(*it))
546 else if (isLower(*it))
551 // are there other characters we need to remove?
552 defaultcssclass_ = d;
553 return defaultcssclass_;
557 void InsetLayout::makeDefaultCSS() const
559 if (!htmldefaultstyle_.empty())
561 docstring const mainfontCSS = font_.asCSS();
562 if (!mainfontCSS.empty())
564 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
565 mainfontCSS + from_ascii("\n}\n");
569 docstring InsetLayout::htmlstyle() const
571 if (!htmlstyle_.empty() && !htmlforcecss_)
573 if (htmldefaultstyle_.empty())
575 docstring retval = htmldefaultstyle_;
576 if (!htmlstyle_.empty())
577 retval += '\n' + htmlstyle_ + '\n';
581 void InsetLayout::readArgument(Lexer & lex)
583 Layout::latexarg arg;
584 arg.mandatory = false;
585 arg.autoinsert = false;
586 arg.insertcotext = false;
588 bool finished = false;
589 arg.font = inherit_font;
590 arg.labelfont = inherit_font;
591 arg.is_toc_caption = false;
592 arg.passthru = PT_INHERITED;
595 bool const postcmd = prefixIs(nr, "post:");
596 while (!finished && lex.isOK() && !error) {
598 string const tok = ascii_lowercase(lex.getString());
602 } else if (tok == "endargument") {
604 } else if (tok == "labelstring") {
606 arg.labelstring = lex.getDocString();
607 } else if (tok == "menustring") {
609 arg.menustring = lex.getDocString();
610 } else if (tok == "mandatory") {
612 arg.mandatory = lex.getBool();
613 } else if (tok == "autoinsert") {
615 arg.autoinsert = lex.getBool();
616 } else if (tok == "insertcotext") {
618 arg.insertcotext = lex.getBool();
619 } else if (tok == "leftdelim") {
621 arg.ldelim = lex.getDocString();
622 arg.ldelim = subst(arg.ldelim,
623 from_ascii("<br/>"), from_ascii("\n"));
624 } else if (tok == "rightdelim") {
626 arg.rdelim = lex.getDocString();
627 arg.rdelim = subst(arg.rdelim,
628 from_ascii("<br/>"), from_ascii("\n"));
629 } else if (tok == "defaultarg") {
631 arg.defaultarg = lex.getDocString();
632 } else if (tok == "presetarg") {
634 arg.presetarg = lex.getDocString();
635 } else if (tok == "tooltip") {
637 arg.tooltip = lex.getDocString();
638 } else if (tok == "requires") {
640 arg.requires = lex.getString();
641 } else if (tok == "decoration") {
643 arg.decoration = lex.getString();
644 } else if (tok == "font") {
645 arg.font = lyxRead(lex, arg.font);
646 } else if (tok == "labelfont") {
647 arg.labelfont = lyxRead(lex, arg.labelfont);
648 } else if (tok == "passthruchars") {
650 arg.pass_thru_chars = lex.getDocString();
651 } else if (tok == "passthru") {
653 docstring value = lex.getDocString();
654 if (value == "true" || value == "1")
655 arg.passthru = PT_TRUE;
656 else if (value == "false" || value == "0")
657 arg.passthru = PT_FALSE;
659 arg.passthru = PT_INHERITED;
660 } else if (tok == "istoccaption") {
662 arg.is_toc_caption = lex.getBool();
664 lex.printError("Unknown tag");
668 if (arg.labelstring.empty())
669 LYXERR0("Incomplete Argument definition!");
671 postcommandargs_[nr] = arg;
673 latexargs_[nr] = arg;
677 Layout::LaTeXArgMap InsetLayout::args() const
679 Layout::LaTeXArgMap args = latexargs_;
680 if (!postcommandargs_.empty())
681 args.insert(postcommandargs_.begin(), postcommandargs_.end());
686 unsigned int InsetLayout::optArgs() const
689 Layout::LaTeXArgMap const args = InsetLayout::args();
690 Layout::LaTeXArgMap::const_iterator it = args.begin();
691 for (; it != args.end(); ++it) {
692 if (!(*it).second.mandatory)
699 unsigned int InsetLayout::requiredArgs() const
702 Layout::LaTeXArgMap const args = InsetLayout::args();
703 Layout::LaTeXArgMap::const_iterator it = args.begin();
704 for (; it != args.end(); ++it) {
705 if ((*it).second.mandatory)