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