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