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)
94 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
96 IL_FORCE_LOCAL_FONT_SWITCH,
127 IL_PARBREAKISNEWLINE,
139 LexerKeyword elementTags[] = {
140 { "addtotoc", IL_ADDTOTOC },
141 { "argument", IL_ARGUMENT },
142 { "babelpreamble", IL_BABELPREAMBLE },
143 { "bgcolor", IL_BGCOLOR },
144 { "contentaslabel", IL_CONTENTASLABEL },
145 { "copystyle", IL_COPYSTYLE },
146 { "counter", IL_COUNTER},
147 { "custompars", IL_CUSTOMPARS },
148 { "decoration", IL_DECORATION },
149 { "display", IL_DISPLAY },
150 { "editexternal", IL_EDITEXTERNAL },
152 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
154 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
155 { "forceltr", IL_FORCELTR },
156 { "forceownlines", IL_FORCEOWNLINES },
157 { "forceplain", IL_FORCEPLAIN },
158 { "freespacing", IL_FREESPACING },
159 { "htmlattr", IL_HTMLATTR },
160 { "htmlforcecss", IL_HTMLFORCECSS },
161 { "htmlinnerattr", IL_HTMLINNERATTR},
162 { "htmlinnertag", IL_HTMLINNERTAG},
163 { "htmlisblock", IL_HTMLISBLOCK},
164 { "htmllabel", IL_HTMLLABEL },
165 { "htmlpreamble", IL_HTMLPREAMBLE },
166 { "htmlstyle", IL_HTMLSTYLE },
167 { "htmltag", IL_HTMLTAG },
168 { "intoc", IL_INTOC },
169 { "istoccaption", IL_ISTOCCAPTION },
170 { "keepempty", IL_KEEPEMPTY },
171 { "labelfont", IL_LABELFONT },
172 { "labelstring", IL_LABELSTRING },
173 { "langpreamble", IL_LANGPREAMBLE },
174 { "latexname", IL_LATEXNAME },
175 { "latexparam", IL_LATEXPARAM },
176 { "latextype", IL_LATEXTYPE },
177 { "leftdelim", IL_LEFTDELIM },
178 { "lyxtype", IL_LYXTYPE },
179 { "multipar", IL_MULTIPAR },
180 { "needcprotect", IL_NEEDCPROTECT },
181 { "needprotect", IL_NEEDPROTECT },
182 { "obsoletedby", IL_OBSOLETEDBY },
183 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
184 { "passthru", IL_PASSTHRU },
185 { "passthruchars", IL_PASSTHRU_CHARS },
186 { "preamble", IL_PREAMBLE },
187 { "refprefix", IL_REFPREFIX },
188 { "requires", IL_REQUIRES },
189 { "resetargs", IL_RESETARGS },
190 { "resetsfont", IL_RESETSFONT },
191 { "rightdelim", IL_RIGHTDELIM },
192 { "spellcheck", IL_SPELLCHECK }
195 lex.pushTable(elementTags);
197 labelfont_ = inherit_font;
198 bgcolor_ = Color_none;
200 // whether we've read the CustomPars or ForcePlain tag
201 // for issuing a warning in case MultiPars comes later
202 bool readCustomOrPlain = false;
205 while (!getout && lex.isOK()) {
208 case Lexer::LEX_UNDEF:
209 lex.printError("Unknown InsetLayout tag");
216 // Perhaps a more elegant way to deal with the next two would be the
217 // way this sort of thing is handled in Layout::read(), namely, by
220 // make sure that we have the right sort of name.
221 if (name_ != from_ascii("undefined")
222 && name_.substr(0,5) != from_ascii("Flex:")) {
223 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
224 "This one has the name `" << to_utf8(name_) << "'\n"
225 "Ignoring LyXType declaration.");
230 lyxtype_ = translateLyXType(lt);
231 if (lyxtype_ == NOLYXTYPE)
232 LYXERR0("Unknown LyXType `" << lt << "'.");
233 if (lyxtype_ == CHARSTYLE) {
234 // by default, charstyles force the plain layout
243 latextype_ = translateLaTeXType(lt);
244 if (latextype_ == ILT_ERROR)
245 LYXERR0("Unknown LaTeXType `" << lt << "'.");
253 decoration_ = translateDecoration(tmp);
260 latexparam_ = subst(tmp, """, "\"");
264 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
267 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
268 lex >> fixedwidthpreambleencoding_;
270 case IL_FORCE_LOCAL_FONT_SWITCH:
271 lex >> forcelocalfontswitch_;
275 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
279 labelfont_ = lyxRead(lex, inherit_font);
284 case IL_FORCEOWNLINES:
285 lex >> forceownlines_;
292 // the defaults for these depend upon multipar_
293 if (readCustomOrPlain)
294 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
295 "Previous value may be overwritten!");
296 readCustomOrPlain = false;
297 custompars_ = multipar_;
298 forceplain_ = !multipar_;
305 readCustomOrPlain = true;
309 readCustomOrPlain = true;
314 case IL_PASSTHRU_CHARS:
315 lex >> passthru_chars_;
317 case IL_PARBREAKISNEWLINE:
318 lex >> parbreakisnewline_;
329 case IL_NEEDCPROTECT:
330 lex >> needcprotect_;
332 case IL_CONTENTASLABEL:
333 lex >> contentaslabel_;
336 // initialize with a known style
339 style = subst(style, '_', ' ');
341 // We don't want to apply the algorithm in DocumentClass::insetLayout()
342 // here. So we do it the long way.
343 TextClass::InsetLayouts::const_iterator it =
344 tclass.insetLayouts().find(style);
345 if (it != tclass.insetLayouts().end()) {
346 docstring const tmpname = name_;
347 this->operator=(it->second);
350 LYXERR0("Cannot copy unknown InsetLayout `"
351 << style << "' to InsetLayout `"
353 << "All InsetLayouts so far:");
354 TextClass::InsetLayouts::const_iterator lit =
355 tclass.insetLayouts().begin();
356 TextClass::InsetLayouts::const_iterator len =
357 tclass.insetLayouts().end();
358 for (; lit != len; ++lit)
359 lyxerr << lit->second.name() << "\n";
363 case IL_OBSOLETEDBY: {
366 style = subst(style, '_', ' ');
368 // We don't want to apply the algorithm in DocumentClass::insetLayout()
369 // here. So we do it the long way.
370 TextClass::InsetLayouts::const_iterator it =
371 tclass.insetLayouts().find(style);
372 if (it != tclass.insetLayouts().end()) {
373 docstring const tmpname = name_;
374 this->operator=(it->second);
376 if (obsoleted_by().empty())
377 obsoleted_by_ = style;
379 LYXERR0("Cannot replace InsetLayout `"
381 << "' with unknown InsetLayout `"
383 << "All InsetLayouts so far:");
384 TextClass::InsetLayouts::const_iterator lit =
385 tclass.insetLayouts().begin();
386 TextClass::InsetLayouts::const_iterator len =
387 tclass.insetLayouts().end();
388 for (; lit != len; ++lit)
389 lyxerr << lit->second.name() << "\n";
395 font_ = lyxRead(lex, inherit_font);
396 // If you want to define labelfont, you need to do so after
406 postcommandargs_.clear();
414 bgcolor_ = lcolor.getFromLyXName(tmp);
417 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
419 case IL_BABELPREAMBLE:
420 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
422 case IL_LANGPREAMBLE:
423 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
434 case IL_HTMLFORCECSS:
435 lex >> htmlforcecss_;
437 case IL_HTMLINNERTAG:
438 lex >> htmlinnertag_;
440 case IL_HTMLINNERATTR:
441 lex >> htmlinnerattr_;
450 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
452 case IL_HTMLPREAMBLE:
453 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
457 vector<string> const req
458 = getVectorFromString(lex.getString(true));
459 requires_.insert(req.begin(), req.end());
473 add_to_toc_ = !toc_type_.empty();
475 case IL_ISTOCCAPTION:
476 lex >> is_toc_caption_;
478 case IL_EDITEXTERNAL:
479 lex >> edit_external_;
487 // Here add element to list if getout == true
491 // The label font is generally used as-is without
492 // any realization against a given context.
493 labelfont_.realize(sane_font);
500 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
502 if (compare_ascii_no_case(str, "charstyle") == 0)
503 return InsetLayout::CHARSTYLE;
504 if (compare_ascii_no_case(str, "custom") == 0)
505 return InsetLayout::CUSTOM;
506 if (compare_ascii_no_case(str, "element") == 0)
507 return InsetLayout::ELEMENT;
508 if (compare_ascii_no_case(str, "end") == 0)
509 return InsetLayout::END;
510 if (compare_ascii_no_case(str, "standard") == 0)
511 return InsetLayout::STANDARD;
512 return InsetLayout::NOLYXTYPE;
516 string const & InsetLayout::htmltag() const
518 if (htmltag_.empty())
519 htmltag_ = multipar_ ? "div" : "span";
524 string const & InsetLayout::htmlattr() const
526 if (htmlattr_.empty())
527 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
532 string const & InsetLayout::htmlinnerattr() const
534 if (htmlinnerattr_.empty())
535 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
536 return htmlinnerattr_;
540 string InsetLayout::defaultCSSClass() const
542 if (!defaultcssclass_.empty())
543 return defaultcssclass_;
545 string n = to_utf8(name());
546 string::const_iterator it = n.begin();
547 string::const_iterator en = n.end();
548 for (; it != en; ++it) {
549 if (!isAlphaASCII(*it))
551 else if (isLower(*it))
556 // are there other characters we need to remove?
557 defaultcssclass_ = d;
558 return defaultcssclass_;
562 void InsetLayout::makeDefaultCSS() const
564 if (!htmldefaultstyle_.empty())
566 docstring const mainfontCSS = font_.asCSS();
567 if (!mainfontCSS.empty())
569 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
570 mainfontCSS + from_ascii("\n}\n");
574 docstring InsetLayout::htmlstyle() const
576 if (!htmlstyle_.empty() && !htmlforcecss_)
578 if (htmldefaultstyle_.empty())
580 docstring retval = htmldefaultstyle_;
581 if (!htmlstyle_.empty())
582 retval += '\n' + htmlstyle_ + '\n';
586 void InsetLayout::readArgument(Lexer & lex)
588 Layout::latexarg arg;
589 arg.mandatory = false;
590 arg.autoinsert = false;
591 arg.insertcotext = false;
593 bool finished = false;
594 arg.font = inherit_font;
595 arg.labelfont = inherit_font;
596 arg.is_toc_caption = false;
597 arg.passthru = PT_INHERITED;
598 arg.nodelims = false;
601 bool const postcmd = prefixIs(nr, "post:");
602 while (!finished && lex.isOK() && !error) {
604 string const tok = ascii_lowercase(lex.getString());
608 } else if (tok == "endargument") {
610 } else if (tok == "labelstring") {
612 arg.labelstring = lex.getDocString();
613 } else if (tok == "menustring") {
615 arg.menustring = lex.getDocString();
616 } else if (tok == "mandatory") {
618 arg.mandatory = lex.getBool();
619 } else if (tok == "autoinsert") {
621 arg.autoinsert = lex.getBool();
622 } else if (tok == "insertcotext") {
624 arg.insertcotext = lex.getBool();
625 } else if (tok == "leftdelim") {
627 arg.ldelim = lex.getDocString();
628 arg.ldelim = subst(arg.ldelim,
629 from_ascii("<br/>"), from_ascii("\n"));
630 } else if (tok == "rightdelim") {
632 arg.rdelim = lex.getDocString();
633 arg.rdelim = subst(arg.rdelim,
634 from_ascii("<br/>"), from_ascii("\n"));
635 } else if (tok == "defaultarg") {
637 arg.defaultarg = lex.getDocString();
638 } else if (tok == "presetarg") {
640 arg.presetarg = lex.getDocString();
641 } else if (tok == "tooltip") {
643 arg.tooltip = lex.getDocString();
644 } else if (tok == "requires") {
646 arg.requires = lex.getString();
647 } else if (tok == "decoration") {
649 arg.decoration = lex.getString();
650 } else if (tok == "font") {
651 arg.font = lyxRead(lex, arg.font);
652 } else if (tok == "labelfont") {
653 arg.labelfont = lyxRead(lex, arg.labelfont);
654 } else if (tok == "passthruchars") {
656 arg.pass_thru_chars = lex.getDocString();
657 } else if (tok == "passthru") {
659 docstring value = lex.getDocString();
660 if (value == "true" || value == "1")
661 arg.passthru = PT_TRUE;
662 else if (value == "false" || value == "0")
663 arg.passthru = PT_FALSE;
665 arg.passthru = PT_INHERITED;
666 } else if (tok == "istoccaption") {
668 arg.is_toc_caption = lex.getBool();
670 lex.printError("Unknown tag");
674 if (arg.labelstring.empty())
675 LYXERR0("Incomplete Argument definition!");
677 postcommandargs_[nr] = arg;
679 latexargs_[nr] = arg;
683 Layout::LaTeXArgMap InsetLayout::args() const
685 Layout::LaTeXArgMap args = latexargs_;
686 if (!postcommandargs_.empty())
687 args.insert(postcommandargs_.begin(), postcommandargs_.end());
692 unsigned int InsetLayout::optArgs() const
695 Layout::LaTeXArgMap const args = InsetLayout::args();
696 Layout::LaTeXArgMap::const_iterator it = args.begin();
697 for (; it != args.end(); ++it) {
698 if (!(*it).second.mandatory)
705 unsigned int InsetLayout::requiredArgs() const
708 Layout::LaTeXArgMap const args = InsetLayout::args();
709 Layout::LaTeXArgMap::const_iterator it = args.begin();
710 for (; it != args.end(); ++it) {
711 if ((*it).second.mandatory)