]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
0d9e88e909064e95bbcb40f4edfb01e366248e51
[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 "Layout.h"
19 #include "Lexer.h"
20 #include "TextClass.h"
21
22 #include "support/debug.h"
23 #include "support/lstrings.h"
24 #include "support/textutils.h"
25
26 #include <vector>
27
28 using std::string;
29 using std::set;
30 using std::vector;
31
32 namespace lyx {
33
34 InsetLayout::InsetLayout() :
35         name_(from_ascii("undefined")), lyxtype_(STANDARD),
36         labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
37         decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(sane_font), 
38         labelfont_(sane_font), bgcolor_(Color_error), 
39         htmlforcecss_ (false), htmlisblock_(true),
40         multipar_(true), custompars_(true), forceplain_(false), 
41         passthru_(false), parbreakisnewline_(false), freespacing_(false), 
42         keepempty_(false), forceltr_(false), 
43         needprotect_(false), intoc_(false), spellcheck_(true), 
44         resetsfont_(true), display_(true), forcelocalfontswitch_(false)
45
46         labelfont_.setColor(Color_error);
47 }
48
49
50 namespace {
51
52 InsetLayout::InsetDecoration translateDecoration(std::string const & str) 
53 {
54         if (support::compare_ascii_no_case(str, "classic") == 0)
55                 return InsetLayout::CLASSIC;
56         if (support::compare_ascii_no_case(str, "minimalistic") == 0)
57                 return InsetLayout::MINIMALISTIC;
58         if (support::compare_ascii_no_case(str, "conglomerate") == 0)
59                 return InsetLayout::CONGLOMERATE;
60         return InsetLayout::DEFAULT;
61 }
62
63 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
64 {
65         if (support::compare_ascii_no_case(str, "command") == 0)
66                 return InsetLayout::COMMAND;
67         if (support::compare_ascii_no_case(str, "environment") == 0)
68                 return InsetLayout::ENVIRONMENT;
69         if (support::compare_ascii_no_case(str, "none") == 0)
70                 return InsetLayout::NOLATEXTYPE;
71         return InsetLayout::ILT_ERROR;
72 }
73
74 } // namespace anon
75
76
77 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
78 {
79         enum {
80                 IL_ARGUMENT,
81                 IL_BABELPREAMBLE,
82                 IL_BGCOLOR,
83                 IL_CONTENTASLABEL,
84                 IL_COPYSTYLE,
85                 IL_COUNTER,
86                 IL_CUSTOMPARS,
87                 IL_DECORATION,
88                 IL_DISPLAY,
89                 IL_FONT,
90                 IL_FORCE_LOCAL_FONT_SWITCH,
91                 IL_FORCELTR,
92                 IL_FORCEPLAIN,
93                 IL_FREESPACING,
94                 IL_HTMLTAG,
95                 IL_HTMLATTR,
96                 IL_HTMLFORCECSS,
97                 IL_HTMLINNERTAG,
98                 IL_HTMLINNERATTR,
99                 IL_HTMLISBLOCK,
100                 IL_HTMLLABEL,
101                 IL_HTMLSTYLE,
102                 IL_HTMLPREAMBLE,
103                 IL_INTOC,
104                 IL_LABELFONT,
105                 IL_LABELSTRING,
106                 IL_LANGPREAMBLE,
107                 IL_LATEXNAME,
108                 IL_LATEXPARAM,
109                 IL_LATEXTYPE,
110                 IL_LEFTDELIM,
111                 IL_LYXTYPE,
112                 IL_KEEPEMPTY,
113                 IL_MULTIPAR,
114                 IL_NEEDPROTECT,
115                 IL_PASSTHRU,
116                 IL_PARBREAKISNEWLINE,
117                 IL_PREAMBLE,
118                 IL_REQUIRES,
119                 IL_RIGHTDELIM,
120                 IL_SPELLCHECK,
121                 IL_REFPREFIX,
122                 IL_RESETSFONT,
123                 IL_END
124         };
125
126
127         LexerKeyword elementTags[] = {
128                 { "argument", IL_ARGUMENT },
129                 { "babelpreamble", IL_BABELPREAMBLE },
130                 { "bgcolor", IL_BGCOLOR },
131                 { "contentaslabel", IL_CONTENTASLABEL },
132                 { "copystyle", IL_COPYSTYLE }, 
133                 { "counter", IL_COUNTER},
134                 { "custompars", IL_CUSTOMPARS },
135                 { "decoration", IL_DECORATION },
136                 { "display", IL_DISPLAY },
137                 { "end", IL_END },
138                 { "font", IL_FONT },
139                 { "forcelocalfontswitch", IL_FORCE_LOCAL_FONT_SWITCH },
140                 { "forceltr", IL_FORCELTR },
141                 { "forceplain", IL_FORCEPLAIN },
142                 { "freespacing", IL_FREESPACING },
143                 { "htmlattr", IL_HTMLATTR },
144                 { "htmlforcecss", IL_HTMLFORCECSS },
145                 { "htmlinnerattr", IL_HTMLINNERATTR},
146                 { "htmlinnertag", IL_HTMLINNERTAG},
147                 { "htmlisblock", IL_HTMLISBLOCK},
148                 { "htmllabel", IL_HTMLLABEL },
149                 { "htmlpreamble", IL_HTMLPREAMBLE },
150                 { "htmlstyle", IL_HTMLSTYLE },
151                 { "htmltag", IL_HTMLTAG },
152                 { "intoc", IL_INTOC },
153                 { "keepempty", IL_KEEPEMPTY },
154                 { "labelfont", IL_LABELFONT },
155                 { "labelstring", IL_LABELSTRING },
156                 { "langpreamble", IL_LANGPREAMBLE },
157                 { "latexname", IL_LATEXNAME },
158                 { "latexparam", IL_LATEXPARAM },
159                 { "latextype", IL_LATEXTYPE },
160                 { "leftdelim", IL_LEFTDELIM },
161                 { "lyxtype", IL_LYXTYPE },
162                 { "multipar", IL_MULTIPAR },
163                 { "needprotect", IL_NEEDPROTECT },
164                 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
165                 { "passthru", IL_PASSTHRU },
166                 { "preamble", IL_PREAMBLE },
167                 { "refprefix", IL_REFPREFIX },
168                 { "requires", IL_REQUIRES },
169                 { "resetsfont", IL_RESETSFONT },
170                 { "rightdelim", IL_RIGHTDELIM },
171                 { "spellcheck", IL_SPELLCHECK }
172         };
173
174         lex.pushTable(elementTags);
175
176         labelfont_ = inherit_font;
177         bgcolor_ = Color_none;
178         bool getout = false;
179         // whether we've read the CustomPars or ForcePlain tag
180         // for issuing a warning in case MultiPars comes later
181         bool readCustomOrPlain = false;
182
183         string tmp;     
184         while (!getout && lex.isOK()) {
185                 int le = lex.lex();
186                 switch (le) {
187                 case Lexer::LEX_UNDEF:
188                         lex.printError("Unknown InsetLayout tag");
189                         continue;
190                 default:
191                         break;
192                 }
193                 switch (le) {
194                 // FIXME
195                 // Perhaps a more elegant way to deal with the next two would be the
196                 // way this sort of thing is handled in Layout::read(), namely, by
197                 // using the Lexer.
198                 case IL_LYXTYPE: {
199                         // make sure that we have the right sort of name.
200                         if (name_ != from_ascii("undefined")
201                             && name_.substr(0,5) != from_ascii("Flex:")) {
202                                 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
203                                         "This one has the name `" << to_utf8(name_) << "'\n"
204                                         "Ignoring LyXType declaration.");
205                                 break;
206                         }
207                         string lt;
208                         lex >> lt;
209                         lyxtype_ = translateLyXType(lt);
210                         if (lyxtype_  == NOLYXTYPE)
211                                 LYXERR0("Unknown LyXType `" << lt << "'.");
212                         if (lyxtype_ == CHARSTYLE)
213                                 multipar_ = false;
214                         break;
215                 }
216                 case IL_LATEXTYPE:  {
217                         string lt;
218                         lex >> lt;
219                         latextype_ = translateLaTeXType(lt);
220                         if (latextype_  == ILT_ERROR)
221                                 LYXERR0("Unknown LaTeXType `" << lt << "'.");
222                         break;
223                 }
224                 case IL_LABELSTRING:
225                         lex >> labelstring_;
226                         break;
227                 case IL_DECORATION:
228                         lex >> tmp;
229                         decoration_ = translateDecoration(tmp);
230                         break;
231                 case IL_LATEXNAME:
232                         lex >> latexname_;
233                         break;
234                 case IL_LATEXPARAM:
235                         lex >> tmp;
236                         latexparam_ = support::subst(tmp, "&quot;", "\"");
237                         break;
238                 case IL_LEFTDELIM:
239                         lex >> leftdelim_;
240                         leftdelim_ = support::subst(leftdelim_, from_ascii("<br/>"),
241                                                     from_ascii("\n"));
242                         break;
243                 case IL_FORCE_LOCAL_FONT_SWITCH:
244                         lex >> forcelocalfontswitch_;
245                         break;
246                 case IL_RIGHTDELIM:
247                         lex >> rightdelim_;
248                         rightdelim_ = support::subst(rightdelim_, from_ascii("<br/>"),
249                                                      from_ascii("\n"));
250                         break;
251                 case IL_LABELFONT:
252                         labelfont_ = lyxRead(lex, inherit_font);
253                         break;
254                 case IL_FORCELTR:
255                         lex >> forceltr_;
256                         break;
257                 case IL_INTOC:
258                         lex >> intoc_;
259                         break;
260                 case IL_MULTIPAR:
261                         lex >> multipar_;
262                         // the defaults for these depend upon multipar_
263                         if (readCustomOrPlain)
264                                 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
265                                         "Previous value may be overwritten!");
266                         readCustomOrPlain = false;
267                         custompars_ = multipar_;
268                         forceplain_ = !multipar_;
269                         break;
270                 case IL_COUNTER:
271                         lex >> counter_;
272                         break;
273                 case IL_CUSTOMPARS:
274                         lex >> custompars_;
275                         readCustomOrPlain = true;
276                         break;
277                 case IL_FORCEPLAIN:
278                         lex >> forceplain_;
279                         readCustomOrPlain = true;
280                         break;
281                 case IL_PASSTHRU:
282                         lex >> passthru_;
283                         break;
284                 case IL_PARBREAKISNEWLINE:
285                         lex >> parbreakisnewline_;
286                         break;
287                 case IL_KEEPEMPTY:
288                         lex >> keepempty_;
289                         break;
290                 case IL_FREESPACING:
291                         lex >> freespacing_;
292                         break;
293                 case IL_NEEDPROTECT:
294                         lex >> needprotect_;
295                         break;
296                 case IL_CONTENTASLABEL:
297                         lex >> contentaslabel_;
298                         break;
299                 case IL_COPYSTYLE: {
300                         // initialize with a known style
301                         docstring style;
302                         lex >> style;
303                         style = support::subst(style, '_', ' ');
304
305                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
306                         // here. So we do it the long way.
307                         TextClass::InsetLayouts::const_iterator it = 
308                                         tclass.insetLayouts().find(style);
309                         if (it != tclass.insetLayouts().end()) {
310                                 docstring const tmpname = name_;
311                                 this->operator=(it->second);
312                                 name_ = tmpname;
313                         } else {
314                                 LYXERR0("Cannot copy unknown InsetLayout `"
315                                         << style << "'\n"
316                                         << "All InsetLayouts so far:");
317                                 TextClass::InsetLayouts::const_iterator lit = 
318                                                 tclass.insetLayouts().begin();
319                                 TextClass::InsetLayouts::const_iterator len = 
320                                                 tclass.insetLayouts().end();
321                                 for (; lit != len; ++lit)
322                                         lyxerr << lit->second.name() << "\n";
323                         }
324                         break;
325                 }
326
327                 case IL_FONT: {
328                         font_ = lyxRead(lex, inherit_font);
329                         // If you want to define labelfont, you need to do so after
330                         // font is defined.
331                         labelfont_ = font_;
332                         break;
333                 }
334                 case IL_ARGUMENT:
335                         readArgument(lex);
336                         break;
337                 case IL_BGCOLOR:
338                         lex >> tmp;
339                         bgcolor_ = lcolor.getFromLyXName(tmp);
340                         break;
341                 case IL_PREAMBLE:
342                         preamble_ = from_utf8(lex.getLongString("EndPreamble"));
343                         break;
344                 case IL_BABELPREAMBLE:
345                         babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
346                         break;
347                 case IL_LANGPREAMBLE:
348                         langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
349                         break;
350                 case IL_REFPREFIX:
351                         lex >> refprefix_;
352                         break;
353                 case IL_HTMLTAG:
354                         lex >> htmltag_;
355                         break;
356                 case IL_HTMLATTR:
357                         lex >> htmlattr_;
358                         break;
359                 case IL_HTMLFORCECSS:
360                         lex >> htmlforcecss_;
361                         break;
362                 case IL_HTMLINNERTAG:
363                         lex >> htmlinnertag_;
364                         break;
365                 case IL_HTMLINNERATTR:
366                         lex >> htmlinnerattr_;
367                         break;
368                 case IL_HTMLLABEL:
369                         lex >> htmllabel_;
370                         break;
371                 case IL_HTMLISBLOCK:
372                         lex >> htmlisblock_;
373                         break;
374                 case IL_HTMLSTYLE:
375                         htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
376                         break;
377                 case IL_HTMLPREAMBLE:
378                         htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
379                         break;
380                 case IL_REQUIRES: {
381                         lex.eatLine();
382                         vector<string> const req 
383                                 = support::getVectorFromString(lex.getString());
384                         requires_.insert(req.begin(), req.end());
385                         break;
386                 }
387                 case IL_SPELLCHECK:
388                         lex >> spellcheck_;
389                         break;
390                 case IL_RESETSFONT:
391                         lex >> resetsfont_;
392                         break;
393                 case IL_DISPLAY:
394                         lex >> display_;
395                         break;
396                 case IL_END:
397                         getout = true;
398                         break;
399                 }
400         }
401
402         // Here add element to list if getout == true
403         if (!getout)
404                 return false;
405         
406         // The label font is generally used as-is without
407         // any realization against a given context.
408         labelfont_.realize(sane_font);
409
410         lex.popTable();
411         return true;
412 }
413
414
415 InsetLayout::InsetLyXType translateLyXType(std::string const & str) 
416 {
417         
418         if (support::compare_ascii_no_case(str, "charstyle") == 0)
419                 return InsetLayout::CHARSTYLE;
420         if (support::compare_ascii_no_case(str, "custom") == 0)
421                 return InsetLayout::CUSTOM;
422         if (support::compare_ascii_no_case(str, "element") == 0)
423                 return InsetLayout::ELEMENT;
424         if (support::compare_ascii_no_case(str, "end") == 0)
425                 return InsetLayout::END;
426         if (support::compare_ascii_no_case(str, "standard") == 0)
427                 return InsetLayout::STANDARD;
428         return InsetLayout::NOLYXTYPE;
429 }
430
431
432 string const & InsetLayout::htmltag() const
433 {
434         if (htmltag_.empty())
435                 htmltag_ = multipar_ ? "div" : "span";
436         return htmltag_; 
437 }
438
439
440 string const & InsetLayout::htmlattr() const
441 {
442         if (htmlattr_.empty())
443                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
444         return htmlattr_; 
445 }
446
447
448 string const & InsetLayout::htmlinnerattr() const
449 {
450         if (htmlinnerattr_.empty())
451                 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
452         return htmlinnerattr_; 
453 }
454
455
456 string InsetLayout::defaultCSSClass() const
457
458         if (!defaultcssclass_.empty())
459                 return defaultcssclass_;
460         string d;
461         string n = to_utf8(name());
462         string::const_iterator it = n.begin();
463         string::const_iterator en = n.end();
464         for (; it != en; ++it) {
465                 if (!isAlphaASCII(*it))
466                         d += "_";
467                 else if (isLower(*it))
468                         d += *it;
469                 else
470                         d += support::lowercase(*it);
471         }
472         // are there other characters we need to remove?
473         defaultcssclass_ = d;
474         return defaultcssclass_;
475 }
476
477
478 void InsetLayout::makeDefaultCSS() const
479 {
480         if (!htmldefaultstyle_.empty()) 
481                 return;
482         docstring const mainfontCSS = font_.asCSS();
483         if (!mainfontCSS.empty())
484                 htmldefaultstyle_ = 
485                                 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
486                                 mainfontCSS + from_ascii("\n}\n");
487 }
488
489
490 docstring InsetLayout::htmlstyle() const 
491
492         if (!htmlstyle_.empty() && !htmlforcecss_)
493                 return htmlstyle_;
494         if (htmldefaultstyle_.empty())
495                 makeDefaultCSS();
496         docstring retval = htmldefaultstyle_;
497         if (!htmlstyle_.empty())
498                 retval += '\n' + htmlstyle_ + '\n';
499         return retval;
500 }
501
502 void InsetLayout::readArgument(Lexer & lex)
503 {
504         Layout::latexarg arg;
505         arg.mandatory = false;
506         bool error = false;
507         bool finished = false;
508         arg.font = inherit_font;
509         arg.labelfont = inherit_font;
510         string nr;
511         lex >> nr;
512         while (!finished && lex.isOK() && !error) {
513                 lex.next();
514                 string const tok = support::ascii_lowercase(lex.getString());
515
516                 if (tok.empty()) {
517                         continue;
518                 } else if (tok == "endargument") {
519                         finished = true;
520                 } else if (tok == "labelstring") {
521                         lex.next();
522                         arg.labelstring = lex.getDocString();
523                 } else if (tok == "mandatory") {
524                         lex.next();
525                         arg.mandatory = lex.getBool();
526                 } else if (tok == "leftdelim") {
527                         lex.next();
528                         arg.ldelim = lex.getDocString();
529                         arg.ldelim = support::subst(arg.ldelim,
530                                                     from_ascii("<br/>"), from_ascii("\n"));
531                 } else if (tok == "rightdelim") {
532                         lex.next();
533                         arg.rdelim = lex.getDocString();
534                         arg.rdelim = support::subst(arg.rdelim,
535                                                     from_ascii("<br/>"), from_ascii("\n"));
536                 } else if (tok == "tooltip") {
537                         lex.next();
538                         arg.tooltip = lex.getDocString();
539                 } else if (tok == "requires") {
540                         lex.next();
541                         arg.requires = lex.getString();
542                 } else if (tok == "decoration") {
543                         lex.next();
544                         arg.decoration = lex.getString();
545                 } else if (tok == "font") {
546                         arg.font = lyxRead(lex, arg.font);
547                 } else if (tok == "labelfont") {
548                         arg.labelfont = lyxRead(lex, arg.labelfont);
549                 } else {
550                         lex.printError("Unknown tag");
551                         error = true;
552                 }
553         }
554         if (arg.labelstring.empty())
555                 LYXERR0("Incomplete Argument definition!");
556         else
557                 latexargs_[nr] = arg;
558 }
559
560 unsigned int InsetLayout::optArgs() const
561 {
562         unsigned int nr = 0;
563         Layout::LaTeXArgMap::const_iterator it = latexargs_.begin();
564         for (; it != latexargs_.end(); ++it) {
565                 if (!(*it).second.mandatory)
566                         ++nr;
567         }
568         return nr;
569 }
570
571
572 unsigned int InsetLayout::requiredArgs() const
573 {
574         unsigned int nr = 0;
575         Layout::LaTeXArgMap::const_iterator it = latexargs_.begin();
576         for (; it != latexargs_.end(); ++it) {
577                 if ((*it).second.mandatory)
578                         ++nr;
579         }
580         return nr;
581 }
582
583
584 } //namespace lyx