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_DOCBOOKWRAPPERTAGTYPE,
99 IL_DOCBOOKWRAPPERATTR,
121 IL_PARBREAKISNEWLINE,
133 LexerKeyword elementTags[] = {
134 { "addtotoc", IL_ADDTOTOC },
135 { "argument", IL_ARGUMENT },
136 { "babelpreamble", IL_BABELPREAMBLE },
137 { "bgcolor", IL_BGCOLOR },
138 { "contentaslabel", IL_CONTENTASLABEL },
139 { "copystyle", IL_COPYSTYLE },
140 { "counter", IL_COUNTER},
141 { "custompars", IL_CUSTOMPARS },
142 { "decoration", IL_DECORATION },
143 { "display", IL_DISPLAY },
144 { "docbookattr", IL_DOCBOOKATTR },
145 { "docbooksection", IL_DOCBOOKSECTION },
146 { "docbooktag", IL_DOCBOOKTAG },
147 { "docbooktagtype", IL_DOCBOOKTAGTYPE },
148 { "docbookwrapperattr", IL_DOCBOOKWRAPPERATTR },
149 { "docbookwrappertag", IL_DOCBOOKWRAPPERTAG },
150 { "docbookwrappertagtype", IL_DOCBOOKWRAPPERTAGTYPE },
151 { "editexternal", IL_EDITEXTERNAL },
153 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
155 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
156 { "forceltr", IL_FORCELTR },
157 { "forceownlines", IL_FORCEOWNLINES },
158 { "forceplain", IL_FORCEPLAIN },
159 { "freespacing", IL_FREESPACING },
160 { "htmlattr", IL_HTMLATTR },
161 { "htmlforcecss", IL_HTMLFORCECSS },
162 { "htmlinnerattr", IL_HTMLINNERATTR},
163 { "htmlinnertag", IL_HTMLINNERTAG},
164 { "htmlisblock", IL_HTMLISBLOCK},
165 { "htmllabel", IL_HTMLLABEL },
166 { "htmlpreamble", IL_HTMLPREAMBLE },
167 { "htmlstyle", IL_HTMLSTYLE },
168 { "htmltag", IL_HTMLTAG },
169 { "intoc", IL_INTOC },
170 { "istoccaption", IL_ISTOCCAPTION },
171 { "keepempty", IL_KEEPEMPTY },
172 { "labelfont", IL_LABELFONT },
173 { "labelstring", IL_LABELSTRING },
174 { "langpreamble", IL_LANGPREAMBLE },
175 { "latexname", IL_LATEXNAME },
176 { "latexparam", IL_LATEXPARAM },
177 { "latextype", IL_LATEXTYPE },
178 { "leftdelim", IL_LEFTDELIM },
179 { "lyxtype", IL_LYXTYPE },
180 { "menustring", IL_MENUSTRING },
181 { "multipar", IL_MULTIPAR },
182 { "needcprotect", IL_NEEDCPROTECT },
183 { "needmboxprotect", IL_NEEDMBOXPROTECT },
184 { "needprotect", IL_NEEDPROTECT },
185 { "newlinecmd", IL_NEWLINE_CMD },
186 { "obsoletedby", IL_OBSOLETEDBY },
187 { "parbreakignored", IL_PARBREAKIGNORED },
188 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
189 { "passthru", IL_PASSTHRU },
190 { "passthruchars", IL_PASSTHRU_CHARS },
191 { "preamble", IL_PREAMBLE },
192 { "refprefix", IL_REFPREFIX },
193 { "requires", IL_REQUIRES },
194 { "resetargs", IL_RESETARGS },
195 { "resetsfont", IL_RESETSFONT },
196 { "rightdelim", IL_RIGHTDELIM },
197 { "spellcheck", IL_SPELLCHECK }
200 lex.pushTable(elementTags);
202 labelfont_ = inherit_font;
203 bgcolor_ = Color_none;
205 // whether we've read the CustomPars or ForcePlain tag
206 // for issuing a warning in case MultiPars comes later
207 bool readCustomOrPlain = false;
210 while (!getout && lex.isOK()) {
213 case Lexer::LEX_UNDEF:
214 lex.printError("Unknown InsetLayout tag");
223 // Perhaps a more elegant way to deal with the next two would be the
224 // way this sort of thing is handled in Layout::read(), namely, by
227 // make sure that we have the right sort of name.
228 if (name_ != from_ascii("undefined")
229 && name_.substr(0,5) != from_ascii("Flex:")) {
230 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
231 "This one has the name `" << to_utf8(name_) << "'\n"
232 "Ignoring LyXType declaration.");
233 // this is not really a reason to abort
240 lyxtype_ = translateLyXType(lt);
241 if (lyxtype_ == NOLYXTYPE) {
242 LYXERR0("Unknown LyXType `" << lt << "'.");
243 // this is not really a reason to abort
247 if (lyxtype_ == CHARSTYLE) {
248 // by default, charstyles force the plain layout
257 latextype_ = translateLaTeXType(lt);
258 if (latextype_ == ILT_ERROR) {
259 LYXERR0("Unknown LaTeXType `" << lt << "'.");
260 // this is not really a reason to abort
274 decoration_ = translateDecoration(tmp);
281 latexparam_ = subst(tmp, """, "\"");
285 leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
288 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
289 lex >> fixedwidthpreambleencoding_;
291 case IL_FORCE_LOCAL_FONT_SWITCH:
292 lex >> forcelocalfontswitch_;
296 rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
300 labelfont_ = lyxRead(lex, inherit_font);
305 case IL_FORCEOWNLINES:
306 lex >> forceownlines_;
313 // the defaults for these depend upon multipar_
314 if (readCustomOrPlain)
315 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
316 "Previous value may be overwritten!");
317 readCustomOrPlain = false;
318 custompars_ = multipar_;
319 forceplain_ = !multipar_;
326 readCustomOrPlain = true;
330 readCustomOrPlain = true;
335 case IL_PASSTHRU_CHARS:
336 lex >> passthru_chars_;
341 case IL_PARBREAKIGNORED:
342 lex >> parbreakignored_;
344 case IL_PARBREAKISNEWLINE:
345 lex >> parbreakisnewline_;
356 case IL_NEEDCPROTECT:
357 lex >> needcprotect_;
359 case IL_NEEDMBOXPROTECT:
360 lex >> needmboxprotect_;
362 case IL_CONTENTASLABEL:
363 lex >> contentaslabel_;
366 // initialize with a known style
369 style = subst(style, '_', ' ');
371 // We don't want to apply the algorithm in DocumentClass::insetLayout()
372 // here. So we do it the long way.
373 TextClass::InsetLayouts::const_iterator it =
374 tclass.insetLayouts().find(style);
375 if (it != tclass.insetLayouts().end()) {
376 docstring const tmpname = name_;
377 this->operator=(it->second);
380 LYXERR0("Cannot copy unknown InsetLayout `"
381 << style << "' to InsetLayout `"
383 << "All InsetLayouts so far:");
384 TextClass::InsetLayouts::const_iterator lit =
385 tclass.insetLayouts().begin();
386 TextClass::InsetLayouts::const_iterator len =
387 tclass.insetLayouts().end();
388 for (; lit != len; ++lit)
389 lyxerr << lit->second.name() << "\n";
390 // this is not really a reason to abort
396 case IL_OBSOLETEDBY: {
399 style = subst(style, '_', ' ');
401 // We don't want to apply the algorithm in DocumentClass::insetLayout()
402 // here. So we do it the long way.
403 TextClass::InsetLayouts::const_iterator it =
404 tclass.insetLayouts().find(style);
405 if (it != tclass.insetLayouts().end()) {
406 docstring const tmpname = name_;
407 this->operator=(it->second);
409 if (obsoleted_by().empty())
410 obsoleted_by_ = style;
412 LYXERR0("Cannot replace InsetLayout `"
414 << "' with unknown InsetLayout `"
416 << "All InsetLayouts so far:");
417 TextClass::InsetLayouts::const_iterator lit =
418 tclass.insetLayouts().begin();
419 TextClass::InsetLayouts::const_iterator len =
420 tclass.insetLayouts().end();
421 for (; lit != len; ++lit)
422 lyxerr << lit->second.name() << "\n";
423 // this is not really a reason to abort
431 font_ = lyxRead(lex, inherit_font);
432 // If you want to define labelfont, you need to do so after
442 postcommandargs_.clear();
450 bgcolor_ = lcolor.getFromLyXName(tmp);
453 preamble_ = lex.getLongString(from_ascii("EndPreamble"));
455 case IL_BABELPREAMBLE:
456 babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
458 case IL_LANGPREAMBLE:
459 langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
470 case IL_HTMLFORCECSS:
471 lex >> htmlforcecss_;
473 case IL_HTMLINNERTAG:
474 lex >> htmlinnertag_;
476 case IL_HTMLINNERATTR:
477 lex >> htmlinnerattr_;
486 htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
488 case IL_HTMLPREAMBLE:
489 htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
494 case IL_DOCBOOKTAGTYPE:
495 lex >> docbooktagtype_;
500 case IL_DOCBOOKSECTION:
501 lex >> docbooksection_;
503 case IL_DOCBOOKWRAPPERTAG:
504 lex >> docbookwrappertag_;
506 case IL_DOCBOOKWRAPPERTAGTYPE:
507 lex >> docbookwrappertagtype_;
509 case IL_DOCBOOKWRAPPERATTR:
510 lex >> docbookwrapperattr_;
514 vector<string> const req
515 = getVectorFromString(lex.getString(true));
516 required_.insert(req.begin(), req.end());
530 add_to_toc_ = !toc_type_.empty();
532 case IL_ISTOCCAPTION:
533 lex >> is_toc_caption_;
535 case IL_EDITEXTERNAL:
536 lex >> edit_external_;
544 // Here add element to list if getout == true
548 // The label font is generally used as-is without
549 // any realization against a given context.
550 labelfont_.realize(sane_font);
557 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
559 if (compare_ascii_no_case(str, "charstyle") == 0)
560 return InsetLayout::CHARSTYLE;
561 if (compare_ascii_no_case(str, "custom") == 0)
562 return InsetLayout::CUSTOM;
563 if (compare_ascii_no_case(str, "end") == 0)
564 return InsetLayout::END;
565 if (compare_ascii_no_case(str, "standard") == 0)
566 return InsetLayout::STANDARD;
567 return InsetLayout::NOLYXTYPE;
571 string const & InsetLayout::htmltag() const
573 if (htmltag_.empty())
574 htmltag_ = multipar_ ? "div" : "span";
579 string const & InsetLayout::htmlattr() const
581 if (htmlattr_.empty())
582 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
587 string const & InsetLayout::htmlinnerattr() const
589 if (htmlinnerattr_.empty())
590 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
591 return htmlinnerattr_;
595 string InsetLayout::defaultCSSClass() const
597 if (!defaultcssclass_.empty())
598 return defaultcssclass_;
600 string n = to_utf8(name());
601 string::const_iterator it = n.begin();
602 string::const_iterator en = n.end();
603 for (; it != en; ++it) {
604 if (!isAlphaASCII(*it))
606 else if (isLower(*it))
611 // are there other characters we need to remove?
612 defaultcssclass_ = d;
613 return defaultcssclass_;
617 void InsetLayout::makeDefaultCSS() const
619 if (!htmldefaultstyle_.empty())
621 docstring const mainfontCSS = font_.asCSS();
622 if (!mainfontCSS.empty())
624 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
625 mainfontCSS + from_ascii("\n}\n");
629 docstring InsetLayout::htmlstyle() const
631 if (!htmlstyle_.empty() && !htmlforcecss_)
633 if (htmldefaultstyle_.empty())
635 docstring retval = htmldefaultstyle_;
636 if (!htmlstyle_.empty())
637 retval += '\n' + htmlstyle_ + '\n';
641 void InsetLayout::readArgument(Lexer & lex)
643 Layout::latexarg arg;
644 arg.mandatory = false;
645 arg.autoinsert = false;
646 arg.insertcotext = false;
647 arg.insertonnewline = false;
649 bool finished = false;
650 arg.font = inherit_font;
651 arg.labelfont = inherit_font;
652 arg.is_toc_caption = false;
653 arg.free_spacing = false;
654 arg.passthru = PT_INHERITED;
655 arg.nodelims = false;
658 bool const postcmd = prefixIs(nr, "post:");
659 while (!finished && lex.isOK() && !error) {
661 string const tok = ascii_lowercase(lex.getString());
665 } else if (tok == "endargument") {
667 } else if (tok == "labelstring") {
669 arg.labelstring = lex.getDocString();
670 } else if (tok == "menustring") {
672 arg.menustring = lex.getDocString();
673 } else if (tok == "mandatory") {
675 arg.mandatory = lex.getBool();
676 } else if (tok == "autoinsert") {
678 arg.autoinsert = lex.getBool();
679 } else if (tok == "insertcotext") {
681 arg.insertcotext = lex.getBool();
682 } else if (tok == "insertonnewline") {
684 arg.insertonnewline = lex.getBool();
685 } else if (tok == "leftdelim") {
687 arg.ldelim = lex.getDocString();
688 arg.ldelim = subst(arg.ldelim,
689 from_ascii("<br/>"), from_ascii("\n"));
690 } else if (tok == "rightdelim") {
692 arg.rdelim = lex.getDocString();
693 arg.rdelim = subst(arg.rdelim,
694 from_ascii("<br/>"), from_ascii("\n"));
695 } else if (tok == "defaultarg") {
697 arg.defaultarg = lex.getDocString();
698 } else if (tok == "presetarg") {
700 arg.presetarg = lex.getDocString();
701 } else if (tok == "tooltip") {
703 arg.tooltip = lex.getDocString();
704 } else if (tok == "requires") {
706 arg.required = lex.getString();
707 } else if (tok == "decoration") {
709 arg.decoration = lex.getString();
710 } else if (tok == "newlinecmd") {
712 arg.newlinecmd = lex.getString();
713 } else if (tok == "font") {
714 arg.font = lyxRead(lex, arg.font);
715 } else if (tok == "labelfont") {
716 arg.labelfont = lyxRead(lex, arg.labelfont);
717 } else if (tok == "passthruchars") {
719 arg.pass_thru_chars = lex.getDocString();
720 } else if (tok == "passthru") {
722 docstring value = lex.getDocString();
723 if (value == "true" || value == "1")
724 arg.passthru = PT_TRUE;
725 else if (value == "false" || value == "0")
726 arg.passthru = PT_FALSE;
728 arg.passthru = PT_INHERITED;
729 } else if (tok == "istoccaption") {
731 arg.is_toc_caption = lex.getBool();
732 } else if (tok == "freespacing") {
734 arg.free_spacing = lex.getBool();
736 lex.printError("Unknown tag");
740 if (arg.labelstring.empty())
741 LYXERR0("Incomplete Argument definition!");
743 postcommandargs_[nr] = arg;
745 latexargs_[nr] = arg;
749 Layout::LaTeXArgMap InsetLayout::args() const
751 Layout::LaTeXArgMap args = latexargs_;
752 if (!postcommandargs_.empty())
753 args.insert(postcommandargs_.begin(), postcommandargs_.end());
758 int InsetLayout::optArgs() const
761 Layout::LaTeXArgMap const args = InsetLayout::args();
762 Layout::LaTeXArgMap::const_iterator it = args.begin();
763 for (; it != args.end(); ++it) {
764 if (!(*it).second.mandatory)
771 int InsetLayout::requiredArgs() const
774 Layout::LaTeXArgMap const args = InsetLayout::args();
775 Layout::LaTeXArgMap::const_iterator it = args.begin();
776 for (; it != args.end(); ++it) {
777 if ((*it).second.mandatory)