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_, '_', ' ');
331 postcommandargs_.clear();
339 case LT_NEED_PROTECT:
348 font = lyxRead(lex, font);
353 font = lyxRead(lex, font);
357 labelfont = lyxRead(lex, labelfont);
360 case LT_NEXTNOINDENT:
364 case LT_COMMANDDEPTH:
374 latexparam_ = subst(latexparam_, """, "\"");
379 leftdelim_ = support::subst(leftdelim_, from_ascii("<br/>"),
385 rightdelim_ = support::subst(rightdelim_, from_ascii("<br/>"),
406 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
409 case LT_LANGPREAMBLE:
410 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
413 case LT_BABELPREAMBLE:
414 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
421 case LT_ENDLABELTYPE:
422 readEndLabelType(lex);
457 case LT_LABEL_BOTTOMSEP:
458 lex >> labelbottomsep;
463 labelsep = subst(labelsep, 'x', ' ');
471 lex >> newline_allowed;
478 case LT_ALIGNPOSSIBLE:
479 readAlignPossible(lex);
483 // FIXME: this means LT_ENDLABELSTRING may only
484 // occur after LT_LABELSTRING
486 labelstring_ = trim(labelstring_);
487 labelstring_appendix_ = labelstring_;
490 case LT_ENDLABELSTRING:
491 lex >> endlabelstring_;
492 endlabelstring_ = trim(endlabelstring_);
495 case LT_LABELSTRING_APPENDIX:
496 lex >> labelstring_appendix_;
497 labelstring_appendix_ = trim(labelstring_appendix_);
500 case LT_LABELCOUNTER:
502 counter = trim(counter);
505 case LT_FREE_SPACING:
513 case LT_PARBREAK_IS_NEWLINE:
514 lex >> parbreak_is_newline;
523 vector<string> const req =
524 getVectorFromString(lex.getString());
525 requires_.insert(req.begin(), req.end());
551 case LT_HTMLITEMATTR:
552 lex >> htmlitemattr_;
556 lex >> htmllabeltag_;
559 case LT_HTMLLABELATTR:
560 lex >> htmllabelattr_;
563 case LT_HTMLLABELFIRST:
564 lex >> htmllabelfirst_;
568 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
571 case LT_HTMLFORCECSS:
572 lex >> htmlforcecss_;
575 case LT_HTMLPREAMBLE:
576 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
589 // make sure we only have inpreamble = true for commands
590 if (inpreamble && latextype != LATEX_COMMAND && latextype != LATEX_PARAGRAPH) {
591 LYXERR0("InPreamble not permitted except with command and paragraph layouts.");
592 LYXERR0("Layout name: " << name());
596 return finished && !error;
609 LexerKeyword alignTags[] = {
610 { "block", AT_BLOCK },
611 { "center", AT_CENTER },
612 { "layout", AT_LAYOUT },
614 { "right", AT_RIGHT }
618 void Layout::readAlign(Lexer & lex)
620 PushPopHelper pph(lex, alignTags);
623 case Lexer::LEX_UNDEF:
624 lex.printError("Unknown alignment `$$Token'");
630 align = LYX_ALIGN_BLOCK;
633 align = LYX_ALIGN_LEFT;
636 align = LYX_ALIGN_RIGHT;
639 align = LYX_ALIGN_CENTER;
642 align = LYX_ALIGN_LAYOUT;
648 void Layout::readAlignPossible(Lexer & lex)
650 lex.pushTable(alignTags);
651 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
652 int lineno = lex.lineNumber();
656 case Lexer::LEX_UNDEF:
657 lex.printError("Unknown alignment `$$Token'");
663 alignpossible |= LYX_ALIGN_BLOCK;
666 alignpossible |= LYX_ALIGN_LEFT;
669 alignpossible |= LYX_ALIGN_RIGHT;
672 alignpossible |= LYX_ALIGN_CENTER;
675 alignpossible |= LYX_ALIGN_LAYOUT;
678 } while (lineno == lex.lineNumber());
683 void Layout::readLabelType(Lexer & lex)
689 LA_CENTERED_TOP_ENVIRONMENT,
699 LexerKeyword labelTypeTags[] = {
700 { "bibliography", LA_BIBLIO },
701 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
702 { "counter", LA_COUNTER },
703 { "enumerate", LA_ENUMERATE },
704 { "itemize", LA_ITEMIZE },
705 { "manual", LA_MANUAL },
706 { "no_label", LA_NO_LABEL },
707 { "sensitive", LA_SENSITIVE },
708 { "static", LA_STATIC },
709 { "top_environment", LA_TOP_ENVIRONMENT }
712 PushPopHelper pph(lex, labelTypeTags);
715 case Lexer::LEX_UNDEF:
716 lex.printError("Unknown labeltype tag `$$Token'");
722 labeltype = LABEL_NO_LABEL;
725 labeltype = LABEL_MANUAL;
727 case LA_TOP_ENVIRONMENT:
728 labeltype = LABEL_TOP_ENVIRONMENT;
730 case LA_CENTERED_TOP_ENVIRONMENT:
731 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
734 labeltype = LABEL_STATIC;
737 labeltype = LABEL_SENSITIVE;
740 labeltype = LABEL_COUNTER;
743 labeltype = LABEL_ENUMERATE;
746 labeltype = LABEL_ITEMIZE;
749 labeltype = LABEL_BIBLIO;
755 void Layout::readEndLabelType(Lexer & lex)
757 static LexerKeyword endlabelTypeTags[] = {
758 { "box", END_LABEL_BOX },
759 { "filled_box", END_LABEL_FILLED_BOX },
760 { "no_label", END_LABEL_NO_LABEL },
761 { "static", END_LABEL_STATIC }
764 PushPopHelper pph(lex, endlabelTypeTags);
767 case Lexer::LEX_UNDEF:
768 lex.printError("Unknown labeltype tag `$$Token'");
770 case END_LABEL_STATIC:
772 case END_LABEL_FILLED_BOX:
773 case END_LABEL_NO_LABEL:
774 endlabeltype = static_cast<EndLabelType>(le);
777 LYXERR0("Unhandled value " << le);
783 void Layout::readMargin(Lexer & lex)
785 LexerKeyword marginTags[] = {
786 { "dynamic", MARGIN_DYNAMIC },
787 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
788 { "manual", MARGIN_MANUAL },
789 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
790 { "static", MARGIN_STATIC }
793 PushPopHelper pph(lex, marginTags);
797 case Lexer::LEX_UNDEF:
798 lex.printError("Unknown margin type tag `$$Token'");
803 case MARGIN_FIRST_DYNAMIC:
804 case MARGIN_RIGHT_ADDRESS_BOX:
805 margintype = static_cast<MarginType>(le);
808 LYXERR0("Unhandled value " << le);
814 void Layout::readLatexType(Lexer & lex)
816 LexerKeyword latexTypeTags[] = {
817 { "bib_environment", LATEX_BIB_ENVIRONMENT },
818 { "command", LATEX_COMMAND },
819 { "environment", LATEX_ENVIRONMENT },
820 { "item_environment", LATEX_ITEM_ENVIRONMENT },
821 { "list_environment", LATEX_LIST_ENVIRONMENT },
822 { "paragraph", LATEX_PARAGRAPH }
825 PushPopHelper pph(lex, latexTypeTags);
828 case Lexer::LEX_UNDEF:
829 lex.printError("Unknown latextype tag `$$Token'");
831 case LATEX_PARAGRAPH:
833 case LATEX_ENVIRONMENT:
834 case LATEX_ITEM_ENVIRONMENT:
835 case LATEX_BIB_ENVIRONMENT:
836 case LATEX_LIST_ENVIRONMENT:
837 latextype = static_cast<LatexType>(le);
840 LYXERR0("Unhandled value " << le);
846 void Layout::readSpacing(Lexer & lex)
849 ST_SPACING_SINGLE = 1,
855 LexerKeyword spacingTags[] = {
856 {"double", ST_SPACING_DOUBLE },
857 {"onehalf", ST_SPACING_ONEHALF },
858 {"other", ST_OTHER },
859 {"single", ST_SPACING_SINGLE }
862 PushPopHelper pph(lex, spacingTags);
865 case Lexer::LEX_UNDEF:
866 lex.printError("Unknown spacing token `$$Token'");
871 case ST_SPACING_SINGLE:
872 spacing.set(Spacing::Single);
874 case ST_SPACING_ONEHALF:
875 spacing.set(Spacing::Onehalf);
877 case ST_SPACING_DOUBLE:
878 spacing.set(Spacing::Double);
882 spacing.set(Spacing::Other, lex.getString());
888 void Layout::readArgument(Lexer & lex)
891 arg.mandatory = false;
892 arg.autoinsert = false;
894 bool finished = false;
895 arg.font = inherit_font;
896 arg.labelfont = inherit_font;
899 bool const itemarg = prefixIs(id, "item:");
900 bool const postcmd = prefixIs(id, "post:");
902 while (!finished && lex.isOK() && !error) {
904 string const tok = ascii_lowercase(lex.getString());
908 } else if (tok == "endargument") {
910 } else if (tok == "labelstring") {
912 arg.labelstring = lex.getDocString();
913 } else if (tok == "menustring") {
915 arg.menustring = lex.getDocString();
916 } else if (tok == "mandatory") {
918 arg.mandatory = lex.getBool();
919 } else if (tok == "autoinsert") {
921 arg.autoinsert = lex.getBool();
922 } else if (tok == "leftdelim") {
924 arg.ldelim = lex.getDocString();
925 arg.ldelim = support::subst(arg.ldelim, from_ascii("<br/>"),
927 } else if (tok == "rightdelim") {
929 arg.rdelim = lex.getDocString();
930 arg.rdelim = support::subst(arg.rdelim, from_ascii("<br/>"),
932 } else if (tok == "presetarg") {
934 arg.presetarg = lex.getDocString();
935 } else if (tok == "tooltip") {
937 arg.tooltip = lex.getDocString();
938 } else if (tok == "requires") {
940 arg.requires = lex.getString();
941 } else if (tok == "decoration") {
943 arg.decoration = lex.getString();
944 } else if (tok == "font") {
945 arg.font = lyxRead(lex, arg.font);
946 } else if (tok == "labelfont") {
947 arg.labelfont = lyxRead(lex, arg.labelfont);
949 lex.printError("Unknown tag");
953 if (arg.labelstring.empty())
954 LYXERR0("Incomplete Argument definition!");
958 postcommandargs_[id] = arg;
960 latexargs_[id] = arg;
964 Layout::LaTeXArgMap Layout::args() const
966 LaTeXArgMap args = latexargs_;
967 if (!postcommandargs_.empty())
968 args.insert(postcommandargs_.begin(), postcommandargs_.end());
969 if (!itemargs_.empty())
970 args.insert(itemargs_.begin(), itemargs_.end());
975 int Layout::optArgs() const
978 LaTeXArgMap::const_iterator it = latexargs_.begin();
979 for (; it != latexargs_.end(); ++it) {
980 if (!(*it).second.mandatory)
983 LaTeXArgMap::const_iterator iit = postcommandargs_.begin();
984 for (; iit != postcommandargs_.end(); ++iit) {
985 if (!(*iit).second.mandatory)
992 int Layout::requiredArgs() const
995 LaTeXArgMap::const_iterator it = latexargs_.begin();
996 for (; it != latexargs_.end(); ++it) {
997 if ((*it).second.mandatory)
1000 LaTeXArgMap::const_iterator iit = postcommandargs_.begin();
1001 for (; iit != postcommandargs_.end(); ++iit) {
1002 if (!(*iit).second.mandatory)
1009 string const & Layout::htmltag() const
1011 if (htmltag_.empty())
1017 string const & Layout::htmlattr() const
1019 if (htmlattr_.empty())
1020 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
1025 string const & Layout::htmlitemtag() const
1027 if (htmlitemtag_.empty())
1028 htmlitemtag_ = "div";
1029 return htmlitemtag_;
1033 string const & Layout::htmlitemattr() const
1035 if (htmlitemattr_.empty())
1036 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
1037 return htmlitemattr_;
1041 string const & Layout::htmllabeltag() const
1043 if (htmllabeltag_.empty()) {
1044 if (labeltype != LABEL_TOP_ENVIRONMENT &&
1045 labeltype != LABEL_CENTERED_TOP_ENVIRONMENT)
1046 htmllabeltag_ = "span";
1048 htmllabeltag_ = "div";
1050 return htmllabeltag_;
1054 string const & Layout::htmllabelattr() const
1056 if (htmllabelattr_.empty())
1057 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
1058 return htmllabelattr_;
1062 docstring Layout::htmlstyle() const
1064 if (!htmlstyle_.empty() && !htmlforcecss_)
1066 if (htmldefaultstyle_.empty())
1068 docstring retval = htmldefaultstyle_;
1069 if (!htmlstyle_.empty())
1070 retval += '\n' + htmlstyle_;
1075 string Layout::defaultCSSClass() const
1077 if (!defaultcssclass_.empty())
1078 return defaultcssclass_;
1080 docstring::const_iterator it = name().begin();
1081 docstring::const_iterator en = name().end();
1082 for (; it != en; ++it) {
1083 char_type const c = *it;
1084 if (!isAlphaASCII(c)) {
1086 // make sure we don't start with an underscore,
1087 // as that sometimes causes problems.
1088 d = from_ascii("lyx_");
1091 } else if (isLower(c))
1094 // this is slow, so do it only if necessary
1097 defaultcssclass_ = to_utf8(d);
1098 return defaultcssclass_;
1104 string makeMarginValue(char const * side, double d)
1107 os << "margin-" << side << ": " << d << "ex;\n";
1114 void Layout::makeDefaultCSS() const
1116 // this never needs to be redone, since reloading layouts will
1117 // wipe out what we did before.
1118 if (!htmldefaultstyle_.empty())
1122 htmldefaultstyle_ = font.asCSS();
1127 tmp += makeMarginValue("top", topsep);
1129 tmp += makeMarginValue("bottom", bottomsep);
1130 if (!leftmargin.empty()) {
1131 // we can't really do what LyX does with the margin, so
1132 // we'll just figure out how many characters it is
1133 int const len = leftmargin.length();
1134 tmp += makeMarginValue("left", len);
1136 if (!rightmargin.empty()) {
1137 int const len = rightmargin.length();
1138 tmp += makeMarginValue("right", len);
1142 if (!htmldefaultstyle_.empty())
1143 htmldefaultstyle_ += from_ascii("\n");
1144 htmldefaultstyle_ += from_ascii(tmp);
1147 // tex2lyx does not see output_xhtml.cpp
1150 string where = alignmentToCSS(align);
1151 if (!where.empty()) {
1152 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1156 // wrap up what we have, if anything
1157 if (!htmldefaultstyle_.empty())
1159 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1160 htmldefaultstyle_ + from_ascii("\n}\n");
1162 if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1168 if (labelfont != font)
1169 labelCSS = labelfont.asCSS() + from_ascii("\n");
1170 if (labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1171 labelCSS += from_ascii("text-align: center;\n");
1173 if (!labelCSS.empty())
1174 htmldefaultstyle_ +=
1175 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1176 labelCSS + from_ascii("\n}\n");
1180 bool Layout::operator==(Layout const & rhs) const
1182 // This is enough for the applications we actually make,
1183 // at least at the moment. But we could check more.
1184 return name() == rhs.name()
1185 && latexname() == rhs.latexname()
1186 && latextype == rhs.latextype;