]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
Introduce a wrapper class for odocstream to help ensuring that no
[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                         string lt;
187                         lex >> lt;
188                         lyxtype_ = translateLyXType(lt);
189                         if (lyxtype_  == NOLYXTYPE)
190                                 LYXERR0("Unknown LyXType `" << lt << "'.");
191                         if (lyxtype_ == CHARSTYLE)
192                                 multipar_ = false;
193                         break;
194                 }
195                 case IL_LATEXTYPE:  {
196                         string lt;
197                         lex >> lt;
198                         latextype_ = translateLaTeXType(lt);
199                         if (latextype_  == ILT_ERROR)
200                                 LYXERR0("Unknown LaTeXType `" << lt << "'.");
201                         break;
202                 }
203                 case IL_LABELSTRING:
204                         lex >> labelstring_;
205                         break;
206                 case IL_DECORATION:
207                         lex >> tmp;
208                         decoration_ = translateDecoration(tmp);
209                         break;
210                 case IL_LATEXNAME:
211                         lex >> latexname_;
212                         break;
213                 case IL_LATEXPARAM:
214                         lex >> tmp;
215                         latexparam_ = support::subst(tmp, "&quot;", "\"");
216                         break;
217                 case IL_LABELFONT:
218                         labelfont_ = lyxRead(lex, inherit_font);
219                         break;
220                 case IL_FORCELTR:
221                         lex >> forceltr_;
222                         break;
223                 case IL_INTOC:
224                         lex >> intoc_;
225                         break;
226                 case IL_MULTIPAR:
227                         lex >> multipar_;
228                         // the defaults for these depend upon multipar_
229                         if (readCustomOrPlain)
230                                 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
231                                         "Previous value may be overwritten!");
232                         readCustomOrPlain = false;
233                         custompars_ = multipar_;
234                         forceplain_ = !multipar_;
235                         break;
236                 case IL_COUNTER:
237                         lex >> counter_;
238                         break;
239                 case IL_CUSTOMPARS:
240                         lex >> custompars_;
241                         readCustomOrPlain = true;
242                         break;
243                 case IL_FORCEPLAIN:
244                         lex >> forceplain_;
245                         readCustomOrPlain = true;
246                         break;
247                 case IL_PASSTHRU:
248                         lex >> passthru_;
249                         break;
250                 case IL_PARBREAKISNEWLINE:
251                         lex >> parbreakisnewline_;
252                         break;
253                 case IL_KEEPEMPTY:
254                         lex >> keepempty_;
255                         break;
256                 case IL_FREESPACING:
257                         lex >> freespacing_;
258                         break;
259                 case IL_NEEDPROTECT:
260                         lex >> needprotect_;
261                         break;
262                 case IL_CONTENTASLABEL:
263                         lex >> contentaslabel_;
264                         break;
265                 case IL_COPYSTYLE: {
266                         // initialize with a known style
267                         docstring style;
268                         lex >> style;
269                         style = support::subst(style, '_', ' ');
270
271                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
272                         // here. So we do it the long way.
273                         TextClass::InsetLayouts::const_iterator it = 
274                                         tclass.insetLayouts().find(style);
275                         if (it != tclass.insetLayouts().end()) {
276                                 docstring const tmpname = name_;
277                                 this->operator=(it->second);
278                                 name_ = tmpname;
279                         } else {
280                                 LYXERR0("Cannot copy unknown InsetLayout `"
281                                         << style << "'\n"
282                                         << "All InsetLayouts so far:");
283                                 TextClass::InsetLayouts::const_iterator lit = 
284                                                 tclass.insetLayouts().begin();
285                                 TextClass::InsetLayouts::const_iterator len = 
286                                                 tclass.insetLayouts().end();
287                                 for (; lit != len; ++lit)
288                                         lyxerr << lit->second.name() << "\n";
289                         }
290                         break;
291                 }
292
293                 case IL_FONT: {
294                         font_ = lyxRead(lex, inherit_font);
295                         // If you want to define labelfont, you need to do so after
296                         // font is defined.
297                         labelfont_ = font_;
298                         break;
299                 }
300                 case IL_BGCOLOR:
301                         lex >> tmp;
302                         bgcolor_ = lcolor.getFromLyXName(tmp);
303                         break;
304                 case IL_PREAMBLE:
305                         preamble_ = from_utf8(lex.getLongString("EndPreamble"));
306                         break;
307                 case IL_REFPREFIX:
308                         lex >> refprefix_;
309                         break;
310                 case IL_HTMLTAG:
311                         lex >> htmltag_;
312                         break;
313                 case IL_HTMLATTR:
314                         lex >> htmlattr_;
315                         break;
316                 case IL_HTMLFORCECSS:
317                         lex >> htmlforcecss_;
318                         break;
319                 case IL_HTMLINNERTAG:
320                         lex >> htmlinnertag_;
321                         break;
322                 case IL_HTMLINNERATTR:
323                         lex >> htmlinnerattr_;
324                         break;
325                 case IL_HTMLLABEL:
326                         lex >> htmllabel_;
327                         break;
328                 case IL_HTMLISBLOCK:
329                         lex >> htmlisblock_;
330                         break;
331                 case IL_HTMLSTYLE:
332                         htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
333                         break;
334                 case IL_HTMLPREAMBLE:
335                         htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
336                         break;
337                 case IL_REQUIRES: {
338                         lex.eatLine();
339                         vector<string> const req 
340                                 = support::getVectorFromString(lex.getString());
341                         requires_.insert(req.begin(), req.end());
342                         break;
343                 }
344                 case IL_SPELLCHECK:
345                         lex >> spellcheck_;
346                         break;
347                 case IL_RESETSFONT:
348                         lex >> resetsfont_;
349                         break;
350                 case IL_DISPLAY:
351                         lex >> display_;
352                         break;
353                 case IL_END:
354                         getout = true;
355                         break;
356                 }
357         }
358
359         // Here add element to list if getout == true
360         if (!getout)
361                 return false;
362         
363         // The label font is generally used as-is without
364         // any realization against a given context.
365         labelfont_.realize(sane_font);
366
367         lex.popTable();
368         return true;
369 }
370
371
372 InsetLayout::InsetLyXType translateLyXType(std::string const & str) 
373 {
374         
375         if (support::compare_ascii_no_case(str, "charstyle") == 0)
376                 return InsetLayout::CHARSTYLE;
377         if (support::compare_ascii_no_case(str, "custom") == 0)
378                 return InsetLayout::CUSTOM;
379         if (support::compare_ascii_no_case(str, "element") == 0)
380                 return InsetLayout::ELEMENT;
381         if (support::compare_ascii_no_case(str, "end") == 0)
382                 return InsetLayout::END;
383         if (support::compare_ascii_no_case(str, "standard") == 0)
384                 return InsetLayout::STANDARD;
385         return InsetLayout::NOLYXTYPE;
386 }
387
388
389 string const & InsetLayout::htmltag() const
390 {
391         if (htmltag_.empty())
392                 htmltag_ = multipar_ ? "div" : "span";
393         return htmltag_; 
394 }
395
396
397 string const & InsetLayout::htmlattr() const
398 {
399         if (htmlattr_.empty())
400                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
401         return htmlattr_; 
402 }
403
404
405 string const & InsetLayout::htmlinnerattr() const
406 {
407         if (htmlinnerattr_.empty())
408                 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
409         return htmlinnerattr_; 
410 }
411
412
413 string InsetLayout::defaultCSSClass() const
414
415         if (!defaultcssclass_.empty())
416                 return defaultcssclass_;
417         string d;
418         string n = to_utf8(name());
419         string::const_iterator it = n.begin();
420         string::const_iterator en = n.end();
421         for (; it != en; ++it) {
422                 if (!isalpha(*it))
423                         d += "_";
424                 else if (islower(*it))
425                         d += *it;
426                 else
427                         d += support::lowercase(*it);
428         }
429         // are there other characters we need to remove?
430         defaultcssclass_ = d;
431         return defaultcssclass_;
432 }
433
434
435 void InsetLayout::makeDefaultCSS() const
436 {
437         if (!htmldefaultstyle_.empty()) 
438                 return;
439         docstring const mainfontCSS = font_.asCSS();
440         if (!mainfontCSS.empty())
441                 htmldefaultstyle_ = 
442                                 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
443                                 mainfontCSS + from_ascii("\n}\n");
444 }
445
446
447 docstring InsetLayout::htmlstyle() const 
448
449         if (!htmlstyle_.empty() && !htmlforcecss_)
450                 return htmlstyle_;
451         if (htmldefaultstyle_.empty())
452                 makeDefaultCSS();
453         docstring retval = htmldefaultstyle_;
454         if (!htmlstyle_.empty())
455                 retval += '\n' + htmlstyle_ + '\n';
456         return retval;
457 }
458
459
460 } //namespace lyx