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