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