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.
20 #include "output_xhtml.h"
21 #include "TextClass.h"
23 #include "support/debug.h"
24 #include "support/lassert.h"
25 #include "support/lstrings.h"
26 #include "support/Messages.h"
27 #include "support/textutils.h"
31 using namespace lyx::support;
35 /// Special value of toclevel for layouts that to not belong in a TOC
36 const int Layout::NOT_IN_TOC = -1000;
38 // The order of the LayoutTags enum is no more important. [asierra300396]
54 LT_PARBREAK_IS_NEWLINE,
63 LT_LABELSTRING_APPENDIX,
105 LT_INTITLE // keep this last!
108 /////////////////////
113 margintype = MARGIN_STATIC;
114 latextype = LATEX_PARAGRAPH;
120 labelfont = inherit_font;
122 reslabelfont = sane_font;
123 nextnoindent = false;
128 labelbottomsep = 0.0;
130 align = LYX_ALIGN_BLOCK;
131 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
132 labeltype = LABEL_NO_LABEL;
133 endlabeltype = END_LABEL_NO_LABEL;
134 // Should or should not. That is the question.
135 // spacing.set(Spacing::OneHalf);
136 newline_allowed = true;
137 free_spacing = false;
139 parbreak_is_newline = false;
140 toclevel = NOT_IN_TOC;
142 htmllabelfirst_ = false;
143 htmlforcecss_ = false;
151 bool Layout::read(Lexer & lex, TextClass const & tclass)
153 // This table is sorted alphabetically [asierra 30March96]
154 LexerKeyword layoutTags[] = {
155 { "align", LT_ALIGN },
156 { "alignpossible", LT_ALIGNPOSSIBLE },
157 { "babelpreamble", LT_BABELPREAMBLE },
158 { "bottomsep", LT_BOTTOMSEP },
159 { "category", LT_CATEGORY },
160 { "commanddepth", LT_COMMANDDEPTH },
161 { "copystyle", LT_COPYSTYLE },
162 { "dependson", LT_DEPENDSON },
164 { "endlabelstring", LT_ENDLABELSTRING },
165 { "endlabeltype", LT_ENDLABELTYPE },
167 { "freespacing", LT_FREE_SPACING },
168 { "htmlattr", LT_HTMLATTR },
169 { "htmlforcecss", LT_HTMLFORCECSS },
170 { "htmlitem", LT_HTMLITEM },
171 { "htmlitemattr", LT_HTMLITEMATTR },
172 { "htmllabel", LT_HTMLLABEL },
173 { "htmllabelattr", LT_HTMLLABELATTR },
174 { "htmllabelfirst", LT_HTMLLABELFIRST },
175 { "htmlpreamble", LT_HTMLPREAMBLE },
176 { "htmlstyle", LT_HTMLSTYLE },
177 { "htmltag", LT_HTMLTAG },
178 { "htmltitle", LT_HTMLTITLE },
179 { "innertag", LT_INNERTAG },
180 { "inpreamble", LT_INPREAMBLE },
181 { "intitle", LT_INTITLE },
182 { "itemsep", LT_ITEMSEP },
183 { "itemtag", LT_ITEMTAG },
184 { "keepempty", LT_KEEPEMPTY },
185 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
186 { "labelcounter", LT_LABELCOUNTER },
187 { "labelfont", LT_LABELFONT },
188 { "labelindent", LT_LABELINDENT },
189 { "labelsep", LT_LABELSEP },
190 { "labelstring", LT_LABELSTRING },
191 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
192 { "labeltag", LT_LABELTAG },
193 { "labeltype", LT_LABELTYPE },
194 { "langpreamble", LT_LANGPREAMBLE },
195 { "latexname", LT_LATEXNAME },
196 { "latexparam", LT_LATEXPARAM },
197 { "latextype", LT_LATEXTYPE },
198 { "leftmargin", LT_LEFTMARGIN },
199 { "margin", LT_MARGIN },
200 { "needprotect", LT_NEED_PROTECT },
201 { "newline", LT_NEWLINE },
202 { "nextnoindent", LT_NEXTNOINDENT },
203 { "obsoletedby", LT_OBSOLETEDBY },
204 { "optionalargs", LT_OPTARGS },
205 { "parbreakisnewline", LT_PARBREAK_IS_NEWLINE },
206 { "parindent", LT_PARINDENT },
207 { "parsep", LT_PARSEP },
208 { "parskip", LT_PARSKIP },
209 { "passthru", LT_PASS_THRU },
210 { "preamble", LT_PREAMBLE },
211 { "refprefix", LT_REFPREFIX },
212 { "requiredargs", LT_REQARGS },
213 { "requires", LT_REQUIRES },
214 { "rightmargin", LT_RIGHTMARGIN },
215 { "spacing", LT_SPACING },
216 { "spellcheck", LT_SPELLCHECK },
217 { "textfont", LT_TEXTFONT },
218 { "toclevel", LT_TOCLEVEL },
219 { "topsep", LT_TOPSEP }
223 bool finished = false;
224 lex.pushTable(layoutTags);
226 // parse style section
227 while (!finished && lex.isOK() && !error) {
229 // See comment in LyXRC.cpp.
231 case Lexer::LEX_FEOF:
234 case Lexer::LEX_UNDEF:
236 lex.printError("Unknown layout tag `$$Token'");
243 switch (static_cast<LayoutTags>(le)) {
255 style = subst(style, '_', ' ');
257 if (tclass.hasLayout(style)) {
258 docstring const tmpname = name_;
259 this->operator=(tclass[style]);
262 LYXERR0("Cannot copy unknown style `"
264 << "All layouts so far:");
265 DocumentClass::const_iterator lit = tclass.begin();
266 DocumentClass::const_iterator len = tclass.end();
267 for (; lit != len; ++lit)
268 LYXERR0(lit->name());
273 case LT_OBSOLETEDBY: {
276 style = subst(style, '_', ' ');
278 if (tclass.hasLayout(style)) {
279 docstring const tmpname = name_;
280 this->operator=(tclass[style]);
282 if (obsoleted_by().empty())
283 obsoleted_by_ = style;
285 LYXERR0("Cannot replace with unknown style `"
288 //lex.printError("Cannot replace with"
297 depends_on_ = subst(depends_on_, '_', ' ');
328 case LT_NEED_PROTECT:
337 font = lyxRead(lex, font);
342 font = lyxRead(lex, font);
346 labelfont = lyxRead(lex, labelfont);
349 case LT_NEXTNOINDENT:
353 case LT_COMMANDDEPTH:
363 latexparam_ = subst(latexparam_, """, "\"");
379 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
382 case LT_LANGPREAMBLE:
383 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
386 case LT_BABELPREAMBLE:
387 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
394 case LT_ENDLABELTYPE:
395 readEndLabelType(lex);
430 case LT_LABEL_BOTTOMSEP:
431 lex >> labelbottomsep;
436 labelsep = subst(labelsep, 'x', ' ');
444 lex >> newline_allowed;
451 case LT_ALIGNPOSSIBLE:
452 readAlignPossible(lex);
456 // FIXME: this means LT_ENDLABELSTRING may only
457 // occur after LT_LABELSTRING
459 labelstring_ = trim(labelstring_);
460 labelstring_appendix_ = labelstring_;
463 case LT_ENDLABELSTRING:
464 lex >> endlabelstring_;
465 endlabelstring_ = trim(endlabelstring_);
468 case LT_LABELSTRING_APPENDIX:
469 lex >> labelstring_appendix_;
470 labelstring_appendix_ = trim(labelstring_appendix_);
473 case LT_LABELCOUNTER:
475 counter = trim(counter);
478 case LT_FREE_SPACING:
486 case LT_PARBREAK_IS_NEWLINE:
487 lex >> parbreak_is_newline;
496 vector<string> const req =
497 getVectorFromString(lex.getString());
498 requires_.insert(req.begin(), req.end());
524 case LT_HTMLITEMATTR:
525 lex >> htmlitemattr_;
529 lex >> htmllabeltag_;
532 case LT_HTMLLABELATTR:
533 lex >> htmllabelattr_;
536 case LT_HTMLLABELFIRST:
537 lex >> htmllabelfirst_;
541 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
544 case LT_HTMLFORCECSS:
545 lex >> htmlforcecss_;
548 case LT_HTMLPREAMBLE:
549 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
562 // make sure we only have inpreamble = true for commands
563 if (inpreamble && latextype != LATEX_COMMAND && latextype != LATEX_PARAGRAPH) {
564 LYXERR0("InPreamble not permitted except with command and paragraph layouts.");
565 LYXERR0("Layout name: " << name());
569 return finished && !error;
582 LexerKeyword alignTags[] = {
583 { "block", AT_BLOCK },
584 { "center", AT_CENTER },
585 { "layout", AT_LAYOUT },
587 { "right", AT_RIGHT }
591 void Layout::readAlign(Lexer & lex)
593 PushPopHelper pph(lex, alignTags);
596 case Lexer::LEX_UNDEF:
597 lex.printError("Unknown alignment `$$Token'");
603 align = LYX_ALIGN_BLOCK;
606 align = LYX_ALIGN_LEFT;
609 align = LYX_ALIGN_RIGHT;
612 align = LYX_ALIGN_CENTER;
615 align = LYX_ALIGN_LAYOUT;
621 void Layout::readAlignPossible(Lexer & lex)
623 lex.pushTable(alignTags);
624 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
625 int lineno = lex.lineNumber();
629 case Lexer::LEX_UNDEF:
630 lex.printError("Unknown alignment `$$Token'");
636 alignpossible |= LYX_ALIGN_BLOCK;
639 alignpossible |= LYX_ALIGN_LEFT;
642 alignpossible |= LYX_ALIGN_RIGHT;
645 alignpossible |= LYX_ALIGN_CENTER;
648 alignpossible |= LYX_ALIGN_LAYOUT;
651 } while (lineno == lex.lineNumber());
656 void Layout::readLabelType(Lexer & lex)
662 LA_CENTERED_TOP_ENVIRONMENT,
672 LexerKeyword labelTypeTags[] = {
673 { "bibliography", LA_BIBLIO },
674 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
675 { "counter", LA_COUNTER },
676 { "enumerate", LA_ENUMERATE },
677 { "itemize", LA_ITEMIZE },
678 { "manual", LA_MANUAL },
679 { "no_label", LA_NO_LABEL },
680 { "sensitive", LA_SENSITIVE },
681 { "static", LA_STATIC },
682 { "top_environment", LA_TOP_ENVIRONMENT }
685 PushPopHelper pph(lex, labelTypeTags);
688 case Lexer::LEX_UNDEF:
689 lex.printError("Unknown labeltype tag `$$Token'");
695 labeltype = LABEL_NO_LABEL;
698 labeltype = LABEL_MANUAL;
700 case LA_TOP_ENVIRONMENT:
701 labeltype = LABEL_TOP_ENVIRONMENT;
703 case LA_CENTERED_TOP_ENVIRONMENT:
704 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
707 labeltype = LABEL_STATIC;
710 labeltype = LABEL_SENSITIVE;
713 labeltype = LABEL_COUNTER;
716 labeltype = LABEL_ENUMERATE;
719 labeltype = LABEL_ITEMIZE;
722 labeltype = LABEL_BIBLIO;
728 void Layout::readEndLabelType(Lexer & lex)
730 static LexerKeyword endlabelTypeTags[] = {
731 { "box", END_LABEL_BOX },
732 { "filled_box", END_LABEL_FILLED_BOX },
733 { "no_label", END_LABEL_NO_LABEL },
734 { "static", END_LABEL_STATIC }
737 PushPopHelper pph(lex, endlabelTypeTags);
740 case Lexer::LEX_UNDEF:
741 lex.printError("Unknown labeltype tag `$$Token'");
743 case END_LABEL_STATIC:
745 case END_LABEL_FILLED_BOX:
746 case END_LABEL_NO_LABEL:
747 endlabeltype = static_cast<EndLabelType>(le);
750 LYXERR0("Unhandled value " << le);
756 void Layout::readMargin(Lexer & lex)
758 LexerKeyword marginTags[] = {
759 { "dynamic", MARGIN_DYNAMIC },
760 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
761 { "manual", MARGIN_MANUAL },
762 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
763 { "static", MARGIN_STATIC }
766 PushPopHelper pph(lex, marginTags);
770 case Lexer::LEX_UNDEF:
771 lex.printError("Unknown margin type tag `$$Token'");
776 case MARGIN_FIRST_DYNAMIC:
777 case MARGIN_RIGHT_ADDRESS_BOX:
778 margintype = static_cast<MarginType>(le);
781 LYXERR0("Unhandled value " << le);
787 void Layout::readLatexType(Lexer & lex)
789 LexerKeyword latexTypeTags[] = {
790 { "bib_environment", LATEX_BIB_ENVIRONMENT },
791 { "command", LATEX_COMMAND },
792 { "environment", LATEX_ENVIRONMENT },
793 { "item_environment", LATEX_ITEM_ENVIRONMENT },
794 { "list_environment", LATEX_LIST_ENVIRONMENT },
795 { "paragraph", LATEX_PARAGRAPH }
798 PushPopHelper pph(lex, latexTypeTags);
801 case Lexer::LEX_UNDEF:
802 lex.printError("Unknown latextype tag `$$Token'");
804 case LATEX_PARAGRAPH:
806 case LATEX_ENVIRONMENT:
807 case LATEX_ITEM_ENVIRONMENT:
808 case LATEX_BIB_ENVIRONMENT:
809 case LATEX_LIST_ENVIRONMENT:
810 latextype = static_cast<LatexType>(le);
813 LYXERR0("Unhandled value " << le);
819 void Layout::readSpacing(Lexer & lex)
822 ST_SPACING_SINGLE = 1,
828 LexerKeyword spacingTags[] = {
829 {"double", ST_SPACING_DOUBLE },
830 {"onehalf", ST_SPACING_ONEHALF },
831 {"other", ST_OTHER },
832 {"single", ST_SPACING_SINGLE }
835 PushPopHelper pph(lex, spacingTags);
838 case Lexer::LEX_UNDEF:
839 lex.printError("Unknown spacing token `$$Token'");
844 case ST_SPACING_SINGLE:
845 spacing.set(Spacing::Single);
847 case ST_SPACING_ONEHALF:
848 spacing.set(Spacing::Onehalf);
850 case ST_SPACING_DOUBLE:
851 spacing.set(Spacing::Double);
855 spacing.set(Spacing::Other, lex.getString());
861 string const & Layout::htmltag() const
863 if (htmltag_.empty())
869 string const & Layout::htmlattr() const
871 if (htmlattr_.empty())
872 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
877 string const & Layout::htmlitemtag() const
879 if (htmlitemtag_.empty())
880 htmlitemtag_ = "div";
885 string const & Layout::htmlitemattr() const
887 if (htmlitemattr_.empty())
888 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
889 return htmlitemattr_;
893 string const & Layout::htmllabeltag() const
895 if (htmllabeltag_.empty()) {
896 if (labeltype != LABEL_TOP_ENVIRONMENT &&
897 labeltype != LABEL_CENTERED_TOP_ENVIRONMENT)
898 htmllabeltag_ = "span";
900 htmllabeltag_ = "div";
902 return htmllabeltag_;
906 string const & Layout::htmllabelattr() const
908 if (htmllabelattr_.empty())
909 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
910 return htmllabelattr_;
914 docstring Layout::htmlstyle() const {
915 if (!htmlstyle_.empty() && !htmlforcecss_)
917 if (htmldefaultstyle_.empty())
919 docstring retval = htmldefaultstyle_;
920 if (!htmlstyle_.empty())
921 retval += '\n' + htmlstyle_;
926 string Layout::defaultCSSClass() const
928 if (!defaultcssclass_.empty())
929 return defaultcssclass_;
931 docstring::const_iterator it = name().begin();
932 docstring::const_iterator en = name().end();
933 for (; it != en; ++it) {
934 char_type const c = *it;
935 if (!isAlphaASCII(c)) {
937 // make sure we don't start with an underscore,
938 // as that sometimes causes problems.
939 d = from_ascii("lyx_");
942 } else if (isLower(c))
945 // this is slow, so do it only if necessary
948 defaultcssclass_ = to_utf8(d);
949 return defaultcssclass_;
954 string makeMarginValue(char const * side, double d) {
956 os << "margin-" << side << ": " << d << "ex;\n";
962 void Layout::makeDefaultCSS() const {
963 // this never needs to be redone, since reloading layouts will
964 // wipe out what we did before.
965 if (!htmldefaultstyle_.empty())
969 htmldefaultstyle_ = font.asCSS();
974 tmp += makeMarginValue("top", topsep);
976 tmp += makeMarginValue("bottom", bottomsep);
977 if (!leftmargin.empty()) {
978 // we can't really do what LyX does with the margin, so
979 // we'll just figure out how many characters it is
980 int const len = leftmargin.length();
981 tmp += makeMarginValue("left", len);
983 if (!rightmargin.empty()) {
984 int const len = rightmargin.length();
985 tmp += makeMarginValue("right", len);
989 if (!htmldefaultstyle_.empty())
990 htmldefaultstyle_ += from_ascii("\n");
991 htmldefaultstyle_ += from_ascii(tmp);
994 // tex2lyx does not see output_xhtml.cpp
997 string where = alignmentToCSS(align);
998 if (!where.empty()) {
999 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1003 // wrap up what we have, if anything
1004 if (!htmldefaultstyle_.empty())
1006 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1007 htmldefaultstyle_ + from_ascii("\n}\n");
1009 if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1015 if (labelfont != font)
1016 labelCSS = labelfont.asCSS() + from_ascii("\n");
1017 if (labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1018 labelCSS += from_ascii("text-align: center;\n");
1020 if (!labelCSS.empty())
1021 htmldefaultstyle_ +=
1022 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1023 labelCSS + from_ascii("\n}\n");
1027 bool Layout::operator==(Layout const & rhs) const
1029 // This is enough for the applications we actually make,
1030 // at least at the moment. But we could check more.
1031 return name() == rhs.name()
1032 && latexname() == rhs.latexname()
1033 && latextype == rhs.latextype;