]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
Amend 369828194361164 once more
[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_ALLOWED_IN_INSET,
68                 IL_ALLOWED_IN_LAYOUT,
69                 IL_ALLOWED_OCCURRENCES,
70                 IL_ALLOWED_OCCURRENCES_PER_ITEM,
71                 IL_ARGUMENT,
72                 IL_BABELPREAMBLE,
73                 IL_BGCOLOR,
74                 IL_CONTENTASLABEL,
75                 IL_COPYSTYLE,
76                 IL_COUNTER,
77                 IL_CUSTOMPARS,
78                 IL_DECORATION,
79                 IL_DISPLAY,
80                 IL_EDITEXTERNAL,
81                 IL_FIXEDWIDTH_PREAMBLE_ENCODING,
82                 IL_FONT,
83                 IL_FORCE_LOCAL_FONT_SWITCH,
84                 IL_FORCELTR,
85                 IL_FORCEOWNLINES,
86                 IL_FORCEPLAIN,
87                 IL_FREESPACING,
88                 IL_HTMLTAG,
89                 IL_HTMLATTR,
90                 IL_HTMLCLASS,
91                 IL_HTMLFORCECSS,
92                 IL_HTMLINNERTAG,
93                 IL_HTMLINNERATTR,
94                 IL_HTMLISBLOCK,
95                 IL_HTMLLABEL,
96                 IL_HTMLSTYLE,
97                 IL_HTMLPREAMBLE,
98                 IL_DOCBOOKTAG,
99                 IL_DOCBOOKATTR,
100                 IL_DOCBOOKTAGTYPE,
101                 IL_DOCBOOKSECTION,
102                 IL_DOCBOOKININFO,
103                 IL_DOCBOOKARGUMENTBEFOREMAINTAG,
104                 IL_DOCBOOKARGUMENTAFTERMAINTAG,
105                 IL_DOCBOOKNOTINPARA,
106                 IL_DOCBOOKWRAPPERTAG,
107                 IL_DOCBOOKWRAPPERTAGTYPE,
108                 IL_DOCBOOKWRAPPERATTR,
109                 IL_DOCBOOKITEMTAG,
110                 IL_DOCBOOKITEMTAGTYPE,
111                 IL_DOCBOOKITEMATTR,
112                 IL_DOCBOOKITEMWRAPPERTAG,
113                 IL_DOCBOOKITEMWRAPPERTAGTYPE,
114                 IL_DOCBOOKITEMWRAPPERATTR,
115                 IL_DOCBOOKINNERTAG,
116                 IL_DOCBOOKINNERTAGTYPE,
117                 IL_DOCBOOKINNERATTR,
118                 IL_DOCBOOKNOFONTINSIDE,
119                 IL_DOCBOOKRENDERASIMAGE,
120                 IL_INHERITFONT,
121                 IL_INTOC,
122                 IL_ISTOCCAPTION,
123                 IL_LABELFONT,
124                 IL_LABELSTRING,
125                 IL_LANGPREAMBLE,
126                 IL_LATEXNAME,
127                 IL_LATEXPARAM,
128                 IL_LATEXTYPE,
129                 IL_LEFTDELIM,
130                 IL_LYXTYPE,
131                 IL_OBSOLETEDBY,
132                 IL_KEEPEMPTY,
133                 IL_MENUSTRING,
134                 IL_MULTIPAR,
135                 IL_NEEDCPROTECT,
136                 IL_NEEDMBOXPROTECT,
137                 IL_NEEDPROTECT,
138                 IL_NEWLINE_CMD,
139                 IL_PASSTHRU,
140                 IL_PASSTHRU_CHARS,
141                 IL_PARBREAKIGNORED,
142                 IL_PARBREAKISNEWLINE,
143                 IL_PREAMBLE,
144                 IL_REQUIRES,
145                 IL_RIGHTDELIM,
146                 IL_REFPREFIX,
147                 IL_RESETARGS,
148                 IL_RESETSFONT,
149                 IL_SPELLCHECK,
150                 IL_END
151         };
152
153
154         LexerKeyword elementTags[] = {
155                 { "addtotoc", IL_ADDTOTOC },
156                 { "allowedininsets", IL_ALLOWED_IN_INSET },
157                 { "allowedinlayouts", IL_ALLOWED_IN_LAYOUT },
158                 { "allowedoccurrences", IL_ALLOWED_OCCURRENCES },
159                 { "allowedoccurrencesperitem", IL_ALLOWED_OCCURRENCES_PER_ITEM },
160                 { "argument", IL_ARGUMENT },
161                 { "babelpreamble", IL_BABELPREAMBLE },
162                 { "bgcolor", IL_BGCOLOR },
163                 { "contentaslabel", IL_CONTENTASLABEL },
164                 { "copystyle", IL_COPYSTYLE },
165                 { "counter", IL_COUNTER},
166                 { "custompars", IL_CUSTOMPARS },
167                 { "decoration", IL_DECORATION },
168                 { "display", IL_DISPLAY },
169                 { "docbookargumentaftermaintag", IL_DOCBOOKARGUMENTAFTERMAINTAG },
170                 { "docbookargumentbeforemaintag", IL_DOCBOOKARGUMENTBEFOREMAINTAG },
171                 { "docbookattr", IL_DOCBOOKATTR },
172                 { "docbookininfo", IL_DOCBOOKININFO },
173                 { "docbookinnerattr", IL_DOCBOOKINNERATTR },
174                 { "docbookinnertag", IL_DOCBOOKINNERTAG },
175                 { "docbookinnertagtype", IL_DOCBOOKINNERTAGTYPE },
176                 { "docbookitemattr", IL_DOCBOOKITEMATTR },
177                 { "docbookitemtag", IL_DOCBOOKITEMTAG },
178                 { "docbookitemtagtype", IL_DOCBOOKITEMTAGTYPE },
179                 { "docbookitemwrapperattr", IL_DOCBOOKITEMWRAPPERATTR },
180                 { "docbookitemwrappertag", IL_DOCBOOKITEMWRAPPERTAG },
181                 { "docbookitemwrappertagtype", IL_DOCBOOKITEMWRAPPERTAGTYPE },
182                 { "docbooknofontinside", IL_DOCBOOKNOFONTINSIDE },
183                 { "docbooknotinpara", IL_DOCBOOKNOTINPARA },
184                 { "docbookrenderasimage", IL_DOCBOOKRENDERASIMAGE },
185                 { "docbooksection", IL_DOCBOOKSECTION },
186                 { "docbooktag", IL_DOCBOOKTAG },
187                 { "docbooktagtype", IL_DOCBOOKTAGTYPE },
188                 { "docbookwrapperattr", IL_DOCBOOKWRAPPERATTR },
189                 { "docbookwrappertag", IL_DOCBOOKWRAPPERTAG },
190                 { "docbookwrappertagtype", IL_DOCBOOKWRAPPERTAGTYPE },
191                 { "editexternal", IL_EDITEXTERNAL },
192                 { "end", IL_END },
193                 { "fixedwidthpreambleencoding", IL_FIXEDWIDTH_PREAMBLE_ENCODING },
194                 { "font", IL_FONT },
195                 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
196                 { "forceltr", IL_FORCELTR },
197                 { "forceownlines", IL_FORCEOWNLINES },
198                 { "forceplain", IL_FORCEPLAIN },
199                 { "freespacing", IL_FREESPACING },
200                 { "htmlattr", IL_HTMLATTR },
201                 { "htmlclass", IL_HTMLCLASS },
202                 { "htmlforcecss", IL_HTMLFORCECSS },
203                 { "htmlinnerattr", IL_HTMLINNERATTR},
204                 { "htmlinnertag", IL_HTMLINNERTAG},
205                 { "htmlisblock", IL_HTMLISBLOCK},
206                 { "htmllabel", IL_HTMLLABEL },
207                 { "htmlpreamble", IL_HTMLPREAMBLE },
208                 { "htmlstyle", IL_HTMLSTYLE },
209                 { "htmltag", IL_HTMLTAG },
210                 { "inheritfont", IL_INHERITFONT },
211                 { "intoc", IL_INTOC },
212                 { "istoccaption", IL_ISTOCCAPTION },
213                 { "keepempty", IL_KEEPEMPTY },
214                 { "labelfont", IL_LABELFONT },
215                 { "labelstring", IL_LABELSTRING },
216                 { "langpreamble", IL_LANGPREAMBLE },
217                 { "latexname", IL_LATEXNAME },
218                 { "latexparam", IL_LATEXPARAM },
219                 { "latextype", IL_LATEXTYPE },
220                 { "leftdelim", IL_LEFTDELIM },
221                 { "lyxtype", IL_LYXTYPE },
222                 { "menustring", IL_MENUSTRING },
223                 { "multipar", IL_MULTIPAR },
224                 { "needcprotect", IL_NEEDCPROTECT },
225                 { "needmboxprotect", IL_NEEDMBOXPROTECT },
226                 { "needprotect", IL_NEEDPROTECT },
227                 { "newlinecmd", IL_NEWLINE_CMD },
228                 { "obsoletedby", IL_OBSOLETEDBY },
229                 { "parbreakignored", IL_PARBREAKIGNORED },
230                 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
231                 { "passthru", IL_PASSTHRU },
232                 { "passthruchars", IL_PASSTHRU_CHARS },
233                 { "preamble", IL_PREAMBLE },
234                 { "refprefix", IL_REFPREFIX },
235                 { "requires", IL_REQUIRES },
236                 { "resetargs", IL_RESETARGS },
237                 { "resetsfont", IL_RESETSFONT },
238                 { "rightdelim", IL_RIGHTDELIM },
239                 { "spellcheck", IL_SPELLCHECK }
240         };
241
242         lex.pushTable(elementTags);
243
244         if (labelfont_ == sane_font)
245                 labelfont_ = inherit_font;
246         if (bgcolor_ == Color_error)
247                 bgcolor_ = Color_none;
248         bool getout = false;
249         // whether we've read the CustomPars or ForcePlain tag
250         // for issuing a warning in case MultiPars comes later
251         bool readCustomOrPlain = false;
252
253         string tmp;
254         while (!getout && lex.isOK()) {
255                 int le = lex.lex();
256                 switch (le) {
257                 case Lexer::LEX_UNDEF:
258                         lex.printError("Unknown InsetLayout tag");
259                         if (validating)
260                                 return false;
261                         continue;
262                 default:
263                         break;
264                 }
265                 switch (le) {
266                 // FIXME
267                 // Perhaps a more elegant way to deal with the next two would be the
268                 // way this sort of thing is handled in Layout::read(), namely, by
269                 // using the Lexer.
270                 case IL_LYXTYPE: {
271                         // make sure that we have the right sort of name.
272                         if (name_ != from_ascii("undefined")
273                             && name_.substr(0,5) != from_ascii("Flex:")) {
274                                 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
275                                         "This one has the name `" << to_utf8(name_) << "'\n"
276                                         "Ignoring LyXType declaration.");
277                                 // this is not really a reason to abort
278                                 if (validating)
279                                         return false;
280                                 break;
281                         }
282                         string lt;
283                         lex >> lt;
284                         lyxtype_ = translateLyXType(lt);
285                         if (lyxtype_  == InsetLyXType::NOLYXTYPE) {
286                                 LYXERR0("Unknown LyXType `" << lt << "'.");
287                                 // this is not really a reason to abort
288                                 if (validating)
289                                         return false;
290                         }
291                         if (lyxtype_ == InsetLyXType::CHARSTYLE) {
292                                 // by default, charstyles force the plain layout
293                                 multipar_ = false;
294                                 forceplain_ = true;
295                         }
296                         break;
297                 }
298                 case IL_LATEXTYPE:  {
299                         string lt;
300                         lex >> lt;
301                         latextype_ = translateLaTeXType(lt);
302                         if (latextype_  == InsetLaTeXType::ILT_ERROR) {
303                                 LYXERR0("Unknown LaTeXType `" << lt << "'.");
304                                 // this is not really a reason to abort
305                                 if (validating)
306                                         return false;
307                         }
308                         break;
309                 }
310                 case IL_LABELSTRING:
311                         lex >> labelstring_;
312                         break;
313                 case IL_MENUSTRING:
314                         lex >> menustring_;
315                         break;
316                 case IL_DECORATION:
317                         lex >> tmp;
318                         decoration_ = translateDecoration(tmp);
319                         break;
320                 case IL_LATEXNAME:
321                         lex >> latexname_;
322                         break;
323                 case IL_LATEXPARAM:
324                         lex >> tmp;
325                         latexparam_ = subst(tmp, "&#34;", "\"");
326                         break;
327                 case IL_LEFTDELIM:
328                         lex >> leftdelim_;
329                         leftdelim_ = subst(leftdelim_, from_ascii("<br/>"),
330                                                     from_ascii("\n"));
331                         break;
332                 case IL_FIXEDWIDTH_PREAMBLE_ENCODING:
333                         lex >> fixedwidthpreambleencoding_;
334                         break;
335                 case IL_FORCE_LOCAL_FONT_SWITCH:
336                         lex >> forcelocalfontswitch_;
337                         break;
338                 case IL_RIGHTDELIM:
339                         lex >> rightdelim_;
340                         rightdelim_ = subst(rightdelim_, from_ascii("<br/>"),
341                                                      from_ascii("\n"));
342                         break;
343                 case IL_LABELFONT:
344                         labelfont_ = lyxRead(lex, inherit_font);
345                         break;
346                 case IL_FORCELTR:
347                         lex >> forceltr_;
348                         break;
349                 case IL_FORCEOWNLINES:
350                         lex >> forceownlines_;
351                         break;
352                 case IL_INHERITFONT:
353                         lex >> inheritfont_;
354                         break;
355                 case IL_INTOC:
356                         lex >> intoc_;
357                         break;
358                 case IL_MULTIPAR:
359                         lex >> multipar_;
360                         // the defaults for these depend upon multipar_
361                         if (readCustomOrPlain)
362                                 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
363                                         "Previous value may be overwritten!");
364                         readCustomOrPlain = false;
365                         custompars_ = multipar_;
366                         forceplain_ = !multipar_;
367                         break;
368                 case IL_COUNTER:
369                         lex >> counter_;
370                         break;
371                 case IL_CUSTOMPARS:
372                         lex >> custompars_;
373                         readCustomOrPlain = true;
374                         break;
375                 case IL_FORCEPLAIN:
376                         lex >> forceplain_;
377                         readCustomOrPlain = true;
378                         break;
379                 case IL_PASSTHRU:
380                         lex >> passthru_;
381                         break;
382                 case IL_PASSTHRU_CHARS:
383                         lex >> passthru_chars_;
384                         break;
385                 case IL_NEWLINE_CMD:
386                         lex >> newline_cmd_;
387                         break;
388                 case IL_PARBREAKIGNORED:
389                         lex >> parbreakignored_;
390                         break;
391                 case IL_PARBREAKISNEWLINE:
392                         lex >> parbreakisnewline_;
393                         break;
394                 case IL_KEEPEMPTY:
395                         lex >> keepempty_;
396                         break;
397                 case IL_FREESPACING:
398                         lex >> freespacing_;
399                         break;
400                 case IL_NEEDPROTECT:
401                         lex >> needprotect_;
402                         break;
403                 case IL_NEEDCPROTECT:
404                         lex >> needcprotect_;
405                         break;
406                 case IL_NEEDMBOXPROTECT:
407                         lex >> needmboxprotect_;
408                         break;
409                 case IL_CONTENTASLABEL:
410                         lex >> contentaslabel_;
411                         break;
412                 case IL_COPYSTYLE: {
413                         // initialize with a known style
414                         docstring style;
415                         lex >> style;
416                         style = subst(style, '_', ' ');
417
418                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
419                         // here. So we do it the long way.
420                         TextClass::InsetLayouts::const_iterator it =
421                                         tclass.insetLayouts().find(style);
422                         if (it != tclass.insetLayouts().end()) {
423                                 docstring const tmpname = name_;
424                                 this->operator=(it->second);
425                                 name_ = tmpname;
426                         } else {
427                                 LYXERR0("Cannot copy unknown InsetLayout `"
428                                         << style << "' to InsetLayout `"
429                                         << name() << "'\n"
430                                         << "All InsetLayouts so far:");
431                                 TextClass::InsetLayouts::const_iterator lit =
432                                                 tclass.insetLayouts().begin();
433                                 TextClass::InsetLayouts::const_iterator len =
434                                                 tclass.insetLayouts().end();
435                                 for (; lit != len; ++lit)
436                                         lyxerr << lit->second.name() << "\n";
437                                 // this is not really a reason to abort
438                                 if (validating)
439                                         return false;
440                         }
441                         break;
442                 }
443                 case IL_OBSOLETEDBY: {
444                         docstring style;
445                         lex >> style;
446                         style = subst(style, '_', ' ');
447
448                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
449                         // here. So we do it the long way.
450                         TextClass::InsetLayouts::const_iterator it =
451                                         tclass.insetLayouts().find(style);
452                         if (it != tclass.insetLayouts().end()) {
453                                 docstring const tmpname = name_;
454                                 this->operator=(it->second);
455                                 name_ = tmpname;
456                                 if (obsoleted_by().empty())
457                                         obsoleted_by_ = style;
458                         } else {
459                                 LYXERR0("Cannot replace InsetLayout `"
460                                         << name()
461                                         << "' with unknown InsetLayout `"
462                                         << style << "'\n"
463                                         << "All InsetLayouts so far:");
464                                 TextClass::InsetLayouts::const_iterator lit =
465                                                 tclass.insetLayouts().begin();
466                                 TextClass::InsetLayouts::const_iterator len =
467                                                 tclass.insetLayouts().end();
468                                 for (; lit != len; ++lit)
469                                         lyxerr << lit->second.name() << "\n";
470                                 // this is not really a reason to abort
471                                 if (validating)
472                                         return false;
473                         }
474                         break;
475                 }
476
477                 case IL_FONT: {
478                         font_ = lyxRead(lex, inherit_font);
479                         // If you want to define labelfont, you need to do so after
480                         // font is defined.
481                         labelfont_ = font_;
482                         break;
483                 }
484                 case IL_RESETARGS:
485                         bool reset;
486                         lex >> reset;
487                         if (reset) {
488                                 latexargs_.clear();
489                                 postcommandargs_.clear();
490                         }
491                         break;
492                 case IL_ARGUMENT:
493                         readArgument(lex);
494                         break;
495                 case IL_BGCOLOR:
496                         lex >> tmp;
497                         bgcolor_ = lcolor.getFromLyXName(tmp);
498                         break;
499                 case IL_PREAMBLE:
500                         preamble_ = lex.getLongString(from_ascii("EndPreamble"));
501                         break;
502                 case IL_BABELPREAMBLE:
503                         babelpreamble_ = lex.getLongString(from_ascii("EndBabelPreamble"));
504                         break;
505                 case IL_LANGPREAMBLE:
506                         langpreamble_ = lex.getLongString(from_ascii("EndLangPreamble"));
507                         break;
508                 case IL_REFPREFIX:
509                         lex >> refprefix_;
510                         break;
511                 case IL_HTMLTAG:
512                         lex >> htmltag_;
513                         break;
514                 case IL_HTMLATTR:
515                         lex >> htmlattr_;
516                         break;
517                 case IL_HTMLCLASS:
518                         lex >> htmlclass_;
519                         break;
520                 case IL_HTMLFORCECSS:
521                         lex >> htmlforcecss_;
522                         break;
523                 case IL_HTMLINNERTAG:
524                         lex >> htmlinnertag_;
525                         break;
526                 case IL_HTMLINNERATTR:
527                         lex >> htmlinnerattr_;
528                         break;
529                 case IL_HTMLLABEL:
530                         lex >> htmllabel_;
531                         break;
532                 case IL_HTMLISBLOCK:
533                         lex >> htmlisblock_;
534                         break;
535                 case IL_HTMLSTYLE:
536                         htmlstyle_ = lex.getLongString(from_ascii("EndHTMLStyle"));
537                         break;
538                 case IL_HTMLPREAMBLE:
539                         htmlpreamble_ = lex.getLongString(from_ascii("EndPreamble"));
540                         break;
541                 case IL_DOCBOOKTAG:
542                         lex >> docbooktag_;
543                         break;
544                 case IL_DOCBOOKATTR:
545                         lex >> docbookattr_;
546                         break;
547                 case IL_DOCBOOKTAGTYPE:
548                         lex >> docbooktagtype_;
549                         break;
550                 case IL_DOCBOOKINNERTAG:
551                         lex >> docbookinnertag_;
552                         break;
553                 case IL_DOCBOOKINNERATTR:
554                         lex >> docbookinnerattr_;
555                         break;
556                 case IL_DOCBOOKINNERTAGTYPE:
557                         lex >> docbookinnertagtype_;
558                         break;
559                 case IL_DOCBOOKININFO:
560                         lex >> docbookininfo_;
561                         break;
562                 case IL_DOCBOOKARGUMENTBEFOREMAINTAG:
563                         lex >> docbookargumentbeforemaintag_;
564                         break;
565                 case IL_DOCBOOKARGUMENTAFTERMAINTAG:
566                         lex >> docbookargumentaftermaintag_;
567                         break;
568                 case IL_DOCBOOKNOTINPARA:
569                         lex >> docbooknotinpara_;
570                         break;
571                 case IL_DOCBOOKSECTION:
572                         lex >> docbooksection_;
573                         break;
574                 case IL_DOCBOOKITEMTAG:
575                         lex >> docbookitemtag_;
576                         break;
577                 case IL_DOCBOOKITEMTAGTYPE:
578                         lex >> docbookitemtagtype_;
579                         break;
580                 case IL_DOCBOOKITEMATTR:
581                         lex >> docbookitemattr_;
582                         break;
583                 case IL_DOCBOOKITEMWRAPPERTAG:
584                         lex >> docbookitemwrappertag_;
585                         break;
586                 case IL_DOCBOOKITEMWRAPPERTAGTYPE:
587                         lex >> docbookitemwrappertagtype_;
588                         break;
589                 case IL_DOCBOOKITEMWRAPPERATTR:
590                         lex >> docbookitemwrapperattr_;
591                         break;
592                 case IL_DOCBOOKWRAPPERTAG:
593                         lex >> docbookwrappertag_;
594                         break;
595                 case IL_DOCBOOKWRAPPERTAGTYPE:
596                         lex >> docbookwrappertagtype_;
597                         break;
598                 case IL_DOCBOOKWRAPPERATTR:
599                         lex >> docbookwrapperattr_;
600                         break;
601                 case IL_DOCBOOKNOFONTINSIDE:
602                         lex >> docbooknofontinside_;
603                         break;
604                 case IL_DOCBOOKRENDERASIMAGE:
605                         lex >> docbookrenderasimage_;
606                         break;
607                 case IL_REQUIRES: {
608                         lex.eatLine();
609                         vector<string> const req
610                                 = getVectorFromString(lex.getString(true));
611                         required_.insert(req.begin(), req.end());
612                         break;
613                 }
614                 case IL_SPELLCHECK:
615                         lex >> spellcheck_;
616                         break;
617                 case IL_RESETSFONT:
618                         lex >> resetsfont_;
619                         break;
620                 case IL_DISPLAY:
621                         lex >> display_;
622                         break;
623                 case IL_ADDTOTOC:
624                         lex >> toc_type_;
625                         add_to_toc_ = !toc_type_.empty();
626                         break;
627                 case IL_ISTOCCAPTION:
628                         lex >> is_toc_caption_;
629                         break;
630                 case IL_EDITEXTERNAL:
631                         lex >> edit_external_;
632                         break;
633                 case IL_ALLOWED_IN_INSET: {
634                         docstring allowed = lex.getLongString(from_ascii("EndAllowedInInsets"));
635                         allowed = subst(allowed, from_ascii("\n"), docstring());
636                         allowed = subst(allowed, from_ascii("\t"), docstring());
637                         allowed = subst(allowed, from_ascii("\""), docstring());
638                         allowed = subst(allowed, '_', ' ');
639                         vector<docstring> const allowances =
640                                 getVectorFromString(allowed, from_ascii(","), false, true);
641                         allowed_in_insets_.clear();
642                         allowed_in_insets_.insert(allowances.begin(), allowances.end());
643                         break;
644                 }
645
646                 case IL_ALLOWED_IN_LAYOUT: {
647                         docstring allowed = lex.getLongString(from_ascii("EndAllowedInLayouts"));
648                         allowed = subst(allowed, from_ascii("\n"), docstring());
649                         allowed = subst(allowed, from_ascii("\t"), docstring());
650                         allowed = subst(allowed, from_ascii("\""), docstring());
651                         allowed = subst(allowed, '_', ' ');
652                         vector<docstring> const allowances =
653                                 getVectorFromString(allowed, from_ascii(","), false, true);
654                         allowed_in_layouts_.clear();
655                         allowed_in_layouts_.insert(allowances.begin(), allowances.end());
656                         break;
657                 }
658                 case IL_ALLOWED_OCCURRENCES:
659                         lex >> allowed_occurrences_;
660                         break;
661                 case IL_ALLOWED_OCCURRENCES_PER_ITEM:
662                         lex >> allowed_occurrences_per_item_;
663                         break;
664                 case IL_END:
665                         getout = true;
666                         break;
667                 }
668         }
669
670         // Here add element to list if getout == true
671         if (!getout)
672                 return false;
673
674         // The label font is generally used as-is without
675         // any realization against a given context.
676         labelfont_.realize(sane_font);
677
678         lex.popTable();
679         return true;
680 }
681
682
683 InsetLyXType translateLyXType(std::string const & str)
684 {
685         if (compare_ascii_no_case(str, "charstyle") == 0)
686                 return InsetLyXType::CHARSTYLE;
687         if (compare_ascii_no_case(str, "custom") == 0)
688                 return InsetLyXType::CUSTOM;
689         if (compare_ascii_no_case(str, "end") == 0)
690                 return InsetLyXType::END;
691         if (compare_ascii_no_case(str, "standard") == 0)
692                 return InsetLyXType::STANDARD;
693         return InsetLyXType::NOLYXTYPE;
694 }
695
696
697 string const & InsetLayout::htmltag() const
698 {
699         if (htmltag_.empty())
700                 htmltag_ = multipar_ ? "div" : "span";
701         return htmltag_;
702 }
703
704
705 string const & InsetLayout::htmlclass() const
706 {
707         if (htmlclass_.empty())
708                 htmlclass_ = defaultCSSClass();
709         return htmlclass_;
710 }
711
712
713 std::string const & InsetLayout::htmlGetAttrString() const {
714         if (!htmlfullattrs_.empty())
715                 return htmlfullattrs_;
716         htmlfullattrs_ = htmlclass();
717         if (!htmlfullattrs_.empty())
718                 htmlfullattrs_ = "class='" + htmlfullattrs_ + "'";
719         if (!htmlattr_.empty())
720                 htmlfullattrs_ += " " + htmlattr_;
721         return htmlfullattrs_;
722 }
723
724
725 string const & InsetLayout::htmlinnerattr() const
726 {
727         if (htmlinnerattr_.empty())
728                 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
729         return htmlinnerattr_;
730 }
731
732
733 string InsetLayout::defaultCSSClass() const
734 {
735         if (!defaultcssclass_.empty())
736                 return defaultcssclass_;
737         string d;
738         string n = to_utf8(name());
739         string::const_iterator it = n.begin();
740         string::const_iterator en = n.end();
741         for (; it != en; ++it) {
742                 if (!isAlphaASCII(*it))
743                         d += "_";
744                 else if (isLower(*it))
745                         d += *it;
746                 else
747                         d += lowercase(*it);
748         }
749         // are there other characters we need to remove?
750         defaultcssclass_ = d;
751         return defaultcssclass_;
752 }
753
754
755 void InsetLayout::makeDefaultCSS() const
756 {
757         if (!htmldefaultstyle_.empty())
758                 return;
759         docstring const mainfontCSS = font_.asCSS();
760         if (!mainfontCSS.empty())
761                 htmldefaultstyle_ =
762                                 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
763                                 mainfontCSS + from_ascii("\n}\n");
764 }
765
766
767 docstring InsetLayout::htmlstyle() const
768 {
769         if (!htmlstyle_.empty() && !htmlforcecss_)
770                 return htmlstyle_;
771         if (htmldefaultstyle_.empty())
772                 makeDefaultCSS();
773         docstring retval = htmldefaultstyle_;
774         if (!htmlstyle_.empty())
775                 retval += '\n' + htmlstyle_ + '\n';
776         return retval;
777 }
778
779
780 std::string const & InsetLayout::docbookininfo() const
781 {
782         // Same as Layout::docbookininfo.
783         // Indeed, a trilean. Only titles should be "maybe": otherwise, metadata is "always", content is "never".
784         if (docbookininfo_.empty() || (docbookininfo_ != "never" && docbookininfo_ != "always" && docbookininfo_ != "maybe"))
785                 docbookininfo_ = "never";
786         return docbookininfo_;
787 }
788
789
790 std::string InsetLayout::docbooktagtype() const
791 {
792         if (docbooktagtype_ != "block" && docbooktagtype_ != "paragraph" && docbooktagtype_ != "inline")
793                 docbooktagtype_ = "block";
794         return docbooktagtype_;
795 }
796
797
798 std::string InsetLayout::docbookinnertagtype() const
799 {
800         if (docbookinnertagtype_ != "block" && docbookinnertagtype_ != "paragraph" && docbookinnertagtype_ != "inline")
801                 docbookinnertagtype_ = "block";
802         return docbookinnertagtype_;
803 }
804
805
806 std::string InsetLayout::docbookwrappertagtype() const
807 {
808         if (docbookwrappertagtype_ != "block" && docbookwrappertagtype_ != "paragraph" && docbookwrappertagtype_ != "inline")
809                 docbookwrappertagtype_ = "block";
810         return docbookwrappertagtype_;
811 }
812
813
814 std::string InsetLayout::docbookitemtagtype() const
815 {
816         if (docbookitemtagtype_ != "block" && docbookitemtagtype_ != "paragraph" && docbookitemtagtype_ != "inline")
817                 docbookitemtagtype_ = "block";
818         return docbookitemtagtype_;
819 }
820
821
822 std::string InsetLayout::docbookitemwrappertagtype() const
823 {
824         if (docbookitemwrappertagtype_ != "block" && docbookitemwrappertagtype_ != "paragraph" && docbookitemwrappertagtype_ != "inline")
825                 docbookitemwrappertagtype_ = "block";
826         return docbookitemwrappertagtype_;
827 }
828
829
830 void InsetLayout::readArgument(Lexer & lex)
831 {
832         Layout::latexarg arg;
833         arg.mandatory = false;
834         arg.autoinsert = false;
835         arg.insertcotext = false;
836         arg.insertonnewline = false;
837         bool error = false;
838         bool finished = false;
839         arg.font = inherit_font;
840         arg.labelfont = inherit_font;
841         arg.is_toc_caption = false;
842         arg.free_spacing = false;
843         arg.passthru = PT_INHERITED;
844         arg.nodelims = false;
845         string nr;
846         lex >> nr;
847         bool const postcmd = prefixIs(nr, "post:");
848         while (!finished && lex.isOK() && !error) {
849                 lex.next();
850                 string const tok = ascii_lowercase(lex.getString());
851
852                 if (tok.empty()) {
853                         continue;
854                 } else if (tok == "endargument") {
855                         finished = true;
856                 } else if (tok == "labelstring") {
857                         lex.next();
858                         arg.labelstring = lex.getDocString();
859                 } else if (tok == "menustring") {
860                         lex.next();
861                         arg.menustring = lex.getDocString();
862                 } else if (tok == "mandatory") {
863                         lex.next();
864                         arg.mandatory = lex.getBool();
865                 } else if (tok == "autoinsert") {
866                         lex.next();
867                         arg.autoinsert = lex.getBool();
868                 } else if (tok == "insertcotext") {
869                         lex.next();
870                         arg.insertcotext = lex.getBool();
871                 } else if (tok == "insertonnewline") {
872                         lex.next();
873                         arg.insertonnewline = lex.getBool();
874                 } else if (tok == "leftdelim") {
875                         lex.next();
876                         arg.ldelim = lex.getDocString();
877                         arg.ldelim = subst(arg.ldelim,
878                                                     from_ascii("<br/>"), from_ascii("\n"));
879                 } else if (tok == "rightdelim") {
880                         lex.next();
881                         arg.rdelim = lex.getDocString();
882                         arg.rdelim = subst(arg.rdelim,
883                                                     from_ascii("<br/>"), from_ascii("\n"));
884                 } else if (tok == "defaultarg") {
885                         lex.next();
886                         arg.defaultarg = lex.getDocString();
887                 } else if (tok == "presetarg") {
888                         lex.next();
889                         arg.presetarg = lex.getDocString();
890                 } else if (tok == "tooltip") {
891                         lex.next();
892                         arg.tooltip = lex.getDocString();
893                 } else if (tok == "requires") {
894                         lex.eatLine();
895                         arg.required = trim(lex.getString(true));
896                 } else if (tok == "decoration") {
897                         lex.next();
898                         arg.decoration = lex.getString();
899                 } else if (tok == "newlinecmd") {
900                         lex.next();
901                         arg.newlinecmd = lex.getString();
902                 } else if (tok == "font") {
903                         arg.font = lyxRead(lex, arg.font);
904                 } else if (tok == "labelfont") {
905                         arg.labelfont = lyxRead(lex, arg.labelfont);
906                 } else if (tok == "passthruchars") {
907                         lex.next();
908                         arg.pass_thru_chars = lex.getDocString();
909                 } else if (tok == "passthru") {
910                         lex.next();
911                         docstring value = lex.getDocString();
912                         if (value == "true" || value == "1")
913                                 arg.passthru = PT_TRUE;
914                         else if (value == "false" || value == "0")
915                                 arg.passthru = PT_FALSE;
916                         else
917                                 arg.passthru = PT_INHERITED;
918                 } else if (tok == "istoccaption") {
919                         lex.next();
920                         arg.is_toc_caption = lex.getBool();
921                 } else if (tok == "freespacing") {
922                         lex.next();
923                         arg.free_spacing = lex.getBool();
924                 } else if (tok == "docbooktag") {
925                         lex.next();
926                         arg.docbooktag = lex.getDocString();
927                 } else if (tok == "docbookattr") {
928                         lex.next();
929                         arg.docbookattr = lex.getDocString();
930                 } else if (tok == "docbooktagtype") {
931                         lex.next();
932                         arg.docbooktagtype = lex.getDocString();
933                 } else if (tok == "docbookargumentbeforemaintag") {
934                         lex.next();
935                         arg.docbookargumentbeforemaintag = lex.getBool();
936                 } else if (tok == "docbookargumentaftermaintag") {
937                         lex.next();
938                         arg.docbookargumentaftermaintag = lex.getBool();
939                 } else {
940                         lex.printError("Unknown tag");
941                         error = true;
942                 }
943         }
944         if (arg.labelstring.empty())
945                 LYXERR0("Incomplete Argument definition!");
946         else if (postcmd)
947                 postcommandargs_[nr] = arg;
948         else
949                 latexargs_[nr] = arg;
950 }
951
952
953 Layout::LaTeXArgMap InsetLayout::args() const
954 {
955         Layout::LaTeXArgMap args = latexargs_;
956         if (!postcommandargs_.empty())
957                 args.insert(postcommandargs_.begin(), postcommandargs_.end());
958         return args;
959 }
960
961
962 int InsetLayout::optArgs() const
963 {
964         int nr = 0;
965         Layout::LaTeXArgMap const args = InsetLayout::args();
966         Layout::LaTeXArgMap::const_iterator it = args.begin();
967         for (; it != args.end(); ++it) {
968                 if (!(*it).second.mandatory)
969                         ++nr;
970         }
971         return nr;
972 }
973
974
975 int InsetLayout::requiredArgs() const
976 {
977         int nr = 0;
978         Layout::LaTeXArgMap const args = InsetLayout::args();
979         Layout::LaTeXArgMap::const_iterator it = args.begin();
980         for (; it != args.end(); ++it) {
981                 if ((*it).second.mandatory)
982                         ++nr;
983         }
984         return nr;
985 }
986
987
988 } //namespace lyx