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"
19 #include "TextClass.h"
21 #include "support/debug.h"
22 #include "support/lstrings.h"
23 #include "support/textutils.h"
31 using namespace lyx::support;
35 InsetLayout::InsetLayout() :
36 name_(from_ascii("undefined")), lyxtype_(STANDARD),
37 labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
38 decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(inherit_font),
39 labelfont_(sane_font), bgcolor_(Color_error),
40 fixedwidthpreambleencoding_(false), htmlforcecss_ (false),
41 htmlisblock_(true), multipar_(true), custompars_(true),
42 forceplain_(false), passthru_(false), parbreakisnewline_(false),
43 parbreakignored_(false), freespacing_(false), keepempty_(false), forceltr_(false),
44 forceownlines_(false), needprotect_(false), needcprotect_(false),
45 needmboxprotect_(false), intoc_(false), spellcheck_(true),
46 resetsfont_(false), display_(true), forcelocalfontswitch_(false),
47 add_to_toc_(false), is_toc_caption_(false), edit_external_(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,
95 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
97 IL_FORCE_LOCAL_FONT_SWITCH,
114 IL_DOCBOOKWRAPPERTAG,
115 IL_DOCBOOKWRAPPERTAGTYPE,
116 IL_DOCBOOKWRAPPERATTR,
138 IL_PARBREAKISNEWLINE,
150 LexerKeyword elementTags[] = {
151 { "addtotoc", IL_ADDTOTOC },
152 { "argument", IL_ARGUMENT },
153 { "babelpreamble", IL_BABELPREAMBLE },
154 { "bgcolor", IL_BGCOLOR },
155 { "contentaslabel", IL_CONTENTASLABEL },
156 { "copystyle", IL_COPYSTYLE },
157 { "counter", IL_COUNTER},
158 { "custompars", IL_CUSTOMPARS },
159 { "decoration", IL_DECORATION },
160 { "display", IL_DISPLAY },
161 { "docbookattr", IL_DOCBOOKATTR },
162 { "docbooktag", IL_DOCBOOKTAG },
163 { "docbooktagtype", IL_DOCBOOKTAGTYPE },
164 { "docbookwrapperattr", IL_DOCBOOKWRAPPERATTR },
165 { "docbookwrappertag", IL_DOCBOOKWRAPPERTAG },
166 { "docbookwrappertagtype", IL_DOCBOOKWRAPPERTAGTYPE },
167 { "editexternal", IL_EDITEXTERNAL },
169 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
171 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
172 { "forceltr", IL_FORCELTR },
173 { "forceownlines", IL_FORCEOWNLINES },
174 { "forceplain", IL_FORCEPLAIN },
175 { "freespacing", IL_FREESPACING },
176 { "htmlattr", IL_HTMLATTR },
177 { "htmlforcecss", IL_HTMLFORCECSS },
178 { "htmlinnerattr", IL_HTMLINNERATTR},
179 { "htmlinnertag", IL_HTMLINNERTAG},
180 { "htmlisblock", IL_HTMLISBLOCK},
181 { "htmllabel", IL_HTMLLABEL },
182 { "htmlpreamble", IL_HTMLPREAMBLE },
183 { "htmlstyle", IL_HTMLSTYLE },
184 { "htmltag", IL_HTMLTAG },
185 { "intoc", IL_INTOC },
186 { "istoccaption", IL_ISTOCCAPTION },
187 { "keepempty", IL_KEEPEMPTY },
188 { "labelfont", IL_LABELFONT },
189 { "labelstring", IL_LABELSTRING },
190 { "langpreamble", IL_LANGPREAMBLE },
191 { "latexname", IL_LATEXNAME },
192 { "latexparam", IL_LATEXPARAM },
193 { "latextype", IL_LATEXTYPE },
194 { "leftdelim", IL_LEFTDELIM },
195 { "lyxtype", IL_LYXTYPE },
196 { "menustring", IL_MENUSTRING },
197 { "multipar", IL_MULTIPAR },
198 { "needcprotect", IL_NEEDCPROTECT },
199 { "needmboxprotect", IL_NEEDMBOXPROTECT },
200 { "needprotect", IL_NEEDPROTECT },
201 { "newlinecmd", IL_NEWLINE_CMD },
202 { "obsoletedby", IL_OBSOLETEDBY },
203 { "parbreakignored", IL_PARBREAKIGNORED },
204 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
205 { "passthru", IL_PASSTHRU },
206 { "passthruchars", IL_PASSTHRU_CHARS },
207 { "preamble", IL_PREAMBLE },
208 { "refprefix", IL_REFPREFIX },
209 { "requires", IL_REQUIRES },
210 { "resetargs", IL_RESETARGS },
211 { "resetsfont", IL_RESETSFONT },
212 { "rightdelim", IL_RIGHTDELIM },
213 { "spellcheck", IL_SPELLCHECK }
216 lex.pushTable(elementTags);
218 labelfont_ = inherit_font;
219 bgcolor_ = Color_none;
221 // whether we've read the CustomPars or ForcePlain tag
222 // for issuing a warning in case MultiPars comes later
223 bool readCustomOrPlain = false;
226 while (!getout && lex.isOK()) {
229 case Lexer::LEX_UNDEF:
230 lex.printError("Unknown InsetLayout tag");
239 // Perhaps a more elegant way to deal with the next two would be the
240 // way this sort of thing is handled in Layout::read(), namely, by
243 // make sure that we have the right sort of name.
244 if (name_ != from_ascii("undefined")
245 && name_.substr(0,5) != from_ascii("Flex:")) {
246 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
247 "This one has the name `" << to_utf8(name_) << "'\n"
248 "Ignoring LyXType declaration.");
249 // this is not really a reason to abort
256 lyxtype_ = translateLyXType(lt);
257 if (lyxtype_ == NOLYXTYPE) {
258 LYXERR0("Unknown LyXType `" << lt << "'.");
259 // this is not really a reason to abort
263 if (lyxtype_ == CHARSTYLE) {
264 // by default, charstyles force the plain layout
273 latextype_ = translateLaTeXType(lt);
274 if (latextype_ == ILT_ERROR) {
275 LYXERR0("Unknown LaTeXType `" << lt << "'.");
276 // this is not really a reason to abort
290 decoration_ = translateDecoration(tmp);
297 latexparam_ = subst(tmp, """, "\"");
301 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
304 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
305 lex >> fixedwidthpreambleencoding_;
307 case IL_FORCE_LOCAL_FONT_SWITCH:
308 lex >> forcelocalfontswitch_;
312 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
316 labelfont_ = lyxRead(lex, inherit_font);
321 case IL_FORCEOWNLINES:
322 lex >> forceownlines_;
329 // the defaults for these depend upon multipar_
330 if (readCustomOrPlain)
331 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
332 "Previous value may be overwritten!");
333 readCustomOrPlain = false;
334 custompars_ = multipar_;
335 forceplain_ = !multipar_;
342 readCustomOrPlain = true;
346 readCustomOrPlain = true;
351 case IL_PASSTHRU_CHARS:
352 lex >> passthru_chars_;
357 case IL_PARBREAKIGNORED:
358 lex >> parbreakignored_;
360 case IL_PARBREAKISNEWLINE:
361 lex >> parbreakisnewline_;
372 case IL_NEEDCPROTECT:
373 lex >> needcprotect_;
375 case IL_NEEDMBOXPROTECT:
376 lex >> needmboxprotect_;
378 case IL_CONTENTASLABEL:
379 lex >> contentaslabel_;
382 // initialize with a known style
385 style = subst(style, '_', ' ');
387 // We don't want to apply the algorithm in DocumentClass::insetLayout()
388 // here. So we do it the long way.
389 TextClass::InsetLayouts::const_iterator it =
390 tclass.insetLayouts().find(style);
391 if (it != tclass.insetLayouts().end()) {
392 docstring const tmpname = name_;
393 this->operator=(it->second);
396 LYXERR0("Cannot copy unknown InsetLayout `"
397 << style << "' to InsetLayout `"
399 << "All InsetLayouts so far:");
400 TextClass::InsetLayouts::const_iterator lit =
401 tclass.insetLayouts().begin();
402 TextClass::InsetLayouts::const_iterator len =
403 tclass.insetLayouts().end();
404 for (; lit != len; ++lit)
405 lyxerr << lit->second.name() << "\n";
406 // this is not really a reason to abort
412 case IL_OBSOLETEDBY: {
415 style = subst(style, '_', ' ');
417 // We don't want to apply the algorithm in DocumentClass::insetLayout()
418 // here. So we do it the long way.
419 TextClass::InsetLayouts::const_iterator it =
420 tclass.insetLayouts().find(style);
421 if (it != tclass.insetLayouts().end()) {
422 docstring const tmpname = name_;
423 this->operator=(it->second);
425 if (obsoleted_by().empty())
426 obsoleted_by_ = style;
428 LYXERR0("Cannot replace InsetLayout `"
430 << "' with unknown InsetLayout `"
432 << "All InsetLayouts so far:");
433 TextClass::InsetLayouts::const_iterator lit =
434 tclass.insetLayouts().begin();
435 TextClass::InsetLayouts::const_iterator len =
436 tclass.insetLayouts().end();
437 for (; lit != len; ++lit)
438 lyxerr << lit->second.name() << "\n";
439 // this is not really a reason to abort
447 font_ = lyxRead(lex, inherit_font);
448 // If you want to define labelfont, you need to do so after
458 postcommandargs_.clear();
466 bgcolor_ = lcolor.getFromLyXName(tmp);
469 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
471 case IL_BABELPREAMBLE:
472 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
474 case IL_LANGPREAMBLE:
475 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
486 case IL_HTMLFORCECSS:
487 lex >> htmlforcecss_;
489 case IL_HTMLINNERTAG:
490 lex >> htmlinnertag_;
492 case IL_HTMLINNERATTR:
493 lex >> htmlinnerattr_;
502 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
504 case IL_HTMLPREAMBLE:
505 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
510 case IL_DOCBOOKTAGTYPE:
511 lex >> docbooktagtype_;
516 case IL_DOCBOOKWRAPPERTAG:
517 lex >> docbookwrappertag_;
519 case IL_DOCBOOKWRAPPERTAGTYPE:
520 lex >> docbookwrappertagtype_;
522 case IL_DOCBOOKWRAPPERATTR:
523 lex >> docbookwrapperattr_;
527 vector<string> const req
528 = getVectorFromString(lex.getString(true));
529 required_.insert(req.begin(), req.end());
543 add_to_toc_ = !toc_type_.empty();
545 case IL_ISTOCCAPTION:
546 lex >> is_toc_caption_;
548 case IL_EDITEXTERNAL:
549 lex >> edit_external_;
557 // Here add element to list if getout == true
561 // The label font is generally used as-is without
562 // any realization against a given context.
563 labelfont_.realize(sane_font);
570 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
572 if (compare_ascii_no_case(str, "charstyle") == 0)
573 return InsetLayout::CHARSTYLE;
574 if (compare_ascii_no_case(str, "custom") == 0)
575 return InsetLayout::CUSTOM;
576 if (compare_ascii_no_case(str, "end") == 0)
577 return InsetLayout::END;
578 if (compare_ascii_no_case(str, "standard") == 0)
579 return InsetLayout::STANDARD;
580 return InsetLayout::NOLYXTYPE;
584 string const & InsetLayout::htmltag() const
586 if (htmltag_.empty())
587 htmltag_ = multipar_ ? "div" : "span";
592 string const & InsetLayout::htmlattr() const
594 if (htmlattr_.empty())
595 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
600 string const & InsetLayout::htmlinnerattr() const
602 if (htmlinnerattr_.empty())
603 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
604 return htmlinnerattr_;
608 string InsetLayout::defaultCSSClass() const
610 if (!defaultcssclass_.empty())
611 return defaultcssclass_;
613 string n = to_utf8(name());
614 string::const_iterator it = n.begin();
615 string::const_iterator en = n.end();
616 for (; it != en; ++it) {
617 if (!isAlphaASCII(*it))
619 else if (isLower(*it))
624 // are there other characters we need to remove?
625 defaultcssclass_ = d;
626 return defaultcssclass_;
630 void InsetLayout::makeDefaultCSS() const
632 if (!htmldefaultstyle_.empty())
634 docstring const mainfontCSS = font_.asCSS();
635 if (!mainfontCSS.empty())
637 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
638 mainfontCSS + from_ascii("\n}\n");
642 docstring InsetLayout::htmlstyle() const
644 if (!htmlstyle_.empty() && !htmlforcecss_)
646 if (htmldefaultstyle_.empty())
648 docstring retval = htmldefaultstyle_;
649 if (!htmlstyle_.empty())
650 retval += '\n' + htmlstyle_ + '\n';
654 void InsetLayout::readArgument(Lexer & lex)
656 Layout::latexarg arg;
657 arg.mandatory = false;
658 arg.autoinsert = false;
659 arg.insertcotext = false;
660 arg.insertonnewline = false;
662 bool finished = false;
663 arg.font = inherit_font;
664 arg.labelfont = inherit_font;
665 arg.is_toc_caption = false;
666 arg.free_spacing = false;
667 arg.passthru = PT_INHERITED;
668 arg.nodelims = false;
671 bool const postcmd = prefixIs(nr, "post:");
672 while (!finished && lex.isOK() && !error) {
674 string const tok = ascii_lowercase(lex.getString());
678 } else if (tok == "endargument") {
680 } else if (tok == "labelstring") {
682 arg.labelstring = lex.getDocString();
683 } else if (tok == "menustring") {
685 arg.menustring = lex.getDocString();
686 } else if (tok == "mandatory") {
688 arg.mandatory = lex.getBool();
689 } else if (tok == "autoinsert") {
691 arg.autoinsert = lex.getBool();
692 } else if (tok == "insertcotext") {
694 arg.insertcotext = lex.getBool();
695 } else if (tok == "insertonnewline") {
697 arg.insertonnewline = lex.getBool();
698 } else if (tok == "leftdelim") {
700 arg.ldelim = lex.getDocString();
701 arg.ldelim = subst(arg.ldelim,
702 from_ascii("<br/>"), from_ascii("\n"));
703 } else if (tok == "rightdelim") {
705 arg.rdelim = lex.getDocString();
706 arg.rdelim = subst(arg.rdelim,
707 from_ascii("<br/>"), from_ascii("\n"));
708 } else if (tok == "defaultarg") {
710 arg.defaultarg = lex.getDocString();
711 } else if (tok == "presetarg") {
713 arg.presetarg = lex.getDocString();
714 } else if (tok == "tooltip") {
716 arg.tooltip = lex.getDocString();
717 } else if (tok == "requires") {
719 arg.required = lex.getString();
720 } else if (tok == "decoration") {
722 arg.decoration = lex.getString();
723 } else if (tok == "newlinecmd") {
725 arg.newlinecmd = lex.getString();
726 } else if (tok == "font") {
727 arg.font = lyxRead(lex, arg.font);
728 } else if (tok == "labelfont") {
729 arg.labelfont = lyxRead(lex, arg.labelfont);
730 } else if (tok == "passthruchars") {
732 arg.pass_thru_chars = lex.getDocString();
733 } else if (tok == "passthru") {
735 docstring value = lex.getDocString();
736 if (value == "true" || value == "1")
737 arg.passthru = PT_TRUE;
738 else if (value == "false" || value == "0")
739 arg.passthru = PT_FALSE;
741 arg.passthru = PT_INHERITED;
742 } else if (tok == "istoccaption") {
744 arg.is_toc_caption = lex.getBool();
745 } else if (tok == "freespacing") {
747 arg.free_spacing = lex.getBool();
749 lex.printError("Unknown tag");
753 if (arg.labelstring.empty())
754 LYXERR0("Incomplete Argument definition!");
756 postcommandargs_[nr] = arg;
758 latexargs_[nr] = arg;
762 Layout::LaTeXArgMap InsetLayout::args() const
764 Layout::LaTeXArgMap args = latexargs_;
765 if (!postcommandargs_.empty())
766 args.insert(postcommandargs_.begin(), postcommandargs_.end());
771 int InsetLayout::optArgs() const
774 Layout::LaTeXArgMap const args = InsetLayout::args();
775 Layout::LaTeXArgMap::const_iterator it = args.begin();
776 for (; it != args.end(); ++it) {
777 if (!(*it).second.mandatory)
784 int InsetLayout::requiredArgs() const
787 Layout::LaTeXArgMap const args = InsetLayout::args();
788 Layout::LaTeXArgMap::const_iterator it = args.begin();
789 for (; it != args.end(); ++it) {
790 if ((*it).second.mandatory)