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