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,
100 IL_DOCBOOKWRAPPERTAG,
101 IL_DOCBOOKWRAPPERTAGTYPE,
102 IL_DOCBOOKWRAPPERATTR,
104 IL_DOCBOOKITEMTAGTYPE,
106 IL_DOCBOOKITEMWRAPPERTAG,
107 IL_DOCBOOKITEMWRAPPERTAGTYPE,
108 IL_DOCBOOKITEMWRAPPERATTR,
130 IL_PARBREAKISNEWLINE,
142 LexerKeyword elementTags[] = {
143 { "addtotoc", IL_ADDTOTOC },
144 { "argument", IL_ARGUMENT },
145 { "babelpreamble", IL_BABELPREAMBLE },
146 { "bgcolor", IL_BGCOLOR },
147 { "contentaslabel", IL_CONTENTASLABEL },
148 { "copystyle", IL_COPYSTYLE },
149 { "counter", IL_COUNTER},
150 { "custompars", IL_CUSTOMPARS },
151 { "decoration", IL_DECORATION },
152 { "display", IL_DISPLAY },
153 { "docbookargumentbeforemaintag", IL_DOCBOOKARGUMENTBEFOREMAINTAG },
154 { "docbookattr", IL_DOCBOOKATTR },
155 { "docbookininfo", IL_DOCBOOKININFO },
156 { "docbookitemattr", IL_DOCBOOKITEMATTR },
157 { "docbookitemtag", IL_DOCBOOKITEMTAG },
158 { "docbookitemtagtype", IL_DOCBOOKITEMTAGTYPE },
159 { "docbookitemwrapperattr", IL_DOCBOOKITEMWRAPPERATTR },
160 { "docbookitemwrappertag", IL_DOCBOOKITEMWRAPPERTAG },
161 { "docbookitemwrappertagtype", IL_DOCBOOKITEMWRAPPERTAGTYPE },
162 { "docbooknotinpara", IL_DOCBOOKNOTINPARA },
163 { "docbooksection", IL_DOCBOOKSECTION },
164 { "docbooktag", IL_DOCBOOKTAG },
165 { "docbooktagtype", IL_DOCBOOKTAGTYPE },
166 { "docbookwrapperattr", IL_DOCBOOKWRAPPERATTR },
167 { "docbookwrappertag", IL_DOCBOOKWRAPPERTAG },
168 { "docbookwrappertagtype", IL_DOCBOOKWRAPPERTAGTYPE },
169 { "editexternal", IL_EDITEXTERNAL },
171 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
173 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
174 { "forceltr", IL_FORCELTR },
175 { "forceownlines", IL_FORCEOWNLINES },
176 { "forceplain", IL_FORCEPLAIN },
177 { "freespacing", IL_FREESPACING },
178 { "htmlattr", IL_HTMLATTR },
179 { "htmlforcecss", IL_HTMLFORCECSS },
180 { "htmlinnerattr", IL_HTMLINNERATTR},
181 { "htmlinnertag", IL_HTMLINNERTAG},
182 { "htmlisblock", IL_HTMLISBLOCK},
183 { "htmllabel", IL_HTMLLABEL },
184 { "htmlpreamble", IL_HTMLPREAMBLE },
185 { "htmlstyle", IL_HTMLSTYLE },
186 { "htmltag", IL_HTMLTAG },
187 { "intoc", IL_INTOC },
188 { "istoccaption", IL_ISTOCCAPTION },
189 { "keepempty", IL_KEEPEMPTY },
190 { "labelfont", IL_LABELFONT },
191 { "labelstring", IL_LABELSTRING },
192 { "langpreamble", IL_LANGPREAMBLE },
193 { "latexname", IL_LATEXNAME },
194 { "latexparam", IL_LATEXPARAM },
195 { "latextype", IL_LATEXTYPE },
196 { "leftdelim", IL_LEFTDELIM },
197 { "lyxtype", IL_LYXTYPE },
198 { "menustring", IL_MENUSTRING },
199 { "multipar", IL_MULTIPAR },
200 { "needcprotect", IL_NEEDCPROTECT },
201 { "needmboxprotect", IL_NEEDMBOXPROTECT },
202 { "needprotect", IL_NEEDPROTECT },
203 { "newlinecmd", IL_NEWLINE_CMD },
204 { "obsoletedby", IL_OBSOLETEDBY },
205 { "parbreakignored", IL_PARBREAKIGNORED },
206 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
207 { "passthru", IL_PASSTHRU },
208 { "passthruchars", IL_PASSTHRU_CHARS },
209 { "preamble", IL_PREAMBLE },
210 { "refprefix", IL_REFPREFIX },
211 { "requires", IL_REQUIRES },
212 { "resetargs", IL_RESETARGS },
213 { "resetsfont", IL_RESETSFONT },
214 { "rightdelim", IL_RIGHTDELIM },
215 { "spellcheck", IL_SPELLCHECK }
218 lex.pushTable(elementTags);
220 if (labelfont_ == sane_font)
221 labelfont_ = inherit_font;
222 if (bgcolor_ == Color_error)
223 bgcolor_ = Color_none;
225 // whether we've read the CustomPars or ForcePlain tag
226 // for issuing a warning in case MultiPars comes later
227 bool readCustomOrPlain = false;
230 while (!getout && lex.isOK()) {
233 case Lexer::LEX_UNDEF:
234 lex.printError("Unknown InsetLayout tag");
243 // Perhaps a more elegant way to deal with the next two would be the
244 // way this sort of thing is handled in Layout::read(), namely, by
247 // make sure that we have the right sort of name.
248 if (name_ != from_ascii("undefined")
249 && name_.substr(0,5) != from_ascii("Flex:")) {
250 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
251 "This one has the name `" << to_utf8(name_) << "'\n"
252 "Ignoring LyXType declaration.");
253 // this is not really a reason to abort
260 lyxtype_ = translateLyXType(lt);
261 if (lyxtype_ == InsetLyXType::NOLYXTYPE) {
262 LYXERR0("Unknown LyXType `" << lt << "'.");
263 // this is not really a reason to abort
267 if (lyxtype_ == InsetLyXType::CHARSTYLE) {
268 // by default, charstyles force the plain layout
277 latextype_ = translateLaTeXType(lt);
278 if (latextype_ == InsetLaTeXType::ILT_ERROR) {
279 LYXERR0("Unknown LaTeXType `" << lt << "'.");
280 // this is not really a reason to abort
294 decoration_ = translateDecoration(tmp);
301 latexparam_ = subst(tmp, """, "\"");
305 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
308 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
309 lex >> fixedwidthpreambleencoding_;
311 case IL_FORCE_LOCAL_FONT_SWITCH:
312 lex >> forcelocalfontswitch_;
316 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
320 labelfont_ = lyxRead(lex, inherit_font);
325 case IL_FORCEOWNLINES:
326 lex >> forceownlines_;
333 // the defaults for these depend upon multipar_
334 if (readCustomOrPlain)
335 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
336 "Previous value may be overwritten!");
337 readCustomOrPlain = false;
338 custompars_ = multipar_;
339 forceplain_ = !multipar_;
346 readCustomOrPlain = true;
350 readCustomOrPlain = true;
355 case IL_PASSTHRU_CHARS:
356 lex >> passthru_chars_;
361 case IL_PARBREAKIGNORED:
362 lex >> parbreakignored_;
364 case IL_PARBREAKISNEWLINE:
365 lex >> parbreakisnewline_;
376 case IL_NEEDCPROTECT:
377 lex >> needcprotect_;
379 case IL_NEEDMBOXPROTECT:
380 lex >> needmboxprotect_;
382 case IL_CONTENTASLABEL:
383 lex >> contentaslabel_;
386 // initialize with a known style
389 style = subst(style, '_', ' ');
391 // We don't want to apply the algorithm in DocumentClass::insetLayout()
392 // here. So we do it the long way.
393 TextClass::InsetLayouts::const_iterator it =
394 tclass.insetLayouts().find(style);
395 if (it != tclass.insetLayouts().end()) {
396 docstring const tmpname = name_;
397 this->operator=(it->second);
400 LYXERR0("Cannot copy unknown InsetLayout `"
401 << style << "' to InsetLayout `"
403 << "All InsetLayouts so far:");
404 TextClass::InsetLayouts::const_iterator lit =
405 tclass.insetLayouts().begin();
406 TextClass::InsetLayouts::const_iterator len =
407 tclass.insetLayouts().end();
408 for (; lit != len; ++lit)
409 lyxerr << lit->second.name() << "\n";
410 // this is not really a reason to abort
416 case IL_OBSOLETEDBY: {
419 style = subst(style, '_', ' ');
421 // We don't want to apply the algorithm in DocumentClass::insetLayout()
422 // here. So we do it the long way.
423 TextClass::InsetLayouts::const_iterator it =
424 tclass.insetLayouts().find(style);
425 if (it != tclass.insetLayouts().end()) {
426 docstring const tmpname = name_;
427 this->operator=(it->second);
429 if (obsoleted_by().empty())
430 obsoleted_by_ = style;
432 LYXERR0("Cannot replace InsetLayout `"
434 << "' with unknown InsetLayout `"
436 << "All InsetLayouts so far:");
437 TextClass::InsetLayouts::const_iterator lit =
438 tclass.insetLayouts().begin();
439 TextClass::InsetLayouts::const_iterator len =
440 tclass.insetLayouts().end();
441 for (; lit != len; ++lit)
442 lyxerr << lit->second.name() << "\n";
443 // this is not really a reason to abort
451 font_ = lyxRead(lex, inherit_font);
452 // If you want to define labelfont, you need to do so after
462 postcommandargs_.clear();
470 bgcolor_ = lcolor.getFromLyXName(tmp);
473 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
475 case IL_BABELPREAMBLE:
476 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
478 case IL_LANGPREAMBLE:
479 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
490 case IL_HTMLFORCECSS:
491 lex >> htmlforcecss_;
493 case IL_HTMLINNERTAG:
494 lex >> htmlinnertag_;
496 case IL_HTMLINNERATTR:
497 lex >> htmlinnerattr_;
506 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
508 case IL_HTMLPREAMBLE:
509 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
517 case IL_DOCBOOKTAGTYPE:
518 lex >> docbooktagtype_;
520 case IL_DOCBOOKININFO:
521 lex >> docbookininfo_;
523 case IL_DOCBOOKARGUMENTBEFOREMAINTAG:
524 lex >> docbookargumentbeforemaintag_;
526 case IL_DOCBOOKNOTINPARA:
527 lex >> docbooknotinpara_;
529 case IL_DOCBOOKSECTION:
530 lex >> docbooksection_;
532 case IL_DOCBOOKITEMTAG:
533 lex >> docbookitemtag_;
535 case IL_DOCBOOKITEMTAGTYPE:
536 lex >> docbookitemtagtype_;
538 case IL_DOCBOOKITEMATTR:
539 lex >> docbookitemattr_;
541 case IL_DOCBOOKITEMWRAPPERTAG:
542 lex >> docbookitemwrappertag_;
544 case IL_DOCBOOKITEMWRAPPERTAGTYPE:
545 lex >> docbookitemwrappertagtype_;
547 case IL_DOCBOOKITEMWRAPPERATTR:
548 lex >> docbookitemwrapperattr_;
550 case IL_DOCBOOKWRAPPERTAG:
551 lex >> docbookwrappertag_;
553 case IL_DOCBOOKWRAPPERTAGTYPE:
554 lex >> docbookwrappertagtype_;
556 case IL_DOCBOOKWRAPPERATTR:
557 lex >> docbookwrapperattr_;
561 vector<string> const req
562 = getVectorFromString(lex.getString(true));
563 required_.insert(req.begin(), req.end());
577 add_to_toc_ = !toc_type_.empty();
579 case IL_ISTOCCAPTION:
580 lex >> is_toc_caption_;
582 case IL_EDITEXTERNAL:
583 lex >> edit_external_;
591 // Here add element to list if getout == true
595 // The label font is generally used as-is without
596 // any realization against a given context.
597 labelfont_.realize(sane_font);
604 InsetLyXType translateLyXType(std::string const & str)
606 if (compare_ascii_no_case(str, "charstyle") == 0)
607 return InsetLyXType::CHARSTYLE;
608 if (compare_ascii_no_case(str, "custom") == 0)
609 return InsetLyXType::CUSTOM;
610 if (compare_ascii_no_case(str, "end") == 0)
611 return InsetLyXType::END;
612 if (compare_ascii_no_case(str, "standard") == 0)
613 return InsetLyXType::STANDARD;
614 return InsetLyXType::NOLYXTYPE;
618 string const & InsetLayout::htmltag() const
620 if (htmltag_.empty())
621 htmltag_ = multipar_ ? "div" : "span";
626 string const & InsetLayout::htmlattr() const
628 if (htmlattr_.empty())
629 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
634 string const & InsetLayout::htmlinnerattr() const
636 if (htmlinnerattr_.empty())
637 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
638 return htmlinnerattr_;
642 string InsetLayout::defaultCSSClass() const
644 if (!defaultcssclass_.empty())
645 return defaultcssclass_;
647 string n = to_utf8(name());
648 string::const_iterator it = n.begin();
649 string::const_iterator en = n.end();
650 for (; it != en; ++it) {
651 if (!isAlphaASCII(*it))
653 else if (isLower(*it))
658 // are there other characters we need to remove?
659 defaultcssclass_ = d;
660 return defaultcssclass_;
664 void InsetLayout::makeDefaultCSS() const
666 if (!htmldefaultstyle_.empty())
668 docstring const mainfontCSS = font_.asCSS();
669 if (!mainfontCSS.empty())
671 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
672 mainfontCSS + from_ascii("\n}\n");
676 docstring InsetLayout::htmlstyle() const
678 if (!htmlstyle_.empty() && !htmlforcecss_)
680 if (htmldefaultstyle_.empty())
682 docstring retval = htmldefaultstyle_;
683 if (!htmlstyle_.empty())
684 retval += '\n' + htmlstyle_ + '\n';
689 std::string const & InsetLayout::docbookininfo() const
691 // Same as Layout::docbookininfo.
692 // Indeed, a trilean. Only titles should be "maybe": otherwise, metadata is "always", content is "never".
693 if (docbookininfo_.empty() || (docbookininfo_ != "never" && docbookininfo_ != "always" && docbookininfo_ != "maybe"))
694 docbookininfo_ = "never";
695 return docbookininfo_;
699 std::string InsetLayout::docbooktagtype() const
701 if (docbooktagtype_ != "block" && docbooktagtype_ != "paragraph" && docbooktagtype_ != "inline")
702 docbooktagtype_ = "block";
703 return docbooktagtype_;
707 std::string InsetLayout::docbookwrappertagtype() const
709 if (docbookwrappertagtype_ != "block" && docbookwrappertagtype_ != "paragraph" && docbookwrappertagtype_ != "inline")
710 docbookwrappertagtype_ = "block";
711 return docbookwrappertagtype_;
715 std::string InsetLayout::docbookitemtagtype() const
717 if (docbookitemtagtype_ != "block" && docbookitemtagtype_ != "paragraph" && docbookitemtagtype_ != "inline")
718 docbookitemtagtype_ = "block";
719 return docbookitemtagtype_;
723 std::string InsetLayout::docbookitemwrappertagtype() const
725 if (docbookitemwrappertagtype_ != "block" && docbookitemwrappertagtype_ != "paragraph" && docbookitemwrappertagtype_ != "inline")
726 docbookitemwrappertagtype_ = "block";
727 return docbookitemwrappertagtype_;
731 void InsetLayout::readArgument(Lexer & lex)
733 Layout::latexarg arg;
734 arg.mandatory = false;
735 arg.autoinsert = false;
736 arg.insertcotext = false;
737 arg.insertonnewline = false;
739 bool finished = false;
740 arg.font = inherit_font;
741 arg.labelfont = inherit_font;
742 arg.is_toc_caption = false;
743 arg.free_spacing = false;
744 arg.passthru = PT_INHERITED;
745 arg.nodelims = false;
748 bool const postcmd = prefixIs(nr, "post:");
749 while (!finished && lex.isOK() && !error) {
751 string const tok = ascii_lowercase(lex.getString());
755 } else if (tok == "endargument") {
757 } else if (tok == "labelstring") {
759 arg.labelstring = lex.getDocString();
760 } else if (tok == "menustring") {
762 arg.menustring = lex.getDocString();
763 } else if (tok == "mandatory") {
765 arg.mandatory = lex.getBool();
766 } else if (tok == "autoinsert") {
768 arg.autoinsert = lex.getBool();
769 } else if (tok == "insertcotext") {
771 arg.insertcotext = lex.getBool();
772 } else if (tok == "insertonnewline") {
774 arg.insertonnewline = lex.getBool();
775 } else if (tok == "leftdelim") {
777 arg.ldelim = lex.getDocString();
778 arg.ldelim = subst(arg.ldelim,
779 from_ascii("<br/>"), from_ascii("\n"));
780 } else if (tok == "rightdelim") {
782 arg.rdelim = lex.getDocString();
783 arg.rdelim = subst(arg.rdelim,
784 from_ascii("<br/>"), from_ascii("\n"));
785 } else if (tok == "defaultarg") {
787 arg.defaultarg = lex.getDocString();
788 } else if (tok == "presetarg") {
790 arg.presetarg = lex.getDocString();
791 } else if (tok == "tooltip") {
793 arg.tooltip = lex.getDocString();
794 } else if (tok == "requires") {
796 arg.required = lex.getString();
797 } else if (tok == "decoration") {
799 arg.decoration = lex.getString();
800 } else if (tok == "newlinecmd") {
802 arg.newlinecmd = lex.getString();
803 } else if (tok == "font") {
804 arg.font = lyxRead(lex, arg.font);
805 } else if (tok == "labelfont") {
806 arg.labelfont = lyxRead(lex, arg.labelfont);
807 } else if (tok == "passthruchars") {
809 arg.pass_thru_chars = lex.getDocString();
810 } else if (tok == "passthru") {
812 docstring value = lex.getDocString();
813 if (value == "true" || value == "1")
814 arg.passthru = PT_TRUE;
815 else if (value == "false" || value == "0")
816 arg.passthru = PT_FALSE;
818 arg.passthru = PT_INHERITED;
819 } else if (tok == "istoccaption") {
821 arg.is_toc_caption = lex.getBool();
822 } else if (tok == "freespacing") {
824 arg.free_spacing = lex.getBool();
825 } else if (tok == "docbooktag") {
827 arg.docbooktag = lex.getDocString();
828 } else if (tok == "docbookattr") {
830 arg.docbookattr = lex.getDocString();
831 } else if (tok == "docbooktagtype") {
833 arg.docbooktagtype = lex.getDocString();
834 } else if (tok == "docbookargumentbeforemaintag") {
836 arg.docbookargumentbeforemaintag = lex.getBool();
838 lex.printError("Unknown tag");
842 if (arg.labelstring.empty())
843 LYXERR0("Incomplete Argument definition!");
845 postcommandargs_[nr] = arg;
847 latexargs_[nr] = arg;
851 Layout::LaTeXArgMap InsetLayout::args() const
853 Layout::LaTeXArgMap args = latexargs_;
854 if (!postcommandargs_.empty())
855 args.insert(postcommandargs_.begin(), postcommandargs_.end());
860 int InsetLayout::optArgs() const
863 Layout::LaTeXArgMap const args = InsetLayout::args();
864 Layout::LaTeXArgMap::const_iterator it = args.begin();
865 for (; it != args.end(); ++it) {
866 if (!(*it).second.mandatory)
873 int InsetLayout::requiredArgs() const
876 Layout::LaTeXArgMap const args = InsetLayout::args();
877 Layout::LaTeXArgMap::const_iterator it = args.begin();
878 for (; it != args.end(); ++it) {
879 if ((*it).second.mandatory)