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