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,
109 IL_DOCBOOKNOFONTINSIDE,
131 IL_PARBREAKISNEWLINE,
143 LexerKeyword elementTags[] = {
144 { "addtotoc", IL_ADDTOTOC },
145 { "argument", IL_ARGUMENT },
146 { "babelpreamble", IL_BABELPREAMBLE },
147 { "bgcolor", IL_BGCOLOR },
148 { "contentaslabel", IL_CONTENTASLABEL },
149 { "copystyle", IL_COPYSTYLE },
150 { "counter", IL_COUNTER},
151 { "custompars", IL_CUSTOMPARS },
152 { "decoration", IL_DECORATION },
153 { "display", IL_DISPLAY },
154 { "docbookargumentbeforemaintag", IL_DOCBOOKARGUMENTBEFOREMAINTAG },
155 { "docbookattr", IL_DOCBOOKATTR },
156 { "docbookininfo", IL_DOCBOOKININFO },
157 { "docbookitemattr", IL_DOCBOOKITEMATTR },
158 { "docbookitemtag", IL_DOCBOOKITEMTAG },
159 { "docbookitemtagtype", IL_DOCBOOKITEMTAGTYPE },
160 { "docbookitemwrapperattr", IL_DOCBOOKITEMWRAPPERATTR },
161 { "docbookitemwrappertag", IL_DOCBOOKITEMWRAPPERTAG },
162 { "docbookitemwrappertagtype", IL_DOCBOOKITEMWRAPPERTAGTYPE },
163 { "docbooknofontinside", IL_DOCBOOKNOFONTINSIDE },
164 { "docbooknotinpara", IL_DOCBOOKNOTINPARA },
165 { "docbooksection", IL_DOCBOOKSECTION },
166 { "docbooktag", IL_DOCBOOKTAG },
167 { "docbooktagtype", IL_DOCBOOKTAGTYPE },
168 { "docbookwrapperattr", IL_DOCBOOKWRAPPERATTR },
169 { "docbookwrappertag", IL_DOCBOOKWRAPPERTAG },
170 { "docbookwrappertagtype", IL_DOCBOOKWRAPPERTAGTYPE },
171 { "editexternal", IL_EDITEXTERNAL },
173 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
175 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
176 { "forceltr", IL_FORCELTR },
177 { "forceownlines", IL_FORCEOWNLINES },
178 { "forceplain", IL_FORCEPLAIN },
179 { "freespacing", IL_FREESPACING },
180 { "htmlattr", IL_HTMLATTR },
181 { "htmlforcecss", IL_HTMLFORCECSS },
182 { "htmlinnerattr", IL_HTMLINNERATTR},
183 { "htmlinnertag", IL_HTMLINNERTAG},
184 { "htmlisblock", IL_HTMLISBLOCK},
185 { "htmllabel", IL_HTMLLABEL },
186 { "htmlpreamble", IL_HTMLPREAMBLE },
187 { "htmlstyle", IL_HTMLSTYLE },
188 { "htmltag", IL_HTMLTAG },
189 { "intoc", IL_INTOC },
190 { "istoccaption", IL_ISTOCCAPTION },
191 { "keepempty", IL_KEEPEMPTY },
192 { "labelfont", IL_LABELFONT },
193 { "labelstring", IL_LABELSTRING },
194 { "langpreamble", IL_LANGPREAMBLE },
195 { "latexname", IL_LATEXNAME },
196 { "latexparam", IL_LATEXPARAM },
197 { "latextype", IL_LATEXTYPE },
198 { "leftdelim", IL_LEFTDELIM },
199 { "lyxtype", IL_LYXTYPE },
200 { "menustring", IL_MENUSTRING },
201 { "multipar", IL_MULTIPAR },
202 { "needcprotect", IL_NEEDCPROTECT },
203 { "needmboxprotect", IL_NEEDMBOXPROTECT },
204 { "needprotect", IL_NEEDPROTECT },
205 { "newlinecmd", IL_NEWLINE_CMD },
206 { "obsoletedby", IL_OBSOLETEDBY },
207 { "parbreakignored", IL_PARBREAKIGNORED },
208 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
209 { "passthru", IL_PASSTHRU },
210 { "passthruchars", IL_PASSTHRU_CHARS },
211 { "preamble", IL_PREAMBLE },
212 { "refprefix", IL_REFPREFIX },
213 { "requires", IL_REQUIRES },
214 { "resetargs", IL_RESETARGS },
215 { "resetsfont", IL_RESETSFONT },
216 { "rightdelim", IL_RIGHTDELIM },
217 { "spellcheck", IL_SPELLCHECK }
220 lex.pushTable(elementTags);
222 if (labelfont_ == sane_font)
223 labelfont_ = inherit_font;
224 if (bgcolor_ == Color_error)
225 bgcolor_ = Color_none;
227 // whether we've read the CustomPars or ForcePlain tag
228 // for issuing a warning in case MultiPars comes later
229 bool readCustomOrPlain = false;
232 while (!getout && lex.isOK()) {
235 case Lexer::LEX_UNDEF:
236 lex.printError("Unknown InsetLayout tag");
245 // Perhaps a more elegant way to deal with the next two would be the
246 // way this sort of thing is handled in Layout::read(), namely, by
249 // make sure that we have the right sort of name.
250 if (name_ != from_ascii("undefined")
251 && name_.substr(0,5) != from_ascii("Flex:")) {
252 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
253 "This one has the name `" << to_utf8(name_) << "'\n"
254 "Ignoring LyXType declaration.");
255 // this is not really a reason to abort
262 lyxtype_ = translateLyXType(lt);
263 if (lyxtype_ == InsetLyXType::NOLYXTYPE) {
264 LYXERR0("Unknown LyXType `" << lt << "'.");
265 // this is not really a reason to abort
269 if (lyxtype_ == InsetLyXType::CHARSTYLE) {
270 // by default, charstyles force the plain layout
279 latextype_ = translateLaTeXType(lt);
280 if (latextype_ == InsetLaTeXType::ILT_ERROR) {
281 LYXERR0("Unknown LaTeXType `" << lt << "'.");
282 // this is not really a reason to abort
296 decoration_ = translateDecoration(tmp);
303 latexparam_ = subst(tmp, """, "\"");
307 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
310 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
311 lex >> fixedwidthpreambleencoding_;
313 case IL_FORCE_LOCAL_FONT_SWITCH:
314 lex >> forcelocalfontswitch_;
318 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
322 labelfont_ = lyxRead(lex, inherit_font);
327 case IL_FORCEOWNLINES:
328 lex >> forceownlines_;
335 // the defaults for these depend upon multipar_
336 if (readCustomOrPlain)
337 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
338 "Previous value may be overwritten!");
339 readCustomOrPlain = false;
340 custompars_ = multipar_;
341 forceplain_ = !multipar_;
348 readCustomOrPlain = true;
352 readCustomOrPlain = true;
357 case IL_PASSTHRU_CHARS:
358 lex >> passthru_chars_;
363 case IL_PARBREAKIGNORED:
364 lex >> parbreakignored_;
366 case IL_PARBREAKISNEWLINE:
367 lex >> parbreakisnewline_;
378 case IL_NEEDCPROTECT:
379 lex >> needcprotect_;
381 case IL_NEEDMBOXPROTECT:
382 lex >> needmboxprotect_;
384 case IL_CONTENTASLABEL:
385 lex >> contentaslabel_;
388 // initialize with a known style
391 style = subst(style, '_', ' ');
393 // We don't want to apply the algorithm in DocumentClass::insetLayout()
394 // here. So we do it the long way.
395 TextClass::InsetLayouts::const_iterator it =
396 tclass.insetLayouts().find(style);
397 if (it != tclass.insetLayouts().end()) {
398 docstring const tmpname = name_;
399 this->operator=(it->second);
402 LYXERR0("Cannot copy unknown InsetLayout `"
403 << style << "' to InsetLayout `"
405 << "All InsetLayouts so far:");
406 TextClass::InsetLayouts::const_iterator lit =
407 tclass.insetLayouts().begin();
408 TextClass::InsetLayouts::const_iterator len =
409 tclass.insetLayouts().end();
410 for (; lit != len; ++lit)
411 lyxerr << lit->second.name() << "\n";
412 // this is not really a reason to abort
418 case IL_OBSOLETEDBY: {
421 style = subst(style, '_', ' ');
423 // We don't want to apply the algorithm in DocumentClass::insetLayout()
424 // here. So we do it the long way.
425 TextClass::InsetLayouts::const_iterator it =
426 tclass.insetLayouts().find(style);
427 if (it != tclass.insetLayouts().end()) {
428 docstring const tmpname = name_;
429 this->operator=(it->second);
431 if (obsoleted_by().empty())
432 obsoleted_by_ = style;
434 LYXERR0("Cannot replace InsetLayout `"
436 << "' with unknown InsetLayout `"
438 << "All InsetLayouts so far:");
439 TextClass::InsetLayouts::const_iterator lit =
440 tclass.insetLayouts().begin();
441 TextClass::InsetLayouts::const_iterator len =
442 tclass.insetLayouts().end();
443 for (; lit != len; ++lit)
444 lyxerr << lit->second.name() << "\n";
445 // this is not really a reason to abort
453 font_ = lyxRead(lex, inherit_font);
454 // If you want to define labelfont, you need to do so after
464 postcommandargs_.clear();
472 bgcolor_ = lcolor.getFromLyXName(tmp);
475 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
477 case IL_BABELPREAMBLE:
478 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
480 case IL_LANGPREAMBLE:
481 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
492 case IL_HTMLFORCECSS:
493 lex >> htmlforcecss_;
495 case IL_HTMLINNERTAG:
496 lex >> htmlinnertag_;
498 case IL_HTMLINNERATTR:
499 lex >> htmlinnerattr_;
508 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
510 case IL_HTMLPREAMBLE:
511 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
519 case IL_DOCBOOKTAGTYPE:
520 lex >> docbooktagtype_;
522 case IL_DOCBOOKININFO:
523 lex >> docbookininfo_;
525 case IL_DOCBOOKARGUMENTBEFOREMAINTAG:
526 lex >> docbookargumentbeforemaintag_;
528 case IL_DOCBOOKNOTINPARA:
529 lex >> docbooknotinpara_;
531 case IL_DOCBOOKSECTION:
532 lex >> docbooksection_;
534 case IL_DOCBOOKITEMTAG:
535 lex >> docbookitemtag_;
537 case IL_DOCBOOKITEMTAGTYPE:
538 lex >> docbookitemtagtype_;
540 case IL_DOCBOOKITEMATTR:
541 lex >> docbookitemattr_;
543 case IL_DOCBOOKITEMWRAPPERTAG:
544 lex >> docbookitemwrappertag_;
546 case IL_DOCBOOKITEMWRAPPERTAGTYPE:
547 lex >> docbookitemwrappertagtype_;
549 case IL_DOCBOOKITEMWRAPPERATTR:
550 lex >> docbookitemwrapperattr_;
552 case IL_DOCBOOKWRAPPERTAG:
553 lex >> docbookwrappertag_;
555 case IL_DOCBOOKWRAPPERTAGTYPE:
556 lex >> docbookwrappertagtype_;
558 case IL_DOCBOOKWRAPPERATTR:
559 lex >> docbookwrapperattr_;
561 case IL_DOCBOOKNOFONTINSIDE:
562 lex >> docbooknofontinside_;
566 vector<string> const req
567 = getVectorFromString(lex.getString(true));
568 required_.insert(req.begin(), req.end());
582 add_to_toc_ = !toc_type_.empty();
584 case IL_ISTOCCAPTION:
585 lex >> is_toc_caption_;
587 case IL_EDITEXTERNAL:
588 lex >> edit_external_;
596 // Here add element to list if getout == true
600 // The label font is generally used as-is without
601 // any realization against a given context.
602 labelfont_.realize(sane_font);
609 InsetLyXType translateLyXType(std::string const & str)
611 if (compare_ascii_no_case(str, "charstyle") == 0)
612 return InsetLyXType::CHARSTYLE;
613 if (compare_ascii_no_case(str, "custom") == 0)
614 return InsetLyXType::CUSTOM;
615 if (compare_ascii_no_case(str, "end") == 0)
616 return InsetLyXType::END;
617 if (compare_ascii_no_case(str, "standard") == 0)
618 return InsetLyXType::STANDARD;
619 return InsetLyXType::NOLYXTYPE;
623 string const & InsetLayout::htmltag() const
625 if (htmltag_.empty())
626 htmltag_ = multipar_ ? "div" : "span";
631 string const & InsetLayout::htmlattr() const
633 if (htmlattr_.empty())
634 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
639 string const & InsetLayout::htmlinnerattr() const
641 if (htmlinnerattr_.empty())
642 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
643 return htmlinnerattr_;
647 string InsetLayout::defaultCSSClass() const
649 if (!defaultcssclass_.empty())
650 return defaultcssclass_;
652 string n = to_utf8(name());
653 string::const_iterator it = n.begin();
654 string::const_iterator en = n.end();
655 for (; it != en; ++it) {
656 if (!isAlphaASCII(*it))
658 else if (isLower(*it))
663 // are there other characters we need to remove?
664 defaultcssclass_ = d;
665 return defaultcssclass_;
669 void InsetLayout::makeDefaultCSS() const
671 if (!htmldefaultstyle_.empty())
673 docstring const mainfontCSS = font_.asCSS();
674 if (!mainfontCSS.empty())
676 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
677 mainfontCSS + from_ascii("\n}\n");
681 docstring InsetLayout::htmlstyle() const
683 if (!htmlstyle_.empty() && !htmlforcecss_)
685 if (htmldefaultstyle_.empty())
687 docstring retval = htmldefaultstyle_;
688 if (!htmlstyle_.empty())
689 retval += '\n' + htmlstyle_ + '\n';
694 std::string const & InsetLayout::docbookininfo() const
696 // Same as Layout::docbookininfo.
697 // Indeed, a trilean. Only titles should be "maybe": otherwise, metadata is "always", content is "never".
698 if (docbookininfo_.empty() || (docbookininfo_ != "never" && docbookininfo_ != "always" && docbookininfo_ != "maybe"))
699 docbookininfo_ = "never";
700 return docbookininfo_;
704 std::string InsetLayout::docbooktagtype() const
706 if (docbooktagtype_ != "block" && docbooktagtype_ != "paragraph" && docbooktagtype_ != "inline")
707 docbooktagtype_ = "block";
708 return docbooktagtype_;
712 std::string InsetLayout::docbookwrappertagtype() const
714 if (docbookwrappertagtype_ != "block" && docbookwrappertagtype_ != "paragraph" && docbookwrappertagtype_ != "inline")
715 docbookwrappertagtype_ = "block";
716 return docbookwrappertagtype_;
720 std::string InsetLayout::docbookitemtagtype() const
722 if (docbookitemtagtype_ != "block" && docbookitemtagtype_ != "paragraph" && docbookitemtagtype_ != "inline")
723 docbookitemtagtype_ = "block";
724 return docbookitemtagtype_;
728 std::string InsetLayout::docbookitemwrappertagtype() const
730 if (docbookitemwrappertagtype_ != "block" && docbookitemwrappertagtype_ != "paragraph" && docbookitemwrappertagtype_ != "inline")
731 docbookitemwrappertagtype_ = "block";
732 return docbookitemwrappertagtype_;
736 void InsetLayout::readArgument(Lexer & lex)
738 Layout::latexarg arg;
739 arg.mandatory = false;
740 arg.autoinsert = false;
741 arg.insertcotext = false;
742 arg.insertonnewline = false;
744 bool finished = false;
745 arg.font = inherit_font;
746 arg.labelfont = inherit_font;
747 arg.is_toc_caption = false;
748 arg.free_spacing = false;
749 arg.passthru = PT_INHERITED;
750 arg.nodelims = false;
753 bool const postcmd = prefixIs(nr, "post:");
754 while (!finished && lex.isOK() && !error) {
756 string const tok = ascii_lowercase(lex.getString());
760 } else if (tok == "endargument") {
762 } else if (tok == "labelstring") {
764 arg.labelstring = lex.getDocString();
765 } else if (tok == "menustring") {
767 arg.menustring = lex.getDocString();
768 } else if (tok == "mandatory") {
770 arg.mandatory = lex.getBool();
771 } else if (tok == "autoinsert") {
773 arg.autoinsert = lex.getBool();
774 } else if (tok == "insertcotext") {
776 arg.insertcotext = lex.getBool();
777 } else if (tok == "insertonnewline") {
779 arg.insertonnewline = lex.getBool();
780 } else if (tok == "leftdelim") {
782 arg.ldelim = lex.getDocString();
783 arg.ldelim = subst(arg.ldelim,
784 from_ascii("<br/>"), from_ascii("\n"));
785 } else if (tok == "rightdelim") {
787 arg.rdelim = lex.getDocString();
788 arg.rdelim = subst(arg.rdelim,
789 from_ascii("<br/>"), from_ascii("\n"));
790 } else if (tok == "defaultarg") {
792 arg.defaultarg = lex.getDocString();
793 } else if (tok == "presetarg") {
795 arg.presetarg = lex.getDocString();
796 } else if (tok == "tooltip") {
798 arg.tooltip = lex.getDocString();
799 } else if (tok == "requires") {
801 arg.required = lex.getString();
802 } else if (tok == "decoration") {
804 arg.decoration = lex.getString();
805 } else if (tok == "newlinecmd") {
807 arg.newlinecmd = lex.getString();
808 } else if (tok == "font") {
809 arg.font = lyxRead(lex, arg.font);
810 } else if (tok == "labelfont") {
811 arg.labelfont = lyxRead(lex, arg.labelfont);
812 } else if (tok == "passthruchars") {
814 arg.pass_thru_chars = lex.getDocString();
815 } else if (tok == "passthru") {
817 docstring value = lex.getDocString();
818 if (value == "true" || value == "1")
819 arg.passthru = PT_TRUE;
820 else if (value == "false" || value == "0")
821 arg.passthru = PT_FALSE;
823 arg.passthru = PT_INHERITED;
824 } else if (tok == "istoccaption") {
826 arg.is_toc_caption = lex.getBool();
827 } else if (tok == "freespacing") {
829 arg.free_spacing = lex.getBool();
830 } else if (tok == "docbooktag") {
832 arg.docbooktag = lex.getDocString();
833 } else if (tok == "docbookattr") {
835 arg.docbookattr = lex.getDocString();
836 } else if (tok == "docbooktagtype") {
838 arg.docbooktagtype = lex.getDocString();
839 } else if (tok == "docbookargumentbeforemaintag") {
841 arg.docbookargumentbeforemaintag = lex.getBool();
843 lex.printError("Unknown tag");
847 if (arg.labelstring.empty())
848 LYXERR0("Incomplete Argument definition!");
850 postcommandargs_[nr] = arg;
852 latexargs_[nr] = arg;
856 Layout::LaTeXArgMap InsetLayout::args() const
858 Layout::LaTeXArgMap args = latexargs_;
859 if (!postcommandargs_.empty())
860 args.insert(postcommandargs_.begin(), postcommandargs_.end());
865 int InsetLayout::optArgs() const
868 Layout::LaTeXArgMap const args = InsetLayout::args();
869 Layout::LaTeXArgMap::const_iterator it = args.begin();
870 for (; it != args.end(); ++it) {
871 if (!(*it).second.mandatory)
878 int InsetLayout::requiredArgs() const
881 Layout::LaTeXArgMap const args = InsetLayout::args();
882 Layout::LaTeXArgMap::const_iterator it = args.begin();
883 for (; it != args.end(); ++it) {
884 if ((*it).second.mandatory)