]> git.lyx.org Git - lyx.git/blob - src/Layout.cpp
add new lyx funcs introduced in r34598 to doxy
[lyx.git] / src / Layout.cpp
1 /**
2  * \file Layout.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author André Pönitz
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "Layout.h"
16 #include "Language.h"
17 #include "TextClass.h"
18 #include "Lexer.h"
19 #include "FontInfo.h"
20
21 #include "support/Messages.h"
22 #include "support/debug.h"
23 #include "support/lassert.h"
24 #include "support/lstrings.h"
25
26 #include <boost/regex.hpp>
27
28 using namespace std;
29 using namespace lyx::support;
30
31 namespace lyx {
32
33 /// Special value of toclevel for layouts that to not belong in a TOC
34 const int Layout::NOT_IN_TOC = -1000;
35
36 //  The order of the LayoutTags enum is no more important. [asierra300396]
37 // Tags indexes.
38 enum LayoutTags {
39         LT_ALIGN = 1,
40         LT_ALIGNPOSSIBLE,
41         LT_MARGIN,
42         LT_BOTTOMSEP,
43         LT_CATEGORY,
44         LT_COMMANDDEPTH,
45         LT_COPYSTYLE,
46         LT_DEPENDSON,
47         LT_OBSOLETEDBY,
48         //LT_EMPTY,
49         LT_END,
50         //LT_ENVIRONMENT_DEFAULT,
51         //LT_FANCYHDR,
52         LT_FILL_BOTTOM,
53         LT_FILL_TOP,
54         //LT_FIRST_COUNTER,
55         LT_FONT,
56         LT_FREE_SPACING,
57         LT_PASS_THRU,
58         //LT_HEADINGS,
59         LT_ITEMSEP,
60         LT_KEEPEMPTY,
61         LT_LABEL_BOTTOMSEP,
62         LT_LABELFONT,
63         LT_TEXTFONT,
64         LT_LABELINDENT,
65         LT_LABELSEP,
66         LT_LABELSTRING,
67         LT_LABELSTRING_APPENDIX,
68         LT_LABELCOUNTER,
69         LT_LABELTYPE,
70         LT_ENDLABELSTRING,
71         LT_ENDLABELTYPE,
72         LT_LATEXNAME,
73         LT_LATEXPARAM,
74         LT_OPTARGS,
75         LT_LATEXTYPE,
76         LT_LEFTMARGIN,
77         LT_NEED_PROTECT,
78         LT_NEWLINE,
79         LT_NEXTNOINDENT,
80         LT_PARINDENT,
81         LT_PARSEP,
82         LT_PARSKIP,
83         //LT_PLAIN,
84         LT_PREAMBLE,
85         LT_LANGPREAMBLE,
86         LT_BABELPREAMBLE,
87         LT_REQUIRES,
88         LT_RIGHTMARGIN,
89         LT_SPACING,
90         LT_TOPSEP,
91         LT_TOCLEVEL,
92         LT_INNERTAG,
93         LT_LABELTAG,
94         LT_ITEMTAG,
95         LT_HTMLTAG,
96         LT_HTMLATTR,
97         LT_HTMLITEM,
98         LT_HTMLITEMATTR,
99         LT_HTMLLABEL,
100         LT_HTMLLABELATTR, 
101         LT_HTMLLABELFIRST,
102         LT_HTMLPREAMBLE,
103         LT_HTMLSTYLE,
104         LT_HTMLFORCECSS,
105         LT_INPREAMBLE,
106         LT_HTMLTITLE,
107         LT_SPELLCHECK,
108         LT_REFPREFIX,
109         LT_REQARGS,
110         LT_INTITLE // keep this last!
111 };
112
113 /////////////////////
114
115 Layout::Layout()
116 {
117         unknown_ = false;
118         margintype = MARGIN_STATIC;
119         latextype = LATEX_PARAGRAPH;
120         intitle = false;
121         inpreamble = false;
122         needprotect = false;
123         keepempty = false;
124         font = inherit_font;
125         labelfont = inherit_font;
126         resfont = sane_font;
127         reslabelfont = sane_font;
128         nextnoindent = false;
129         parskip = 0.0;
130         itemsep = 0;
131         topsep = 0.0;
132         bottomsep = 0.0;
133         labelbottomsep = 0.0;
134         parsep = 0;
135         align = LYX_ALIGN_BLOCK;
136         alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
137         labeltype = LABEL_NO_LABEL;
138         endlabeltype = END_LABEL_NO_LABEL;
139         // Should or should not. That is the question.
140         // spacing.set(Spacing::OneHalf);
141         fill_top = false;
142         fill_bottom = false;
143         newline_allowed = true;
144         free_spacing = false;
145         pass_thru = false;
146         toclevel = NOT_IN_TOC;
147         commanddepth = 0;
148         htmllabelfirst_ = false;
149         htmlforcecss_ = false;
150         htmltitle_ = false;
151         spellcheck = true;
152         optargs = 0;
153         reqargs = 0;
154 }
155
156
157 bool Layout::read(Lexer & lex, TextClass const & tclass)
158 {
159         // This table is sorted alphabetically [asierra 30March96]
160         LexerKeyword layoutTags[] = {
161                 { "align",          LT_ALIGN },
162                 { "alignpossible",  LT_ALIGNPOSSIBLE },
163                 { "babelpreamble",  LT_BABELPREAMBLE },
164                 { "bottomsep",      LT_BOTTOMSEP },
165                 { "category",       LT_CATEGORY },
166                 { "commanddepth",   LT_COMMANDDEPTH },
167                 { "copystyle",      LT_COPYSTYLE },
168                 { "dependson",      LT_DEPENDSON },
169                 { "end",            LT_END },
170                 { "endlabelstring", LT_ENDLABELSTRING },
171                 { "endlabeltype",   LT_ENDLABELTYPE },
172                 { "fill_bottom",    LT_FILL_BOTTOM },
173                 { "fill_top",       LT_FILL_TOP },
174                 { "font",           LT_FONT },
175                 { "freespacing",    LT_FREE_SPACING },
176                 { "htmlattr",       LT_HTMLATTR },
177                 { "htmlforcecss",   LT_HTMLFORCECSS },
178                 { "htmlitem",       LT_HTMLITEM },
179                 { "htmlitemattr",   LT_HTMLITEMATTR },
180                 { "htmllabel",      LT_HTMLLABEL },
181                 { "htmllabelattr",  LT_HTMLLABELATTR },
182                 { "htmllabelfirst", LT_HTMLLABELFIRST },
183                 { "htmlpremable",   LT_HTMLPREAMBLE },
184                 { "htmlstyle",      LT_HTMLSTYLE },
185                 { "htmltag",        LT_HTMLTAG },
186                 { "htmltitle",      LT_HTMLTITLE },
187                 { "innertag",       LT_INNERTAG },
188                 { "inpreamble",     LT_INPREAMBLE },
189                 { "intitle",        LT_INTITLE },
190                 { "itemsep",        LT_ITEMSEP },
191                 { "itemtag",        LT_ITEMTAG },
192                 { "keepempty",      LT_KEEPEMPTY },
193                 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
194                 { "labelcounter",   LT_LABELCOUNTER },
195                 { "labelfont",      LT_LABELFONT },
196                 { "labelindent",    LT_LABELINDENT },
197                 { "labelsep",       LT_LABELSEP },
198                 { "labelstring",    LT_LABELSTRING },
199                 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
200                 { "labeltag",       LT_LABELTAG },
201                 { "labeltype",      LT_LABELTYPE },
202                 { "langpreamble",   LT_LANGPREAMBLE },
203                 { "latexname",      LT_LATEXNAME },
204                 { "latexparam",     LT_LATEXPARAM },
205                 { "latextype",      LT_LATEXTYPE },
206                 { "leftmargin",     LT_LEFTMARGIN },
207                 { "margin",         LT_MARGIN },
208                 { "needprotect",    LT_NEED_PROTECT },
209                 { "newline",        LT_NEWLINE },
210                 { "nextnoindent",   LT_NEXTNOINDENT },
211                 { "obsoletedby",    LT_OBSOLETEDBY },
212                 { "optionalargs",   LT_OPTARGS },
213                 { "parindent",      LT_PARINDENT },
214                 { "parsep",         LT_PARSEP },
215                 { "parskip",        LT_PARSKIP },
216                 { "passthru",       LT_PASS_THRU },
217                 { "preamble",       LT_PREAMBLE },
218                 { "refprefix",      LT_REFPREFIX },
219                 { "requiredargs",   LT_REQARGS },
220                 { "requires",       LT_REQUIRES },
221                 { "rightmargin",    LT_RIGHTMARGIN },
222                 { "spacing",        LT_SPACING },
223                 { "spellcheck",     LT_SPELLCHECK },
224                 { "textfont",       LT_TEXTFONT },
225                 { "toclevel",       LT_TOCLEVEL },
226                 { "topsep",         LT_TOPSEP }
227         };
228
229         bool error = false;
230         bool finished = false;
231         lex.pushTable(layoutTags);
232         // parse style section
233         while (!finished && lex.isOK() && !error) {
234                 int le = lex.lex();
235                 // See comment in LyXRC.cpp.
236                 switch (le) {
237                 case Lexer::LEX_FEOF:
238                         continue;
239
240                 case Lexer::LEX_UNDEF:          // parse error
241                         lex.printError("Unknown layout tag `$$Token'");
242                         error = true;
243                         continue;
244
245                 default: 
246                         break;
247                 }
248                 switch (static_cast<LayoutTags>(le)) {
249                 case LT_END:            // end of structure
250                         finished = true;
251                         break;
252
253                 case LT_CATEGORY:
254                         lex >> category_;
255                         break;
256
257                 case LT_COPYSTYLE: {     // initialize with a known style
258                         docstring style;
259                         lex >> style;
260                         style = subst(style, '_', ' ');
261
262                         if (tclass.hasLayout(style)) {
263                                 docstring const tmpname = name_;
264                                 this->operator=(tclass[style]);
265                                 name_ = tmpname;
266                         } else {
267                                 LYXERR0("Cannot copy unknown style `"
268                                         << style << "'\n"
269                                         << "All layouts so far:");
270                                 DocumentClass::const_iterator lit = tclass.begin();
271                                 DocumentClass::const_iterator len = tclass.end();
272                                 for (; lit != len; ++lit)
273                                         LYXERR0(lit->name());
274                         }
275                         break;
276                         }
277
278                 case LT_OBSOLETEDBY: {   // replace with a known style
279                         docstring style;
280                         lex >> style;
281                         style = subst(style, '_', ' ');
282
283                         if (tclass.hasLayout(style)) {
284                                 docstring const tmpname = name_;
285                                 this->operator=(tclass[style]);
286                                 name_ = tmpname;
287                                 if (obsoleted_by().empty())
288                                         obsoleted_by_ = style;
289                         } else {
290                                 LYXERR0("Cannot replace with unknown style `" 
291                                         << style << '\'');
292
293                                 //lex.printError("Cannot replace with"
294                                 //               " unknown style "
295                                 //               "`$$Token'");
296                         }
297                         break;
298                 }
299
300                 case LT_DEPENDSON:
301                         lex >> depends_on_;
302                         depends_on_ = subst(depends_on_, '_', ' ');
303                         break;
304
305                 case LT_MARGIN:         // margin style definition.
306                         readMargin(lex);
307                         break;
308
309                 case LT_LATEXTYPE:      // LaTeX style definition.
310                         readLatexType(lex);
311                         break;
312
313                 case LT_INTITLE:
314                         lex >> intitle;
315                         break;
316
317                 case LT_INPREAMBLE:
318                         lex >> inpreamble;
319                         break;
320
321                 case LT_TOCLEVEL:
322                         lex >> toclevel;
323                         break;
324
325                 case LT_OPTARGS:
326                         lex >> optargs;
327                         break;
328
329                 case LT_REQARGS:
330                         lex >> reqargs;
331                         break;
332
333                 case LT_NEED_PROTECT:
334                         lex >> needprotect;
335                         break;
336
337                 case LT_KEEPEMPTY:
338                         lex >> keepempty;
339                         break;
340
341                 case LT_FONT:
342                         font = lyxRead(lex, font);
343                         labelfont = font;
344                         break;
345
346                 case LT_TEXTFONT:
347                         font = lyxRead(lex, font);
348                         break;
349
350                 case LT_LABELFONT:
351                         labelfont = lyxRead(lex, labelfont);
352                         break;
353
354                 case LT_NEXTNOINDENT:   // Indent next paragraph?
355                         lex >> nextnoindent;
356                         break;
357
358                 case LT_COMMANDDEPTH:
359                         lex >> commanddepth;
360                         break;
361
362                 case LT_LATEXNAME:
363                         lex >> latexname_;
364                         break;
365
366                 case LT_LATEXPARAM:
367                         lex >> latexparam_;
368                         latexparam_ = subst(latexparam_, "&quot;", "\"");
369                         break;
370
371                 case LT_INNERTAG:
372                         lex >> innertag_;
373                         break;
374
375                 case LT_LABELTAG:
376                         lex >> labeltag_;
377                         break;
378
379                 case LT_ITEMTAG:
380                         lex >> itemtag_;
381                         break;
382
383                 case LT_PREAMBLE:
384                         preamble_ = from_utf8(lex.getLongString("EndPreamble"));
385                         break;
386
387                 case LT_LANGPREAMBLE:
388                         langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
389                         break;
390
391                 case LT_BABELPREAMBLE:
392                         babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
393                         break;
394
395                 case LT_LABELTYPE:
396                         readLabelType(lex);
397                         break;
398
399                 case LT_ENDLABELTYPE:
400                         readEndLabelType(lex);
401                         break;
402
403                 case LT_LEFTMARGIN:     // left margin type
404                         lex >> leftmargin;
405                         break;
406
407                 case LT_RIGHTMARGIN:    // right margin type
408                         lex >> rightmargin;
409                         break;
410
411                 case LT_LABELINDENT:    // label indenting flag
412                         lex >> labelindent;
413                         break;
414
415                 case LT_PARINDENT:      // paragraph indent. flag
416                         lex >> parindent;
417                         break;
418
419                 case LT_PARSKIP:        // paragraph skip size
420                         lex >> parskip;
421                         break;
422
423                 case LT_ITEMSEP:        // item separation size
424                         lex >> itemsep;
425                         break;
426
427                 case LT_TOPSEP:         // top separation size
428                         lex >> topsep;
429                         break;
430
431                 case LT_BOTTOMSEP:      // bottom separation size
432                         lex >> bottomsep;
433                         break;
434
435                 case LT_LABEL_BOTTOMSEP: // label bottom separation size
436                         lex >> labelbottomsep;
437                         break;
438
439                 case LT_LABELSEP:       // label separator
440                         lex >> labelsep;
441                         labelsep = subst(labelsep, 'x', ' ');
442                         break;
443
444                 case LT_PARSEP:         // par. separation size
445                         lex >> parsep;
446                         break;
447
448                 case LT_FILL_TOP:       // fill top flag
449                         lex >> fill_top;
450                         break;
451
452                 case LT_FILL_BOTTOM:    // fill bottom flag
453                         lex >> fill_bottom;
454                         break;
455
456                 case LT_NEWLINE:        // newlines allowed?
457                         lex >> newline_allowed;
458                         break;
459
460                 case LT_ALIGN:          // paragraph align
461                         readAlign(lex);
462                         break;
463                 case LT_ALIGNPOSSIBLE:  // paragraph allowed align
464                         readAlignPossible(lex);
465                         break;
466
467                 case LT_LABELSTRING:    // label string definition
468                         // FIXME: this means LT_ENDLABELSTRING may only
469                         // occur after LT_LABELSTRING
470                         lex >> labelstring_;
471                         labelstring_ = trim(labelstring_);
472                         labelstring_appendix_ = labelstring_;
473                         break;
474
475                 case LT_ENDLABELSTRING: // endlabel string definition
476                         lex >> endlabelstring_; 
477                         endlabelstring_ = trim(endlabelstring_);
478                         break;
479
480                 case LT_LABELSTRING_APPENDIX: // label string appendix definition
481                         lex >> labelstring_appendix_;   
482                         labelstring_appendix_ = trim(labelstring_appendix_);
483                         break;
484
485                 case LT_LABELCOUNTER: // name of counter to use
486                         lex >> counter; 
487                         counter = trim(counter);
488                         break;
489
490                 case LT_FREE_SPACING:   // Allow for free spacing.
491                         lex >> free_spacing;
492                         break;
493
494                 case LT_PASS_THRU:      // Allow for pass thru.
495                         lex >> pass_thru;
496                         break;
497
498                 case LT_SPACING: // setspace.sty
499                         readSpacing(lex);
500                         break;
501
502                 case LT_REQUIRES: {
503                         lex.eatLine();
504                         vector<string> const req = 
505                                 getVectorFromString(lex.getString());
506                         requires_.insert(req.begin(), req.end());
507                         break;
508                 }
509                         
510                 case LT_REFPREFIX:
511                         lex >> refprefix;
512                         break;
513
514                 case LT_HTMLTAG:
515                         lex >> htmltag_;
516                         break;
517         
518                 case LT_HTMLATTR:
519                         lex >> htmlattr_;
520                         break;
521
522                 case LT_HTMLITEM:
523                         lex >> htmlitemtag_;
524                         break;
525         
526                 case LT_HTMLITEMATTR:
527                         lex >> htmlitemattr_;
528                         break;
529         
530                 case LT_HTMLLABEL:
531                         lex >> htmllabeltag_;
532                         break;
533
534                 case LT_HTMLLABELATTR: 
535                         lex >> htmllabelattr_;
536                         break;
537
538                 case LT_HTMLLABELFIRST:
539                         lex >> htmllabelfirst_;
540                         break;
541                         
542                 case LT_HTMLSTYLE:
543                         htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
544                         break;
545
546                 case LT_HTMLFORCECSS:
547                         lex >> htmlforcecss_;
548
549                 case LT_HTMLPREAMBLE:
550                         htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
551                         break;
552                 
553                 case LT_HTMLTITLE:
554                         lex >> htmltitle_;
555                         break;
556
557                 case LT_SPELLCHECK:
558                         lex >> spellcheck;
559                         break;
560                 }
561         }
562         lex.popTable();
563
564         return !error;
565 }
566
567
568 enum {
569         AT_BLOCK = 1,
570         AT_LEFT,
571         AT_RIGHT,
572         AT_CENTER,
573         AT_LAYOUT
574 };
575
576
577 LexerKeyword alignTags[] = {
578         { "block",  AT_BLOCK },
579         { "center", AT_CENTER },
580         { "layout", AT_LAYOUT },
581         { "left",   AT_LEFT },
582         { "right",  AT_RIGHT }
583 };
584
585
586 void Layout::readAlign(Lexer & lex)
587 {
588         PushPopHelper pph(lex, alignTags);
589         int le = lex.lex();
590         switch (le) {
591         case Lexer::LEX_UNDEF:
592                 lex.printError("Unknown alignment `$$Token'");
593                 return;
594         default: break;
595         };
596         switch (le) {
597         case AT_BLOCK:
598                 align = LYX_ALIGN_BLOCK;
599                 break;
600         case AT_LEFT:
601                 align = LYX_ALIGN_LEFT;
602                 break;
603         case AT_RIGHT:
604                 align = LYX_ALIGN_RIGHT;
605                 break;
606         case AT_CENTER:
607                 align = LYX_ALIGN_CENTER;
608                 break;
609         case AT_LAYOUT:
610                 align = LYX_ALIGN_LAYOUT;
611                 break;
612         }
613 }
614
615
616 void Layout::readAlignPossible(Lexer & lex)
617 {
618         lex.pushTable(alignTags);
619         alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
620         int lineno = lex.lineNumber();
621         do {
622                 int le = lex.lex();
623                 switch (le) {
624                 case Lexer::LEX_UNDEF:
625                         lex.printError("Unknown alignment `$$Token'");
626                         continue;
627                 default: break;
628                 };
629                 switch (le) {
630                 case AT_BLOCK:
631                         alignpossible |= LYX_ALIGN_BLOCK;
632                         break;
633                 case AT_LEFT:
634                         alignpossible |= LYX_ALIGN_LEFT;
635                         break;
636                 case AT_RIGHT:
637                         alignpossible |= LYX_ALIGN_RIGHT;
638                         break;
639                 case AT_CENTER:
640                         alignpossible |= LYX_ALIGN_CENTER;
641                         break;
642                 case AT_LAYOUT:
643                         alignpossible |= LYX_ALIGN_LAYOUT;
644                         break;
645                 }
646         } while (lineno == lex.lineNumber());
647         lex.popTable();
648 }
649
650
651 void Layout::readLabelType(Lexer & lex)
652 {
653         enum {
654                 LA_NO_LABEL = 1,
655                 LA_MANUAL,
656                 LA_TOP_ENVIRONMENT,
657                 LA_CENTERED_TOP_ENVIRONMENT,
658                 LA_STATIC,
659                 LA_SENSITIVE,
660                 LA_COUNTER,
661                 LA_ENUMERATE,
662                 LA_ITEMIZE,
663                 LA_BIBLIO
664         };
665
666
667         LexerKeyword labelTypeTags[] = {
668                 { "bibliography",             LA_BIBLIO },
669                 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
670                 { "counter",                  LA_COUNTER },
671                 { "enumerate",                LA_ENUMERATE },
672                 { "itemize",                  LA_ITEMIZE },
673                 { "manual",                   LA_MANUAL },
674                 { "no_label",                 LA_NO_LABEL },
675                 { "sensitive",                LA_SENSITIVE },
676                 { "static",                   LA_STATIC },
677                 { "top_environment",          LA_TOP_ENVIRONMENT }
678         };
679
680         PushPopHelper pph(lex, labelTypeTags);
681         int le = lex.lex();
682         switch (le) {
683         case Lexer::LEX_UNDEF:
684                 lex.printError("Unknown labeltype tag `$$Token'");
685                 return;
686         default: break;
687         }
688         switch (le) {
689         case LA_NO_LABEL:
690                 labeltype = LABEL_NO_LABEL;
691                 break;
692         case LA_MANUAL:
693                 labeltype = LABEL_MANUAL;
694                 break;
695         case LA_TOP_ENVIRONMENT:
696                 labeltype = LABEL_TOP_ENVIRONMENT;
697                 break;
698         case LA_CENTERED_TOP_ENVIRONMENT:
699                 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
700                 break;
701         case LA_STATIC:
702                 labeltype = LABEL_STATIC;
703                 break;
704         case LA_SENSITIVE:
705                 labeltype = LABEL_SENSITIVE;
706                 break;
707         case LA_COUNTER:
708                 labeltype = LABEL_COUNTER;
709                 break;
710         case LA_ENUMERATE:
711                 labeltype = LABEL_ENUMERATE;
712                 break;
713         case LA_ITEMIZE:
714                 labeltype = LABEL_ITEMIZE;
715                 break;
716         case LA_BIBLIO:
717                 labeltype = LABEL_BIBLIO;
718                 break;
719         }
720 }
721
722
723 void Layout::readEndLabelType(Lexer & lex)
724 {
725         static LexerKeyword endlabelTypeTags[] = {
726                 { "box",              END_LABEL_BOX },
727                 { "filled_box", END_LABEL_FILLED_BOX },
728                 { "no_label",     END_LABEL_NO_LABEL },
729                 { "static",     END_LABEL_STATIC }
730         };
731
732         PushPopHelper pph(lex, endlabelTypeTags);
733         int le = lex.lex();
734         switch (le) {
735         case Lexer::LEX_UNDEF:
736                 lex.printError("Unknown labeltype tag `$$Token'");
737                 break;
738         case END_LABEL_STATIC:
739         case END_LABEL_BOX:
740         case END_LABEL_FILLED_BOX:
741         case END_LABEL_NO_LABEL:
742                 endlabeltype = static_cast<EndLabelType>(le);
743                 break;
744         default:
745                 LYXERR0("Unhandled value " << le);
746                 break;
747         }
748 }
749
750
751 void Layout::readMargin(Lexer & lex)
752 {
753         LexerKeyword marginTags[] = {
754                 { "dynamic",           MARGIN_DYNAMIC },
755                 { "first_dynamic",     MARGIN_FIRST_DYNAMIC },
756                 { "manual",            MARGIN_MANUAL },
757                 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
758                 { "static",            MARGIN_STATIC }
759         };
760
761         PushPopHelper pph(lex, marginTags);
762
763         int le = lex.lex();
764         switch (le) {
765         case Lexer::LEX_UNDEF:
766                 lex.printError("Unknown margin type tag `$$Token'");
767                 return;
768         case MARGIN_STATIC:
769         case MARGIN_MANUAL:
770         case MARGIN_DYNAMIC:
771         case MARGIN_FIRST_DYNAMIC:
772         case MARGIN_RIGHT_ADDRESS_BOX:
773                 margintype = static_cast<MarginType>(le);
774                 break;
775         default:
776                 LYXERR0("Unhandled value " << le);
777                 break;
778         }
779 }
780
781
782 void Layout::readLatexType(Lexer & lex)
783 {
784         LexerKeyword latexTypeTags[] = {
785                 { "bib_environment",  LATEX_BIB_ENVIRONMENT },
786                 { "command",          LATEX_COMMAND },
787                 { "environment",      LATEX_ENVIRONMENT },
788                 { "item_environment", LATEX_ITEM_ENVIRONMENT },
789                 { "list_environment", LATEX_LIST_ENVIRONMENT },
790                 { "paragraph",        LATEX_PARAGRAPH }
791         };
792
793         PushPopHelper pph(lex, latexTypeTags);
794         int le = lex.lex();
795         switch (le) {
796         case Lexer::LEX_UNDEF:
797                 lex.printError("Unknown latextype tag `$$Token'");
798                 return;
799         case LATEX_PARAGRAPH:
800         case LATEX_COMMAND:
801         case LATEX_ENVIRONMENT:
802         case LATEX_ITEM_ENVIRONMENT:
803         case LATEX_BIB_ENVIRONMENT:
804         case LATEX_LIST_ENVIRONMENT:
805                 latextype = static_cast<LatexType>(le);
806                 break;
807         default:
808                 LYXERR0("Unhandled value " << le);
809                 break;
810         }
811 }
812
813
814 void Layout::readSpacing(Lexer & lex)
815 {
816         enum {
817                 ST_SPACING_SINGLE = 1,
818                 ST_SPACING_ONEHALF,
819                 ST_SPACING_DOUBLE,
820                 ST_OTHER
821         };
822
823         LexerKeyword spacingTags[] = {
824                 {"double",  ST_SPACING_DOUBLE },
825                 {"onehalf", ST_SPACING_ONEHALF },
826                 {"other",   ST_OTHER },
827                 {"single",  ST_SPACING_SINGLE }
828         };
829
830         PushPopHelper pph(lex, spacingTags);
831         int le = lex.lex();
832         switch (le) {
833         case Lexer::LEX_UNDEF:
834                 lex.printError("Unknown spacing token `$$Token'");
835                 return;
836         default: break;
837         }
838         switch (le) {
839         case ST_SPACING_SINGLE:
840                 spacing.set(Spacing::Single);
841                 break;
842         case ST_SPACING_ONEHALF:
843                 spacing.set(Spacing::Onehalf);
844                 break;
845         case ST_SPACING_DOUBLE:
846                 spacing.set(Spacing::Double);
847                 break;
848         case ST_OTHER:
849                 lex.next();
850                 spacing.set(Spacing::Other, lex.getString());
851                 break;
852         }
853 }
854
855
856 namespace {
857
858 docstring const i18npreamble(Language const * lang, docstring const & templ)
859 {
860         if (templ.empty())
861                 return templ;
862
863         string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
864
865 #ifdef TEX2LYX
866         // tex2lyx does not have getMessages()
867         LASSERT(false, /**/);
868 #else
869         // FIXME UNICODE
870         // boost::regex is not unicode-safe.
871         // Should use QRegExp or (boost::u32regex, but that requires ICU)
872         static boost::regex const reg("_\\(([^\\)]+)\\)");
873         boost::smatch sub;
874         while (boost::regex_search(preamble, sub, reg)) {
875                 string const key = sub.str(1);
876                 string translated;
877                 if (isAscii(key))
878                         translated = to_utf8(getMessages(lang->code()).get(key));
879                 else {
880                         lyxerr << "Warning: not translating `" << key
881                                << "' because it is not pure ASCII." << endl;
882                         translated = key;
883                 }
884                 preamble = subst(preamble, sub.str(), translated);
885         }
886 #endif
887         return from_utf8(preamble);
888 }
889
890 }
891
892
893 docstring const Layout::langpreamble(Language const * lang) const
894 {
895         return i18npreamble(lang, langpreamble_);
896 }
897
898
899 docstring const Layout::babelpreamble(Language const * lang) const
900 {
901         return i18npreamble(lang, babelpreamble_);
902 }
903
904
905 string const & Layout::htmltag() const 
906
907         if (htmltag_.empty())
908                 htmltag_ =  "div";
909         return htmltag_;
910 }
911
912
913 string const & Layout::htmlattr() const 
914
915         if (htmlattr_.empty())
916                 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
917         return htmlattr_; 
918 }
919
920
921 string const & Layout::htmlitemtag() const 
922
923         if (htmlitemtag_.empty())
924                 htmlitemtag_ = "div";
925         return htmlitemtag_; 
926 }
927
928
929 string const & Layout::htmlitemattr() const 
930
931         if (htmlitemattr_.empty())
932                 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
933         return htmlitemattr_; 
934 }
935
936
937 string const & Layout::htmllabeltag() const 
938
939         if (htmllabeltag_.empty())
940                 htmllabeltag_ = "span";
941         return htmllabeltag_; 
942 }
943
944
945 string const & Layout::htmllabelattr() const 
946
947         if (htmllabelattr_.empty())
948                 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
949         return htmllabelattr_; 
950 }
951
952
953 docstring Layout::htmlstyle() const {
954         if (!htmlstyle_.empty() && !htmlforcecss_)
955                 return htmlstyle_;
956         if (htmldefaultstyle_.empty()) 
957                 makeDefaultCSS();
958         docstring retval = htmldefaultstyle_;
959         if (!htmlstyle_.empty())
960                 retval += '\n' + htmlstyle_;
961         return retval;
962 }
963
964
965 string Layout::defaultCSSClass() const
966
967         if (!defaultcssclass_.empty())
968                 return defaultcssclass_;
969         docstring d;
970         docstring::const_iterator it = name().begin();
971         docstring::const_iterator en = name().end();
972         for (; it != en; ++it) {
973                 if (!isalpha(*it))
974                         continue;
975                 if (islower(*it))
976                         d += *it;
977                 else 
978                         d += lowercase(*it);
979         }
980         // are there other characters we need to remove?
981         defaultcssclass_ = to_utf8(d);
982         return defaultcssclass_;
983 }
984
985
986 // NOTE There is a whole ton of stuff that could go into this.
987 // Things like bottomsep, topsep, and parsep could become various
988 // sorts of margins or padding, for example. But for now we are
989 // going to keep it simple.
990 void Layout::makeDefaultCSS() const {
991         // this never needs to be redone, since reloading layouts will
992         // wipe out what we did before.
993         if (!htmldefaultstyle_.empty()) 
994                 return;
995         docstring const mainfontCSS = font.asCSS();
996         if (!mainfontCSS.empty())
997                 htmldefaultstyle_ = 
998                         from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
999                         mainfontCSS + from_ascii("\n}\n");
1000         if (labelfont == font || htmllabeltag() == "NONE")
1001                 return;
1002         docstring const labelfontCSS = labelfont.asCSS();
1003         if (!labelfontCSS.empty())
1004                 htmldefaultstyle_ +=
1005                         from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1006                         labelfontCSS + from_ascii("\n}\n");
1007 }
1008
1009
1010 bool Layout::operator==(Layout const & rhs) const
1011 {
1012         // This is enough for the applications we actually make,
1013         // at least at the moment. But we could check more.
1014         return name() == rhs.name()
1015                 && latexname() == rhs.latexname()
1016                 && latextype == rhs.latextype;
1017 }
1018
1019
1020 } // namespace lyx