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]
55 LT_PARBREAK_IS_NEWLINE,
65 LT_LABELSTRING_APPENDIX,
108 LT_INTITLE // keep this last!
111 /////////////////////
116 margintype = MARGIN_STATIC;
117 latextype = LATEX_PARAGRAPH;
123 labelfont = inherit_font;
125 reslabelfont = sane_font;
126 nextnoindent = false;
131 labelbottomsep = 0.0;
133 align = LYX_ALIGN_BLOCK;
134 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
135 labeltype = LABEL_NO_LABEL;
136 endlabeltype = END_LABEL_NO_LABEL;
137 // Should or should not. That is the question.
138 // spacing.set(Spacing::OneHalf);
139 newline_allowed = true;
140 free_spacing = false;
142 parbreak_is_newline = false;
143 toclevel = NOT_IN_TOC;
145 htmllabelfirst_ = false;
146 htmlforcecss_ = false;
149 itemcommand_ = "item";
153 bool Layout::read(Lexer & lex, TextClass const & tclass)
155 // This table is sorted alphabetically [asierra 30March96]
156 LexerKeyword layoutTags[] = {
157 { "align", LT_ALIGN },
158 { "alignpossible", LT_ALIGNPOSSIBLE },
159 { "argument", LT_ARGUMENT },
160 { "babelpreamble", LT_BABELPREAMBLE },
161 { "bottomsep", LT_BOTTOMSEP },
162 { "category", LT_CATEGORY },
163 { "commanddepth", LT_COMMANDDEPTH },
164 { "copystyle", LT_COPYSTYLE },
165 { "dependson", LT_DEPENDSON },
167 { "endlabelstring", LT_ENDLABELSTRING },
168 { "endlabeltype", LT_ENDLABELTYPE },
170 { "freespacing", LT_FREE_SPACING },
171 { "htmlattr", LT_HTMLATTR },
172 { "htmlforcecss", LT_HTMLFORCECSS },
173 { "htmlitem", LT_HTMLITEM },
174 { "htmlitemattr", LT_HTMLITEMATTR },
175 { "htmllabel", LT_HTMLLABEL },
176 { "htmllabelattr", LT_HTMLLABELATTR },
177 { "htmllabelfirst", LT_HTMLLABELFIRST },
178 { "htmlpreamble", LT_HTMLPREAMBLE },
179 { "htmlstyle", LT_HTMLSTYLE },
180 { "htmltag", LT_HTMLTAG },
181 { "htmltitle", LT_HTMLTITLE },
182 { "innertag", LT_INNERTAG },
183 { "inpreamble", LT_INPREAMBLE },
184 { "intitle", LT_INTITLE },
185 { "itemcommand", LT_ITEMCOMMAND },
186 { "itemsep", LT_ITEMSEP },
187 { "itemtag", LT_ITEMTAG },
188 { "keepempty", LT_KEEPEMPTY },
189 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
190 { "labelcounter", LT_LABELCOUNTER },
191 { "labelfont", LT_LABELFONT },
192 { "labelindent", LT_LABELINDENT },
193 { "labelsep", LT_LABELSEP },
194 { "labelstring", LT_LABELSTRING },
195 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
196 { "labeltag", LT_LABELTAG },
197 { "labeltype", LT_LABELTYPE },
198 { "langpreamble", LT_LANGPREAMBLE },
199 { "latexname", LT_LATEXNAME },
200 { "latexparam", LT_LATEXPARAM },
201 { "latextype", LT_LATEXTYPE },
202 { "leftdelim", LT_LEFTDELIM },
203 { "leftmargin", LT_LEFTMARGIN },
204 { "margin", LT_MARGIN },
205 { "needprotect", LT_NEED_PROTECT },
206 { "newline", LT_NEWLINE },
207 { "nextnoindent", LT_NEXTNOINDENT },
208 { "obsoletedby", LT_OBSOLETEDBY },
209 { "parbreakisnewline", LT_PARBREAK_IS_NEWLINE },
210 { "parindent", LT_PARINDENT },
211 { "parsep", LT_PARSEP },
212 { "parskip", LT_PARSKIP },
213 { "passthru", LT_PASS_THRU },
214 { "preamble", LT_PREAMBLE },
215 { "refprefix", LT_REFPREFIX },
216 { "requires", LT_REQUIRES },
217 { "resetargs", LT_RESETARGS },
218 { "rightdelim", LT_RIGHTDELIM },
219 { "rightmargin", LT_RIGHTMARGIN },
220 { "spacing", LT_SPACING },
221 { "spellcheck", LT_SPELLCHECK },
222 { "textfont", LT_TEXTFONT },
223 { "toclevel", LT_TOCLEVEL },
224 { "topsep", LT_TOPSEP }
228 bool finished = false;
229 lex.pushTable(layoutTags);
231 // parse style section
232 while (!finished && lex.isOK() && !error) {
234 // See comment in LyXRC.cpp.
236 case Lexer::LEX_FEOF:
239 case Lexer::LEX_UNDEF:
241 lex.printError("Unknown layout tag `$$Token'");
248 switch (static_cast<LayoutTags>(le)) {
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: {
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_, '_', ' ');
338 case LT_NEED_PROTECT:
347 font = lyxRead(lex, font);
352 font = lyxRead(lex, font);
356 labelfont = lyxRead(lex, labelfont);
359 case LT_NEXTNOINDENT:
363 case LT_COMMANDDEPTH:
373 latexparam_ = subst(latexparam_, """, "\"");
378 leftdelim_ = support::subst(leftdelim_, from_ascii("<br/>"),
384 rightdelim_ = support::subst(rightdelim_, from_ascii("<br/>"),
405 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
408 case LT_LANGPREAMBLE:
409 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
412 case LT_BABELPREAMBLE:
413 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
420 case LT_ENDLABELTYPE:
421 readEndLabelType(lex);
456 case LT_LABEL_BOTTOMSEP:
457 lex >> labelbottomsep;
462 labelsep = subst(labelsep, 'x', ' ');
470 lex >> newline_allowed;
477 case LT_ALIGNPOSSIBLE:
478 readAlignPossible(lex);
482 // FIXME: this means LT_ENDLABELSTRING may only
483 // occur after LT_LABELSTRING
485 labelstring_ = trim(labelstring_);
486 labelstring_appendix_ = labelstring_;
489 case LT_ENDLABELSTRING:
490 lex >> endlabelstring_;
491 endlabelstring_ = trim(endlabelstring_);
494 case LT_LABELSTRING_APPENDIX:
495 lex >> labelstring_appendix_;
496 labelstring_appendix_ = trim(labelstring_appendix_);
499 case LT_LABELCOUNTER:
501 counter = trim(counter);
504 case LT_FREE_SPACING:
512 case LT_PARBREAK_IS_NEWLINE:
513 lex >> parbreak_is_newline;
522 vector<string> const req =
523 getVectorFromString(lex.getString());
524 requires_.insert(req.begin(), req.end());
550 case LT_HTMLITEMATTR:
551 lex >> htmlitemattr_;
555 lex >> htmllabeltag_;
558 case LT_HTMLLABELATTR:
559 lex >> htmllabelattr_;
562 case LT_HTMLLABELFIRST:
563 lex >> htmllabelfirst_;
567 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
570 case LT_HTMLFORCECSS:
571 lex >> htmlforcecss_;
574 case LT_HTMLPREAMBLE:
575 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
588 // make sure we only have inpreamble = true for commands
589 if (inpreamble && latextype != LATEX_COMMAND && latextype != LATEX_PARAGRAPH) {
590 LYXERR0("InPreamble not permitted except with command and paragraph layouts.");
591 LYXERR0("Layout name: " << name());
595 return finished && !error;
608 LexerKeyword alignTags[] = {
609 { "block", AT_BLOCK },
610 { "center", AT_CENTER },
611 { "layout", AT_LAYOUT },
613 { "right", AT_RIGHT }
617 void Layout::readAlign(Lexer & lex)
619 PushPopHelper pph(lex, alignTags);
622 case Lexer::LEX_UNDEF:
623 lex.printError("Unknown alignment `$$Token'");
629 align = LYX_ALIGN_BLOCK;
632 align = LYX_ALIGN_LEFT;
635 align = LYX_ALIGN_RIGHT;
638 align = LYX_ALIGN_CENTER;
641 align = LYX_ALIGN_LAYOUT;
647 void Layout::readAlignPossible(Lexer & lex)
649 lex.pushTable(alignTags);
650 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
651 int lineno = lex.lineNumber();
655 case Lexer::LEX_UNDEF:
656 lex.printError("Unknown alignment `$$Token'");
662 alignpossible |= LYX_ALIGN_BLOCK;
665 alignpossible |= LYX_ALIGN_LEFT;
668 alignpossible |= LYX_ALIGN_RIGHT;
671 alignpossible |= LYX_ALIGN_CENTER;
674 alignpossible |= LYX_ALIGN_LAYOUT;
677 } while (lineno == lex.lineNumber());
682 void Layout::readLabelType(Lexer & lex)
688 LA_CENTERED_TOP_ENVIRONMENT,
698 LexerKeyword labelTypeTags[] = {
699 { "bibliography", LA_BIBLIO },
700 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
701 { "counter", LA_COUNTER },
702 { "enumerate", LA_ENUMERATE },
703 { "itemize", LA_ITEMIZE },
704 { "manual", LA_MANUAL },
705 { "no_label", LA_NO_LABEL },
706 { "sensitive", LA_SENSITIVE },
707 { "static", LA_STATIC },
708 { "top_environment", LA_TOP_ENVIRONMENT }
711 PushPopHelper pph(lex, labelTypeTags);
714 case Lexer::LEX_UNDEF:
715 lex.printError("Unknown labeltype tag `$$Token'");
721 labeltype = LABEL_NO_LABEL;
724 labeltype = LABEL_MANUAL;
726 case LA_TOP_ENVIRONMENT:
727 labeltype = LABEL_TOP_ENVIRONMENT;
729 case LA_CENTERED_TOP_ENVIRONMENT:
730 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
733 labeltype = LABEL_STATIC;
736 labeltype = LABEL_SENSITIVE;
739 labeltype = LABEL_COUNTER;
742 labeltype = LABEL_ENUMERATE;
745 labeltype = LABEL_ITEMIZE;
748 labeltype = LABEL_BIBLIO;
754 void Layout::readEndLabelType(Lexer & lex)
756 static LexerKeyword endlabelTypeTags[] = {
757 { "box", END_LABEL_BOX },
758 { "filled_box", END_LABEL_FILLED_BOX },
759 { "no_label", END_LABEL_NO_LABEL },
760 { "static", END_LABEL_STATIC }
763 PushPopHelper pph(lex, endlabelTypeTags);
766 case Lexer::LEX_UNDEF:
767 lex.printError("Unknown labeltype tag `$$Token'");
769 case END_LABEL_STATIC:
771 case END_LABEL_FILLED_BOX:
772 case END_LABEL_NO_LABEL:
773 endlabeltype = static_cast<EndLabelType>(le);
776 LYXERR0("Unhandled value " << le);
782 void Layout::readMargin(Lexer & lex)
784 LexerKeyword marginTags[] = {
785 { "dynamic", MARGIN_DYNAMIC },
786 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
787 { "manual", MARGIN_MANUAL },
788 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
789 { "static", MARGIN_STATIC }
792 PushPopHelper pph(lex, marginTags);
796 case Lexer::LEX_UNDEF:
797 lex.printError("Unknown margin type tag `$$Token'");
802 case MARGIN_FIRST_DYNAMIC:
803 case MARGIN_RIGHT_ADDRESS_BOX:
804 margintype = static_cast<MarginType>(le);
807 LYXERR0("Unhandled value " << le);
813 void Layout::readLatexType(Lexer & lex)
815 LexerKeyword latexTypeTags[] = {
816 { "bib_environment", LATEX_BIB_ENVIRONMENT },
817 { "command", LATEX_COMMAND },
818 { "environment", LATEX_ENVIRONMENT },
819 { "item_environment", LATEX_ITEM_ENVIRONMENT },
820 { "list_environment", LATEX_LIST_ENVIRONMENT },
821 { "paragraph", LATEX_PARAGRAPH }
824 PushPopHelper pph(lex, latexTypeTags);
827 case Lexer::LEX_UNDEF:
828 lex.printError("Unknown latextype tag `$$Token'");
830 case LATEX_PARAGRAPH:
832 case LATEX_ENVIRONMENT:
833 case LATEX_ITEM_ENVIRONMENT:
834 case LATEX_BIB_ENVIRONMENT:
835 case LATEX_LIST_ENVIRONMENT:
836 latextype = static_cast<LatexType>(le);
839 LYXERR0("Unhandled value " << le);
845 void Layout::readSpacing(Lexer & lex)
848 ST_SPACING_SINGLE = 1,
854 LexerKeyword spacingTags[] = {
855 {"double", ST_SPACING_DOUBLE },
856 {"onehalf", ST_SPACING_ONEHALF },
857 {"other", ST_OTHER },
858 {"single", ST_SPACING_SINGLE }
861 PushPopHelper pph(lex, spacingTags);
864 case Lexer::LEX_UNDEF:
865 lex.printError("Unknown spacing token `$$Token'");
870 case ST_SPACING_SINGLE:
871 spacing.set(Spacing::Single);
873 case ST_SPACING_ONEHALF:
874 spacing.set(Spacing::Onehalf);
876 case ST_SPACING_DOUBLE:
877 spacing.set(Spacing::Double);
881 spacing.set(Spacing::Other, lex.getString());
887 void Layout::readArgument(Lexer & lex)
890 arg.mandatory = false;
891 arg.autoinsert = false;
893 bool finished = false;
894 arg.font = inherit_font;
895 arg.labelfont = inherit_font;
898 bool const itemarg = prefixIs(id, "item:");
900 while (!finished && lex.isOK() && !error) {
902 string const tok = ascii_lowercase(lex.getString());
906 } else if (tok == "endargument") {
908 } else if (tok == "labelstring") {
910 arg.labelstring = lex.getDocString();
911 } else if (tok == "menustring") {
913 arg.menustring = lex.getDocString();
914 } else if (tok == "mandatory") {
916 arg.mandatory = lex.getBool();
917 } else if (tok == "autoinsert") {
919 arg.autoinsert = lex.getBool();
920 } else if (tok == "leftdelim") {
922 arg.ldelim = lex.getDocString();
923 arg.ldelim = support::subst(arg.ldelim, from_ascii("<br/>"),
925 } else if (tok == "rightdelim") {
927 arg.rdelim = lex.getDocString();
928 arg.rdelim = support::subst(arg.rdelim, from_ascii("<br/>"),
930 } else if (tok == "presetarg") {
932 arg.presetarg = lex.getDocString();
933 } else if (tok == "tooltip") {
935 arg.tooltip = lex.getDocString();
936 } else if (tok == "requires") {
938 arg.requires = lex.getString();
939 } else if (tok == "decoration") {
941 arg.decoration = lex.getString();
942 } else if (tok == "font") {
943 arg.font = lyxRead(lex, arg.font);
944 } else if (tok == "labelfont") {
945 arg.labelfont = lyxRead(lex, arg.labelfont);
947 lex.printError("Unknown tag");
951 if (arg.labelstring.empty())
952 LYXERR0("Incomplete Argument definition!");
956 latexargs_[id] = arg;
960 Layout::LaTeXArgMap Layout::args() const
962 LaTeXArgMap args = latexargs_;
963 if (!itemargs_.empty())
964 args.insert(itemargs_.begin(), itemargs_.end());
969 int Layout::optArgs() const
972 LaTeXArgMap::const_iterator it = latexargs_.begin();
973 for (; it != latexargs_.end(); ++it) {
974 if (!(*it).second.mandatory)
981 int Layout::requiredArgs() const
984 LaTeXArgMap::const_iterator it = latexargs_.begin();
985 for (; it != latexargs_.end(); ++it) {
986 if ((*it).second.mandatory)
993 string const & Layout::htmltag() const
995 if (htmltag_.empty())
1001 string const & Layout::htmlattr() const
1003 if (htmlattr_.empty())
1004 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
1009 string const & Layout::htmlitemtag() const
1011 if (htmlitemtag_.empty())
1012 htmlitemtag_ = "div";
1013 return htmlitemtag_;
1017 string const & Layout::htmlitemattr() const
1019 if (htmlitemattr_.empty())
1020 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
1021 return htmlitemattr_;
1025 string const & Layout::htmllabeltag() const
1027 if (htmllabeltag_.empty()) {
1028 if (labeltype != LABEL_TOP_ENVIRONMENT &&
1029 labeltype != LABEL_CENTERED_TOP_ENVIRONMENT)
1030 htmllabeltag_ = "span";
1032 htmllabeltag_ = "div";
1034 return htmllabeltag_;
1038 string const & Layout::htmllabelattr() const
1040 if (htmllabelattr_.empty())
1041 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
1042 return htmllabelattr_;
1046 docstring Layout::htmlstyle() const
1048 if (!htmlstyle_.empty() && !htmlforcecss_)
1050 if (htmldefaultstyle_.empty())
1052 docstring retval = htmldefaultstyle_;
1053 if (!htmlstyle_.empty())
1054 retval += '\n' + htmlstyle_;
1059 string Layout::defaultCSSClass() const
1061 if (!defaultcssclass_.empty())
1062 return defaultcssclass_;
1064 docstring::const_iterator it = name().begin();
1065 docstring::const_iterator en = name().end();
1066 for (; it != en; ++it) {
1067 char_type const c = *it;
1068 if (!isAlphaASCII(c)) {
1070 // make sure we don't start with an underscore,
1071 // as that sometimes causes problems.
1072 d = from_ascii("lyx_");
1075 } else if (isLower(c))
1078 // this is slow, so do it only if necessary
1081 defaultcssclass_ = to_utf8(d);
1082 return defaultcssclass_;
1088 string makeMarginValue(char const * side, double d)
1091 os << "margin-" << side << ": " << d << "ex;\n";
1098 void Layout::makeDefaultCSS() const
1100 // this never needs to be redone, since reloading layouts will
1101 // wipe out what we did before.
1102 if (!htmldefaultstyle_.empty())
1106 htmldefaultstyle_ = font.asCSS();
1111 tmp += makeMarginValue("top", topsep);
1113 tmp += makeMarginValue("bottom", bottomsep);
1114 if (!leftmargin.empty()) {
1115 // we can't really do what LyX does with the margin, so
1116 // we'll just figure out how many characters it is
1117 int const len = leftmargin.length();
1118 tmp += makeMarginValue("left", len);
1120 if (!rightmargin.empty()) {
1121 int const len = rightmargin.length();
1122 tmp += makeMarginValue("right", len);
1126 if (!htmldefaultstyle_.empty())
1127 htmldefaultstyle_ += from_ascii("\n");
1128 htmldefaultstyle_ += from_ascii(tmp);
1131 // tex2lyx does not see output_xhtml.cpp
1134 string where = alignmentToCSS(align);
1135 if (!where.empty()) {
1136 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1140 // wrap up what we have, if anything
1141 if (!htmldefaultstyle_.empty())
1143 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1144 htmldefaultstyle_ + from_ascii("\n}\n");
1146 if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1152 if (labelfont != font)
1153 labelCSS = labelfont.asCSS() + from_ascii("\n");
1154 if (labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1155 labelCSS += from_ascii("text-align: center;\n");
1157 if (!labelCSS.empty())
1158 htmldefaultstyle_ +=
1159 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1160 labelCSS + from_ascii("\n}\n");
1164 bool Layout::operator==(Layout const & rhs) const
1166 // This is enough for the applications we actually make,
1167 // at least at the moment. But we could check more.
1168 return name() == rhs.name()
1169 && latexname() == rhs.latexname()
1170 && latextype == rhs.latextype;