3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
7 * \author Jean-Marc Lasgouttes
10 * Full author contact details are available in file CREDITS.
17 #include "TextClass.h"
21 #include "support/Messages.h"
22 #include "support/debug.h"
23 #include "support/lassert.h"
24 #include "support/lstrings.h"
26 #include "support/regex.h"
29 using namespace lyx::support;
33 /// Special value of toclevel for layouts that to not belong in a TOC
34 const int Layout::NOT_IN_TOC = -1000;
36 // The order of the LayoutTags enum is no more important. [asierra300396]
50 //LT_ENVIRONMENT_DEFAULT,
67 LT_LABELSTRING_APPENDIX,
110 LT_INTITLE // keep this last!
113 /////////////////////
118 margintype = MARGIN_STATIC;
119 latextype = LATEX_PARAGRAPH;
125 labelfont = inherit_font;
127 reslabelfont = sane_font;
128 nextnoindent = false;
133 labelbottomsep = 0.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);
143 newline_allowed = true;
144 free_spacing = false;
146 toclevel = NOT_IN_TOC;
148 htmllabelfirst_ = false;
149 htmlforcecss_ = false;
157 bool Layout::read(Lexer & lex, TextClass const & tclass)
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 },
170 { "endlabelstring", LT_ENDLABELSTRING },
171 { "endlabeltype", LT_ENDLABELTYPE },
172 { "fill_bottom", LT_FILL_BOTTOM },
173 { "fill_top", LT_FILL_TOP },
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 }
230 bool finished = false;
231 lex.pushTable(layoutTags);
232 // parse style section
233 while (!finished && lex.isOK() && !error) {
235 // See comment in LyXRC.cpp.
237 case Lexer::LEX_FEOF:
240 case Lexer::LEX_UNDEF: // parse error
241 lex.printError("Unknown layout tag `$$Token'");
248 switch (static_cast<LayoutTags>(le)) {
249 case LT_END: // end of structure
257 case LT_COPYSTYLE: { // initialize with a known style
260 style = subst(style, '_', ' ');
262 if (tclass.hasLayout(style)) {
263 docstring const tmpname = name_;
264 this->operator=(tclass[style]);
267 LYXERR0("Cannot copy unknown style `"
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());
278 case LT_OBSOLETEDBY: { // replace with a known style
281 style = subst(style, '_', ' ');
283 if (tclass.hasLayout(style)) {
284 docstring const tmpname = name_;
285 this->operator=(tclass[style]);
287 if (obsoleted_by().empty())
288 obsoleted_by_ = style;
290 LYXERR0("Cannot replace with unknown style `"
293 //lex.printError("Cannot replace with"
302 depends_on_ = subst(depends_on_, '_', ' ');
305 case LT_MARGIN: // margin style definition.
309 case LT_LATEXTYPE: // LaTeX style definition.
333 case LT_NEED_PROTECT:
342 font = lyxRead(lex, font);
347 font = lyxRead(lex, font);
351 labelfont = lyxRead(lex, labelfont);
354 case LT_NEXTNOINDENT: // Indent next paragraph?
358 case LT_COMMANDDEPTH:
368 latexparam_ = subst(latexparam_, """, "\"");
384 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
387 case LT_LANGPREAMBLE:
388 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
391 case LT_BABELPREAMBLE:
392 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
399 case LT_ENDLABELTYPE:
400 readEndLabelType(lex);
403 case LT_LEFTMARGIN: // left margin type
407 case LT_RIGHTMARGIN: // right margin type
411 case LT_LABELINDENT: // label indenting flag
415 case LT_PARINDENT: // paragraph indent. flag
419 case LT_PARSKIP: // paragraph skip size
423 case LT_ITEMSEP: // item separation size
427 case LT_TOPSEP: // top separation size
431 case LT_BOTTOMSEP: // bottom separation size
435 case LT_LABEL_BOTTOMSEP: // label bottom separation size
436 lex >> labelbottomsep;
439 case LT_LABELSEP: // label separator
441 labelsep = subst(labelsep, 'x', ' ');
444 case LT_PARSEP: // par. separation size
448 case LT_FILL_TOP: // fill top flag
452 case LT_FILL_BOTTOM: // fill bottom flag
456 case LT_NEWLINE: // newlines allowed?
457 lex >> newline_allowed;
460 case LT_ALIGN: // paragraph align
463 case LT_ALIGNPOSSIBLE: // paragraph allowed align
464 readAlignPossible(lex);
467 case LT_LABELSTRING: // label string definition
468 // FIXME: this means LT_ENDLABELSTRING may only
469 // occur after LT_LABELSTRING
471 labelstring_ = trim(labelstring_);
472 labelstring_appendix_ = labelstring_;
475 case LT_ENDLABELSTRING: // endlabel string definition
476 lex >> endlabelstring_;
477 endlabelstring_ = trim(endlabelstring_);
480 case LT_LABELSTRING_APPENDIX: // label string appendix definition
481 lex >> labelstring_appendix_;
482 labelstring_appendix_ = trim(labelstring_appendix_);
485 case LT_LABELCOUNTER: // name of counter to use
487 counter = trim(counter);
490 case LT_FREE_SPACING: // Allow for free spacing.
494 case LT_PASS_THRU: // Allow for pass thru.
498 case LT_SPACING: // setspace.sty
504 vector<string> const req =
505 getVectorFromString(lex.getString());
506 requires_.insert(req.begin(), req.end());
526 case LT_HTMLITEMATTR:
527 lex >> htmlitemattr_;
531 lex >> htmllabeltag_;
534 case LT_HTMLLABELATTR:
535 lex >> htmllabelattr_;
538 case LT_HTMLLABELFIRST:
539 lex >> htmllabelfirst_;
543 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
546 case LT_HTMLFORCECSS:
547 lex >> htmlforcecss_;
549 case LT_HTMLPREAMBLE:
550 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
563 // make sure we only have inpreamble = true for commands
564 if (inpreamble && latextype != LATEX_COMMAND) {
565 LYXERR0("InPreamble not permitted except with Command-type layouts.");
566 LYXERR0("Layout name: " << name());
583 LexerKeyword alignTags[] = {
584 { "block", AT_BLOCK },
585 { "center", AT_CENTER },
586 { "layout", AT_LAYOUT },
588 { "right", AT_RIGHT }
592 void Layout::readAlign(Lexer & lex)
594 PushPopHelper pph(lex, alignTags);
597 case Lexer::LEX_UNDEF:
598 lex.printError("Unknown alignment `$$Token'");
604 align = LYX_ALIGN_BLOCK;
607 align = LYX_ALIGN_LEFT;
610 align = LYX_ALIGN_RIGHT;
613 align = LYX_ALIGN_CENTER;
616 align = LYX_ALIGN_LAYOUT;
622 void Layout::readAlignPossible(Lexer & lex)
624 lex.pushTable(alignTags);
625 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
626 int lineno = lex.lineNumber();
630 case Lexer::LEX_UNDEF:
631 lex.printError("Unknown alignment `$$Token'");
637 alignpossible |= LYX_ALIGN_BLOCK;
640 alignpossible |= LYX_ALIGN_LEFT;
643 alignpossible |= LYX_ALIGN_RIGHT;
646 alignpossible |= LYX_ALIGN_CENTER;
649 alignpossible |= LYX_ALIGN_LAYOUT;
652 } while (lineno == lex.lineNumber());
657 void Layout::readLabelType(Lexer & lex)
663 LA_CENTERED_TOP_ENVIRONMENT,
673 LexerKeyword labelTypeTags[] = {
674 { "bibliography", LA_BIBLIO },
675 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
676 { "counter", LA_COUNTER },
677 { "enumerate", LA_ENUMERATE },
678 { "itemize", LA_ITEMIZE },
679 { "manual", LA_MANUAL },
680 { "no_label", LA_NO_LABEL },
681 { "sensitive", LA_SENSITIVE },
682 { "static", LA_STATIC },
683 { "top_environment", LA_TOP_ENVIRONMENT }
686 PushPopHelper pph(lex, labelTypeTags);
689 case Lexer::LEX_UNDEF:
690 lex.printError("Unknown labeltype tag `$$Token'");
696 labeltype = LABEL_NO_LABEL;
699 labeltype = LABEL_MANUAL;
701 case LA_TOP_ENVIRONMENT:
702 labeltype = LABEL_TOP_ENVIRONMENT;
704 case LA_CENTERED_TOP_ENVIRONMENT:
705 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
708 labeltype = LABEL_STATIC;
711 labeltype = LABEL_SENSITIVE;
714 labeltype = LABEL_COUNTER;
717 labeltype = LABEL_ENUMERATE;
720 labeltype = LABEL_ITEMIZE;
723 labeltype = LABEL_BIBLIO;
729 void Layout::readEndLabelType(Lexer & lex)
731 static LexerKeyword endlabelTypeTags[] = {
732 { "box", END_LABEL_BOX },
733 { "filled_box", END_LABEL_FILLED_BOX },
734 { "no_label", END_LABEL_NO_LABEL },
735 { "static", END_LABEL_STATIC }
738 PushPopHelper pph(lex, endlabelTypeTags);
741 case Lexer::LEX_UNDEF:
742 lex.printError("Unknown labeltype tag `$$Token'");
744 case END_LABEL_STATIC:
746 case END_LABEL_FILLED_BOX:
747 case END_LABEL_NO_LABEL:
748 endlabeltype = static_cast<EndLabelType>(le);
751 LYXERR0("Unhandled value " << le);
757 void Layout::readMargin(Lexer & lex)
759 LexerKeyword marginTags[] = {
760 { "dynamic", MARGIN_DYNAMIC },
761 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
762 { "manual", MARGIN_MANUAL },
763 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
764 { "static", MARGIN_STATIC }
767 PushPopHelper pph(lex, marginTags);
771 case Lexer::LEX_UNDEF:
772 lex.printError("Unknown margin type tag `$$Token'");
777 case MARGIN_FIRST_DYNAMIC:
778 case MARGIN_RIGHT_ADDRESS_BOX:
779 margintype = static_cast<MarginType>(le);
782 LYXERR0("Unhandled value " << le);
788 void Layout::readLatexType(Lexer & lex)
790 LexerKeyword latexTypeTags[] = {
791 { "bib_environment", LATEX_BIB_ENVIRONMENT },
792 { "command", LATEX_COMMAND },
793 { "environment", LATEX_ENVIRONMENT },
794 { "item_environment", LATEX_ITEM_ENVIRONMENT },
795 { "list_environment", LATEX_LIST_ENVIRONMENT },
796 { "paragraph", LATEX_PARAGRAPH }
799 PushPopHelper pph(lex, latexTypeTags);
802 case Lexer::LEX_UNDEF:
803 lex.printError("Unknown latextype tag `$$Token'");
805 case LATEX_PARAGRAPH:
807 case LATEX_ENVIRONMENT:
808 case LATEX_ITEM_ENVIRONMENT:
809 case LATEX_BIB_ENVIRONMENT:
810 case LATEX_LIST_ENVIRONMENT:
811 latextype = static_cast<LatexType>(le);
814 LYXERR0("Unhandled value " << le);
820 void Layout::readSpacing(Lexer & lex)
823 ST_SPACING_SINGLE = 1,
829 LexerKeyword spacingTags[] = {
830 {"double", ST_SPACING_DOUBLE },
831 {"onehalf", ST_SPACING_ONEHALF },
832 {"other", ST_OTHER },
833 {"single", ST_SPACING_SINGLE }
836 PushPopHelper pph(lex, spacingTags);
839 case Lexer::LEX_UNDEF:
840 lex.printError("Unknown spacing token `$$Token'");
845 case ST_SPACING_SINGLE:
846 spacing.set(Spacing::Single);
848 case ST_SPACING_ONEHALF:
849 spacing.set(Spacing::Onehalf);
851 case ST_SPACING_DOUBLE:
852 spacing.set(Spacing::Double);
856 spacing.set(Spacing::Other, lex.getString());
864 docstring const i18npreamble(Language const * lang, docstring const & templ)
869 string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
872 // tex2lyx does not have getMessages()
873 LASSERT(false, /**/);
876 // lyx::regex is not unicode-safe.
877 // Should use QRegExp or (boost::u32regex, but that requires ICU)
878 static regex const reg("_\\(([^\\)]+)\\)");
880 while (regex_search(preamble, sub, reg)) {
881 string const key = sub.str(1);
884 translated = to_utf8(getMessages(lang->code()).get(key));
886 lyxerr << "Warning: not translating `" << key
887 << "' because it is not pure ASCII." << endl;
890 preamble = subst(preamble, sub.str(), translated);
893 return from_utf8(preamble);
899 docstring const Layout::langpreamble(Language const * lang) const
901 return i18npreamble(lang, langpreamble_);
905 docstring const Layout::babelpreamble(Language const * lang) const
907 return i18npreamble(lang, babelpreamble_);
911 string const & Layout::htmltag() const
913 if (htmltag_.empty())
919 string const & Layout::htmlattr() const
921 if (htmlattr_.empty())
922 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
927 string const & Layout::htmlitemtag() const
929 if (htmlitemtag_.empty())
930 htmlitemtag_ = "div";
935 string const & Layout::htmlitemattr() const
937 if (htmlitemattr_.empty())
938 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
939 return htmlitemattr_;
943 string const & Layout::htmllabeltag() const
945 if (htmllabeltag_.empty())
946 htmllabeltag_ = "span";
947 return htmllabeltag_;
951 string const & Layout::htmllabelattr() const
953 if (htmllabelattr_.empty())
954 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
955 return htmllabelattr_;
959 docstring Layout::htmlstyle() const {
960 if (!htmlstyle_.empty() && !htmlforcecss_)
962 if (htmldefaultstyle_.empty())
964 docstring retval = htmldefaultstyle_;
965 if (!htmlstyle_.empty())
966 retval += '\n' + htmlstyle_;
971 string Layout::defaultCSSClass() const
973 if (!defaultcssclass_.empty())
974 return defaultcssclass_;
976 docstring::const_iterator it = name().begin();
977 docstring::const_iterator en = name().end();
978 for (; it != en; ++it) {
986 // are there other characters we need to remove?
987 defaultcssclass_ = to_utf8(d);
988 return defaultcssclass_;
992 // NOTE There is a whole ton of stuff that could go into this.
993 // Things like bottomsep, topsep, and parsep could become various
994 // sorts of margins or padding, for example. But for now we are
995 // going to keep it simple.
996 void Layout::makeDefaultCSS() const {
997 // this never needs to be redone, since reloading layouts will
998 // wipe out what we did before.
999 if (!htmldefaultstyle_.empty())
1001 docstring const mainfontCSS = font.asCSS();
1002 if (!mainfontCSS.empty())
1004 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1005 mainfontCSS + from_ascii("\n}\n");
1006 if (labelfont == font || htmllabeltag() == "NONE")
1008 docstring const labelfontCSS = labelfont.asCSS();
1009 if (!labelfontCSS.empty())
1010 htmldefaultstyle_ +=
1011 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1012 labelfontCSS + from_ascii("\n}\n");
1016 bool Layout::operator==(Layout const & rhs) const
1018 // This is enough for the applications we actually make,
1019 // at least at the moment. But we could check more.
1020 return name() == rhs.name()
1021 && latexname() == rhs.latexname()
1022 && latextype == rhs.latextype;