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/debug.h"
22 #include "support/lassert.h"
23 #include "support/lstrings.h"
24 #include "support/Messages.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_;
563 case LT_HTMLPREAMBLE:
564 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
577 // make sure we only have inpreamble = true for commands
578 if (inpreamble && latextype != LATEX_COMMAND) {
579 LYXERR0("InPreamble not permitted except with Command-type layouts.");
580 LYXERR0("Layout name: " << name());
584 return finished && !error;
597 LexerKeyword alignTags[] = {
598 { "block", AT_BLOCK },
599 { "center", AT_CENTER },
600 { "layout", AT_LAYOUT },
602 { "right", AT_RIGHT }
606 void Layout::readAlign(Lexer & lex)
608 PushPopHelper pph(lex, alignTags);
611 case Lexer::LEX_UNDEF:
612 lex.printError("Unknown alignment `$$Token'");
618 align = LYX_ALIGN_BLOCK;
621 align = LYX_ALIGN_LEFT;
624 align = LYX_ALIGN_RIGHT;
627 align = LYX_ALIGN_CENTER;
630 align = LYX_ALIGN_LAYOUT;
636 void Layout::readAlignPossible(Lexer & lex)
638 lex.pushTable(alignTags);
639 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
640 int lineno = lex.lineNumber();
644 case Lexer::LEX_UNDEF:
645 lex.printError("Unknown alignment `$$Token'");
651 alignpossible |= LYX_ALIGN_BLOCK;
654 alignpossible |= LYX_ALIGN_LEFT;
657 alignpossible |= LYX_ALIGN_RIGHT;
660 alignpossible |= LYX_ALIGN_CENTER;
663 alignpossible |= LYX_ALIGN_LAYOUT;
666 } while (lineno == lex.lineNumber());
671 void Layout::readLabelType(Lexer & lex)
677 LA_CENTERED_TOP_ENVIRONMENT,
687 LexerKeyword labelTypeTags[] = {
688 { "bibliography", LA_BIBLIO },
689 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
690 { "counter", LA_COUNTER },
691 { "enumerate", LA_ENUMERATE },
692 { "itemize", LA_ITEMIZE },
693 { "manual", LA_MANUAL },
694 { "no_label", LA_NO_LABEL },
695 { "sensitive", LA_SENSITIVE },
696 { "static", LA_STATIC },
697 { "top_environment", LA_TOP_ENVIRONMENT }
700 PushPopHelper pph(lex, labelTypeTags);
703 case Lexer::LEX_UNDEF:
704 lex.printError("Unknown labeltype tag `$$Token'");
710 labeltype = LABEL_NO_LABEL;
713 labeltype = LABEL_MANUAL;
715 case LA_TOP_ENVIRONMENT:
716 labeltype = LABEL_TOP_ENVIRONMENT;
718 case LA_CENTERED_TOP_ENVIRONMENT:
719 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
722 labeltype = LABEL_STATIC;
725 labeltype = LABEL_SENSITIVE;
728 labeltype = LABEL_COUNTER;
731 labeltype = LABEL_ENUMERATE;
734 labeltype = LABEL_ITEMIZE;
737 labeltype = LABEL_BIBLIO;
743 void Layout::readEndLabelType(Lexer & lex)
745 static LexerKeyword endlabelTypeTags[] = {
746 { "box", END_LABEL_BOX },
747 { "filled_box", END_LABEL_FILLED_BOX },
748 { "no_label", END_LABEL_NO_LABEL },
749 { "static", END_LABEL_STATIC }
752 PushPopHelper pph(lex, endlabelTypeTags);
755 case Lexer::LEX_UNDEF:
756 lex.printError("Unknown labeltype tag `$$Token'");
758 case END_LABEL_STATIC:
760 case END_LABEL_FILLED_BOX:
761 case END_LABEL_NO_LABEL:
762 endlabeltype = static_cast<EndLabelType>(le);
765 LYXERR0("Unhandled value " << le);
771 void Layout::readMargin(Lexer & lex)
773 LexerKeyword marginTags[] = {
774 { "dynamic", MARGIN_DYNAMIC },
775 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
776 { "manual", MARGIN_MANUAL },
777 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
778 { "static", MARGIN_STATIC }
781 PushPopHelper pph(lex, marginTags);
785 case Lexer::LEX_UNDEF:
786 lex.printError("Unknown margin type tag `$$Token'");
791 case MARGIN_FIRST_DYNAMIC:
792 case MARGIN_RIGHT_ADDRESS_BOX:
793 margintype = static_cast<MarginType>(le);
796 LYXERR0("Unhandled value " << le);
802 void Layout::readLatexType(Lexer & lex)
804 LexerKeyword latexTypeTags[] = {
805 { "bib_environment", LATEX_BIB_ENVIRONMENT },
806 { "command", LATEX_COMMAND },
807 { "environment", LATEX_ENVIRONMENT },
808 { "item_environment", LATEX_ITEM_ENVIRONMENT },
809 { "list_environment", LATEX_LIST_ENVIRONMENT },
810 { "paragraph", LATEX_PARAGRAPH }
813 PushPopHelper pph(lex, latexTypeTags);
816 case Lexer::LEX_UNDEF:
817 lex.printError("Unknown latextype tag `$$Token'");
819 case LATEX_PARAGRAPH:
821 case LATEX_ENVIRONMENT:
822 case LATEX_ITEM_ENVIRONMENT:
823 case LATEX_BIB_ENVIRONMENT:
824 case LATEX_LIST_ENVIRONMENT:
825 latextype = static_cast<LatexType>(le);
828 LYXERR0("Unhandled value " << le);
834 void Layout::readSpacing(Lexer & lex)
837 ST_SPACING_SINGLE = 1,
843 LexerKeyword spacingTags[] = {
844 {"double", ST_SPACING_DOUBLE },
845 {"onehalf", ST_SPACING_ONEHALF },
846 {"other", ST_OTHER },
847 {"single", ST_SPACING_SINGLE }
850 PushPopHelper pph(lex, spacingTags);
853 case Lexer::LEX_UNDEF:
854 lex.printError("Unknown spacing token `$$Token'");
859 case ST_SPACING_SINGLE:
860 spacing.set(Spacing::Single);
862 case ST_SPACING_ONEHALF:
863 spacing.set(Spacing::Onehalf);
865 case ST_SPACING_DOUBLE:
866 spacing.set(Spacing::Double);
870 spacing.set(Spacing::Other, lex.getString());
878 docstring const i18npreamble(Language const * lang, docstring const & templ)
883 string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
886 // tex2lyx does not have getMessages()
887 LASSERT(false, /**/);
890 // lyx::regex is not unicode-safe.
891 // Should use QRegExp or (boost::u32regex, but that requires ICU)
892 static regex const reg("_\\(([^\\)]+)\\)");
894 while (regex_search(preamble, sub, reg)) {
895 string const key = sub.str(1);
898 translated = to_utf8(getMessages(lang->code()).get(key));
900 lyxerr << "Warning: not translating `" << key
901 << "' because it is not pure ASCII." << endl;
904 preamble = subst(preamble, sub.str(), translated);
907 return from_utf8(preamble);
913 docstring const Layout::langpreamble(Language const * lang) const
915 return i18npreamble(lang, langpreamble_);
919 docstring const Layout::babelpreamble(Language const * lang) const
921 return i18npreamble(lang, babelpreamble_);
925 string const & Layout::htmltag() const
927 if (htmltag_.empty())
933 string const & Layout::htmlattr() const
935 if (htmlattr_.empty())
936 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
941 string const & Layout::htmlitemtag() const
943 if (htmlitemtag_.empty())
944 htmlitemtag_ = "div";
949 string const & Layout::htmlitemattr() const
951 if (htmlitemattr_.empty())
952 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
953 return htmlitemattr_;
957 string const & Layout::htmllabeltag() const
959 if (htmllabeltag_.empty())
960 htmllabeltag_ = "span";
961 return htmllabeltag_;
965 string const & Layout::htmllabelattr() const
967 if (htmllabelattr_.empty())
968 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
969 return htmllabelattr_;
973 docstring Layout::htmlstyle() const {
974 if (!htmlstyle_.empty() && !htmlforcecss_)
976 if (htmldefaultstyle_.empty())
978 docstring retval = htmldefaultstyle_;
979 if (!htmlstyle_.empty())
980 retval += '\n' + htmlstyle_;
985 string Layout::defaultCSSClass() const
987 if (!defaultcssclass_.empty())
988 return defaultcssclass_;
990 docstring::const_iterator it = name().begin();
991 docstring::const_iterator en = name().end();
992 for (; it != en; ++it) {
1000 // are there other characters we need to remove?
1001 defaultcssclass_ = to_utf8(d);
1002 return defaultcssclass_;
1007 string makeMarginValue(double d) {
1015 void Layout::makeDefaultCSS() const {
1016 // this never needs to be redone, since reloading layouts will
1017 // wipe out what we did before.
1018 if (!htmldefaultstyle_.empty())
1022 htmldefaultstyle_ = font.asCSS();
1024 // top and bottom margins
1027 tmp += "margin-top: " + makeMarginValue(topsep) + ";\n";
1029 tmp += "margin-bottom: " + makeMarginValue(bottomsep) + ";\n";
1031 if (!htmldefaultstyle_.empty())
1032 htmldefaultstyle_ += from_ascii("\n");
1033 htmldefaultstyle_ += from_ascii(tmp);
1036 // wrap up what we have, if anything
1037 if (!htmldefaultstyle_.empty())
1039 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1040 htmldefaultstyle_ + from_ascii("\n}\n");
1042 if (labelfont == font || htmllabeltag() == "NONE")
1046 docstring const labelfontCSS = labelfont.asCSS();
1047 if (!labelfontCSS.empty())
1048 htmldefaultstyle_ +=
1049 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1050 labelfontCSS + from_ascii("\n}\n");
1054 bool Layout::operator==(Layout const & rhs) const
1056 // This is enough for the applications we actually make,
1057 // at least at the moment. But we could check more.
1058 return name() == rhs.name()
1059 && latexname() == rhs.latexname()
1060 && latextype == rhs.latextype;