]> git.lyx.org Git - lyx.git/blob - src/insets/InsetLayout.cpp
e326d26c9fdcd454798c24d4dbf34e35739fe84b
[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 {}
34
35
36 InsetLayout::InsetLayout(docstring const & name) :
37         name_(name), lyxtype_(STANDARD),
38         labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
39         decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(sane_font), 
40         labelfont_(sane_font), bgcolor_(Color_error), 
41         htmlforcecss_ (false), htmlisblock_(true),
42         multipar_(true), custompars_(true), forceplain_(false), 
43         passthru_(false), needprotect_(false), freespacing_(false), 
44         keepempty_(false), forceltr_(false), intoc_(false)
45
46         labelfont_.setColor(Color_error);
47 }
48
49
50 namespace {
51
52 InsetLayout::InsetDecoration translateDecoration(std::string const & str) 
53 {
54         if (support::compare_ascii_no_case(str, "classic") == 0)
55                 return InsetLayout::CLASSIC;
56         if (support::compare_ascii_no_case(str, "minimalistic") == 0)
57                 return InsetLayout::MINIMALISTIC;
58         if (support::compare_ascii_no_case(str, "conglomerate") == 0)
59                 return InsetLayout::CONGLOMERATE;
60         return InsetLayout::DEFAULT;
61 }
62
63 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
64 {
65         if (support::compare_ascii_no_case(str, "command") == 0)
66                 return InsetLayout::COMMAND;
67         if (support::compare_ascii_no_case(str, "environment") == 0)
68                 return InsetLayout::ENVIRONMENT;
69         if (support::compare_ascii_no_case(str, "none") == 0)
70                 return InsetLayout::NOLATEXTYPE;
71         return InsetLayout::ILT_ERROR;
72 }
73
74 } // namespace anon
75
76
77 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
78 {
79         enum {
80                 IL_BGCOLOR,
81                 IL_CONTENTASLABEL,
82                 IL_COPYSTYLE,
83                 IL_COUNTER,
84                 IL_CUSTOMPARS,
85                 IL_DECORATION,
86                 IL_FONT,
87                 IL_FORCELTR,
88                 IL_FORCEPLAIN,
89                 IL_FREESPACING,
90                 IL_HTMLTAG,
91                 IL_HTMLATTR,
92                 IL_HTMLFORCECSS,
93                 IL_HTMLINNERTAG,
94                 IL_HTMLINNERATTR,
95                 IL_HTMLISBLOCK,
96                 IL_HTMLLABEL,
97                 IL_HTMLSTYLE,
98                 IL_HTMLPREAMBLE,
99                 IL_INTOC,
100                 IL_LABELFONT,
101                 IL_LABELSTRING,
102                 IL_LATEXNAME,
103                 IL_LATEXPARAM,
104                 IL_LATEXTYPE,
105                 IL_LYXTYPE,
106                 IL_KEEPEMPTY,
107                 IL_MULTIPAR,
108                 IL_NEEDPROTECT,
109                 IL_PASSTHRU,
110                 IL_PREAMBLE,
111                 IL_REQUIRES,
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                 { "passthru", IL_PASSTHRU },
148                 { "preamble", IL_PREAMBLE },
149                 { "requires", IL_REQUIRES }
150         };
151
152         lex.pushTable(elementTags);
153
154         FontInfo font = inherit_font;
155         labelfont_ = inherit_font;
156         bgcolor_ = Color_none;
157         bool getout = false;
158         // whether we've read the CustomPars or ForcePlain tag
159         // for issuing a warning in case MultiPars comes later
160         bool readCustomOrPlain = false;
161
162         string tmp;     
163         while (!getout && lex.isOK()) {
164                 int le = lex.lex();
165                 switch (le) {
166                 case Lexer::LEX_UNDEF:
167                         lex.printError("Unknown InsetLayout tag");
168                         continue;
169                 default:
170                         break;
171                 }
172                 switch (le) {
173                 // FIXME
174                 // Perhaps a more elegant way to deal with the next two would be the
175                 // way this sort of thing is handled in Layout::read(), namely, by
176                 // using the Lexer.
177                 case IL_LYXTYPE: {
178                         string lt;
179                         lex >> lt;
180                         lyxtype_ = translateLyXType(lt);
181                         if (lyxtype_  == NOLYXTYPE)
182                                 LYXERR0("Unknown LyXType `" << lt << "'.");
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_END:
328                         getout = true;
329                         break;
330                 }
331         }
332
333         // Here add element to list if getout == true
334         if (!getout)
335                 return false;
336         
337         // The label font is generally used as-is without
338         // any realization against a given context.
339         labelfont_.realize(sane_font);
340
341         lex.popTable();
342         return true;
343 }
344
345
346 InsetLayout::InsetLyXType translateLyXType(std::string const & str) 
347 {
348         
349         if (support::compare_ascii_no_case(str, "charstyle") == 0)
350                 return InsetLayout::CHARSTYLE;
351         if (support::compare_ascii_no_case(str, "custom") == 0)
352                 return InsetLayout::CUSTOM;
353         if (support::compare_ascii_no_case(str, "element") == 0)
354                 return InsetLayout::ELEMENT;
355         if (support::compare_ascii_no_case(str, "end") == 0)
356                 return InsetLayout::END;
357         if (support::compare_ascii_no_case(str, "standard") == 0)
358                 return InsetLayout::STANDARD;
359         return InsetLayout::NOLYXTYPE;
360 }
361
362
363 string const & InsetLayout::htmltag() const
364 {
365         if (htmltag_.empty())
366                 htmltag_ = multipar_ ? "div" : "span";
367         return htmltag_; 
368 }
369
370
371 string const & InsetLayout::htmlattr() const
372 {
373         if (htmlattr_.empty())
374                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
375         return htmlattr_; 
376 }
377
378
379 string const & InsetLayout::htmlinnerattr() const
380 {
381         if (htmlinnerattr_.empty())
382                 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
383         return htmlinnerattr_; 
384 }
385
386
387 string InsetLayout::defaultCSSClass() const
388
389         if (!defaultcssclass_.empty())
390                 return defaultcssclass_;
391         string d;
392         string n = to_utf8(name());
393         string::const_iterator it = n.begin();
394         string::const_iterator en = n.end();
395         for (; it != en; ++it) {
396                 if (!isalpha(*it))
397                         d += "_";
398                 else if (islower(*it))
399                         d += *it;
400                 else
401                         d += support::lowercase(*it);
402         }
403         // are there other characters we need to remove?
404         defaultcssclass_ = d;
405         return defaultcssclass_;
406 }
407
408
409 void InsetLayout::makeDefaultCSS() const
410 {
411         if (!htmldefaultstyle_.empty()) 
412                 return;
413         docstring const mainfontCSS = font_.asCSS();
414         if (!mainfontCSS.empty())
415                 htmldefaultstyle_ = 
416                                 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
417                                 mainfontCSS + from_ascii("\n}\n");
418 }
419
420
421 docstring InsetLayout::htmlstyle() const 
422
423         if (!htmlstyle_.empty() && !htmlforcecss_)
424                 return htmlstyle_;
425         if (htmldefaultstyle_.empty())
426                 makeDefaultCSS();
427         docstring retval = htmldefaultstyle_;
428         if (!htmlstyle_.empty())
429                 retval += '\n' + htmlstyle_ + '\n';
430         return retval;
431 }
432
433
434 } //namespace lyx