]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
Fix bug #12795
[lyx.git] / src / insets / InsetLayout.cpp
1 // -*- C++ -*-
2 /**
3  * \file InsetLayout.cpp
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Martin Vermeer
8  * \author Richard Kimberly Heck
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetLayout.h"
16
17 #include "ColorSet.h"
18 #include "Lexer.h"
19 #include "TextClass.h"
20
21 #include "support/debug.h"
22 #include "support/lstrings.h"
23 #include "support/textutils.h"
24
25 #include <vector>
26
27 using std::string;
28 using std::set;
29 using std::vector;
30
31 using namespace lyx::support;
32
33 namespace lyx {
34
35 InsetDecoration translateDecoration(std::string const & str)
36 {
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;
44 }
45
46 namespace {
47
48 InsetLaTeXType translateLaTeXType(std::string const & str)
49 {
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;
57 }
58
59 } // namespace
60
61
62 bool InsetLayout::read(Lexer & lex, TextClass const & tclass,
63         bool validating)
64 {
65         enum {
66                 IL_ADDTOTOC,
67                 IL_ARGUMENT,
68                 IL_BABELPREAMBLE,
69                 IL_BGCOLOR,
70                 IL_CONTENTASLABEL,
71                 IL_COPYSTYLE,
72                 IL_COUNTER,
73                 IL_CUSTOMPARS,
74                 IL_DECORATION,
75                 IL_DISPLAY,
76                 IL_EDITEXTERNAL,
77                 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
78                 IL_FONT,
79                 IL_FORCE_LOCAL_FONT_SWITCH,
80                 IL_FORCELTR,
81                 IL_FORCEOWNLINES,
82                 IL_FORCEPLAIN,
83                 IL_FREESPACING,
84                 IL_HTMLTAG,
85                 IL_HTMLATTR,
86                 IL_HTMLCLASS,
87                 IL_HTMLFORCECSS,
88                 IL_HTMLINNERTAG,
89                 IL_HTMLINNERATTR,
90                 IL_HTMLISBLOCK,
91                 IL_HTMLLABEL,
92                 IL_HTMLSTYLE,
93                 IL_HTMLPREAMBLE,
94                 IL_DOCBOOKTAG,
95                 IL_DOCBOOKATTR,
96                 IL_DOCBOOKTAGTYPE,
97                 IL_DOCBOOKSECTION,
98                 IL_DOCBOOKININFO,
99                 IL_DOCBOOKARGUMENTBEFOREMAINTAG,
100                 IL_DOCBOOKARGUMENTAFTERMAINTAG,
101                 IL_DOCBOOKNOTINPARA,
102                 IL_DOCBOOKWRAPPERTAG,
103                 IL_DOCBOOKWRAPPERTAGTYPE,
104                 IL_DOCBOOKWRAPPERATTR,
105                 IL_DOCBOOKITEMTAG,
106                 IL_DOCBOOKITEMTAGTYPE,
107                 IL_DOCBOOKITEMATTR,
108                 IL_DOCBOOKITEMWRAPPERTAG,
109                 IL_DOCBOOKITEMWRAPPERTAGTYPE,
110                 IL_DOCBOOKITEMWRAPPERATTR,
111                 IL_DOCBOOKINNERTAG,
112                 IL_DOCBOOKINNERTAGTYPE,
113                 IL_DOCBOOKINNERATTR,
114         IL_DOCBOOKNOFONTINSIDE,
115         IL_DOCBOOKRENDERASIMAGE,
116                 IL_INTOC,
117                 IL_ISTOCCAPTION,
118                 IL_LABELFONT,
119                 IL_LABELSTRING,
120                 IL_LANGPREAMBLE,
121                 IL_LATEXNAME,
122                 IL_LATEXPARAM,
123                 IL_LATEXTYPE,
124                 IL_LEFTDELIM,
125                 IL_LYXTYPE,
126                 IL_OBSOLETEDBY,
127                 IL_KEEPEMPTY,
128                 IL_MENUSTRING,
129                 IL_MULTIPAR,
130                 IL_NEEDCPROTECT,
131                 IL_NEEDMBOXPROTECT,
132                 IL_NEEDPROTECT,
133                 IL_NEWLINE_CMD,
134                 IL_PASSTHRU,
135                 IL_PASSTHRU_CHARS,
136                 IL_PARBREAKIGNORED,
137                 IL_PARBREAKISNEWLINE,
138                 IL_PREAMBLE,
139                 IL_REQUIRES,
140                 IL_RIGHTDELIM,
141                 IL_REFPREFIX,
142                 IL_RESETARGS,
143                 IL_RESETSFONT,
144                 IL_SPELLCHECK,
145                 IL_END
146         };
147
148
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 },
183                 { "end", IL_END },
184                 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
185                 { "font", IL_FONT },
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 }
230         };
231
232         lex.pushTable(elementTags);
233
234         if (labelfont_ == sane_font)
235                 labelfont_ = inherit_font;
236         if (bgcolor_ == Color_error)
237                 bgcolor_ = Color_none;
238         bool getout = false;
239         // whether we've read the CustomPars or ForcePlain tag
240         // for issuing a warning in case MultiPars comes later
241         bool readCustomOrPlain = false;
242
243         string tmp;
244         while (!getout && lex.isOK()) {
245                 int le = lex.lex();
246                 switch (le) {
247                 case Lexer::LEX_UNDEF:
248                         lex.printError("Unknown InsetLayout tag");
249                         if (validating)
250                                 return false;
251                         continue;
252                 default:
253                         break;
254                 }
255                 switch (le) {
256                 // FIXME
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
259                 // using the Lexer.
260                 case IL_LYXTYPE: {
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
268                                 if (validating)
269                                         return false;
270                                 break;
271                         }
272                         string lt;
273                         lex >> lt;
274                         lyxtype_ = translateLyXType(lt);
275                         if (lyxtype_  == InsetLyXType::NOLYXTYPE) {
276                                 LYXERR0("Unknown LyXType `" << lt << "'.");
277                                 // this is not really a reason to abort
278                                 if (validating)
279                                         return false;
280                         }
281                         if (lyxtype_ == InsetLyXType::CHARSTYLE) {
282                                 // by default, charstyles force the plain layout
283                                 multipar_ = false;
284                                 forceplain_ = true;
285                         }
286                         break;
287                 }
288                 case IL_LATEXTYPE:  {
289                         string lt;
290                         lex >> lt;
291                         latextype_ = translateLaTeXType(lt);
292                         if (latextype_  == InsetLaTeXType::ILT_ERROR) {
293                                 LYXERR0("Unknown LaTeXType `" << lt << "'.");
294                                 // this is not really a reason to abort
295                                 if (validating)
296                                         return false;
297                         }
298                         break;
299                 }
300                 case IL_LABELSTRING:
301                         lex >> labelstring_;
302                         break;
303                 case IL_MENUSTRING:
304                         lex >> menustring_;
305                         break;
306                 case IL_DECORATION:
307                         lex >> tmp;
308                         decoration_ = translateDecoration(tmp);
309                         break;
310                 case IL_LATEXNAME:
311                         lex >> latexname_;
312                         break;
313                 case IL_LATEXPARAM:
314                         lex >> tmp;
315                         latexparam_ = subst(tmp, "&#34;", "\"");
316                         break;
317                 case IL_LEFTDELIM:
318                         lex >> leftdelim_;
319                         leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
320                                                     from_ascii("\n"));
321                         break;
322                 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
323                         lex >> fixedwidthpreambleencoding_;
324                         break;
325                 case IL_FORCE_LOCAL_FONT_SWITCH:
326                         lex >> forcelocalfontswitch_;
327                         break;
328                 case IL_RIGHTDELIM:
329                         lex >> rightdelim_;
330                         rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
331                                                      from_ascii("\n"));
332                         break;
333                 case IL_LABELFONT:
334                         labelfont_ = lyxRead(lex, inherit_font);
335                         break;
336                 case IL_FORCELTR:
337                         lex >> forceltr_;
338                         break;
339                 case IL_FORCEOWNLINES:
340                         lex >> forceownlines_;
341                         break;
342                 case IL_INTOC:
343                         lex >> intoc_;
344                         break;
345                 case IL_MULTIPAR:
346                         lex >> multipar_;
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_;
354                         break;
355                 case IL_COUNTER:
356                         lex >> counter_;
357                         break;
358                 case IL_CUSTOMPARS:
359                         lex >> custompars_;
360                         readCustomOrPlain = true;
361                         break;
362                 case IL_FORCEPLAIN:
363                         lex >> forceplain_;
364                         readCustomOrPlain = true;
365                         break;
366                 case IL_PASSTHRU:
367                         lex >> passthru_;
368                         break;
369                 case IL_PASSTHRU_CHARS:
370                         lex >> passthru_chars_;
371                         break;
372                 case IL_NEWLINE_CMD:
373                         lex >> newline_cmd_;
374                         break;
375                 case IL_PARBREAKIGNORED:
376                         lex >> parbreakignored_;
377                         break;
378                 case IL_PARBREAKISNEWLINE:
379                         lex >> parbreakisnewline_;
380                         break;
381                 case IL_KEEPEMPTY:
382                         lex >> keepempty_;
383                         break;
384                 case IL_FREESPACING:
385                         lex >> freespacing_;
386                         break;
387                 case IL_NEEDPROTECT:
388                         lex >> needprotect_;
389                         break;
390                 case IL_NEEDCPROTECT:
391                         lex >> needcprotect_;
392                         break;
393                 case IL_NEEDMBOXPROTECT:
394                         lex >> needmboxprotect_;
395                         break;
396                 case IL_CONTENTASLABEL:
397                         lex >> contentaslabel_;
398                         break;
399                 case IL_COPYSTYLE: {
400                         // initialize with a known style
401                         docstring style;
402                         lex >> style;
403                         style = subst(style, '_', ' ');
404
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);
412                                 name_ = tmpname;
413                         } else {
414                                 LYXERR0("Cannot copy unknown InsetLayout `"
415                                         << style << "' to InsetLayout `"
416                                         << name() << "'\n"
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
425                                 if (validating)
426                                         return false;
427                         }
428                         break;
429                 }
430                 case IL_OBSOLETEDBY: {
431                         docstring style;
432                         lex >> style;
433                         style = subst(style, '_', ' ');
434
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);
442                                 name_ = tmpname;
443                                 if (obsoleted_by().empty())
444                                         obsoleted_by_ = style;
445                         } else {
446                                 LYXERR0("Cannot replace InsetLayout `"
447                                         << name()
448                                         << "' with unknown InsetLayout `"
449                                         << style << "'\n"
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
458                                 if (validating)
459                                         return false;
460                         }
461                         break;
462                 }
463
464                 case IL_FONT: {
465                         font_ = lyxRead(lex, inherit_font);
466                         // If you want to define labelfont, you need to do so after
467                         // font is defined.
468                         labelfont_ = font_;
469                         break;
470                 }
471                 case IL_RESETARGS:
472                         bool reset;
473                         lex >> reset;
474                         if (reset) {
475                                 latexargs_.clear();
476                                 postcommandargs_.clear();
477                         }
478                         break;
479                 case IL_ARGUMENT:
480                         readArgument(lex);
481                         break;
482                 case IL_BGCOLOR:
483                         lex >> tmp;
484                         bgcolor_ = lcolor.getFromLyXName(tmp);
485                         break;
486                 case IL_PREAMBLE:
487                         preamble_ = lex.getLongString(from_ascii("EndPreamble"));
488                         break;
489                 case IL_BABELPREAMBLE:
490                         babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
491                         break;
492                 case IL_LANGPREAMBLE:
493                         langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
494                         break;
495                 case IL_REFPREFIX:
496                         lex >> refprefix_;
497                         break;
498                 case IL_HTMLTAG:
499                         lex >> htmltag_;
500                         break;
501                 case IL_HTMLATTR:
502                         lex >> htmlattr_;
503                         break;
504                 case IL_HTMLCLASS:
505                         lex >> htmlclass_;
506                         break;
507                 case IL_HTMLFORCECSS:
508                         lex >> htmlforcecss_;
509                         break;
510                 case IL_HTMLINNERTAG:
511                         lex >> htmlinnertag_;
512                         break;
513                 case IL_HTMLINNERATTR:
514                         lex >> htmlinnerattr_;
515                         break;
516                 case IL_HTMLLABEL:
517                         lex >> htmllabel_;
518                         break;
519                 case IL_HTMLISBLOCK:
520                         lex >> htmlisblock_;
521                         break;
522                 case IL_HTMLSTYLE:
523                         htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
524                         break;
525                 case IL_HTMLPREAMBLE:
526                         htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
527                         break;
528                 case IL_DOCBOOKTAG:
529                         lex >> docbooktag_;
530                         break;
531                 case IL_DOCBOOKATTR:
532                         lex >> docbookattr_;
533                         break;
534                 case IL_DOCBOOKTAGTYPE:
535                         lex >> docbooktagtype_;
536                         break;
537                 case IL_DOCBOOKINNERTAG:
538                         lex >> docbookinnertag_;
539                         break;
540                 case IL_DOCBOOKINNERATTR:
541                         lex >> docbookinnerattr_;
542                         break;
543                 case IL_DOCBOOKINNERTAGTYPE:
544                         lex >> docbookinnertagtype_;
545                         break;
546                 case IL_DOCBOOKININFO:
547                         lex >> docbookininfo_;
548                         break;
549                 case IL_DOCBOOKARGUMENTBEFOREMAINTAG:
550                         lex >> docbookargumentbeforemaintag_;
551                         break;
552                 case IL_DOCBOOKARGUMENTAFTERMAINTAG:
553                         lex >> docbookargumentaftermaintag_;
554                         break;
555                 case IL_DOCBOOKNOTINPARA:
556                         lex >> docbooknotinpara_;
557                         break;
558                 case IL_DOCBOOKSECTION:
559                         lex >> docbooksection_;
560                         break;
561                 case IL_DOCBOOKITEMTAG:
562                         lex >> docbookitemtag_;
563                         break;
564                 case IL_DOCBOOKITEMTAGTYPE:
565                         lex >> docbookitemtagtype_;
566                         break;
567                 case IL_DOCBOOKITEMATTR:
568                         lex >> docbookitemattr_;
569                         break;
570                 case IL_DOCBOOKITEMWRAPPERTAG:
571                         lex >> docbookitemwrappertag_;
572                         break;
573                 case IL_DOCBOOKITEMWRAPPERTAGTYPE:
574                         lex >> docbookitemwrappertagtype_;
575                         break;
576                 case IL_DOCBOOKITEMWRAPPERATTR:
577                         lex >> docbookitemwrapperattr_;
578                         break;
579                 case IL_DOCBOOKWRAPPERTAG:
580                         lex >> docbookwrappertag_;
581                         break;
582                 case IL_DOCBOOKWRAPPERTAGTYPE:
583                         lex >> docbookwrappertagtype_;
584                         break;
585                 case IL_DOCBOOKWRAPPERATTR:
586                         lex >> docbookwrapperattr_;
587                         break;
588                 case IL_DOCBOOKNOFONTINSIDE:
589                         lex >> docbooknofontinside_;
590                         break;
591                 case IL_DOCBOOKRENDERASIMAGE:
592                         lex >> docbookrenderasimage_;
593                         break;
594                 case IL_REQUIRES: {
595                         lex.eatLine();
596                         vector<string> const req
597                                 = getVectorFromString(lex.getString(true));
598                         required_.insert(req.begin(), req.end());
599                         break;
600                 }
601                 case IL_SPELLCHECK:
602                         lex >> spellcheck_;
603                         break;
604                 case IL_RESETSFONT:
605                         lex >> resetsfont_;
606                         break;
607                 case IL_DISPLAY:
608                         lex >> display_;
609                         break;
610                 case IL_ADDTOTOC:
611                         lex >> toc_type_;
612                         add_to_toc_ = !toc_type_.empty();
613                         break;
614                 case IL_ISTOCCAPTION:
615                         lex >> is_toc_caption_;
616                         break;
617                 case IL_EDITEXTERNAL:
618                         lex >> edit_external_;
619                         break;
620                 case IL_END:
621                         getout = true;
622                         break;
623                 }
624         }
625
626         // Here add element to list if getout == true
627         if (!getout)
628                 return false;
629
630         // The label font is generally used as-is without
631         // any realization against a given context.
632         labelfont_.realize(sane_font);
633
634         lex.popTable();
635         return true;
636 }
637
638
639 InsetLyXType translateLyXType(std::string const & str)
640 {
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;
650 }
651
652
653 string const & InsetLayout::htmltag() const
654 {
655         if (htmltag_.empty())
656                 htmltag_ = multipar_ ? "div" : "span";
657         return htmltag_;
658 }
659
660
661 string const & InsetLayout::htmlclass() const
662 {
663         if (htmlclass_.empty())
664                 htmlclass_ = defaultCSSClass();
665         return htmlclass_;
666 }
667
668
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_;
678 }
679
680
681 string const & InsetLayout::htmlinnerattr() const
682 {
683         if (htmlinnerattr_.empty())
684                 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
685         return htmlinnerattr_;
686 }
687
688
689 string InsetLayout::defaultCSSClass() const
690 {
691         if (!defaultcssclass_.empty())
692                 return defaultcssclass_;
693         string d;
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))
699                         d += "_";
700                 else if (isLower(*it))
701                         d += *it;
702                 else
703                         d += lowercase(*it);
704         }
705         // are there other characters we need to remove?
706         defaultcssclass_ = d;
707         return defaultcssclass_;
708 }
709
710
711 void InsetLayout::makeDefaultCSS() const
712 {
713         if (!htmldefaultstyle_.empty())
714                 return;
715         docstring const mainfontCSS = font_.asCSS();
716         if (!mainfontCSS.empty())
717                 htmldefaultstyle_ =
718                                 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
719                                 mainfontCSS + from_ascii("\n}\n");
720 }
721
722
723 docstring InsetLayout::htmlstyle() const
724 {
725         if (!htmlstyle_.empty() && !htmlforcecss_)
726                 return htmlstyle_;
727         if (htmldefaultstyle_.empty())
728                 makeDefaultCSS();
729         docstring retval = htmldefaultstyle_;
730         if (!htmlstyle_.empty())
731                 retval += '\n' + htmlstyle_ + '\n';
732         return retval;
733 }
734
735
736 std::string const & InsetLayout::docbookininfo() const
737 {
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_;
743 }
744
745
746 std::string InsetLayout::docbooktagtype() const
747 {
748         if (docbooktagtype_ != "block" && docbooktagtype_ != "paragraph" && docbooktagtype_ != "inline")
749                 docbooktagtype_ = "block";
750         return docbooktagtype_;
751 }
752
753
754 std::string InsetLayout::docbookinnertagtype() const
755 {
756         if (docbookinnertagtype_ != "block" && docbookinnertagtype_ != "paragraph" && docbookinnertagtype_ != "inline")
757                 docbookinnertagtype_ = "block";
758         return docbookinnertagtype_;
759 }
760
761
762 std::string InsetLayout::docbookwrappertagtype() const
763 {
764         if (docbookwrappertagtype_ != "block" && docbookwrappertagtype_ != "paragraph" && docbookwrappertagtype_ != "inline")
765                 docbookwrappertagtype_ = "block";
766         return docbookwrappertagtype_;
767 }
768
769
770 std::string InsetLayout::docbookitemtagtype() const
771 {
772         if (docbookitemtagtype_ != "block" && docbookitemtagtype_ != "paragraph" && docbookitemtagtype_ != "inline")
773                 docbookitemtagtype_ = "block";
774         return docbookitemtagtype_;
775 }
776
777
778 std::string InsetLayout::docbookitemwrappertagtype() const
779 {
780         if (docbookitemwrappertagtype_ != "block" && docbookitemwrappertagtype_ != "paragraph" && docbookitemwrappertagtype_ != "inline")
781                 docbookitemwrappertagtype_ = "block";
782         return docbookitemwrappertagtype_;
783 }
784
785
786 void InsetLayout::readArgument(Lexer & lex)
787 {
788         Layout::latexarg arg;
789         arg.mandatory = false;
790         arg.autoinsert = false;
791         arg.insertcotext = false;
792         arg.insertonnewline = false;
793         bool error = 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;
801         string nr;
802         lex >> nr;
803         bool const postcmd = prefixIs(nr, "post:");
804         while (!finished && lex.isOK() && !error) {
805                 lex.next();
806                 string const tok = ascii_lowercase(lex.getString());
807
808                 if (tok.empty()) {
809                         continue;
810                 } else if (tok == "endargument") {
811                         finished = true;
812                 } else if (tok == "labelstring") {
813                         lex.next();
814                         arg.labelstring = lex.getDocString();
815                 } else if (tok == "menustring") {
816                         lex.next();
817                         arg.menustring = lex.getDocString();
818                 } else if (tok == "mandatory") {
819                         lex.next();
820                         arg.mandatory = lex.getBool();
821                 } else if (tok == "autoinsert") {
822                         lex.next();
823                         arg.autoinsert = lex.getBool();
824                 } else if (tok == "insertcotext") {
825                         lex.next();
826                         arg.insertcotext = lex.getBool();
827                 } else if (tok == "insertonnewline") {
828                         lex.next();
829                         arg.insertonnewline = lex.getBool();
830                 } else if (tok == "leftdelim") {
831                         lex.next();
832                         arg.ldelim = lex.getDocString();
833                         arg.ldelim = subst(arg.ldelim,
834                                                     from_ascii("<br/>"), from_ascii("\n"));
835                 } else if (tok == "rightdelim") {
836                         lex.next();
837                         arg.rdelim = lex.getDocString();
838                         arg.rdelim = subst(arg.rdelim,
839                                                     from_ascii("<br/>"), from_ascii("\n"));
840                 } else if (tok == "defaultarg") {
841                         lex.next();
842                         arg.defaultarg = lex.getDocString();
843                 } else if (tok == "presetarg") {
844                         lex.next();
845                         arg.presetarg = lex.getDocString();
846                 } else if (tok == "tooltip") {
847                         lex.next();
848                         arg.tooltip = lex.getDocString();
849                 } else if (tok == "requires") {
850                         lex.next();
851                         arg.required = lex.getString();
852                 } else if (tok == "decoration") {
853                         lex.next();
854                         arg.decoration = lex.getString();
855                 } else if (tok == "newlinecmd") {
856                         lex.next();
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") {
863                         lex.next();
864                         arg.pass_thru_chars = lex.getDocString();
865                 } else if (tok == "passthru") {
866                         lex.next();
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;
872                         else
873                                 arg.passthru = PT_INHERITED;
874                 } else if (tok == "istoccaption") {
875                         lex.next();
876                         arg.is_toc_caption = lex.getBool();
877                 } else if (tok == "freespacing") {
878                         lex.next();
879                         arg.free_spacing = lex.getBool();
880                 } else if (tok == "docbooktag") {
881                         lex.next();
882                         arg.docbooktag = lex.getDocString();
883                 } else if (tok == "docbookattr") {
884                         lex.next();
885                         arg.docbookattr = lex.getDocString();
886                 } else if (tok == "docbooktagtype") {
887                         lex.next();
888                         arg.docbooktagtype = lex.getDocString();
889                 } else if (tok == "docbookargumentbeforemaintag") {
890                         lex.next();
891                         arg.docbookargumentbeforemaintag = lex.getBool();
892                 } else if (tok == "docbookargumentaftermaintag") {
893                         lex.next();
894                         arg.docbookargumentaftermaintag = lex.getBool();
895                 } else {
896                         lex.printError("Unknown tag");
897                         error = true;
898                 }
899         }
900         if (arg.labelstring.empty())
901                 LYXERR0("Incomplete Argument definition!");
902         else if (postcmd)
903                 postcommandargs_[nr] = arg;
904         else
905                 latexargs_[nr] = arg;
906 }
907
908
909 Layout::LaTeXArgMap InsetLayout::args() const
910 {
911         Layout::LaTeXArgMap args = latexargs_;
912         if (!postcommandargs_.empty())
913                 args.insert(postcommandargs_.begin(), postcommandargs_.end());
914         return args;
915 }
916
917
918 int InsetLayout::optArgs() const
919 {
920         int nr = 0;
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)
925                         ++nr;
926         }
927         return nr;
928 }
929
930
931 int InsetLayout::requiredArgs() const
932 {
933         int nr = 0;
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)
938                         ++nr;
939         }
940         return nr;
941 }
942
943
944 } //namespace lyx