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