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