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