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,
64 LT_LABELSTRING_APPENDIX,
107 LT_INTITLE // keep this last!
110 /////////////////////
115 margintype = MARGIN_STATIC;
116 latextype = LATEX_PARAGRAPH;
122 labelfont = inherit_font;
124 reslabelfont = sane_font;
125 nextnoindent = false;
130 labelbottomsep = 0.0;
132 align = LYX_ALIGN_BLOCK;
133 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
134 labeltype = LABEL_NO_LABEL;
135 endlabeltype = END_LABEL_NO_LABEL;
136 // Should or should not. That is the question.
137 // spacing.set(Spacing::OneHalf);
138 newline_allowed = true;
139 free_spacing = false;
141 parbreak_is_newline = false;
142 toclevel = NOT_IN_TOC;
144 htmllabelfirst_ = false;
145 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 { "argument", LT_ARGUMENT },
158 { "babelpreamble", LT_BABELPREAMBLE },
159 { "bottomsep", LT_BOTTOMSEP },
160 { "category", LT_CATEGORY },
161 { "commanddepth", LT_COMMANDDEPTH },
162 { "copystyle", LT_COPYSTYLE },
163 { "dependson", LT_DEPENDSON },
165 { "endlabelstring", LT_ENDLABELSTRING },
166 { "endlabeltype", LT_ENDLABELTYPE },
168 { "freespacing", LT_FREE_SPACING },
169 { "htmlattr", LT_HTMLATTR },
170 { "htmlforcecss", LT_HTMLFORCECSS },
171 { "htmlitem", LT_HTMLITEM },
172 { "htmlitemattr", LT_HTMLITEMATTR },
173 { "htmllabel", LT_HTMLLABEL },
174 { "htmllabelattr", LT_HTMLLABELATTR },
175 { "htmllabelfirst", LT_HTMLLABELFIRST },
176 { "htmlpreamble", LT_HTMLPREAMBLE },
177 { "htmlstyle", LT_HTMLSTYLE },
178 { "htmltag", LT_HTMLTAG },
179 { "htmltitle", LT_HTMLTITLE },
180 { "innertag", LT_INNERTAG },
181 { "inpreamble", LT_INPREAMBLE },
182 { "intitle", LT_INTITLE },
183 { "itemsep", LT_ITEMSEP },
184 { "itemtag", LT_ITEMTAG },
185 { "keepempty", LT_KEEPEMPTY },
186 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
187 { "labelcounter", LT_LABELCOUNTER },
188 { "labelfont", LT_LABELFONT },
189 { "labelindent", LT_LABELINDENT },
190 { "labelsep", LT_LABELSEP },
191 { "labelstring", LT_LABELSTRING },
192 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
193 { "labeltag", LT_LABELTAG },
194 { "labeltype", LT_LABELTYPE },
195 { "langpreamble", LT_LANGPREAMBLE },
196 { "latexname", LT_LATEXNAME },
197 { "latexparam", LT_LATEXPARAM },
198 { "latextype", LT_LATEXTYPE },
199 { "leftdelim", LT_LEFTDELIM },
200 { "leftmargin", LT_LEFTMARGIN },
201 { "margin", LT_MARGIN },
202 { "needprotect", LT_NEED_PROTECT },
203 { "newline", LT_NEWLINE },
204 { "nextnoindent", LT_NEXTNOINDENT },
205 { "obsoletedby", LT_OBSOLETEDBY },
206 { "parbreakisnewline", LT_PARBREAK_IS_NEWLINE },
207 { "parindent", LT_PARINDENT },
208 { "parsep", LT_PARSEP },
209 { "parskip", LT_PARSKIP },
210 { "passthru", LT_PASS_THRU },
211 { "preamble", LT_PREAMBLE },
212 { "refprefix", LT_REFPREFIX },
213 { "requires", LT_REQUIRES },
214 { "resetargs", LT_RESETARGS },
215 { "rightdelim", LT_RIGHTDELIM },
216 { "rightmargin", LT_RIGHTMARGIN },
217 { "spacing", LT_SPACING },
218 { "spellcheck", LT_SPELLCHECK },
219 { "textfont", LT_TEXTFONT },
220 { "toclevel", LT_TOCLEVEL },
221 { "topsep", LT_TOPSEP }
225 bool finished = false;
226 lex.pushTable(layoutTags);
228 // parse style section
229 while (!finished && lex.isOK() && !error) {
231 // See comment in LyXRC.cpp.
233 case Lexer::LEX_FEOF:
236 case Lexer::LEX_UNDEF:
238 lex.printError("Unknown layout tag `$$Token'");
245 switch (static_cast<LayoutTags>(le)) {
257 style = subst(style, '_', ' ');
259 if (tclass.hasLayout(style)) {
260 docstring const tmpname = name_;
261 this->operator=(tclass[style]);
264 LYXERR0("Cannot copy unknown style `"
266 << "All layouts so far:");
267 DocumentClass::const_iterator lit = tclass.begin();
268 DocumentClass::const_iterator len = tclass.end();
269 for (; lit != len; ++lit)
270 LYXERR0(lit->name());
275 case LT_OBSOLETEDBY: {
278 style = subst(style, '_', ' ');
280 if (tclass.hasLayout(style)) {
281 docstring const tmpname = name_;
282 this->operator=(tclass[style]);
284 if (obsoleted_by().empty())
285 obsoleted_by_ = style;
287 LYXERR0("Cannot replace with unknown style `"
290 //lex.printError("Cannot replace with"
299 depends_on_ = subst(depends_on_, '_', ' ');
335 case LT_NEED_PROTECT:
344 font = lyxRead(lex, font);
349 font = lyxRead(lex, font);
353 labelfont = lyxRead(lex, labelfont);
356 case LT_NEXTNOINDENT:
360 case LT_COMMANDDEPTH:
370 latexparam_ = subst(latexparam_, """, "\"");
375 leftdelim_ = support::subst(leftdelim_, from_ascii("<br/>"),
381 rightdelim_ = support::subst(rightdelim_, from_ascii("<br/>"),
398 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
401 case LT_LANGPREAMBLE:
402 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
405 case LT_BABELPREAMBLE:
406 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
413 case LT_ENDLABELTYPE:
414 readEndLabelType(lex);
449 case LT_LABEL_BOTTOMSEP:
450 lex >> labelbottomsep;
455 labelsep = subst(labelsep, 'x', ' ');
463 lex >> newline_allowed;
470 case LT_ALIGNPOSSIBLE:
471 readAlignPossible(lex);
475 // FIXME: this means LT_ENDLABELSTRING may only
476 // occur after LT_LABELSTRING
478 labelstring_ = trim(labelstring_);
479 labelstring_appendix_ = labelstring_;
482 case LT_ENDLABELSTRING:
483 lex >> endlabelstring_;
484 endlabelstring_ = trim(endlabelstring_);
487 case LT_LABELSTRING_APPENDIX:
488 lex >> labelstring_appendix_;
489 labelstring_appendix_ = trim(labelstring_appendix_);
492 case LT_LABELCOUNTER:
494 counter = trim(counter);
497 case LT_FREE_SPACING:
505 case LT_PARBREAK_IS_NEWLINE:
506 lex >> parbreak_is_newline;
515 vector<string> const req =
516 getVectorFromString(lex.getString());
517 requires_.insert(req.begin(), req.end());
543 case LT_HTMLITEMATTR:
544 lex >> htmlitemattr_;
548 lex >> htmllabeltag_;
551 case LT_HTMLLABELATTR:
552 lex >> htmllabelattr_;
555 case LT_HTMLLABELFIRST:
556 lex >> htmllabelfirst_;
560 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
563 case LT_HTMLFORCECSS:
564 lex >> htmlforcecss_;
567 case LT_HTMLPREAMBLE:
568 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
581 // make sure we only have inpreamble = true for commands
582 if (inpreamble && latextype != LATEX_COMMAND && latextype != LATEX_PARAGRAPH) {
583 LYXERR0("InPreamble not permitted except with command and paragraph layouts.");
584 LYXERR0("Layout name: " << name());
588 return finished && !error;
601 LexerKeyword alignTags[] = {
602 { "block", AT_BLOCK },
603 { "center", AT_CENTER },
604 { "layout", AT_LAYOUT },
606 { "right", AT_RIGHT }
610 void Layout::readAlign(Lexer & lex)
612 PushPopHelper pph(lex, alignTags);
615 case Lexer::LEX_UNDEF:
616 lex.printError("Unknown alignment `$$Token'");
622 align = LYX_ALIGN_BLOCK;
625 align = LYX_ALIGN_LEFT;
628 align = LYX_ALIGN_RIGHT;
631 align = LYX_ALIGN_CENTER;
634 align = LYX_ALIGN_LAYOUT;
640 void Layout::readAlignPossible(Lexer & lex)
642 lex.pushTable(alignTags);
643 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
644 int lineno = lex.lineNumber();
648 case Lexer::LEX_UNDEF:
649 lex.printError("Unknown alignment `$$Token'");
655 alignpossible |= LYX_ALIGN_BLOCK;
658 alignpossible |= LYX_ALIGN_LEFT;
661 alignpossible |= LYX_ALIGN_RIGHT;
664 alignpossible |= LYX_ALIGN_CENTER;
667 alignpossible |= LYX_ALIGN_LAYOUT;
670 } while (lineno == lex.lineNumber());
675 void Layout::readLabelType(Lexer & lex)
681 LA_CENTERED_TOP_ENVIRONMENT,
691 LexerKeyword labelTypeTags[] = {
692 { "bibliography", LA_BIBLIO },
693 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
694 { "counter", LA_COUNTER },
695 { "enumerate", LA_ENUMERATE },
696 { "itemize", LA_ITEMIZE },
697 { "manual", LA_MANUAL },
698 { "no_label", LA_NO_LABEL },
699 { "sensitive", LA_SENSITIVE },
700 { "static", LA_STATIC },
701 { "top_environment", LA_TOP_ENVIRONMENT }
704 PushPopHelper pph(lex, labelTypeTags);
707 case Lexer::LEX_UNDEF:
708 lex.printError("Unknown labeltype tag `$$Token'");
714 labeltype = LABEL_NO_LABEL;
717 labeltype = LABEL_MANUAL;
719 case LA_TOP_ENVIRONMENT:
720 labeltype = LABEL_TOP_ENVIRONMENT;
722 case LA_CENTERED_TOP_ENVIRONMENT:
723 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
726 labeltype = LABEL_STATIC;
729 labeltype = LABEL_SENSITIVE;
732 labeltype = LABEL_COUNTER;
735 labeltype = LABEL_ENUMERATE;
738 labeltype = LABEL_ITEMIZE;
741 labeltype = LABEL_BIBLIO;
747 void Layout::readEndLabelType(Lexer & lex)
749 static LexerKeyword endlabelTypeTags[] = {
750 { "box", END_LABEL_BOX },
751 { "filled_box", END_LABEL_FILLED_BOX },
752 { "no_label", END_LABEL_NO_LABEL },
753 { "static", END_LABEL_STATIC }
756 PushPopHelper pph(lex, endlabelTypeTags);
759 case Lexer::LEX_UNDEF:
760 lex.printError("Unknown labeltype tag `$$Token'");
762 case END_LABEL_STATIC:
764 case END_LABEL_FILLED_BOX:
765 case END_LABEL_NO_LABEL:
766 endlabeltype = static_cast<EndLabelType>(le);
769 LYXERR0("Unhandled value " << le);
775 void Layout::readMargin(Lexer & lex)
777 LexerKeyword marginTags[] = {
778 { "dynamic", MARGIN_DYNAMIC },
779 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
780 { "manual", MARGIN_MANUAL },
781 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
782 { "static", MARGIN_STATIC }
785 PushPopHelper pph(lex, marginTags);
789 case Lexer::LEX_UNDEF:
790 lex.printError("Unknown margin type tag `$$Token'");
795 case MARGIN_FIRST_DYNAMIC:
796 case MARGIN_RIGHT_ADDRESS_BOX:
797 margintype = static_cast<MarginType>(le);
800 LYXERR0("Unhandled value " << le);
806 void Layout::readLatexType(Lexer & lex)
808 LexerKeyword latexTypeTags[] = {
809 { "bib_environment", LATEX_BIB_ENVIRONMENT },
810 { "command", LATEX_COMMAND },
811 { "environment", LATEX_ENVIRONMENT },
812 { "item_environment", LATEX_ITEM_ENVIRONMENT },
813 { "list_environment", LATEX_LIST_ENVIRONMENT },
814 { "paragraph", LATEX_PARAGRAPH }
817 PushPopHelper pph(lex, latexTypeTags);
820 case Lexer::LEX_UNDEF:
821 lex.printError("Unknown latextype tag `$$Token'");
823 case LATEX_PARAGRAPH:
825 case LATEX_ENVIRONMENT:
826 case LATEX_ITEM_ENVIRONMENT:
827 case LATEX_BIB_ENVIRONMENT:
828 case LATEX_LIST_ENVIRONMENT:
829 latextype = static_cast<LatexType>(le);
832 LYXERR0("Unhandled value " << le);
838 void Layout::readSpacing(Lexer & lex)
841 ST_SPACING_SINGLE = 1,
847 LexerKeyword spacingTags[] = {
848 {"double", ST_SPACING_DOUBLE },
849 {"onehalf", ST_SPACING_ONEHALF },
850 {"other", ST_OTHER },
851 {"single", ST_SPACING_SINGLE }
854 PushPopHelper pph(lex, spacingTags);
857 case Lexer::LEX_UNDEF:
858 lex.printError("Unknown spacing token `$$Token'");
863 case ST_SPACING_SINGLE:
864 spacing.set(Spacing::Single);
866 case ST_SPACING_ONEHALF:
867 spacing.set(Spacing::Onehalf);
869 case ST_SPACING_DOUBLE:
870 spacing.set(Spacing::Double);
874 spacing.set(Spacing::Other, lex.getString());
880 void Layout::readArgument(Lexer & lex)
883 arg.mandatory = false;
884 arg.autoinsert = false;
886 bool finished = false;
887 arg.font = inherit_font;
888 arg.labelfont = inherit_font;
891 bool const itemarg = prefixIs(id, "item:");
893 while (!finished && lex.isOK() && !error) {
895 string const tok = ascii_lowercase(lex.getString());
899 } else if (tok == "endargument") {
901 } else if (tok == "labelstring") {
903 arg.labelstring = lex.getDocString();
904 } else if (tok == "menustring") {
906 arg.menustring = lex.getDocString();
907 } else if (tok == "mandatory") {
909 arg.mandatory = lex.getBool();
910 } else if (tok == "autoinsert") {
912 arg.autoinsert = lex.getBool();
913 } else if (tok == "leftdelim") {
915 arg.ldelim = lex.getDocString();
916 arg.ldelim = support::subst(arg.ldelim, from_ascii("<br/>"),
918 } else if (tok == "rightdelim") {
920 arg.rdelim = lex.getDocString();
921 arg.rdelim = support::subst(arg.rdelim, from_ascii("<br/>"),
923 } else if (tok == "presetarg") {
925 arg.presetarg = lex.getDocString();
926 } else if (tok == "tooltip") {
928 arg.tooltip = lex.getDocString();
929 } else if (tok == "requires") {
931 arg.requires = lex.getString();
932 } else if (tok == "decoration") {
934 arg.decoration = lex.getString();
935 } else if (tok == "font") {
936 arg.font = lyxRead(lex, arg.font);
937 } else if (tok == "labelfont") {
938 arg.labelfont = lyxRead(lex, arg.labelfont);
940 lex.printError("Unknown tag");
944 if (arg.labelstring.empty())
945 LYXERR0("Incomplete Argument definition!");
949 latexargs_[id] = arg;
953 Layout::LaTeXArgMap Layout::args() const
955 LaTeXArgMap args = latexargs_;
956 if (!itemargs_.empty())
957 args.insert(itemargs_.begin(), itemargs_.end());
962 int Layout::optArgs() const
965 LaTeXArgMap::const_iterator it = latexargs_.begin();
966 for (; it != latexargs_.end(); ++it) {
967 if (!(*it).second.mandatory)
974 int Layout::requiredArgs() const
977 LaTeXArgMap::const_iterator it = latexargs_.begin();
978 for (; it != latexargs_.end(); ++it) {
979 if ((*it).second.mandatory)
986 string const & Layout::htmltag() const
988 if (htmltag_.empty())
994 string const & Layout::htmlattr() const
996 if (htmlattr_.empty())
997 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
1002 string const & Layout::htmlitemtag() const
1004 if (htmlitemtag_.empty())
1005 htmlitemtag_ = "div";
1006 return htmlitemtag_;
1010 string const & Layout::htmlitemattr() const
1012 if (htmlitemattr_.empty())
1013 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
1014 return htmlitemattr_;
1018 string const & Layout::htmllabeltag() const
1020 if (htmllabeltag_.empty()) {
1021 if (labeltype != LABEL_TOP_ENVIRONMENT &&
1022 labeltype != LABEL_CENTERED_TOP_ENVIRONMENT)
1023 htmllabeltag_ = "span";
1025 htmllabeltag_ = "div";
1027 return htmllabeltag_;
1031 string const & Layout::htmllabelattr() const
1033 if (htmllabelattr_.empty())
1034 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
1035 return htmllabelattr_;
1039 docstring Layout::htmlstyle() const
1041 if (!htmlstyle_.empty() && !htmlforcecss_)
1043 if (htmldefaultstyle_.empty())
1045 docstring retval = htmldefaultstyle_;
1046 if (!htmlstyle_.empty())
1047 retval += '\n' + htmlstyle_;
1052 string Layout::defaultCSSClass() const
1054 if (!defaultcssclass_.empty())
1055 return defaultcssclass_;
1057 docstring::const_iterator it = name().begin();
1058 docstring::const_iterator en = name().end();
1059 for (; it != en; ++it) {
1060 char_type const c = *it;
1061 if (!isAlphaASCII(c)) {
1063 // make sure we don't start with an underscore,
1064 // as that sometimes causes problems.
1065 d = from_ascii("lyx_");
1068 } else if (isLower(c))
1071 // this is slow, so do it only if necessary
1074 defaultcssclass_ = to_utf8(d);
1075 return defaultcssclass_;
1081 string makeMarginValue(char const * side, double d)
1084 os << "margin-" << side << ": " << d << "ex;\n";
1091 void Layout::makeDefaultCSS() const
1093 // this never needs to be redone, since reloading layouts will
1094 // wipe out what we did before.
1095 if (!htmldefaultstyle_.empty())
1099 htmldefaultstyle_ = font.asCSS();
1104 tmp += makeMarginValue("top", topsep);
1106 tmp += makeMarginValue("bottom", bottomsep);
1107 if (!leftmargin.empty()) {
1108 // we can't really do what LyX does with the margin, so
1109 // we'll just figure out how many characters it is
1110 int const len = leftmargin.length();
1111 tmp += makeMarginValue("left", len);
1113 if (!rightmargin.empty()) {
1114 int const len = rightmargin.length();
1115 tmp += makeMarginValue("right", len);
1119 if (!htmldefaultstyle_.empty())
1120 htmldefaultstyle_ += from_ascii("\n");
1121 htmldefaultstyle_ += from_ascii(tmp);
1124 // tex2lyx does not see output_xhtml.cpp
1127 string where = alignmentToCSS(align);
1128 if (!where.empty()) {
1129 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1133 // wrap up what we have, if anything
1134 if (!htmldefaultstyle_.empty())
1136 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1137 htmldefaultstyle_ + from_ascii("\n}\n");
1139 if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1145 if (labelfont != font)
1146 labelCSS = labelfont.asCSS() + from_ascii("\n");
1147 if (labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1148 labelCSS += from_ascii("text-align: center;\n");
1150 if (!labelCSS.empty())
1151 htmldefaultstyle_ +=
1152 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1153 labelCSS + from_ascii("\n}\n");
1157 bool Layout::operator==(Layout const & rhs) const
1159 // This is enough for the applications we actually make,
1160 // at least at the moment. But we could check more.
1161 return name() == rhs.name()
1162 && latexname() == rhs.latexname()
1163 && latextype == rhs.latextype;