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,
99 IL_DOCBOOKARGUMENTBEFOREMAINTAG,
100 IL_DOCBOOKARGUMENTAFTERMAINTAG,
102 IL_DOCBOOKWRAPPERTAG,
103 IL_DOCBOOKWRAPPERTAGTYPE,
104 IL_DOCBOOKWRAPPERATTR,
106 IL_DOCBOOKITEMTAGTYPE,
108 IL_DOCBOOKITEMWRAPPERTAG,
109 IL_DOCBOOKITEMWRAPPERTAGTYPE,
110 IL_DOCBOOKITEMWRAPPERATTR,
112 IL_DOCBOOKINNERTAGTYPE,
114 IL_DOCBOOKNOFONTINSIDE,
115 IL_DOCBOOKRENDERASIMAGE,
137 IL_PARBREAKISNEWLINE,
149 LexerKeyword elementTags[] = {
150 { "addtotoc", IL_ADDTOTOC },
151 { "argument", IL_ARGUMENT },
152 { "babelpreamble", IL_BABELPREAMBLE },
153 { "bgcolor", IL_BGCOLOR },
154 { "contentaslabel", IL_CONTENTASLABEL },
155 { "copystyle", IL_COPYSTYLE },
156 { "counter", IL_COUNTER},
157 { "custompars", IL_CUSTOMPARS },
158 { "decoration", IL_DECORATION },
159 { "display", IL_DISPLAY },
160 { "docbookargumentaftermaintag", IL_DOCBOOKARGUMENTAFTERMAINTAG },
161 { "docbookargumentbeforemaintag", IL_DOCBOOKARGUMENTBEFOREMAINTAG },
162 { "docbookattr", IL_DOCBOOKATTR },
163 { "docbookininfo", IL_DOCBOOKININFO },
164 { "docbookinnerattr", IL_DOCBOOKINNERATTR },
165 { "docbookinnertag", IL_DOCBOOKINNERTAG },
166 { "docbookinnertagtype", IL_DOCBOOKINNERTAGTYPE },
167 { "docbookitemattr", IL_DOCBOOKITEMATTR },
168 { "docbookitemtag", IL_DOCBOOKITEMTAG },
169 { "docbookitemtagtype", IL_DOCBOOKITEMTAGTYPE },
170 { "docbookitemwrapperattr", IL_DOCBOOKITEMWRAPPERATTR },
171 { "docbookitemwrappertag", IL_DOCBOOKITEMWRAPPERTAG },
172 { "docbookitemwrappertagtype", IL_DOCBOOKITEMWRAPPERTAGTYPE },
173 { "docbooknofontinside", IL_DOCBOOKNOFONTINSIDE },
174 { "docbooknotinpara", IL_DOCBOOKNOTINPARA },
175 { "docbookrenderasimage", IL_DOCBOOKRENDERASIMAGE },
176 { "docbooksection", IL_DOCBOOKSECTION },
177 { "docbooktag", IL_DOCBOOKTAG },
178 { "docbooktagtype", IL_DOCBOOKTAGTYPE },
179 { "docbookwrapperattr", IL_DOCBOOKWRAPPERATTR },
180 { "docbookwrappertag", IL_DOCBOOKWRAPPERTAG },
181 { "docbookwrappertagtype", IL_DOCBOOKWRAPPERTAGTYPE },
182 { "editexternal", IL_EDITEXTERNAL },
184 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
186 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
187 { "forceltr", IL_FORCELTR },
188 { "forceownlines", IL_FORCEOWNLINES },
189 { "forceplain", IL_FORCEPLAIN },
190 { "freespacing", IL_FREESPACING },
191 { "htmlattr", IL_HTMLATTR },
192 { "htmlclass", IL_HTMLCLASS },
193 { "htmlforcecss", IL_HTMLFORCECSS },
194 { "htmlinnerattr", IL_HTMLINNERATTR},
195 { "htmlinnertag", IL_HTMLINNERTAG},
196 { "htmlisblock", IL_HTMLISBLOCK},
197 { "htmllabel", IL_HTMLLABEL },
198 { "htmlpreamble", IL_HTMLPREAMBLE },
199 { "htmlstyle", IL_HTMLSTYLE },
200 { "htmltag", IL_HTMLTAG },
201 { "intoc", IL_INTOC },
202 { "istoccaption", IL_ISTOCCAPTION },
203 { "keepempty", IL_KEEPEMPTY },
204 { "labelfont", IL_LABELFONT },
205 { "labelstring", IL_LABELSTRING },
206 { "langpreamble", IL_LANGPREAMBLE },
207 { "latexname", IL_LATEXNAME },
208 { "latexparam", IL_LATEXPARAM },
209 { "latextype", IL_LATEXTYPE },
210 { "leftdelim", IL_LEFTDELIM },
211 { "lyxtype", IL_LYXTYPE },
212 { "menustring", IL_MENUSTRING },
213 { "multipar", IL_MULTIPAR },
214 { "needcprotect", IL_NEEDCPROTECT },
215 { "needmboxprotect", IL_NEEDMBOXPROTECT },
216 { "needprotect", IL_NEEDPROTECT },
217 { "newlinecmd", IL_NEWLINE_CMD },
218 { "obsoletedby", IL_OBSOLETEDBY },
219 { "parbreakignored", IL_PARBREAKIGNORED },
220 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
221 { "passthru", IL_PASSTHRU },
222 { "passthruchars", IL_PASSTHRU_CHARS },
223 { "preamble", IL_PREAMBLE },
224 { "refprefix", IL_REFPREFIX },
225 { "requires", IL_REQUIRES },
226 { "resetargs", IL_RESETARGS },
227 { "resetsfont", IL_RESETSFONT },
228 { "rightdelim", IL_RIGHTDELIM },
229 { "spellcheck", IL_SPELLCHECK }
232 lex.pushTable(elementTags);
234 if (labelfont_ == sane_font)
235 labelfont_ = inherit_font;
236 if (bgcolor_ == Color_error)
237 bgcolor_ = Color_none;
239 // whether we've read the CustomPars or ForcePlain tag
240 // for issuing a warning in case MultiPars comes later
241 bool readCustomOrPlain = false;
244 while (!getout && lex.isOK()) {
247 case Lexer::LEX_UNDEF:
248 lex.printError("Unknown InsetLayout tag");
257 // Perhaps a more elegant way to deal with the next two would be the
258 // way this sort of thing is handled in Layout::read(), namely, by
261 // make sure that we have the right sort of name.
262 if (name_ != from_ascii("undefined")
263 && name_.substr(0,5) != from_ascii("Flex:")) {
264 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
265 "This one has the name `" << to_utf8(name_) << "'\n"
266 "Ignoring LyXType declaration.");
267 // this is not really a reason to abort
274 lyxtype_ = translateLyXType(lt);
275 if (lyxtype_ == InsetLyXType::NOLYXTYPE) {
276 LYXERR0("Unknown LyXType `" << lt << "'.");
277 // this is not really a reason to abort
281 if (lyxtype_ == InsetLyXType::CHARSTYLE) {
282 // by default, charstyles force the plain layout
291 latextype_ = translateLaTeXType(lt);
292 if (latextype_ == InsetLaTeXType::ILT_ERROR) {
293 LYXERR0("Unknown LaTeXType `" << lt << "'.");
294 // this is not really a reason to abort
308 decoration_ = translateDecoration(tmp);
315 latexparam_ = subst(tmp, """, "\"");
319 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
322 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
323 lex >> fixedwidthpreambleencoding_;
325 case IL_FORCE_LOCAL_FONT_SWITCH:
326 lex >> forcelocalfontswitch_;
330 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
334 labelfont_ = lyxRead(lex, inherit_font);
339 case IL_FORCEOWNLINES:
340 lex >> forceownlines_;
347 // the defaults for these depend upon multipar_
348 if (readCustomOrPlain)
349 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
350 "Previous value may be overwritten!");
351 readCustomOrPlain = false;
352 custompars_ = multipar_;
353 forceplain_ = !multipar_;
360 readCustomOrPlain = true;
364 readCustomOrPlain = true;
369 case IL_PASSTHRU_CHARS:
370 lex >> passthru_chars_;
375 case IL_PARBREAKIGNORED:
376 lex >> parbreakignored_;
378 case IL_PARBREAKISNEWLINE:
379 lex >> parbreakisnewline_;
390 case IL_NEEDCPROTECT:
391 lex >> needcprotect_;
393 case IL_NEEDMBOXPROTECT:
394 lex >> needmboxprotect_;
396 case IL_CONTENTASLABEL:
397 lex >> contentaslabel_;
400 // initialize with a known style
403 style = subst(style, '_', ' ');
405 // We don't want to apply the algorithm in DocumentClass::insetLayout()
406 // here. So we do it the long way.
407 TextClass::InsetLayouts::const_iterator it =
408 tclass.insetLayouts().find(style);
409 if (it != tclass.insetLayouts().end()) {
410 docstring const tmpname = name_;
411 this->operator=(it->second);
414 LYXERR0("Cannot copy unknown InsetLayout `"
415 << style << "' to InsetLayout `"
417 << "All InsetLayouts so far:");
418 TextClass::InsetLayouts::const_iterator lit =
419 tclass.insetLayouts().begin();
420 TextClass::InsetLayouts::const_iterator len =
421 tclass.insetLayouts().end();
422 for (; lit != len; ++lit)
423 lyxerr << lit->second.name() << "\n";
424 // this is not really a reason to abort
430 case IL_OBSOLETEDBY: {
433 style = subst(style, '_', ' ');
435 // We don't want to apply the algorithm in DocumentClass::insetLayout()
436 // here. So we do it the long way.
437 TextClass::InsetLayouts::const_iterator it =
438 tclass.insetLayouts().find(style);
439 if (it != tclass.insetLayouts().end()) {
440 docstring const tmpname = name_;
441 this->operator=(it->second);
443 if (obsoleted_by().empty())
444 obsoleted_by_ = style;
446 LYXERR0("Cannot replace InsetLayout `"
448 << "' with unknown InsetLayout `"
450 << "All InsetLayouts so far:");
451 TextClass::InsetLayouts::const_iterator lit =
452 tclass.insetLayouts().begin();
453 TextClass::InsetLayouts::const_iterator len =
454 tclass.insetLayouts().end();
455 for (; lit != len; ++lit)
456 lyxerr << lit->second.name() << "\n";
457 // this is not really a reason to abort
465 font_ = lyxRead(lex, inherit_font);
466 // If you want to define labelfont, you need to do so after
476 postcommandargs_.clear();
484 bgcolor_ = lcolor.getFromLyXName(tmp);
487 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
489 case IL_BABELPREAMBLE:
490 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
492 case IL_LANGPREAMBLE:
493 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
507 case IL_HTMLFORCECSS:
508 lex >> htmlforcecss_;
510 case IL_HTMLINNERTAG:
511 lex >> htmlinnertag_;
513 case IL_HTMLINNERATTR:
514 lex >> htmlinnerattr_;
523 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
525 case IL_HTMLPREAMBLE:
526 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
534 case IL_DOCBOOKTAGTYPE:
535 lex >> docbooktagtype_;
537 case IL_DOCBOOKINNERTAG:
538 lex >> docbookinnertag_;
540 case IL_DOCBOOKINNERATTR:
541 lex >> docbookinnerattr_;
543 case IL_DOCBOOKINNERTAGTYPE:
544 lex >> docbookinnertagtype_;
546 case IL_DOCBOOKININFO:
547 lex >> docbookininfo_;
549 case IL_DOCBOOKARGUMENTBEFOREMAINTAG:
550 lex >> docbookargumentbeforemaintag_;
552 case IL_DOCBOOKARGUMENTAFTERMAINTAG:
553 lex >> docbookargumentaftermaintag_;
555 case IL_DOCBOOKNOTINPARA:
556 lex >> docbooknotinpara_;
558 case IL_DOCBOOKSECTION:
559 lex >> docbooksection_;
561 case IL_DOCBOOKITEMTAG:
562 lex >> docbookitemtag_;
564 case IL_DOCBOOKITEMTAGTYPE:
565 lex >> docbookitemtagtype_;
567 case IL_DOCBOOKITEMATTR:
568 lex >> docbookitemattr_;
570 case IL_DOCBOOKITEMWRAPPERTAG:
571 lex >> docbookitemwrappertag_;
573 case IL_DOCBOOKITEMWRAPPERTAGTYPE:
574 lex >> docbookitemwrappertagtype_;
576 case IL_DOCBOOKITEMWRAPPERATTR:
577 lex >> docbookitemwrapperattr_;
579 case IL_DOCBOOKWRAPPERTAG:
580 lex >> docbookwrappertag_;
582 case IL_DOCBOOKWRAPPERTAGTYPE:
583 lex >> docbookwrappertagtype_;
585 case IL_DOCBOOKWRAPPERATTR:
586 lex >> docbookwrapperattr_;
588 case IL_DOCBOOKNOFONTINSIDE:
589 lex >> docbooknofontinside_;
591 case IL_DOCBOOKRENDERASIMAGE:
592 lex >> docbookrenderasimage_;
596 vector<string> const req
597 = getVectorFromString(lex.getString(true));
598 required_.insert(req.begin(), req.end());
612 add_to_toc_ = !toc_type_.empty();
614 case IL_ISTOCCAPTION:
615 lex >> is_toc_caption_;
617 case IL_EDITEXTERNAL:
618 lex >> edit_external_;
626 // Here add element to list if getout == true
630 // The label font is generally used as-is without
631 // any realization against a given context.
632 labelfont_.realize(sane_font);
639 InsetLyXType translateLyXType(std::string const & str)
641 if (compare_ascii_no_case(str, "charstyle") == 0)
642 return InsetLyXType::CHARSTYLE;
643 if (compare_ascii_no_case(str, "custom") == 0)
644 return InsetLyXType::CUSTOM;
645 if (compare_ascii_no_case(str, "end") == 0)
646 return InsetLyXType::END;
647 if (compare_ascii_no_case(str, "standard") == 0)
648 return InsetLyXType::STANDARD;
649 return InsetLyXType::NOLYXTYPE;
653 string const & InsetLayout::htmltag() const
655 if (htmltag_.empty())
656 htmltag_ = multipar_ ? "div" : "span";
661 string const & InsetLayout::htmlclass() const
663 if (htmlclass_.empty())
664 htmlclass_ = defaultCSSClass();
669 std::string const & InsetLayout::htmlGetAttrString() const {
670 if (!htmlfullattrs_.empty())
671 return htmlfullattrs_;
672 htmlfullattrs_ = htmlclass();
673 if (!htmlfullattrs_.empty())
674 htmlfullattrs_ = "class='" + htmlfullattrs_ + "'";
675 if (!htmlattr_.empty())
676 htmlfullattrs_ += " " + htmlattr_;
677 return htmlfullattrs_;
681 string const & InsetLayout::htmlinnerattr() const
683 if (htmlinnerattr_.empty())
684 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
685 return htmlinnerattr_;
689 string InsetLayout::defaultCSSClass() const
691 if (!defaultcssclass_.empty())
692 return defaultcssclass_;
694 string n = to_utf8(name());
695 string::const_iterator it = n.begin();
696 string::const_iterator en = n.end();
697 for (; it != en; ++it) {
698 if (!isAlphaASCII(*it))
700 else if (isLower(*it))
705 // are there other characters we need to remove?
706 defaultcssclass_ = d;
707 return defaultcssclass_;
711 void InsetLayout::makeDefaultCSS() const
713 if (!htmldefaultstyle_.empty())
715 docstring const mainfontCSS = font_.asCSS();
716 if (!mainfontCSS.empty())
718 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
719 mainfontCSS + from_ascii("\n}\n");
723 docstring InsetLayout::htmlstyle() const
725 if (!htmlstyle_.empty() && !htmlforcecss_)
727 if (htmldefaultstyle_.empty())
729 docstring retval = htmldefaultstyle_;
730 if (!htmlstyle_.empty())
731 retval += '\n' + htmlstyle_ + '\n';
736 std::string const & InsetLayout::docbookininfo() const
738 // Same as Layout::docbookininfo.
739 // Indeed, a trilean. Only titles should be "maybe": otherwise, metadata is "always", content is "never".
740 if (docbookininfo_.empty() || (docbookininfo_ != "never" && docbookininfo_ != "always" && docbookininfo_ != "maybe"))
741 docbookininfo_ = "never";
742 return docbookininfo_;
746 std::string InsetLayout::docbooktagtype() const
748 if (docbooktagtype_ != "block" && docbooktagtype_ != "paragraph" && docbooktagtype_ != "inline")
749 docbooktagtype_ = "block";
750 return docbooktagtype_;
754 std::string InsetLayout::docbookinnertagtype() const
756 if (docbookinnertagtype_ != "block" && docbookinnertagtype_ != "paragraph" && docbookinnertagtype_ != "inline")
757 docbookinnertagtype_ = "block";
758 return docbookinnertagtype_;
762 std::string InsetLayout::docbookwrappertagtype() const
764 if (docbookwrappertagtype_ != "block" && docbookwrappertagtype_ != "paragraph" && docbookwrappertagtype_ != "inline")
765 docbookwrappertagtype_ = "block";
766 return docbookwrappertagtype_;
770 std::string InsetLayout::docbookitemtagtype() const
772 if (docbookitemtagtype_ != "block" && docbookitemtagtype_ != "paragraph" && docbookitemtagtype_ != "inline")
773 docbookitemtagtype_ = "block";
774 return docbookitemtagtype_;
778 std::string InsetLayout::docbookitemwrappertagtype() const
780 if (docbookitemwrappertagtype_ != "block" && docbookitemwrappertagtype_ != "paragraph" && docbookitemwrappertagtype_ != "inline")
781 docbookitemwrappertagtype_ = "block";
782 return docbookitemwrappertagtype_;
786 void InsetLayout::readArgument(Lexer & lex)
788 Layout::latexarg arg;
789 arg.mandatory = false;
790 arg.autoinsert = false;
791 arg.insertcotext = false;
792 arg.insertonnewline = false;
794 bool finished = false;
795 arg.font = inherit_font;
796 arg.labelfont = inherit_font;
797 arg.is_toc_caption = false;
798 arg.free_spacing = false;
799 arg.passthru = PT_INHERITED;
800 arg.nodelims = false;
803 bool const postcmd = prefixIs(nr, "post:");
804 while (!finished && lex.isOK() && !error) {
806 string const tok = ascii_lowercase(lex.getString());
810 } else if (tok == "endargument") {
812 } else if (tok == "labelstring") {
814 arg.labelstring = lex.getDocString();
815 } else if (tok == "menustring") {
817 arg.menustring = lex.getDocString();
818 } else if (tok == "mandatory") {
820 arg.mandatory = lex.getBool();
821 } else if (tok == "autoinsert") {
823 arg.autoinsert = lex.getBool();
824 } else if (tok == "insertcotext") {
826 arg.insertcotext = lex.getBool();
827 } else if (tok == "insertonnewline") {
829 arg.insertonnewline = lex.getBool();
830 } else if (tok == "leftdelim") {
832 arg.ldelim = lex.getDocString();
833 arg.ldelim = subst(arg.ldelim,
834 from_ascii("<br/>"), from_ascii("\n"));
835 } else if (tok == "rightdelim") {
837 arg.rdelim = lex.getDocString();
838 arg.rdelim = subst(arg.rdelim,
839 from_ascii("<br/>"), from_ascii("\n"));
840 } else if (tok == "defaultarg") {
842 arg.defaultarg = lex.getDocString();
843 } else if (tok == "presetarg") {
845 arg.presetarg = lex.getDocString();
846 } else if (tok == "tooltip") {
848 arg.tooltip = lex.getDocString();
849 } else if (tok == "requires") {
851 arg.required = lex.getString();
852 } else if (tok == "decoration") {
854 arg.decoration = lex.getString();
855 } else if (tok == "newlinecmd") {
857 arg.newlinecmd = lex.getString();
858 } else if (tok == "font") {
859 arg.font = lyxRead(lex, arg.font);
860 } else if (tok == "labelfont") {
861 arg.labelfont = lyxRead(lex, arg.labelfont);
862 } else if (tok == "passthruchars") {
864 arg.pass_thru_chars = lex.getDocString();
865 } else if (tok == "passthru") {
867 docstring value = lex.getDocString();
868 if (value == "true" || value == "1")
869 arg.passthru = PT_TRUE;
870 else if (value == "false" || value == "0")
871 arg.passthru = PT_FALSE;
873 arg.passthru = PT_INHERITED;
874 } else if (tok == "istoccaption") {
876 arg.is_toc_caption = lex.getBool();
877 } else if (tok == "freespacing") {
879 arg.free_spacing = lex.getBool();
880 } else if (tok == "docbooktag") {
882 arg.docbooktag = lex.getDocString();
883 } else if (tok == "docbookattr") {
885 arg.docbookattr = lex.getDocString();
886 } else if (tok == "docbooktagtype") {
888 arg.docbooktagtype = lex.getDocString();
889 } else if (tok == "docbookargumentbeforemaintag") {
891 arg.docbookargumentbeforemaintag = lex.getBool();
892 } else if (tok == "docbookargumentaftermaintag") {
894 arg.docbookargumentaftermaintag = lex.getBool();
896 lex.printError("Unknown tag");
900 if (arg.labelstring.empty())
901 LYXERR0("Incomplete Argument definition!");
903 postcommandargs_[nr] = arg;
905 latexargs_[nr] = arg;
909 Layout::LaTeXArgMap InsetLayout::args() const
911 Layout::LaTeXArgMap args = latexargs_;
912 if (!postcommandargs_.empty())
913 args.insert(postcommandargs_.begin(), postcommandargs_.end());
918 int InsetLayout::optArgs() const
921 Layout::LaTeXArgMap const args = InsetLayout::args();
922 Layout::LaTeXArgMap::const_iterator it = args.begin();
923 for (; it != args.end(); ++it) {
924 if (!(*it).second.mandatory)
931 int InsetLayout::requiredArgs() const
934 Layout::LaTeXArgMap const args = InsetLayout::args();
935 Layout::LaTeXArgMap::const_iterator it = args.begin();
936 for (; it != args.end(); ++it) {
937 if ((*it).second.mandatory)