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,
58 LT_PARBREAK_IS_NEWLINE,
68 LT_LABELSTRING_APPENDIX,
111 LT_INTITLE // keep this last!
114 /////////////////////
119 margintype = MARGIN_STATIC;
120 latextype = LATEX_PARAGRAPH;
126 labelfont = inherit_font;
128 reslabelfont = sane_font;
129 nextnoindent = false;
134 labelbottomsep = 0.0;
136 align = LYX_ALIGN_BLOCK;
137 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
138 labeltype = LABEL_NO_LABEL;
139 endlabeltype = END_LABEL_NO_LABEL;
140 // Should or should not. That is the question.
141 // spacing.set(Spacing::OneHalf);
144 newline_allowed = true;
145 free_spacing = false;
147 parbreak_is_newline = false;
148 toclevel = NOT_IN_TOC;
150 htmllabelfirst_ = false;
151 htmlforcecss_ = false;
159 bool Layout::read(Lexer & lex, TextClass const & tclass)
161 // This table is sorted alphabetically [asierra 30March96]
162 LexerKeyword layoutTags[] = {
163 { "align", LT_ALIGN },
164 { "alignpossible", LT_ALIGNPOSSIBLE },
165 { "babelpreamble", LT_BABELPREAMBLE },
166 { "bottomsep", LT_BOTTOMSEP },
167 { "category", LT_CATEGORY },
168 { "commanddepth", LT_COMMANDDEPTH },
169 { "copystyle", LT_COPYSTYLE },
170 { "dependson", LT_DEPENDSON },
172 { "endlabelstring", LT_ENDLABELSTRING },
173 { "endlabeltype", LT_ENDLABELTYPE },
174 { "fill_bottom", LT_FILL_BOTTOM },
175 { "fill_top", LT_FILL_TOP },
177 { "freespacing", LT_FREE_SPACING },
178 { "htmlattr", LT_HTMLATTR },
179 { "htmlforcecss", LT_HTMLFORCECSS },
180 { "htmlitem", LT_HTMLITEM },
181 { "htmlitemattr", LT_HTMLITEMATTR },
182 { "htmllabel", LT_HTMLLABEL },
183 { "htmllabelattr", LT_HTMLLABELATTR },
184 { "htmllabelfirst", LT_HTMLLABELFIRST },
185 { "htmlpremable", LT_HTMLPREAMBLE },
186 { "htmlstyle", LT_HTMLSTYLE },
187 { "htmltag", LT_HTMLTAG },
188 { "htmltitle", LT_HTMLTITLE },
189 { "innertag", LT_INNERTAG },
190 { "inpreamble", LT_INPREAMBLE },
191 { "intitle", LT_INTITLE },
192 { "itemsep", LT_ITEMSEP },
193 { "itemtag", LT_ITEMTAG },
194 { "keepempty", LT_KEEPEMPTY },
195 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
196 { "labelcounter", LT_LABELCOUNTER },
197 { "labelfont", LT_LABELFONT },
198 { "labelindent", LT_LABELINDENT },
199 { "labelsep", LT_LABELSEP },
200 { "labelstring", LT_LABELSTRING },
201 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
202 { "labeltag", LT_LABELTAG },
203 { "labeltype", LT_LABELTYPE },
204 { "langpreamble", LT_LANGPREAMBLE },
205 { "latexname", LT_LATEXNAME },
206 { "latexparam", LT_LATEXPARAM },
207 { "latextype", LT_LATEXTYPE },
208 { "leftmargin", LT_LEFTMARGIN },
209 { "margin", LT_MARGIN },
210 { "needprotect", LT_NEED_PROTECT },
211 { "newline", LT_NEWLINE },
212 { "nextnoindent", LT_NEXTNOINDENT },
213 { "obsoletedby", LT_OBSOLETEDBY },
214 { "optionalargs", LT_OPTARGS },
215 { "parbreakisnewline", LT_PARBREAK_IS_NEWLINE },
216 { "parindent", LT_PARINDENT },
217 { "parsep", LT_PARSEP },
218 { "parskip", LT_PARSKIP },
219 { "passthru", LT_PASS_THRU },
220 { "preamble", LT_PREAMBLE },
221 { "refprefix", LT_REFPREFIX },
222 { "requiredargs", LT_REQARGS },
223 { "requires", LT_REQUIRES },
224 { "rightmargin", LT_RIGHTMARGIN },
225 { "spacing", LT_SPACING },
226 { "spellcheck", LT_SPELLCHECK },
227 { "textfont", LT_TEXTFONT },
228 { "toclevel", LT_TOCLEVEL },
229 { "topsep", LT_TOPSEP }
233 bool finished = false;
234 lex.pushTable(layoutTags);
235 // parse style section
236 while (!finished && lex.isOK() && !error) {
238 // See comment in LyXRC.cpp.
240 case Lexer::LEX_FEOF:
243 case Lexer::LEX_UNDEF: // parse error
244 lex.printError("Unknown layout tag `$$Token'");
251 switch (static_cast<LayoutTags>(le)) {
252 case LT_END: // end of structure
260 case LT_COPYSTYLE: { // initialize with a known style
263 style = subst(style, '_', ' ');
265 if (tclass.hasLayout(style)) {
266 docstring const tmpname = name_;
267 this->operator=(tclass[style]);
270 LYXERR0("Cannot copy unknown style `"
272 << "All layouts so far:");
273 DocumentClass::const_iterator lit = tclass.begin();
274 DocumentClass::const_iterator len = tclass.end();
275 for (; lit != len; ++lit)
276 LYXERR0(lit->name());
281 case LT_OBSOLETEDBY: { // replace with a known style
284 style = subst(style, '_', ' ');
286 if (tclass.hasLayout(style)) {
287 docstring const tmpname = name_;
288 this->operator=(tclass[style]);
290 if (obsoleted_by().empty())
291 obsoleted_by_ = style;
293 LYXERR0("Cannot replace with unknown style `"
296 //lex.printError("Cannot replace with"
305 depends_on_ = subst(depends_on_, '_', ' ');
308 case LT_MARGIN: // margin style definition.
312 case LT_LATEXTYPE: // LaTeX style definition.
336 case LT_NEED_PROTECT:
345 font = lyxRead(lex, font);
350 font = lyxRead(lex, font);
354 labelfont = lyxRead(lex, labelfont);
357 case LT_NEXTNOINDENT: // Indent next paragraph?
361 case LT_COMMANDDEPTH:
371 latexparam_ = subst(latexparam_, """, "\"");
387 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
390 case LT_LANGPREAMBLE:
391 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
394 case LT_BABELPREAMBLE:
395 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
402 case LT_ENDLABELTYPE:
403 readEndLabelType(lex);
406 case LT_LEFTMARGIN: // left margin type
410 case LT_RIGHTMARGIN: // right margin type
414 case LT_LABELINDENT: // label indenting flag
418 case LT_PARINDENT: // paragraph indent. flag
422 case LT_PARSKIP: // paragraph skip size
426 case LT_ITEMSEP: // item separation size
430 case LT_TOPSEP: // top separation size
434 case LT_BOTTOMSEP: // bottom separation size
438 case LT_LABEL_BOTTOMSEP: // label bottom separation size
439 lex >> labelbottomsep;
442 case LT_LABELSEP: // label separator
444 labelsep = subst(labelsep, 'x', ' ');
447 case LT_PARSEP: // par. separation size
451 case LT_FILL_TOP: // fill top flag
455 case LT_FILL_BOTTOM: // fill bottom flag
459 case LT_NEWLINE: // newlines allowed?
460 lex >> newline_allowed;
463 case LT_ALIGN: // paragraph align
466 case LT_ALIGNPOSSIBLE: // paragraph allowed align
467 readAlignPossible(lex);
470 case LT_LABELSTRING: // label string definition
471 // FIXME: this means LT_ENDLABELSTRING may only
472 // occur after LT_LABELSTRING
474 labelstring_ = trim(labelstring_);
475 labelstring_appendix_ = labelstring_;
478 case LT_ENDLABELSTRING: // endlabel string definition
479 lex >> endlabelstring_;
480 endlabelstring_ = trim(endlabelstring_);
483 case LT_LABELSTRING_APPENDIX: // label string appendix definition
484 lex >> labelstring_appendix_;
485 labelstring_appendix_ = trim(labelstring_appendix_);
488 case LT_LABELCOUNTER: // name of counter to use
490 counter = trim(counter);
493 case LT_FREE_SPACING: // Allow for free spacing.
497 case LT_PASS_THRU: // Allow for pass thru.
501 case LT_PARBREAK_IS_NEWLINE:
502 lex >> parbreak_is_newline;
505 case LT_SPACING: // setspace.sty
511 vector<string> const req =
512 getVectorFromString(lex.getString());
513 requires_.insert(req.begin(), req.end());
539 case LT_HTMLITEMATTR:
540 lex >> htmlitemattr_;
544 lex >> htmllabeltag_;
547 case LT_HTMLLABELATTR:
548 lex >> htmllabelattr_;
551 case LT_HTMLLABELFIRST:
552 lex >> htmllabelfirst_;
556 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
559 case LT_HTMLFORCECSS:
560 lex >> htmlforcecss_;
562 case LT_HTMLPREAMBLE:
563 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
576 // make sure we only have inpreamble = true for commands
577 if (inpreamble && latextype != LATEX_COMMAND) {
578 LYXERR0("InPreamble not permitted except with Command-type layouts.");
579 LYXERR0("Layout name: " << name());
583 return finished && !error;
596 LexerKeyword alignTags[] = {
597 { "block", AT_BLOCK },
598 { "center", AT_CENTER },
599 { "layout", AT_LAYOUT },
601 { "right", AT_RIGHT }
605 void Layout::readAlign(Lexer & lex)
607 PushPopHelper pph(lex, alignTags);
610 case Lexer::LEX_UNDEF:
611 lex.printError("Unknown alignment `$$Token'");
617 align = LYX_ALIGN_BLOCK;
620 align = LYX_ALIGN_LEFT;
623 align = LYX_ALIGN_RIGHT;
626 align = LYX_ALIGN_CENTER;
629 align = LYX_ALIGN_LAYOUT;
635 void Layout::readAlignPossible(Lexer & lex)
637 lex.pushTable(alignTags);
638 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
639 int lineno = lex.lineNumber();
643 case Lexer::LEX_UNDEF:
644 lex.printError("Unknown alignment `$$Token'");
650 alignpossible |= LYX_ALIGN_BLOCK;
653 alignpossible |= LYX_ALIGN_LEFT;
656 alignpossible |= LYX_ALIGN_RIGHT;
659 alignpossible |= LYX_ALIGN_CENTER;
662 alignpossible |= LYX_ALIGN_LAYOUT;
665 } while (lineno == lex.lineNumber());
670 void Layout::readLabelType(Lexer & lex)
676 LA_CENTERED_TOP_ENVIRONMENT,
686 LexerKeyword labelTypeTags[] = {
687 { "bibliography", LA_BIBLIO },
688 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
689 { "counter", LA_COUNTER },
690 { "enumerate", LA_ENUMERATE },
691 { "itemize", LA_ITEMIZE },
692 { "manual", LA_MANUAL },
693 { "no_label", LA_NO_LABEL },
694 { "sensitive", LA_SENSITIVE },
695 { "static", LA_STATIC },
696 { "top_environment", LA_TOP_ENVIRONMENT }
699 PushPopHelper pph(lex, labelTypeTags);
702 case Lexer::LEX_UNDEF:
703 lex.printError("Unknown labeltype tag `$$Token'");
709 labeltype = LABEL_NO_LABEL;
712 labeltype = LABEL_MANUAL;
714 case LA_TOP_ENVIRONMENT:
715 labeltype = LABEL_TOP_ENVIRONMENT;
717 case LA_CENTERED_TOP_ENVIRONMENT:
718 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
721 labeltype = LABEL_STATIC;
724 labeltype = LABEL_SENSITIVE;
727 labeltype = LABEL_COUNTER;
730 labeltype = LABEL_ENUMERATE;
733 labeltype = LABEL_ITEMIZE;
736 labeltype = LABEL_BIBLIO;
742 void Layout::readEndLabelType(Lexer & lex)
744 static LexerKeyword endlabelTypeTags[] = {
745 { "box", END_LABEL_BOX },
746 { "filled_box", END_LABEL_FILLED_BOX },
747 { "no_label", END_LABEL_NO_LABEL },
748 { "static", END_LABEL_STATIC }
751 PushPopHelper pph(lex, endlabelTypeTags);
754 case Lexer::LEX_UNDEF:
755 lex.printError("Unknown labeltype tag `$$Token'");
757 case END_LABEL_STATIC:
759 case END_LABEL_FILLED_BOX:
760 case END_LABEL_NO_LABEL:
761 endlabeltype = static_cast<EndLabelType>(le);
764 LYXERR0("Unhandled value " << le);
770 void Layout::readMargin(Lexer & lex)
772 LexerKeyword marginTags[] = {
773 { "dynamic", MARGIN_DYNAMIC },
774 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
775 { "manual", MARGIN_MANUAL },
776 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
777 { "static", MARGIN_STATIC }
780 PushPopHelper pph(lex, marginTags);
784 case Lexer::LEX_UNDEF:
785 lex.printError("Unknown margin type tag `$$Token'");
790 case MARGIN_FIRST_DYNAMIC:
791 case MARGIN_RIGHT_ADDRESS_BOX:
792 margintype = static_cast<MarginType>(le);
795 LYXERR0("Unhandled value " << le);
801 void Layout::readLatexType(Lexer & lex)
803 LexerKeyword latexTypeTags[] = {
804 { "bib_environment", LATEX_BIB_ENVIRONMENT },
805 { "command", LATEX_COMMAND },
806 { "environment", LATEX_ENVIRONMENT },
807 { "item_environment", LATEX_ITEM_ENVIRONMENT },
808 { "list_environment", LATEX_LIST_ENVIRONMENT },
809 { "paragraph", LATEX_PARAGRAPH }
812 PushPopHelper pph(lex, latexTypeTags);
815 case Lexer::LEX_UNDEF:
816 lex.printError("Unknown latextype tag `$$Token'");
818 case LATEX_PARAGRAPH:
820 case LATEX_ENVIRONMENT:
821 case LATEX_ITEM_ENVIRONMENT:
822 case LATEX_BIB_ENVIRONMENT:
823 case LATEX_LIST_ENVIRONMENT:
824 latextype = static_cast<LatexType>(le);
827 LYXERR0("Unhandled value " << le);
833 void Layout::readSpacing(Lexer & lex)
836 ST_SPACING_SINGLE = 1,
842 LexerKeyword spacingTags[] = {
843 {"double", ST_SPACING_DOUBLE },
844 {"onehalf", ST_SPACING_ONEHALF },
845 {"other", ST_OTHER },
846 {"single", ST_SPACING_SINGLE }
849 PushPopHelper pph(lex, spacingTags);
852 case Lexer::LEX_UNDEF:
853 lex.printError("Unknown spacing token `$$Token'");
858 case ST_SPACING_SINGLE:
859 spacing.set(Spacing::Single);
861 case ST_SPACING_ONEHALF:
862 spacing.set(Spacing::Onehalf);
864 case ST_SPACING_DOUBLE:
865 spacing.set(Spacing::Double);
869 spacing.set(Spacing::Other, lex.getString());
877 docstring const i18npreamble(Language const * lang, docstring const & templ)
882 string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
885 // tex2lyx does not have getMessages()
886 LASSERT(false, /**/);
889 // lyx::regex is not unicode-safe.
890 // Should use QRegExp or (boost::u32regex, but that requires ICU)
891 static regex const reg("_\\(([^\\)]+)\\)");
893 while (regex_search(preamble, sub, reg)) {
894 string const key = sub.str(1);
897 translated = to_utf8(getMessages(lang->code()).get(key));
899 lyxerr << "Warning: not translating `" << key
900 << "' because it is not pure ASCII." << endl;
903 preamble = subst(preamble, sub.str(), translated);
906 return from_utf8(preamble);
912 docstring const Layout::langpreamble(Language const * lang) const
914 return i18npreamble(lang, langpreamble_);
918 docstring const Layout::babelpreamble(Language const * lang) const
920 return i18npreamble(lang, babelpreamble_);
924 string const & Layout::htmltag() const
926 if (htmltag_.empty())
932 string const & Layout::htmlattr() const
934 if (htmlattr_.empty())
935 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
940 string const & Layout::htmlitemtag() const
942 if (htmlitemtag_.empty())
943 htmlitemtag_ = "div";
948 string const & Layout::htmlitemattr() const
950 if (htmlitemattr_.empty())
951 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
952 return htmlitemattr_;
956 string const & Layout::htmllabeltag() const
958 if (htmllabeltag_.empty())
959 htmllabeltag_ = "span";
960 return htmllabeltag_;
964 string const & Layout::htmllabelattr() const
966 if (htmllabelattr_.empty())
967 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
968 return htmllabelattr_;
972 docstring Layout::htmlstyle() const {
973 if (!htmlstyle_.empty() && !htmlforcecss_)
975 if (htmldefaultstyle_.empty())
977 docstring retval = htmldefaultstyle_;
978 if (!htmlstyle_.empty())
979 retval += '\n' + htmlstyle_;
984 string Layout::defaultCSSClass() const
986 if (!defaultcssclass_.empty())
987 return defaultcssclass_;
989 docstring::const_iterator it = name().begin();
990 docstring::const_iterator en = name().end();
991 for (; it != en; ++it) {
999 // are there other characters we need to remove?
1000 defaultcssclass_ = to_utf8(d);
1001 return defaultcssclass_;
1005 // NOTE There is a whole ton of stuff that could go into this.
1006 // Things like bottomsep, topsep, and parsep could become various
1007 // sorts of margins or padding, for example. But for now we are
1008 // going to keep it simple.
1009 void Layout::makeDefaultCSS() const {
1010 // this never needs to be redone, since reloading layouts will
1011 // wipe out what we did before.
1012 if (!htmldefaultstyle_.empty())
1014 docstring const mainfontCSS = font.asCSS();
1015 if (!mainfontCSS.empty())
1017 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1018 mainfontCSS + from_ascii("\n}\n");
1019 if (labelfont == font || htmllabeltag() == "NONE")
1021 docstring const labelfontCSS = labelfont.asCSS();
1022 if (!labelfontCSS.empty())
1023 htmldefaultstyle_ +=
1024 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1025 labelfontCSS + from_ascii("\n}\n");
1029 bool Layout::operator==(Layout const & rhs) const
1031 // This is enough for the applications we actually make,
1032 // at least at the moment. But we could check more.
1033 return name() == rhs.name()
1034 && latexname() == rhs.latexname()
1035 && latextype == rhs.latextype;