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 needmboxprotect_(false), intoc_(false), spellcheck_(true),
47 resetsfont_(false), display_(true), forcelocalfontswitch_(false),
48 add_to_toc_(false), is_toc_caption_(false), edit_external_(false)
50 labelfont_.setColor(Color_error);
54 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
56 if (compare_ascii_no_case(str, "classic") == 0)
57 return InsetLayout::CLASSIC;
58 if (compare_ascii_no_case(str, "minimalistic") == 0)
59 return InsetLayout::MINIMALISTIC;
60 if (compare_ascii_no_case(str, "conglomerate") == 0)
61 return InsetLayout::CONGLOMERATE;
62 return InsetLayout::DEFAULT;
67 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
69 if (compare_ascii_no_case(str, "command") == 0)
70 return InsetLayout::COMMAND;
71 if (compare_ascii_no_case(str, "environment") == 0)
72 return InsetLayout::ENVIRONMENT;
73 if (compare_ascii_no_case(str, "none") == 0)
74 return InsetLayout::NOLATEXTYPE;
75 return InsetLayout::ILT_ERROR;
81 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
95 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
97 IL_FORCE_LOCAL_FONT_SWITCH,
129 IL_PARBREAKISNEWLINE,
141 LexerKeyword elementTags[] = {
142 { "addtotoc", IL_ADDTOTOC },
143 { "argument", IL_ARGUMENT },
144 { "babelpreamble", IL_BABELPREAMBLE },
145 { "bgcolor", IL_BGCOLOR },
146 { "contentaslabel", IL_CONTENTASLABEL },
147 { "copystyle", IL_COPYSTYLE },
148 { "counter", IL_COUNTER},
149 { "custompars", IL_CUSTOMPARS },
150 { "decoration", IL_DECORATION },
151 { "display", IL_DISPLAY },
152 { "editexternal", IL_EDITEXTERNAL },
154 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
156 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
157 { "forceltr", IL_FORCELTR },
158 { "forceownlines", IL_FORCEOWNLINES },
159 { "forceplain", IL_FORCEPLAIN },
160 { "freespacing", IL_FREESPACING },
161 { "htmlattr", IL_HTMLATTR },
162 { "htmlforcecss", IL_HTMLFORCECSS },
163 { "htmlinnerattr", IL_HTMLINNERATTR},
164 { "htmlinnertag", IL_HTMLINNERTAG},
165 { "htmlisblock", IL_HTMLISBLOCK},
166 { "htmllabel", IL_HTMLLABEL },
167 { "htmlpreamble", IL_HTMLPREAMBLE },
168 { "htmlstyle", IL_HTMLSTYLE },
169 { "htmltag", IL_HTMLTAG },
170 { "intoc", IL_INTOC },
171 { "istoccaption", IL_ISTOCCAPTION },
172 { "keepempty", IL_KEEPEMPTY },
173 { "labelfont", IL_LABELFONT },
174 { "labelstring", IL_LABELSTRING },
175 { "langpreamble", IL_LANGPREAMBLE },
176 { "latexname", IL_LATEXNAME },
177 { "latexparam", IL_LATEXPARAM },
178 { "latextype", IL_LATEXTYPE },
179 { "leftdelim", IL_LEFTDELIM },
180 { "lyxtype", IL_LYXTYPE },
181 { "multipar", IL_MULTIPAR },
182 { "needcprotect", IL_NEEDCPROTECT },
183 { "needmboxprotect", IL_NEEDMBOXPROTECT },
184 { "needprotect", IL_NEEDPROTECT },
185 { "obsoletedby", IL_OBSOLETEDBY },
186 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
187 { "passthru", IL_PASSTHRU },
188 { "passthruchars", IL_PASSTHRU_CHARS },
189 { "preamble", IL_PREAMBLE },
190 { "refprefix", IL_REFPREFIX },
191 { "requires", IL_REQUIRES },
192 { "resetargs", IL_RESETARGS },
193 { "resetsfont", IL_RESETSFONT },
194 { "rightdelim", IL_RIGHTDELIM },
195 { "spellcheck", IL_SPELLCHECK }
198 lex.pushTable(elementTags);
200 labelfont_ = inherit_font;
201 bgcolor_ = Color_none;
203 // whether we've read the CustomPars or ForcePlain tag
204 // for issuing a warning in case MultiPars comes later
205 bool readCustomOrPlain = false;
208 while (!getout && lex.isOK()) {
211 case Lexer::LEX_UNDEF:
212 lex.printError("Unknown InsetLayout tag");
219 // Perhaps a more elegant way to deal with the next two would be the
220 // way this sort of thing is handled in Layout::read(), namely, by
223 // make sure that we have the right sort of name.
224 if (name_ != from_ascii("undefined")
225 && name_.substr(0,5) != from_ascii("Flex:")) {
226 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
227 "This one has the name `" << to_utf8(name_) << "'\n"
228 "Ignoring LyXType declaration.");
233 lyxtype_ = translateLyXType(lt);
234 if (lyxtype_ == NOLYXTYPE)
235 LYXERR0("Unknown LyXType `" << lt << "'.");
236 if (lyxtype_ == CHARSTYLE) {
237 // by default, charstyles force the plain layout
246 latextype_ = translateLaTeXType(lt);
247 if (latextype_ == ILT_ERROR)
248 LYXERR0("Unknown LaTeXType `" << lt << "'.");
256 decoration_ = translateDecoration(tmp);
263 latexparam_ = subst(tmp, """, "\"");
267 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
270 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
271 lex >> fixedwidthpreambleencoding_;
273 case IL_FORCE_LOCAL_FONT_SWITCH:
274 lex >> forcelocalfontswitch_;
278 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
282 labelfont_ = lyxRead(lex, inherit_font);
287 case IL_FORCEOWNLINES:
288 lex >> forceownlines_;
295 // the defaults for these depend upon multipar_
296 if (readCustomOrPlain)
297 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
298 "Previous value may be overwritten!");
299 readCustomOrPlain = false;
300 custompars_ = multipar_;
301 forceplain_ = !multipar_;
308 readCustomOrPlain = true;
312 readCustomOrPlain = true;
317 case IL_PASSTHRU_CHARS:
318 lex >> passthru_chars_;
320 case IL_PARBREAKISNEWLINE:
321 lex >> parbreakisnewline_;
332 case IL_NEEDCPROTECT:
333 lex >> needcprotect_;
335 case IL_NEEDMBOXPROTECT:
336 lex >> needmboxprotect_;
338 case IL_CONTENTASLABEL:
339 lex >> contentaslabel_;
342 // initialize with a known style
345 style = subst(style, '_', ' ');
347 // We don't want to apply the algorithm in DocumentClass::insetLayout()
348 // here. So we do it the long way.
349 TextClass::InsetLayouts::const_iterator it =
350 tclass.insetLayouts().find(style);
351 if (it != tclass.insetLayouts().end()) {
352 docstring const tmpname = name_;
353 this->operator=(it->second);
356 LYXERR0("Cannot copy unknown InsetLayout `"
357 << style << "' to InsetLayout `"
359 << "All InsetLayouts so far:");
360 TextClass::InsetLayouts::const_iterator lit =
361 tclass.insetLayouts().begin();
362 TextClass::InsetLayouts::const_iterator len =
363 tclass.insetLayouts().end();
364 for (; lit != len; ++lit)
365 lyxerr << lit->second.name() << "\n";
369 case IL_OBSOLETEDBY: {
372 style = subst(style, '_', ' ');
374 // We don't want to apply the algorithm in DocumentClass::insetLayout()
375 // here. So we do it the long way.
376 TextClass::InsetLayouts::const_iterator it =
377 tclass.insetLayouts().find(style);
378 if (it != tclass.insetLayouts().end()) {
379 docstring const tmpname = name_;
380 this->operator=(it->second);
382 if (obsoleted_by().empty())
383 obsoleted_by_ = style;
385 LYXERR0("Cannot replace InsetLayout `"
387 << "' with unknown InsetLayout `"
389 << "All InsetLayouts so far:");
390 TextClass::InsetLayouts::const_iterator lit =
391 tclass.insetLayouts().begin();
392 TextClass::InsetLayouts::const_iterator len =
393 tclass.insetLayouts().end();
394 for (; lit != len; ++lit)
395 lyxerr << lit->second.name() << "\n";
401 font_ = lyxRead(lex, inherit_font);
402 // If you want to define labelfont, you need to do so after
412 postcommandargs_.clear();
420 bgcolor_ = lcolor.getFromLyXName(tmp);
423 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
425 case IL_BABELPREAMBLE:
426 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
428 case IL_LANGPREAMBLE:
429 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
440 case IL_HTMLFORCECSS:
441 lex >> htmlforcecss_;
443 case IL_HTMLINNERTAG:
444 lex >> htmlinnertag_;
446 case IL_HTMLINNERATTR:
447 lex >> htmlinnerattr_;
456 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
458 case IL_HTMLPREAMBLE:
459 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
463 vector<string> const req
464 = getVectorFromString(lex.getString(true));
465 requires_.insert(req.begin(), req.end());
479 add_to_toc_ = !toc_type_.empty();
481 case IL_ISTOCCAPTION:
482 lex >> is_toc_caption_;
484 case IL_EDITEXTERNAL:
485 lex >> edit_external_;
493 // Here add element to list if getout == true
497 // The label font is generally used as-is without
498 // any realization against a given context.
499 labelfont_.realize(sane_font);
506 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
508 if (compare_ascii_no_case(str, "charstyle") == 0)
509 return InsetLayout::CHARSTYLE;
510 if (compare_ascii_no_case(str, "custom") == 0)
511 return InsetLayout::CUSTOM;
512 if (compare_ascii_no_case(str, "element") == 0)
513 return InsetLayout::ELEMENT;
514 if (compare_ascii_no_case(str, "end") == 0)
515 return InsetLayout::END;
516 if (compare_ascii_no_case(str, "standard") == 0)
517 return InsetLayout::STANDARD;
518 return InsetLayout::NOLYXTYPE;
522 string const & InsetLayout::htmltag() const
524 if (htmltag_.empty())
525 htmltag_ = multipar_ ? "div" : "span";
530 string const & InsetLayout::htmlattr() const
532 if (htmlattr_.empty())
533 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
538 string const & InsetLayout::htmlinnerattr() const
540 if (htmlinnerattr_.empty())
541 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
542 return htmlinnerattr_;
546 string InsetLayout::defaultCSSClass() const
548 if (!defaultcssclass_.empty())
549 return defaultcssclass_;
551 string n = to_utf8(name());
552 string::const_iterator it = n.begin();
553 string::const_iterator en = n.end();
554 for (; it != en; ++it) {
555 if (!isAlphaASCII(*it))
557 else if (isLower(*it))
562 // are there other characters we need to remove?
563 defaultcssclass_ = d;
564 return defaultcssclass_;
568 void InsetLayout::makeDefaultCSS() const
570 if (!htmldefaultstyle_.empty())
572 docstring const mainfontCSS = font_.asCSS();
573 if (!mainfontCSS.empty())
575 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
576 mainfontCSS + from_ascii("\n}\n");
580 docstring InsetLayout::htmlstyle() const
582 if (!htmlstyle_.empty() && !htmlforcecss_)
584 if (htmldefaultstyle_.empty())
586 docstring retval = htmldefaultstyle_;
587 if (!htmlstyle_.empty())
588 retval += '\n' + htmlstyle_ + '\n';
592 void InsetLayout::readArgument(Lexer & lex)
594 Layout::latexarg arg;
595 arg.mandatory = false;
596 arg.autoinsert = false;
597 arg.insertcotext = false;
599 bool finished = false;
600 arg.font = inherit_font;
601 arg.labelfont = inherit_font;
602 arg.is_toc_caption = false;
603 arg.passthru = PT_INHERITED;
604 arg.nodelims = false;
607 bool const postcmd = prefixIs(nr, "post:");
608 while (!finished && lex.isOK() && !error) {
610 string const tok = ascii_lowercase(lex.getString());
614 } else if (tok == "endargument") {
616 } else if (tok == "labelstring") {
618 arg.labelstring = lex.getDocString();
619 } else if (tok == "menustring") {
621 arg.menustring = lex.getDocString();
622 } else if (tok == "mandatory") {
624 arg.mandatory = lex.getBool();
625 } else if (tok == "autoinsert") {
627 arg.autoinsert = lex.getBool();
628 } else if (tok == "insertcotext") {
630 arg.insertcotext = lex.getBool();
631 } else if (tok == "leftdelim") {
633 arg.ldelim = lex.getDocString();
634 arg.ldelim = subst(arg.ldelim,
635 from_ascii("<br/>"), from_ascii("\n"));
636 } else if (tok == "rightdelim") {
638 arg.rdelim = lex.getDocString();
639 arg.rdelim = subst(arg.rdelim,
640 from_ascii("<br/>"), from_ascii("\n"));
641 } else if (tok == "defaultarg") {
643 arg.defaultarg = lex.getDocString();
644 } else if (tok == "presetarg") {
646 arg.presetarg = lex.getDocString();
647 } else if (tok == "tooltip") {
649 arg.tooltip = lex.getDocString();
650 } else if (tok == "requires") {
652 arg.requires = lex.getString();
653 } else if (tok == "decoration") {
655 arg.decoration = lex.getString();
656 } else if (tok == "font") {
657 arg.font = lyxRead(lex, arg.font);
658 } else if (tok == "labelfont") {
659 arg.labelfont = lyxRead(lex, arg.labelfont);
660 } else if (tok == "passthruchars") {
662 arg.pass_thru_chars = lex.getDocString();
663 } else if (tok == "passthru") {
665 docstring value = lex.getDocString();
666 if (value == "true" || value == "1")
667 arg.passthru = PT_TRUE;
668 else if (value == "false" || value == "0")
669 arg.passthru = PT_FALSE;
671 arg.passthru = PT_INHERITED;
672 } else if (tok == "istoccaption") {
674 arg.is_toc_caption = lex.getBool();
676 lex.printError("Unknown tag");
680 if (arg.labelstring.empty())
681 LYXERR0("Incomplete Argument definition!");
683 postcommandargs_[nr] = arg;
685 latexargs_[nr] = arg;
689 Layout::LaTeXArgMap InsetLayout::args() const
691 Layout::LaTeXArgMap args = latexargs_;
692 if (!postcommandargs_.empty())
693 args.insert(postcommandargs_.begin(), postcommandargs_.end());
698 unsigned int InsetLayout::optArgs() const
701 Layout::LaTeXArgMap const args = InsetLayout::args();
702 Layout::LaTeXArgMap::const_iterator it = args.begin();
703 for (; it != args.end(); ++it) {
704 if (!(*it).second.mandatory)
711 unsigned int InsetLayout::requiredArgs() const
714 Layout::LaTeXArgMap const args = InsetLayout::args();
715 Layout::LaTeXArgMap::const_iterator it = args.begin();
716 for (; it != args.end(); ++it) {
717 if ((*it).second.mandatory)