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