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
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 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
37 if (compare_ascii_no_case(str, "classic") == 0)
38 return InsetLayout::CLASSIC;
39 if (compare_ascii_no_case(str, "minimalistic") == 0)
40 return InsetLayout::MINIMALISTIC;
41 if (compare_ascii_no_case(str, "conglomerate") == 0)
42 return InsetLayout::CONGLOMERATE;
43 return InsetLayout::DEFAULT;
48 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
50 if (compare_ascii_no_case(str, "command") == 0)
51 return InsetLayout::COMMAND;
52 if (compare_ascii_no_case(str, "environment") == 0)
53 return InsetLayout::ENVIRONMENT;
54 if (compare_ascii_no_case(str, "none") == 0)
55 return InsetLayout::NOLATEXTYPE;
56 return InsetLayout::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 labelfont_ = inherit_font;
221 bgcolor_ = Color_none;
223 // whether we've read the CustomPars or ForcePlain tag
224 // for issuing a warning in case MultiPars comes later
225 bool readCustomOrPlain = false;
228 while (!getout && lex.isOK()) {
231 case Lexer::LEX_UNDEF:
232 lex.printError("Unknown InsetLayout tag");
241 // Perhaps a more elegant way to deal with the next two would be the
242 // way this sort of thing is handled in Layout::read(), namely, by
245 // make sure that we have the right sort of name.
246 if (name_ != from_ascii("undefined")
247 && name_.substr(0,5) != from_ascii("Flex:")) {
248 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
249 "This one has the name `" << to_utf8(name_) << "'\n"
250 "Ignoring LyXType declaration.");
251 // this is not really a reason to abort
258 lyxtype_ = translateLyXType(lt);
259 if (lyxtype_ == NOLYXTYPE) {
260 LYXERR0("Unknown LyXType `" << lt << "'.");
261 // this is not really a reason to abort
265 if (lyxtype_ == CHARSTYLE) {
266 // by default, charstyles force the plain layout
275 latextype_ = translateLaTeXType(lt);
276 if (latextype_ == ILT_ERROR) {
277 LYXERR0("Unknown LaTeXType `" << lt << "'.");
278 // this is not really a reason to abort
292 decoration_ = translateDecoration(tmp);
299 latexparam_ = subst(tmp, """, "\"");
303 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
306 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
307 lex >> fixedwidthpreambleencoding_;
309 case IL_FORCE_LOCAL_FONT_SWITCH:
310 lex >> forcelocalfontswitch_;
314 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
318 labelfont_ = lyxRead(lex, inherit_font);
323 case IL_FORCEOWNLINES:
324 lex >> forceownlines_;
331 // the defaults for these depend upon multipar_
332 if (readCustomOrPlain)
333 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
334 "Previous value may be overwritten!");
335 readCustomOrPlain = false;
336 custompars_ = multipar_;
337 forceplain_ = !multipar_;
344 readCustomOrPlain = true;
348 readCustomOrPlain = true;
353 case IL_PASSTHRU_CHARS:
354 lex >> passthru_chars_;
359 case IL_PARBREAKIGNORED:
360 lex >> parbreakignored_;
362 case IL_PARBREAKISNEWLINE:
363 lex >> parbreakisnewline_;
374 case IL_NEEDCPROTECT:
375 lex >> needcprotect_;
377 case IL_NEEDMBOXPROTECT:
378 lex >> needmboxprotect_;
380 case IL_CONTENTASLABEL:
381 lex >> contentaslabel_;
384 // initialize with a known style
387 style = subst(style, '_', ' ');
389 // We don't want to apply the algorithm in DocumentClass::insetLayout()
390 // here. So we do it the long way.
391 TextClass::InsetLayouts::const_iterator it =
392 tclass.insetLayouts().find(style);
393 if (it != tclass.insetLayouts().end()) {
394 docstring const tmpname = name_;
395 this->operator=(it->second);
398 LYXERR0("Cannot copy unknown InsetLayout `"
399 << style << "' to InsetLayout `"
401 << "All InsetLayouts so far:");
402 TextClass::InsetLayouts::const_iterator lit =
403 tclass.insetLayouts().begin();
404 TextClass::InsetLayouts::const_iterator len =
405 tclass.insetLayouts().end();
406 for (; lit != len; ++lit)
407 lyxerr << lit->second.name() << "\n";
408 // this is not really a reason to abort
414 case IL_OBSOLETEDBY: {
417 style = subst(style, '_', ' ');
419 // We don't want to apply the algorithm in DocumentClass::insetLayout()
420 // here. So we do it the long way.
421 TextClass::InsetLayouts::const_iterator it =
422 tclass.insetLayouts().find(style);
423 if (it != tclass.insetLayouts().end()) {
424 docstring const tmpname = name_;
425 this->operator=(it->second);
427 if (obsoleted_by().empty())
428 obsoleted_by_ = style;
430 LYXERR0("Cannot replace InsetLayout `"
432 << "' with unknown InsetLayout `"
434 << "All InsetLayouts so far:");
435 TextClass::InsetLayouts::const_iterator lit =
436 tclass.insetLayouts().begin();
437 TextClass::InsetLayouts::const_iterator len =
438 tclass.insetLayouts().end();
439 for (; lit != len; ++lit)
440 lyxerr << lit->second.name() << "\n";
441 // this is not really a reason to abort
449 font_ = lyxRead(lex, inherit_font);
450 // If you want to define labelfont, you need to do so after
460 postcommandargs_.clear();
468 bgcolor_ = lcolor.getFromLyXName(tmp);
471 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
473 case IL_BABELPREAMBLE:
474 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
476 case IL_LANGPREAMBLE:
477 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
488 case IL_HTMLFORCECSS:
489 lex >> htmlforcecss_;
491 case IL_HTMLINNERTAG:
492 lex >> htmlinnertag_;
494 case IL_HTMLINNERATTR:
495 lex >> htmlinnerattr_;
504 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
506 case IL_HTMLPREAMBLE:
507 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
515 case IL_DOCBOOKTAGTYPE:
516 lex >> docbooktagtype_;
518 case IL_DOCBOOKININFO:
519 lex >> docbookininfo_;
521 case IL_DOCBOOKARGUMENTBEFOREMAINTAG:
522 lex >> docbookargumentbeforemaintag_;
524 case IL_DOCBOOKNOTINPARA:
525 lex >> docbooknotinpara_;
527 case IL_DOCBOOKSECTION:
528 lex >> docbooksection_;
530 case IL_DOCBOOKITEMTAG:
531 lex >> docbookitemtag_;
533 case IL_DOCBOOKITEMTAGTYPE:
534 lex >> docbookitemtagtype_;
536 case IL_DOCBOOKITEMATTR:
537 lex >> docbookitemattr_;
539 case IL_DOCBOOKITEMWRAPPERTAG:
540 lex >> docbookitemwrappertag_;
542 case IL_DOCBOOKITEMWRAPPERTAGTYPE:
543 lex >> docbookitemwrappertagtype_;
545 case IL_DOCBOOKITEMWRAPPERATTR:
546 lex >> docbookitemwrapperattr_;
548 case IL_DOCBOOKWRAPPERTAG:
549 lex >> docbookwrappertag_;
551 case IL_DOCBOOKWRAPPERTAGTYPE:
552 lex >> docbookwrappertagtype_;
554 case IL_DOCBOOKWRAPPERATTR:
555 lex >> docbookwrapperattr_;
559 vector<string> const req
560 = getVectorFromString(lex.getString(true));
561 required_.insert(req.begin(), req.end());
575 add_to_toc_ = !toc_type_.empty();
577 case IL_ISTOCCAPTION:
578 lex >> is_toc_caption_;
580 case IL_EDITEXTERNAL:
581 lex >> edit_external_;
589 // Here add element to list if getout == true
593 // The label font is generally used as-is without
594 // any realization against a given context.
595 labelfont_.realize(sane_font);
602 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
604 if (compare_ascii_no_case(str, "charstyle") == 0)
605 return InsetLayout::CHARSTYLE;
606 if (compare_ascii_no_case(str, "custom") == 0)
607 return InsetLayout::CUSTOM;
608 if (compare_ascii_no_case(str, "end") == 0)
609 return InsetLayout::END;
610 if (compare_ascii_no_case(str, "standard") == 0)
611 return InsetLayout::STANDARD;
612 return InsetLayout::NOLYXTYPE;
616 string const & InsetLayout::htmltag() const
618 if (htmltag_.empty())
619 htmltag_ = multipar_ ? "div" : "span";
624 string const & InsetLayout::htmlattr() const
626 if (htmlattr_.empty())
627 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
632 string const & InsetLayout::htmlinnerattr() const
634 if (htmlinnerattr_.empty())
635 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
636 return htmlinnerattr_;
640 string InsetLayout::defaultCSSClass() const
642 if (!defaultcssclass_.empty())
643 return defaultcssclass_;
645 string n = to_utf8(name());
646 string::const_iterator it = n.begin();
647 string::const_iterator en = n.end();
648 for (; it != en; ++it) {
649 if (!isAlphaASCII(*it))
651 else if (isLower(*it))
656 // are there other characters we need to remove?
657 defaultcssclass_ = d;
658 return defaultcssclass_;
662 void InsetLayout::makeDefaultCSS() const
664 if (!htmldefaultstyle_.empty())
666 docstring const mainfontCSS = font_.asCSS();
667 if (!mainfontCSS.empty())
669 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
670 mainfontCSS + from_ascii("\n}\n");
674 docstring InsetLayout::htmlstyle() const
676 if (!htmlstyle_.empty() && !htmlforcecss_)
678 if (htmldefaultstyle_.empty())
680 docstring retval = htmldefaultstyle_;
681 if (!htmlstyle_.empty())
682 retval += '\n' + htmlstyle_ + '\n';
687 std::string const & InsetLayout::docbookininfo() const
689 // Same as Layout::docbookininfo.
690 // Indeed, a trilean. Only titles should be "maybe": otherwise, metadata is "always", content is "never".
691 if (docbookininfo_.empty() || (docbookininfo_ != "never" && docbookininfo_ != "always" && docbookininfo_ != "maybe"))
692 docbookininfo_ = "never";
693 return docbookininfo_;
697 std::string InsetLayout::docbooktagtype() const
699 if (docbooktagtype_ != "block" && docbooktagtype_ != "paragraph" && docbooktagtype_ != "inline")
700 docbooktagtype_ = "block";
701 return docbooktagtype_;
705 std::string InsetLayout::docbookwrappertagtype() const
707 if (docbookwrappertagtype_ != "block" && docbookwrappertagtype_ != "paragraph" && docbookwrappertagtype_ != "inline")
708 docbookwrappertagtype_ = "block";
709 return docbookwrappertagtype_;
713 std::string InsetLayout::docbookitemtagtype() const
715 if (docbookitemtagtype_ != "block" && docbookitemtagtype_ != "paragraph" && docbookitemtagtype_ != "inline")
716 docbookitemtagtype_ = "block";
717 return docbookitemtagtype_;
721 std::string InsetLayout::docbookitemwrappertagtype() const
723 if (docbookitemwrappertagtype_ != "block" && docbookitemwrappertagtype_ != "paragraph" && docbookitemwrappertagtype_ != "inline")
724 docbookitemwrappertagtype_ = "block";
725 return docbookitemwrappertagtype_;
729 void InsetLayout::readArgument(Lexer & lex)
731 Layout::latexarg arg;
732 arg.mandatory = false;
733 arg.autoinsert = false;
734 arg.insertcotext = false;
735 arg.insertonnewline = false;
737 bool finished = false;
738 arg.font = inherit_font;
739 arg.labelfont = inherit_font;
740 arg.is_toc_caption = false;
741 arg.free_spacing = false;
742 arg.passthru = PT_INHERITED;
743 arg.nodelims = false;
746 bool const postcmd = prefixIs(nr, "post:");
747 while (!finished && lex.isOK() && !error) {
749 string const tok = ascii_lowercase(lex.getString());
753 } else if (tok == "endargument") {
755 } else if (tok == "labelstring") {
757 arg.labelstring = lex.getDocString();
758 } else if (tok == "menustring") {
760 arg.menustring = lex.getDocString();
761 } else if (tok == "mandatory") {
763 arg.mandatory = lex.getBool();
764 } else if (tok == "autoinsert") {
766 arg.autoinsert = lex.getBool();
767 } else if (tok == "insertcotext") {
769 arg.insertcotext = lex.getBool();
770 } else if (tok == "insertonnewline") {
772 arg.insertonnewline = lex.getBool();
773 } else if (tok == "leftdelim") {
775 arg.ldelim = lex.getDocString();
776 arg.ldelim = subst(arg.ldelim,
777 from_ascii("<br/>"), from_ascii("\n"));
778 } else if (tok == "rightdelim") {
780 arg.rdelim = lex.getDocString();
781 arg.rdelim = subst(arg.rdelim,
782 from_ascii("<br/>"), from_ascii("\n"));
783 } else if (tok == "defaultarg") {
785 arg.defaultarg = lex.getDocString();
786 } else if (tok == "presetarg") {
788 arg.presetarg = lex.getDocString();
789 } else if (tok == "tooltip") {
791 arg.tooltip = lex.getDocString();
792 } else if (tok == "requires") {
794 arg.required = lex.getString();
795 } else if (tok == "decoration") {
797 arg.decoration = lex.getString();
798 } else if (tok == "newlinecmd") {
800 arg.newlinecmd = lex.getString();
801 } else if (tok == "font") {
802 arg.font = lyxRead(lex, arg.font);
803 } else if (tok == "labelfont") {
804 arg.labelfont = lyxRead(lex, arg.labelfont);
805 } else if (tok == "passthruchars") {
807 arg.pass_thru_chars = lex.getDocString();
808 } else if (tok == "passthru") {
810 docstring value = lex.getDocString();
811 if (value == "true" || value == "1")
812 arg.passthru = PT_TRUE;
813 else if (value == "false" || value == "0")
814 arg.passthru = PT_FALSE;
816 arg.passthru = PT_INHERITED;
817 } else if (tok == "istoccaption") {
819 arg.is_toc_caption = lex.getBool();
820 } else if (tok == "freespacing") {
822 arg.free_spacing = lex.getBool();
823 } else if (tok == "docbooktag") {
825 arg.docbooktag = lex.getDocString();
826 } else if (tok == "docbookattr") {
828 arg.docbookattr = lex.getDocString();
829 } else if (tok == "docbooktagtype") {
831 arg.docbooktagtype = lex.getDocString();
832 } else if (tok == "docbookargumentbeforemaintag") {
834 arg.docbookargumentbeforemaintag = lex.getBool();
836 lex.printError("Unknown tag");
840 if (arg.labelstring.empty())
841 LYXERR0("Incomplete Argument definition!");
843 postcommandargs_[nr] = arg;
845 latexargs_[nr] = arg;
849 Layout::LaTeXArgMap InsetLayout::args() const
851 Layout::LaTeXArgMap args = latexargs_;
852 if (!postcommandargs_.empty())
853 args.insert(postcommandargs_.begin(), postcommandargs_.end());
858 int InsetLayout::optArgs() const
861 Layout::LaTeXArgMap const args = InsetLayout::args();
862 Layout::LaTeXArgMap::const_iterator it = args.begin();
863 for (; it != args.end(); ++it) {
864 if (!(*it).second.mandatory)
871 int InsetLayout::requiredArgs() const
874 Layout::LaTeXArgMap const args = InsetLayout::args();
875 Layout::LaTeXArgMap::const_iterator it = args.begin();
876 for (; it != args.end(); ++it) {
877 if ((*it).second.mandatory)