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