]> git.lyx.org Git - features.git/blob - src/insets/InsetLayout.cpp
Translations for listings insets
[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         resetsfont_(true), display_(true)
43
44         labelfont_.setColor(Color_error);
45 }
46
47
48 namespace {
49
50 InsetLayout::InsetDecoration translateDecoration(std::string const & str) 
51 {
52         if (support::compare_ascii_no_case(str, "classic") == 0)
53                 return InsetLayout::CLASSIC;
54         if (support::compare_ascii_no_case(str, "minimalistic") == 0)
55                 return InsetLayout::MINIMALISTIC;
56         if (support::compare_ascii_no_case(str, "conglomerate") == 0)
57                 return InsetLayout::CONGLOMERATE;
58         return InsetLayout::DEFAULT;
59 }
60
61 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
62 {
63         if (support::compare_ascii_no_case(str, "command") == 0)
64                 return InsetLayout::COMMAND;
65         if (support::compare_ascii_no_case(str, "environment") == 0)
66                 return InsetLayout::ENVIRONMENT;
67         if (support::compare_ascii_no_case(str, "none") == 0)
68                 return InsetLayout::NOLATEXTYPE;
69         return InsetLayout::ILT_ERROR;
70 }
71
72 } // namespace anon
73
74
75 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
76 {
77         enum {
78                 IL_BABELPREAMBLE,
79                 IL_BGCOLOR,
80                 IL_CONTENTASLABEL,
81                 IL_COPYSTYLE,
82                 IL_COUNTER,
83                 IL_CUSTOMPARS,
84                 IL_DECORATION,
85                 IL_DISPLAY,
86                 IL_FONT,
87                 IL_FORCELTR,
88                 IL_FORCEPLAIN,
89                 IL_FREESPACING,
90                 IL_HTMLTAG,
91                 IL_HTMLATTR,
92                 IL_HTMLFORCECSS,
93                 IL_HTMLINNERTAG,
94                 IL_HTMLINNERATTR,
95                 IL_HTMLISBLOCK,
96                 IL_HTMLLABEL,
97                 IL_HTMLSTYLE,
98                 IL_HTMLPREAMBLE,
99                 IL_INTOC,
100                 IL_LABELFONT,
101                 IL_LABELSTRING,
102                 IL_LANGPREAMBLE,
103                 IL_LATEXNAME,
104                 IL_LATEXPARAM,
105                 IL_LATEXTYPE,
106                 IL_LYXTYPE,
107                 IL_KEEPEMPTY,
108                 IL_MULTIPAR,
109                 IL_NEEDPROTECT,
110                 IL_PASSTHRU,
111                 IL_PARBREAKISNEWLINE,
112                 IL_PREAMBLE,
113                 IL_REQUIRES,
114                 IL_SPELLCHECK,
115                 IL_REFPREFIX,
116                 IL_RESETSFONT,
117                 IL_END
118         };
119
120
121         LexerKeyword elementTags[] = {
122                 { "babelpreamble", IL_BABELPREAMBLE },
123                 { "bgcolor", IL_BGCOLOR },
124                 { "contentaslabel", IL_CONTENTASLABEL },
125                 { "copystyle", IL_COPYSTYLE }, 
126                 { "counter", IL_COUNTER},
127                 { "custompars", IL_CUSTOMPARS },
128                 { "decoration", IL_DECORATION },
129                 { "display", IL_DISPLAY },
130                 { "end", IL_END },
131                 { "font", IL_FONT },
132                 { "forceltr", IL_FORCELTR },
133                 { "forceplain", IL_FORCEPLAIN },
134                 { "freespacing", IL_FREESPACING },
135                 { "htmlattr", IL_HTMLATTR },
136                 { "htmlforcecss", IL_HTMLFORCECSS },
137                 { "htmlinnerattr", IL_HTMLINNERATTR},
138                 { "htmlinnertag", IL_HTMLINNERTAG},
139                 { "htmlisblock", IL_HTMLISBLOCK},
140                 { "htmllabel", IL_HTMLLABEL },
141                 { "htmlpreamble", IL_HTMLPREAMBLE },
142                 { "htmlstyle", IL_HTMLSTYLE },
143                 { "htmltag", IL_HTMLTAG },
144                 { "intoc", IL_INTOC },
145                 { "keepempty", IL_KEEPEMPTY },
146                 { "labelfont", IL_LABELFONT },
147                 { "labelstring", IL_LABELSTRING },
148                 { "langpreamble", IL_LANGPREAMBLE },
149                 { "latexname", IL_LATEXNAME },
150                 { "latexparam", IL_LATEXPARAM },
151                 { "latextype", IL_LATEXTYPE },
152                 { "lyxtype", IL_LYXTYPE },
153                 { "multipar", IL_MULTIPAR },
154                 { "needprotect", IL_NEEDPROTECT },
155                 { "parbreakisnewline", IL_PARBREAKISNEWLINE },
156                 { "passthru", IL_PASSTHRU },
157                 { "preamble", IL_PREAMBLE },
158                 { "refprefix", IL_REFPREFIX },
159                 { "requires", IL_REQUIRES },
160                 { "resetsfont", IL_RESETSFONT },
161                 { "spellcheck", IL_SPELLCHECK }
162         };
163
164         lex.pushTable(elementTags);
165
166         labelfont_ = inherit_font;
167         bgcolor_ = Color_none;
168         bool getout = false;
169         // whether we've read the CustomPars or ForcePlain tag
170         // for issuing a warning in case MultiPars comes later
171         bool readCustomOrPlain = false;
172
173         string tmp;     
174         while (!getout && lex.isOK()) {
175                 int le = lex.lex();
176                 switch (le) {
177                 case Lexer::LEX_UNDEF:
178                         lex.printError("Unknown InsetLayout tag");
179                         continue;
180                 default:
181                         break;
182                 }
183                 switch (le) {
184                 // FIXME
185                 // Perhaps a more elegant way to deal with the next two would be the
186                 // way this sort of thing is handled in Layout::read(), namely, by
187                 // using the Lexer.
188                 case IL_LYXTYPE: {
189                         // make sure that we have the right sort of name.
190                         if (name_ != from_ascii("undefined")
191                             && name_.substr(0,5) != from_ascii("Flex:")) {
192                                 LYXERR0("Flex insets must have names of the form `Flex:<name>'.\n"
193                                         "This one has the name `" << to_utf8(name_) << "'\n"
194                                         "Ignoring LyXType declaration.");
195                                 break;
196                         }
197                         string lt;
198                         lex >> lt;
199                         lyxtype_ = translateLyXType(lt);
200                         if (lyxtype_  == NOLYXTYPE)
201                                 LYXERR0("Unknown LyXType `" << lt << "'.");
202                         if (lyxtype_ == CHARSTYLE)
203                                 multipar_ = false;
204                         break;
205                 }
206                 case IL_LATEXTYPE:  {
207                         string lt;
208                         lex >> lt;
209                         latextype_ = translateLaTeXType(lt);
210                         if (latextype_  == ILT_ERROR)
211                                 LYXERR0("Unknown LaTeXType `" << lt << "'.");
212                         break;
213                 }
214                 case IL_LABELSTRING:
215                         lex >> labelstring_;
216                         break;
217                 case IL_DECORATION:
218                         lex >> tmp;
219                         decoration_ = translateDecoration(tmp);
220                         break;
221                 case IL_LATEXNAME:
222                         lex >> latexname_;
223                         break;
224                 case IL_LATEXPARAM:
225                         lex >> tmp;
226                         latexparam_ = support::subst(tmp, "&quot;", "\"");
227                         break;
228                 case IL_LABELFONT:
229                         labelfont_ = lyxRead(lex, inherit_font);
230                         break;
231                 case IL_FORCELTR:
232                         lex >> forceltr_;
233                         break;
234                 case IL_INTOC:
235                         lex >> intoc_;
236                         break;
237                 case IL_MULTIPAR:
238                         lex >> multipar_;
239                         // the defaults for these depend upon multipar_
240                         if (readCustomOrPlain)
241                                 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
242                                         "Previous value may be overwritten!");
243                         readCustomOrPlain = false;
244                         custompars_ = multipar_;
245                         forceplain_ = !multipar_;
246                         break;
247                 case IL_COUNTER:
248                         lex >> counter_;
249                         break;
250                 case IL_CUSTOMPARS:
251                         lex >> custompars_;
252                         readCustomOrPlain = true;
253                         break;
254                 case IL_FORCEPLAIN:
255                         lex >> forceplain_;
256                         readCustomOrPlain = true;
257                         break;
258                 case IL_PASSTHRU:
259                         lex >> passthru_;
260                         break;
261                 case IL_PARBREAKISNEWLINE:
262                         lex >> parbreakisnewline_;
263                         break;
264                 case IL_KEEPEMPTY:
265                         lex >> keepempty_;
266                         break;
267                 case IL_FREESPACING:
268                         lex >> freespacing_;
269                         break;
270                 case IL_NEEDPROTECT:
271                         lex >> needprotect_;
272                         break;
273                 case IL_CONTENTASLABEL:
274                         lex >> contentaslabel_;
275                         break;
276                 case IL_COPYSTYLE: {
277                         // initialize with a known style
278                         docstring style;
279                         lex >> style;
280                         style = support::subst(style, '_', ' ');
281
282                         // We don't want to apply the algorithm in DocumentClass::insetLayout()
283                         // here. So we do it the long way.
284                         TextClass::InsetLayouts::const_iterator it = 
285                                         tclass.insetLayouts().find(style);
286                         if (it != tclass.insetLayouts().end()) {
287                                 docstring const tmpname = name_;
288                                 this->operator=(it->second);
289                                 name_ = tmpname;
290                         } else {
291                                 LYXERR0("Cannot copy unknown InsetLayout `"
292                                         << style << "'\n"
293                                         << "All InsetLayouts so far:");
294                                 TextClass::InsetLayouts::const_iterator lit = 
295                                                 tclass.insetLayouts().begin();
296                                 TextClass::InsetLayouts::const_iterator len = 
297                                                 tclass.insetLayouts().end();
298                                 for (; lit != len; ++lit)
299                                         lyxerr << lit->second.name() << "\n";
300                         }
301                         break;
302                 }
303
304                 case IL_FONT: {
305                         font_ = lyxRead(lex, inherit_font);
306                         // If you want to define labelfont, you need to do so after
307                         // font is defined.
308                         labelfont_ = font_;
309                         break;
310                 }
311                 case IL_BGCOLOR:
312                         lex >> tmp;
313                         bgcolor_ = lcolor.getFromLyXName(tmp);
314                         break;
315                 case IL_PREAMBLE:
316                         preamble_ = from_utf8(lex.getLongString("EndPreamble"));
317                         break;
318                 case IL_BABELPREAMBLE:
319                         babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
320                         break;
321                 case IL_LANGPREAMBLE:
322                         langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
323                         break;
324                 case IL_REFPREFIX:
325                         lex >> refprefix_;
326                         break;
327                 case IL_HTMLTAG:
328                         lex >> htmltag_;
329                         break;
330                 case IL_HTMLATTR:
331                         lex >> htmlattr_;
332                         break;
333                 case IL_HTMLFORCECSS:
334                         lex >> htmlforcecss_;
335                         break;
336                 case IL_HTMLINNERTAG:
337                         lex >> htmlinnertag_;
338                         break;
339                 case IL_HTMLINNERATTR:
340                         lex >> htmlinnerattr_;
341                         break;
342                 case IL_HTMLLABEL:
343                         lex >> htmllabel_;
344                         break;
345                 case IL_HTMLISBLOCK:
346                         lex >> htmlisblock_;
347                         break;
348                 case IL_HTMLSTYLE:
349                         htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
350                         break;
351                 case IL_HTMLPREAMBLE:
352                         htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
353                         break;
354                 case IL_REQUIRES: {
355                         lex.eatLine();
356                         vector<string> const req 
357                                 = support::getVectorFromString(lex.getString());
358                         requires_.insert(req.begin(), req.end());
359                         break;
360                 }
361                 case IL_SPELLCHECK:
362                         lex >> spellcheck_;
363                         break;
364                 case IL_RESETSFONT:
365                         lex >> resetsfont_;
366                         break;
367                 case IL_DISPLAY:
368                         lex >> display_;
369                         break;
370                 case IL_END:
371                         getout = true;
372                         break;
373                 }
374         }
375
376         // Here add element to list if getout == true
377         if (!getout)
378                 return false;
379         
380         // The label font is generally used as-is without
381         // any realization against a given context.
382         labelfont_.realize(sane_font);
383
384         lex.popTable();
385         return true;
386 }
387
388
389 InsetLayout::InsetLyXType translateLyXType(std::string const & str) 
390 {
391         
392         if (support::compare_ascii_no_case(str, "charstyle") == 0)
393                 return InsetLayout::CHARSTYLE;
394         if (support::compare_ascii_no_case(str, "custom") == 0)
395                 return InsetLayout::CUSTOM;
396         if (support::compare_ascii_no_case(str, "element") == 0)
397                 return InsetLayout::ELEMENT;
398         if (support::compare_ascii_no_case(str, "end") == 0)
399                 return InsetLayout::END;
400         if (support::compare_ascii_no_case(str, "standard") == 0)
401                 return InsetLayout::STANDARD;
402         return InsetLayout::NOLYXTYPE;
403 }
404
405
406 string const & InsetLayout::htmltag() const
407 {
408         if (htmltag_.empty())
409                 htmltag_ = multipar_ ? "div" : "span";
410         return htmltag_; 
411 }
412
413
414 string const & InsetLayout::htmlattr() const
415 {
416         if (htmlattr_.empty())
417                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
418         return htmlattr_; 
419 }
420
421
422 string const & InsetLayout::htmlinnerattr() const
423 {
424         if (htmlinnerattr_.empty())
425                 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
426         return htmlinnerattr_; 
427 }
428
429
430 string InsetLayout::defaultCSSClass() const
431
432         if (!defaultcssclass_.empty())
433                 return defaultcssclass_;
434         string d;
435         string n = to_utf8(name());
436         string::const_iterator it = n.begin();
437         string::const_iterator en = n.end();
438         for (; it != en; ++it) {
439                 if (!isalpha(*it))
440                         d += "_";
441                 else if (islower(*it))
442                         d += *it;
443                 else
444                         d += support::lowercase(*it);
445         }
446         // are there other characters we need to remove?
447         defaultcssclass_ = d;
448         return defaultcssclass_;
449 }
450
451
452 void InsetLayout::makeDefaultCSS() const
453 {
454         if (!htmldefaultstyle_.empty()) 
455                 return;
456         docstring const mainfontCSS = font_.asCSS();
457         if (!mainfontCSS.empty())
458                 htmldefaultstyle_ = 
459                                 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
460                                 mainfontCSS + from_ascii("\n}\n");
461 }
462
463
464 docstring InsetLayout::htmlstyle() const 
465
466         if (!htmlstyle_.empty() && !htmlforcecss_)
467                 return htmlstyle_;
468         if (htmldefaultstyle_.empty())
469                 makeDefaultCSS();
470         docstring retval = htmldefaultstyle_;
471         if (!htmlstyle_.empty())
472                 retval += '\n' + htmlstyle_ + '\n';
473         return retval;
474 }
475
476
477 } //namespace lyx