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
8 * \author Richard Kimberly Heck
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 InsetDecoration translateDecoration(std::string const & str)
37 if (compare_ascii_no_case(str, "classic") == 0)
38 return InsetDecoration::CLASSIC;
39 if (compare_ascii_no_case(str, "minimalistic") == 0)
40 return InsetDecoration::MINIMALISTIC;
41 if (compare_ascii_no_case(str, "conglomerate") == 0)
42 return InsetDecoration::CONGLOMERATE;
43 return InsetDecoration::DEFAULT;
48 InsetLaTeXType translateLaTeXType(std::string const & str)
50 if (compare_ascii_no_case(str, "command") == 0)
51 return InsetLaTeXType::COMMAND;
52 if (compare_ascii_no_case(str, "environment") == 0)
53 return InsetLaTeXType::ENVIRONMENT;
54 if (compare_ascii_no_case(str, "none") == 0)
55 return InsetLaTeXType::NOLATEXTYPE;
56 return InsetLaTeXType::ILT_ERROR;
62 bool InsetLayout::read(Lexer & lex, TextClass const & tclass,
77 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
79 IL_FORCE_LOCAL_FONT_SWITCH,
98 IL_DOCBOOKARGUMENTBEFOREMAINTAG,
99 IL_DOCBOOKARGUMENTAFTERMAINTAG,
101 IL_DOCBOOKWRAPPERTAG,
102 IL_DOCBOOKWRAPPERTAGTYPE,
103 IL_DOCBOOKWRAPPERATTR,
105 IL_DOCBOOKITEMTAGTYPE,
107 IL_DOCBOOKITEMWRAPPERTAG,
108 IL_DOCBOOKITEMWRAPPERTAGTYPE,
109 IL_DOCBOOKITEMWRAPPERATTR,
111 IL_DOCBOOKINNERTAGTYPE,
113 IL_DOCBOOKNOFONTINSIDE,
114 IL_DOCBOOKRENDERASIMAGE,
136 IL_PARBREAKISNEWLINE,
148 LexerKeyword elementTags[] = {
149 { "addtotoc", IL_ADDTOTOC },
150 { "argument", IL_ARGUMENT },
151 { "babelpreamble", IL_BABELPREAMBLE },
152 { "bgcolor", IL_BGCOLOR },
153 { "contentaslabel", IL_CONTENTASLABEL },
154 { "copystyle", IL_COPYSTYLE },
155 { "counter", IL_COUNTER},
156 { "custompars", IL_CUSTOMPARS },
157 { "decoration", IL_DECORATION },
158 { "display", IL_DISPLAY },
159 { "docbookargumentaftermaintag", IL_DOCBOOKARGUMENTAFTERMAINTAG },
160 { "docbookargumentbeforemaintag", IL_DOCBOOKARGUMENTBEFOREMAINTAG },
161 { "docbookattr", IL_DOCBOOKATTR },
162 { "docbookininfo", IL_DOCBOOKININFO },
163 { "docbookinnerattr", IL_DOCBOOKINNERATTR },
164 { "docbookinnertag", IL_DOCBOOKINNERTAG },
165 { "docbookinnertagtype", IL_DOCBOOKINNERTAGTYPE },
166 { "docbookitemattr", IL_DOCBOOKITEMATTR },
167 { "docbookitemtag", IL_DOCBOOKITEMTAG },
168 { "docbookitemtagtype", IL_DOCBOOKITEMTAGTYPE },
169 { "docbookitemwrapperattr", IL_DOCBOOKITEMWRAPPERATTR },
170 { "docbookitemwrappertag", IL_DOCBOOKITEMWRAPPERTAG },
171 { "docbookitemwrappertagtype", IL_DOCBOOKITEMWRAPPERTAGTYPE },
172 { "docbooknofontinside", IL_DOCBOOKNOFONTINSIDE },
173 { "docbooknotinpara", IL_DOCBOOKNOTINPARA },
174 { "docbookrenderasimage", IL_DOCBOOKRENDERASIMAGE },
175 { "docbooksection", IL_DOCBOOKSECTION },
176 { "docbooktag", IL_DOCBOOKTAG },
177 { "docbooktagtype", IL_DOCBOOKTAGTYPE },
178 { "docbookwrapperattr", IL_DOCBOOKWRAPPERATTR },
179 { "docbookwrappertag", IL_DOCBOOKWRAPPERTAG },
180 { "docbookwrappertagtype", IL_DOCBOOKWRAPPERTAGTYPE },
181 { "editexternal", IL_EDITEXTERNAL },
183 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
185 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
186 { "forceltr", IL_FORCELTR },
187 { "forceownlines", IL_FORCEOWNLINES },
188 { "forceplain", IL_FORCEPLAIN },
189 { "freespacing", IL_FREESPACING },
190 { "htmlattr", IL_HTMLATTR },
191 { "htmlforcecss", IL_HTMLFORCECSS },
192 { "htmlinnerattr", IL_HTMLINNERATTR},
193 { "htmlinnertag", IL_HTMLINNERTAG},
194 { "htmlisblock", IL_HTMLISBLOCK},
195 { "htmllabel", IL_HTMLLABEL },
196 { "htmlpreamble", IL_HTMLPREAMBLE },
197 { "htmlstyle", IL_HTMLSTYLE },
198 { "htmltag", IL_HTMLTAG },
199 { "intoc", IL_INTOC },
200 { "istoccaption", IL_ISTOCCAPTION },
201 { "keepempty", IL_KEEPEMPTY },
202 { "labelfont", IL_LABELFONT },
203 { "labelstring", IL_LABELSTRING },
204 { "langpreamble", IL_LANGPREAMBLE },
205 { "latexname", IL_LATEXNAME },
206 { "latexparam", IL_LATEXPARAM },
207 { "latextype", IL_LATEXTYPE },
208 { "leftdelim", IL_LEFTDELIM },
209 { "lyxtype", IL_LYXTYPE },
210 { "menustring", IL_MENUSTRING },
211 { "multipar", IL_MULTIPAR },
212 { "needcprotect", IL_NEEDCPROTECT },
213 { "needmboxprotect", IL_NEEDMBOXPROTECT },
214 { "needprotect", IL_NEEDPROTECT },
215 { "newlinecmd", IL_NEWLINE_CMD },
216 { "obsoletedby", IL_OBSOLETEDBY },
217 { "parbreakignored", IL_PARBREAKIGNORED },
218 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
219 { "passthru", IL_PASSTHRU },
220 { "passthruchars", IL_PASSTHRU_CHARS },
221 { "preamble", IL_PREAMBLE },
222 { "refprefix", IL_REFPREFIX },
223 { "requires", IL_REQUIRES },
224 { "resetargs", IL_RESETARGS },
225 { "resetsfont", IL_RESETSFONT },
226 { "rightdelim", IL_RIGHTDELIM },
227 { "spellcheck", IL_SPELLCHECK }
230 lex.pushTable(elementTags);
232 if (labelfont_ == sane_font)
233 labelfont_ = inherit_font;
234 if (bgcolor_ == Color_error)
235 bgcolor_ = Color_none;
237 // whether we've read the CustomPars or ForcePlain tag
238 // for issuing a warning in case MultiPars comes later
239 bool readCustomOrPlain = false;
242 while (!getout && lex.isOK()) {
245 case Lexer::LEX_UNDEF:
246 lex.printError("Unknown InsetLayout tag");
255 // Perhaps a more elegant way to deal with the next two would be the
256 // way this sort of thing is handled in Layout::read(), namely, by
259 // make sure that we have the right sort of name.
260 if (name_ != from_ascii("undefined")
261 && name_.substr(0,5) != from_ascii("Flex:")) {
262 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
263 "This one has the name `" << to_utf8(name_) << "'\n"
264 "Ignoring LyXType declaration.");
265 // this is not really a reason to abort
272 lyxtype_ = translateLyXType(lt);
273 if (lyxtype_ == InsetLyXType::NOLYXTYPE) {
274 LYXERR0("Unknown LyXType `" << lt << "'.");
275 // this is not really a reason to abort
279 if (lyxtype_ == InsetLyXType::CHARSTYLE) {
280 // by default, charstyles force the plain layout
289 latextype_ = translateLaTeXType(lt);
290 if (latextype_ == InsetLaTeXType::ILT_ERROR) {
291 LYXERR0("Unknown LaTeXType `" << lt << "'.");
292 // this is not really a reason to abort
306 decoration_ = translateDecoration(tmp);
313 latexparam_ = subst(tmp, """, "\"");
317 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
320 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
321 lex >> fixedwidthpreambleencoding_;
323 case IL_FORCE_LOCAL_FONT_SWITCH:
324 lex >> forcelocalfontswitch_;
328 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
332 labelfont_ = lyxRead(lex, inherit_font);
337 case IL_FORCEOWNLINES:
338 lex >> forceownlines_;
345 // the defaults for these depend upon multipar_
346 if (readCustomOrPlain)
347 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
348 "Previous value may be overwritten!");
349 readCustomOrPlain = false;
350 custompars_ = multipar_;
351 forceplain_ = !multipar_;
358 readCustomOrPlain = true;
362 readCustomOrPlain = true;
367 case IL_PASSTHRU_CHARS:
368 lex >> passthru_chars_;
373 case IL_PARBREAKIGNORED:
374 lex >> parbreakignored_;
376 case IL_PARBREAKISNEWLINE:
377 lex >> parbreakisnewline_;
388 case IL_NEEDCPROTECT:
389 lex >> needcprotect_;
391 case IL_NEEDMBOXPROTECT:
392 lex >> needmboxprotect_;
394 case IL_CONTENTASLABEL:
395 lex >> contentaslabel_;
398 // initialize with a known style
401 style = subst(style, '_', ' ');
403 // We don't want to apply the algorithm in DocumentClass::insetLayout()
404 // here. So we do it the long way.
405 TextClass::InsetLayouts::const_iterator it =
406 tclass.insetLayouts().find(style);
407 if (it != tclass.insetLayouts().end()) {
408 docstring const tmpname = name_;
409 this->operator=(it->second);
412 LYXERR0("Cannot copy unknown InsetLayout `"
413 << style << "' to InsetLayout `"
415 << "All InsetLayouts so far:");
416 TextClass::InsetLayouts::const_iterator lit =
417 tclass.insetLayouts().begin();
418 TextClass::InsetLayouts::const_iterator len =
419 tclass.insetLayouts().end();
420 for (; lit != len; ++lit)
421 lyxerr << lit->second.name() << "\n";
422 // this is not really a reason to abort
428 case IL_OBSOLETEDBY: {
431 style = subst(style, '_', ' ');
433 // We don't want to apply the algorithm in DocumentClass::insetLayout()
434 // here. So we do it the long way.
435 TextClass::InsetLayouts::const_iterator it =
436 tclass.insetLayouts().find(style);
437 if (it != tclass.insetLayouts().end()) {
438 docstring const tmpname = name_;
439 this->operator=(it->second);
441 if (obsoleted_by().empty())
442 obsoleted_by_ = style;
444 LYXERR0("Cannot replace InsetLayout `"
446 << "' with unknown InsetLayout `"
448 << "All InsetLayouts so far:");
449 TextClass::InsetLayouts::const_iterator lit =
450 tclass.insetLayouts().begin();
451 TextClass::InsetLayouts::const_iterator len =
452 tclass.insetLayouts().end();
453 for (; lit != len; ++lit)
454 lyxerr << lit->second.name() << "\n";
455 // this is not really a reason to abort
463 font_ = lyxRead(lex, inherit_font);
464 // If you want to define labelfont, you need to do so after
474 postcommandargs_.clear();
482 bgcolor_ = lcolor.getFromLyXName(tmp);
485 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
487 case IL_BABELPREAMBLE:
488 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
490 case IL_LANGPREAMBLE:
491 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
502 case IL_HTMLFORCECSS:
503 lex >> htmlforcecss_;
505 case IL_HTMLINNERTAG:
506 lex >> htmlinnertag_;
508 case IL_HTMLINNERATTR:
509 lex >> htmlinnerattr_;
518 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
520 case IL_HTMLPREAMBLE:
521 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
529 case IL_DOCBOOKTAGTYPE:
530 lex >> docbooktagtype_;
532 case IL_DOCBOOKINNERTAG:
533 lex >> docbookinnertag_;
535 case IL_DOCBOOKINNERATTR:
536 lex >> docbookinnerattr_;
538 case IL_DOCBOOKINNERTAGTYPE:
539 lex >> docbookinnertagtype_;
541 case IL_DOCBOOKININFO:
542 lex >> docbookininfo_;
544 case IL_DOCBOOKARGUMENTBEFOREMAINTAG:
545 lex >> docbookargumentbeforemaintag_;
547 case IL_DOCBOOKARGUMENTAFTERMAINTAG:
548 lex >> docbookargumentaftermaintag_;
550 case IL_DOCBOOKNOTINPARA:
551 lex >> docbooknotinpara_;
553 case IL_DOCBOOKSECTION:
554 lex >> docbooksection_;
556 case IL_DOCBOOKITEMTAG:
557 lex >> docbookitemtag_;
559 case IL_DOCBOOKITEMTAGTYPE:
560 lex >> docbookitemtagtype_;
562 case IL_DOCBOOKITEMATTR:
563 lex >> docbookitemattr_;
565 case IL_DOCBOOKITEMWRAPPERTAG:
566 lex >> docbookitemwrappertag_;
568 case IL_DOCBOOKITEMWRAPPERTAGTYPE:
569 lex >> docbookitemwrappertagtype_;
571 case IL_DOCBOOKITEMWRAPPERATTR:
572 lex >> docbookitemwrapperattr_;
574 case IL_DOCBOOKWRAPPERTAG:
575 lex >> docbookwrappertag_;
577 case IL_DOCBOOKWRAPPERTAGTYPE:
578 lex >> docbookwrappertagtype_;
580 case IL_DOCBOOKWRAPPERATTR:
581 lex >> docbookwrapperattr_;
583 case IL_DOCBOOKNOFONTINSIDE:
584 lex >> docbooknofontinside_;
586 case IL_DOCBOOKRENDERASIMAGE:
587 lex >> docbookrenderasimage_;
591 vector<string> const req
592 = getVectorFromString(lex.getString(true));
593 required_.insert(req.begin(), req.end());
607 add_to_toc_ = !toc_type_.empty();
609 case IL_ISTOCCAPTION:
610 lex >> is_toc_caption_;
612 case IL_EDITEXTERNAL:
613 lex >> edit_external_;
621 // Here add element to list if getout == true
625 // The label font is generally used as-is without
626 // any realization against a given context.
627 labelfont_.realize(sane_font);
634 InsetLyXType translateLyXType(std::string const & str)
636 if (compare_ascii_no_case(str, "charstyle") == 0)
637 return InsetLyXType::CHARSTYLE;
638 if (compare_ascii_no_case(str, "custom") == 0)
639 return InsetLyXType::CUSTOM;
640 if (compare_ascii_no_case(str, "end") == 0)
641 return InsetLyXType::END;
642 if (compare_ascii_no_case(str, "standard") == 0)
643 return InsetLyXType::STANDARD;
644 return InsetLyXType::NOLYXTYPE;
648 string const & InsetLayout::htmltag() const
650 if (htmltag_.empty())
651 htmltag_ = multipar_ ? "div" : "span";
656 string const & InsetLayout::htmlattr() const
658 if (htmlattr_.empty())
659 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
664 string const & InsetLayout::htmlinnerattr() const
666 if (htmlinnerattr_.empty())
667 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
668 return htmlinnerattr_;
672 string InsetLayout::defaultCSSClass() const
674 if (!defaultcssclass_.empty())
675 return defaultcssclass_;
677 string n = to_utf8(name());
678 string::const_iterator it = n.begin();
679 string::const_iterator en = n.end();
680 for (; it != en; ++it) {
681 if (!isAlphaASCII(*it))
683 else if (isLower(*it))
688 // are there other characters we need to remove?
689 defaultcssclass_ = d;
690 return defaultcssclass_;
694 void InsetLayout::makeDefaultCSS() const
696 if (!htmldefaultstyle_.empty())
698 docstring const mainfontCSS = font_.asCSS();
699 if (!mainfontCSS.empty())
701 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
702 mainfontCSS + from_ascii("\n}\n");
706 docstring InsetLayout::htmlstyle() const
708 if (!htmlstyle_.empty() && !htmlforcecss_)
710 if (htmldefaultstyle_.empty())
712 docstring retval = htmldefaultstyle_;
713 if (!htmlstyle_.empty())
714 retval += '\n' + htmlstyle_ + '\n';
719 std::string const & InsetLayout::docbookininfo() const
721 // Same as Layout::docbookininfo.
722 // Indeed, a trilean. Only titles should be "maybe": otherwise, metadata is "always", content is "never".
723 if (docbookininfo_.empty() || (docbookininfo_ != "never" && docbookininfo_ != "always" && docbookininfo_ != "maybe"))
724 docbookininfo_ = "never";
725 return docbookininfo_;
729 std::string InsetLayout::docbooktagtype() const
731 if (docbooktagtype_ != "block" && docbooktagtype_ != "paragraph" && docbooktagtype_ != "inline")
732 docbooktagtype_ = "block";
733 return docbooktagtype_;
737 std::string InsetLayout::docbookinnertagtype() const
739 if (docbookinnertagtype_ != "block" && docbookinnertagtype_ != "paragraph" && docbookinnertagtype_ != "inline")
740 docbookinnertagtype_ = "block";
741 return docbookinnertagtype_;
745 std::string InsetLayout::docbookwrappertagtype() const
747 if (docbookwrappertagtype_ != "block" && docbookwrappertagtype_ != "paragraph" && docbookwrappertagtype_ != "inline")
748 docbookwrappertagtype_ = "block";
749 return docbookwrappertagtype_;
753 std::string InsetLayout::docbookitemtagtype() const
755 if (docbookitemtagtype_ != "block" && docbookitemtagtype_ != "paragraph" && docbookitemtagtype_ != "inline")
756 docbookitemtagtype_ = "block";
757 return docbookitemtagtype_;
761 std::string InsetLayout::docbookitemwrappertagtype() const
763 if (docbookitemwrappertagtype_ != "block" && docbookitemwrappertagtype_ != "paragraph" && docbookitemwrappertagtype_ != "inline")
764 docbookitemwrappertagtype_ = "block";
765 return docbookitemwrappertagtype_;
769 void InsetLayout::readArgument(Lexer & lex)
771 Layout::latexarg arg;
772 arg.mandatory = false;
773 arg.autoinsert = false;
774 arg.insertcotext = false;
775 arg.insertonnewline = false;
777 bool finished = false;
778 arg.font = inherit_font;
779 arg.labelfont = inherit_font;
780 arg.is_toc_caption = false;
781 arg.free_spacing = false;
782 arg.passthru = PT_INHERITED;
783 arg.nodelims = false;
786 bool const postcmd = prefixIs(nr, "post:");
787 while (!finished && lex.isOK() && !error) {
789 string const tok = ascii_lowercase(lex.getString());
793 } else if (tok == "endargument") {
795 } else if (tok == "labelstring") {
797 arg.labelstring = lex.getDocString();
798 } else if (tok == "menustring") {
800 arg.menustring = lex.getDocString();
801 } else if (tok == "mandatory") {
803 arg.mandatory = lex.getBool();
804 } else if (tok == "autoinsert") {
806 arg.autoinsert = lex.getBool();
807 } else if (tok == "insertcotext") {
809 arg.insertcotext = lex.getBool();
810 } else if (tok == "insertonnewline") {
812 arg.insertonnewline = lex.getBool();
813 } else if (tok == "leftdelim") {
815 arg.ldelim = lex.getDocString();
816 arg.ldelim = subst(arg.ldelim,
817 from_ascii("<br/>"), from_ascii("\n"));
818 } else if (tok == "rightdelim") {
820 arg.rdelim = lex.getDocString();
821 arg.rdelim = subst(arg.rdelim,
822 from_ascii("<br/>"), from_ascii("\n"));
823 } else if (tok == "defaultarg") {
825 arg.defaultarg = lex.getDocString();
826 } else if (tok == "presetarg") {
828 arg.presetarg = lex.getDocString();
829 } else if (tok == "tooltip") {
831 arg.tooltip = lex.getDocString();
832 } else if (tok == "requires") {
834 arg.required = lex.getString();
835 } else if (tok == "decoration") {
837 arg.decoration = lex.getString();
838 } else if (tok == "newlinecmd") {
840 arg.newlinecmd = lex.getString();
841 } else if (tok == "font") {
842 arg.font = lyxRead(lex, arg.font);
843 } else if (tok == "labelfont") {
844 arg.labelfont = lyxRead(lex, arg.labelfont);
845 } else if (tok == "passthruchars") {
847 arg.pass_thru_chars = lex.getDocString();
848 } else if (tok == "passthru") {
850 docstring value = lex.getDocString();
851 if (value == "true" || value == "1")
852 arg.passthru = PT_TRUE;
853 else if (value == "false" || value == "0")
854 arg.passthru = PT_FALSE;
856 arg.passthru = PT_INHERITED;
857 } else if (tok == "istoccaption") {
859 arg.is_toc_caption = lex.getBool();
860 } else if (tok == "freespacing") {
862 arg.free_spacing = lex.getBool();
863 } else if (tok == "docbooktag") {
865 arg.docbooktag = lex.getDocString();
866 } else if (tok == "docbookattr") {
868 arg.docbookattr = lex.getDocString();
869 } else if (tok == "docbooktagtype") {
871 arg.docbooktagtype = lex.getDocString();
872 } else if (tok == "docbookargumentbeforemaintag") {
874 arg.docbookargumentbeforemaintag = lex.getBool();
875 } else if (tok == "docbookargumentaftermaintag") {
877 arg.docbookargumentaftermaintag = lex.getBool();
879 lex.printError("Unknown tag");
883 if (arg.labelstring.empty())
884 LYXERR0("Incomplete Argument definition!");
886 postcommandargs_[nr] = arg;
888 latexargs_[nr] = arg;
892 Layout::LaTeXArgMap InsetLayout::args() const
894 Layout::LaTeXArgMap args = latexargs_;
895 if (!postcommandargs_.empty())
896 args.insert(postcommandargs_.begin(), postcommandargs_.end());
901 int InsetLayout::optArgs() const
904 Layout::LaTeXArgMap const args = InsetLayout::args();
905 Layout::LaTeXArgMap::const_iterator it = args.begin();
906 for (; it != args.end(); ++it) {
907 if (!(*it).second.mandatory)
914 int InsetLayout::requiredArgs() const
917 Layout::LaTeXArgMap const args = InsetLayout::args();
918 Layout::LaTeXArgMap::const_iterator it = args.begin();
919 for (; it != args.end(); ++it) {
920 if ((*it).second.mandatory)