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