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