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