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