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