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,
110 IL_DOCBOOKNOFONTINSIDE,
132 IL_PARBREAKISNEWLINE,
144 LexerKeyword elementTags[] = {
145 { "addtotoc", IL_ADDTOTOC },
146 { "argument", IL_ARGUMENT },
147 { "babelpreamble", IL_BABELPREAMBLE },
148 { "bgcolor", IL_BGCOLOR },
149 { "contentaslabel", IL_CONTENTASLABEL },
150 { "copystyle", IL_COPYSTYLE },
151 { "counter", IL_COUNTER},
152 { "custompars", IL_CUSTOMPARS },
153 { "decoration", IL_DECORATION },
154 { "display", IL_DISPLAY },
155 { "docbookargumentaftermaintag", IL_DOCBOOKARGUMENTAFTERMAINTAG },
156 { "docbookargumentbeforemaintag", IL_DOCBOOKARGUMENTBEFOREMAINTAG },
157 { "docbookattr", IL_DOCBOOKATTR },
158 { "docbookininfo", IL_DOCBOOKININFO },
159 { "docbookitemattr", IL_DOCBOOKITEMATTR },
160 { "docbookitemtag", IL_DOCBOOKITEMTAG },
161 { "docbookitemtagtype", IL_DOCBOOKITEMTAGTYPE },
162 { "docbookitemwrapperattr", IL_DOCBOOKITEMWRAPPERATTR },
163 { "docbookitemwrappertag", IL_DOCBOOKITEMWRAPPERTAG },
164 { "docbookitemwrappertagtype", IL_DOCBOOKITEMWRAPPERTAGTYPE },
165 { "docbooknofontinside", IL_DOCBOOKNOFONTINSIDE },
166 { "docbooknotinpara", IL_DOCBOOKNOTINPARA },
167 { "docbooksection", IL_DOCBOOKSECTION },
168 { "docbooktag", IL_DOCBOOKTAG },
169 { "docbooktagtype", IL_DOCBOOKTAGTYPE },
170 { "docbookwrapperattr", IL_DOCBOOKWRAPPERATTR },
171 { "docbookwrappertag", IL_DOCBOOKWRAPPERTAG },
172 { "docbookwrappertagtype", IL_DOCBOOKWRAPPERTAGTYPE },
173 { "editexternal", IL_EDITEXTERNAL },
175 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
177 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
178 { "forceltr", IL_FORCELTR },
179 { "forceownlines", IL_FORCEOWNLINES },
180 { "forceplain", IL_FORCEPLAIN },
181 { "freespacing", IL_FREESPACING },
182 { "htmlattr", IL_HTMLATTR },
183 { "htmlforcecss", IL_HTMLFORCECSS },
184 { "htmlinnerattr", IL_HTMLINNERATTR},
185 { "htmlinnertag", IL_HTMLINNERTAG},
186 { "htmlisblock", IL_HTMLISBLOCK},
187 { "htmllabel", IL_HTMLLABEL },
188 { "htmlpreamble", IL_HTMLPREAMBLE },
189 { "htmlstyle", IL_HTMLSTYLE },
190 { "htmltag", IL_HTMLTAG },
191 { "intoc", IL_INTOC },
192 { "istoccaption", IL_ISTOCCAPTION },
193 { "keepempty", IL_KEEPEMPTY },
194 { "labelfont", IL_LABELFONT },
195 { "labelstring", IL_LABELSTRING },
196 { "langpreamble", IL_LANGPREAMBLE },
197 { "latexname", IL_LATEXNAME },
198 { "latexparam", IL_LATEXPARAM },
199 { "latextype", IL_LATEXTYPE },
200 { "leftdelim", IL_LEFTDELIM },
201 { "lyxtype", IL_LYXTYPE },
202 { "menustring", IL_MENUSTRING },
203 { "multipar", IL_MULTIPAR },
204 { "needcprotect", IL_NEEDCPROTECT },
205 { "needmboxprotect", IL_NEEDMBOXPROTECT },
206 { "needprotect", IL_NEEDPROTECT },
207 { "newlinecmd", IL_NEWLINE_CMD },
208 { "obsoletedby", IL_OBSOLETEDBY },
209 { "parbreakignored", IL_PARBREAKIGNORED },
210 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
211 { "passthru", IL_PASSTHRU },
212 { "passthruchars", IL_PASSTHRU_CHARS },
213 { "preamble", IL_PREAMBLE },
214 { "refprefix", IL_REFPREFIX },
215 { "requires", IL_REQUIRES },
216 { "resetargs", IL_RESETARGS },
217 { "resetsfont", IL_RESETSFONT },
218 { "rightdelim", IL_RIGHTDELIM },
219 { "spellcheck", IL_SPELLCHECK }
222 lex.pushTable(elementTags);
224 if (labelfont_ == sane_font)
225 labelfont_ = inherit_font;
226 if (bgcolor_ == Color_error)
227 bgcolor_ = Color_none;
229 // whether we've read the CustomPars or ForcePlain tag
230 // for issuing a warning in case MultiPars comes later
231 bool readCustomOrPlain = false;
234 while (!getout && lex.isOK()) {
237 case Lexer::LEX_UNDEF:
238 lex.printError("Unknown InsetLayout tag");
247 // Perhaps a more elegant way to deal with the next two would be the
248 // way this sort of thing is handled in Layout::read(), namely, by
251 // make sure that we have the right sort of name.
252 if (name_ != from_ascii("undefined")
253 && name_.substr(0,5) != from_ascii("Flex:")) {
254 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
255 "This one has the name `" << to_utf8(name_) << "'\n"
256 "Ignoring LyXType declaration.");
257 // this is not really a reason to abort
264 lyxtype_ = translateLyXType(lt);
265 if (lyxtype_ == InsetLyXType::NOLYXTYPE) {
266 LYXERR0("Unknown LyXType `" << lt << "'.");
267 // this is not really a reason to abort
271 if (lyxtype_ == InsetLyXType::CHARSTYLE) {
272 // by default, charstyles force the plain layout
281 latextype_ = translateLaTeXType(lt);
282 if (latextype_ == InsetLaTeXType::ILT_ERROR) {
283 LYXERR0("Unknown LaTeXType `" << lt << "'.");
284 // this is not really a reason to abort
298 decoration_ = translateDecoration(tmp);
305 latexparam_ = subst(tmp, """, "\"");
309 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
312 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
313 lex >> fixedwidthpreambleencoding_;
315 case IL_FORCE_LOCAL_FONT_SWITCH:
316 lex >> forcelocalfontswitch_;
320 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
324 labelfont_ = lyxRead(lex, inherit_font);
329 case IL_FORCEOWNLINES:
330 lex >> forceownlines_;
337 // the defaults for these depend upon multipar_
338 if (readCustomOrPlain)
339 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
340 "Previous value may be overwritten!");
341 readCustomOrPlain = false;
342 custompars_ = multipar_;
343 forceplain_ = !multipar_;
350 readCustomOrPlain = true;
354 readCustomOrPlain = true;
359 case IL_PASSTHRU_CHARS:
360 lex >> passthru_chars_;
365 case IL_PARBREAKIGNORED:
366 lex >> parbreakignored_;
368 case IL_PARBREAKISNEWLINE:
369 lex >> parbreakisnewline_;
380 case IL_NEEDCPROTECT:
381 lex >> needcprotect_;
383 case IL_NEEDMBOXPROTECT:
384 lex >> needmboxprotect_;
386 case IL_CONTENTASLABEL:
387 lex >> contentaslabel_;
390 // initialize with a known style
393 style = subst(style, '_', ' ');
395 // We don't want to apply the algorithm in DocumentClass::insetLayout()
396 // here. So we do it the long way.
397 TextClass::InsetLayouts::const_iterator it =
398 tclass.insetLayouts().find(style);
399 if (it != tclass.insetLayouts().end()) {
400 docstring const tmpname = name_;
401 this->operator=(it->second);
404 LYXERR0("Cannot copy unknown InsetLayout `"
405 << style << "' to InsetLayout `"
407 << "All InsetLayouts so far:");
408 TextClass::InsetLayouts::const_iterator lit =
409 tclass.insetLayouts().begin();
410 TextClass::InsetLayouts::const_iterator len =
411 tclass.insetLayouts().end();
412 for (; lit != len; ++lit)
413 lyxerr << lit->second.name() << "\n";
414 // this is not really a reason to abort
420 case IL_OBSOLETEDBY: {
423 style = subst(style, '_', ' ');
425 // We don't want to apply the algorithm in DocumentClass::insetLayout()
426 // here. So we do it the long way.
427 TextClass::InsetLayouts::const_iterator it =
428 tclass.insetLayouts().find(style);
429 if (it != tclass.insetLayouts().end()) {
430 docstring const tmpname = name_;
431 this->operator=(it->second);
433 if (obsoleted_by().empty())
434 obsoleted_by_ = style;
436 LYXERR0("Cannot replace InsetLayout `"
438 << "' with unknown InsetLayout `"
440 << "All InsetLayouts so far:");
441 TextClass::InsetLayouts::const_iterator lit =
442 tclass.insetLayouts().begin();
443 TextClass::InsetLayouts::const_iterator len =
444 tclass.insetLayouts().end();
445 for (; lit != len; ++lit)
446 lyxerr << lit->second.name() << "\n";
447 // this is not really a reason to abort
455 font_ = lyxRead(lex, inherit_font);
456 // If you want to define labelfont, you need to do so after
466 postcommandargs_.clear();
474 bgcolor_ = lcolor.getFromLyXName(tmp);
477 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
479 case IL_BABELPREAMBLE:
480 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
482 case IL_LANGPREAMBLE:
483 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
494 case IL_HTMLFORCECSS:
495 lex >> htmlforcecss_;
497 case IL_HTMLINNERTAG:
498 lex >> htmlinnertag_;
500 case IL_HTMLINNERATTR:
501 lex >> htmlinnerattr_;
510 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
512 case IL_HTMLPREAMBLE:
513 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
521 case IL_DOCBOOKTAGTYPE:
522 lex >> docbooktagtype_;
524 case IL_DOCBOOKININFO:
525 lex >> docbookininfo_;
527 case IL_DOCBOOKARGUMENTBEFOREMAINTAG:
528 lex >> docbookargumentbeforemaintag_;
530 case IL_DOCBOOKARGUMENTAFTERMAINTAG:
531 lex >> docbookargumentaftermaintag_;
533 case IL_DOCBOOKNOTINPARA:
534 lex >> docbooknotinpara_;
536 case IL_DOCBOOKSECTION:
537 lex >> docbooksection_;
539 case IL_DOCBOOKITEMTAG:
540 lex >> docbookitemtag_;
542 case IL_DOCBOOKITEMTAGTYPE:
543 lex >> docbookitemtagtype_;
545 case IL_DOCBOOKITEMATTR:
546 lex >> docbookitemattr_;
548 case IL_DOCBOOKITEMWRAPPERTAG:
549 lex >> docbookitemwrappertag_;
551 case IL_DOCBOOKITEMWRAPPERTAGTYPE:
552 lex >> docbookitemwrappertagtype_;
554 case IL_DOCBOOKITEMWRAPPERATTR:
555 lex >> docbookitemwrapperattr_;
557 case IL_DOCBOOKWRAPPERTAG:
558 lex >> docbookwrappertag_;
560 case IL_DOCBOOKWRAPPERTAGTYPE:
561 lex >> docbookwrappertagtype_;
563 case IL_DOCBOOKWRAPPERATTR:
564 lex >> docbookwrapperattr_;
566 case IL_DOCBOOKNOFONTINSIDE:
567 lex >> docbooknofontinside_;
571 vector<string> const req
572 = getVectorFromString(lex.getString(true));
573 required_.insert(req.begin(), req.end());
587 add_to_toc_ = !toc_type_.empty();
589 case IL_ISTOCCAPTION:
590 lex >> is_toc_caption_;
592 case IL_EDITEXTERNAL:
593 lex >> edit_external_;
601 // Here add element to list if getout == true
605 // The label font is generally used as-is without
606 // any realization against a given context.
607 labelfont_.realize(sane_font);
614 InsetLyXType translateLyXType(std::string const & str)
616 if (compare_ascii_no_case(str, "charstyle") == 0)
617 return InsetLyXType::CHARSTYLE;
618 if (compare_ascii_no_case(str, "custom") == 0)
619 return InsetLyXType::CUSTOM;
620 if (compare_ascii_no_case(str, "end") == 0)
621 return InsetLyXType::END;
622 if (compare_ascii_no_case(str, "standard") == 0)
623 return InsetLyXType::STANDARD;
624 return InsetLyXType::NOLYXTYPE;
628 string const & InsetLayout::htmltag() const
630 if (htmltag_.empty())
631 htmltag_ = multipar_ ? "div" : "span";
636 string const & InsetLayout::htmlattr() const
638 if (htmlattr_.empty())
639 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
644 string const & InsetLayout::htmlinnerattr() const
646 if (htmlinnerattr_.empty())
647 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
648 return htmlinnerattr_;
652 string InsetLayout::defaultCSSClass() const
654 if (!defaultcssclass_.empty())
655 return defaultcssclass_;
657 string n = to_utf8(name());
658 string::const_iterator it = n.begin();
659 string::const_iterator en = n.end();
660 for (; it != en; ++it) {
661 if (!isAlphaASCII(*it))
663 else if (isLower(*it))
668 // are there other characters we need to remove?
669 defaultcssclass_ = d;
670 return defaultcssclass_;
674 void InsetLayout::makeDefaultCSS() const
676 if (!htmldefaultstyle_.empty())
678 docstring const mainfontCSS = font_.asCSS();
679 if (!mainfontCSS.empty())
681 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
682 mainfontCSS + from_ascii("\n}\n");
686 docstring InsetLayout::htmlstyle() const
688 if (!htmlstyle_.empty() && !htmlforcecss_)
690 if (htmldefaultstyle_.empty())
692 docstring retval = htmldefaultstyle_;
693 if (!htmlstyle_.empty())
694 retval += '\n' + htmlstyle_ + '\n';
699 std::string const & InsetLayout::docbookininfo() const
701 // Same as Layout::docbookininfo.
702 // Indeed, a trilean. Only titles should be "maybe": otherwise, metadata is "always", content is "never".
703 if (docbookininfo_.empty() || (docbookininfo_ != "never" && docbookininfo_ != "always" && docbookininfo_ != "maybe"))
704 docbookininfo_ = "never";
705 return docbookininfo_;
709 std::string InsetLayout::docbooktagtype() const
711 if (docbooktagtype_ != "block" && docbooktagtype_ != "paragraph" && docbooktagtype_ != "inline")
712 docbooktagtype_ = "block";
713 return docbooktagtype_;
717 std::string InsetLayout::docbookwrappertagtype() const
719 if (docbookwrappertagtype_ != "block" && docbookwrappertagtype_ != "paragraph" && docbookwrappertagtype_ != "inline")
720 docbookwrappertagtype_ = "block";
721 return docbookwrappertagtype_;
725 std::string InsetLayout::docbookitemtagtype() const
727 if (docbookitemtagtype_ != "block" && docbookitemtagtype_ != "paragraph" && docbookitemtagtype_ != "inline")
728 docbookitemtagtype_ = "block";
729 return docbookitemtagtype_;
733 std::string InsetLayout::docbookitemwrappertagtype() const
735 if (docbookitemwrappertagtype_ != "block" && docbookitemwrappertagtype_ != "paragraph" && docbookitemwrappertagtype_ != "inline")
736 docbookitemwrappertagtype_ = "block";
737 return docbookitemwrappertagtype_;
741 void InsetLayout::readArgument(Lexer & lex)
743 Layout::latexarg arg;
744 arg.mandatory = false;
745 arg.autoinsert = false;
746 arg.insertcotext = false;
747 arg.insertonnewline = false;
749 bool finished = false;
750 arg.font = inherit_font;
751 arg.labelfont = inherit_font;
752 arg.is_toc_caption = false;
753 arg.free_spacing = false;
754 arg.passthru = PT_INHERITED;
755 arg.nodelims = false;
758 bool const postcmd = prefixIs(nr, "post:");
759 while (!finished && lex.isOK() && !error) {
761 string const tok = ascii_lowercase(lex.getString());
765 } else if (tok == "endargument") {
767 } else if (tok == "labelstring") {
769 arg.labelstring = lex.getDocString();
770 } else if (tok == "menustring") {
772 arg.menustring = lex.getDocString();
773 if (arg.labelstring.empty())
774 arg.labelstring = arg.menustring;
775 } else if (tok == "mandatory") {
777 arg.mandatory = lex.getBool();
778 } else if (tok == "autoinsert") {
780 arg.autoinsert = lex.getBool();
781 } else if (tok == "insertcotext") {
783 arg.insertcotext = lex.getBool();
784 } else if (tok == "insertonnewline") {
786 arg.insertonnewline = lex.getBool();
787 } else if (tok == "leftdelim") {
789 arg.ldelim = lex.getDocString();
790 arg.ldelim = subst(arg.ldelim,
791 from_ascii("<br/>"), from_ascii("\n"));
792 } else if (tok == "rightdelim") {
794 arg.rdelim = lex.getDocString();
795 arg.rdelim = subst(arg.rdelim,
796 from_ascii("<br/>"), from_ascii("\n"));
797 } else if (tok == "defaultarg") {
799 arg.defaultarg = lex.getDocString();
800 } else if (tok == "presetarg") {
802 arg.presetarg = lex.getDocString();
803 } else if (tok == "tooltip") {
805 arg.tooltip = lex.getDocString();
806 } else if (tok == "requires") {
808 arg.required = lex.getString();
809 } else if (tok == "decoration") {
811 arg.decoration = lex.getString();
812 } else if (tok == "newlinecmd") {
814 arg.newlinecmd = lex.getString();
815 } else if (tok == "font") {
816 arg.font = lyxRead(lex, arg.font);
817 } else if (tok == "labelfont") {
818 arg.labelfont = lyxRead(lex, arg.labelfont);
819 } else if (tok == "passthruchars") {
821 arg.pass_thru_chars = lex.getDocString();
822 } else if (tok == "passthru") {
824 docstring value = lex.getDocString();
825 if (value == "true" || value == "1")
826 arg.passthru = PT_TRUE;
827 else if (value == "false" || value == "0")
828 arg.passthru = PT_FALSE;
830 arg.passthru = PT_INHERITED;
831 } else if (tok == "istoccaption") {
833 arg.is_toc_caption = lex.getBool();
834 } else if (tok == "freespacing") {
836 arg.free_spacing = lex.getBool();
837 } else if (tok == "docbooktag") {
839 arg.docbooktag = lex.getDocString();
840 } else if (tok == "docbookattr") {
842 arg.docbookattr = lex.getDocString();
843 } else if (tok == "docbooktagtype") {
845 arg.docbooktagtype = lex.getDocString();
846 } else if (tok == "docbookargumentbeforemaintag") {
848 arg.docbookargumentbeforemaintag = lex.getBool();
849 } else if (tok == "docbookargumentaftermaintag") {
851 arg.docbookargumentaftermaintag = lex.getBool();
853 lex.printError("Unknown tag");
857 if (arg.labelstring.empty())
858 LYXERR0("Incomplete Argument definition!");
860 postcommandargs_[nr] = arg;
862 latexargs_[nr] = arg;
866 Layout::LaTeXArgMap InsetLayout::args() const
868 Layout::LaTeXArgMap args = latexargs_;
869 if (!postcommandargs_.empty())
870 args.insert(postcommandargs_.begin(), postcommandargs_.end());
875 int InsetLayout::optArgs() const
878 Layout::LaTeXArgMap const args = InsetLayout::args();
879 Layout::LaTeXArgMap::const_iterator it = args.begin();
880 for (; it != args.end(); ++it) {
881 if (!(*it).second.mandatory)
888 int InsetLayout::requiredArgs() const
891 Layout::LaTeXArgMap const args = InsetLayout::args();
892 Layout::LaTeXArgMap::const_iterator it = args.begin();
893 for (; it != args.end(); ++it) {
894 if ((*it).second.mandatory)