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.
19 #include "output_xhtml.h"
20 #include "TextClass.h"
22 #include "support/debug.h"
23 #include "support/lassert.h"
24 #include "support/lstrings.h"
25 #include "support/Messages.h"
26 #include "support/textutils.h"
30 using namespace lyx::support;
34 /// Special value of toclevel for layouts that to not belong in a TOC
35 const int Layout::NOT_IN_TOC = -1000;
37 // The order of the LayoutTags enum is no more important. [asierra300396]
54 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;
148 itemcommand_ = "item";
152 bool Layout::read(Lexer & lex, TextClass const & tclass)
154 // This table is sorted alphabetically [asierra 30March96]
155 LexerKeyword layoutTags[] = {
156 { "align", LT_ALIGN },
157 { "alignpossible", LT_ALIGNPOSSIBLE },
158 { "argument", LT_ARGUMENT },
159 { "babelpreamble", LT_BABELPREAMBLE },
160 { "bottomsep", LT_BOTTOMSEP },
161 { "category", LT_CATEGORY },
162 { "commanddepth", LT_COMMANDDEPTH },
163 { "copystyle", LT_COPYSTYLE },
164 { "dependson", LT_DEPENDSON },
166 { "endlabelstring", LT_ENDLABELSTRING },
167 { "endlabeltype", LT_ENDLABELTYPE },
169 { "freespacing", LT_FREE_SPACING },
170 { "htmlattr", LT_HTMLATTR },
171 { "htmlforcecss", LT_HTMLFORCECSS },
172 { "htmlitem", LT_HTMLITEM },
173 { "htmlitemattr", LT_HTMLITEMATTR },
174 { "htmllabel", LT_HTMLLABEL },
175 { "htmllabelattr", LT_HTMLLABELATTR },
176 { "htmllabelfirst", LT_HTMLLABELFIRST },
177 { "htmlpreamble", LT_HTMLPREAMBLE },
178 { "htmlstyle", LT_HTMLSTYLE },
179 { "htmltag", LT_HTMLTAG },
180 { "htmltitle", LT_HTMLTITLE },
181 { "innertag", LT_INNERTAG },
182 { "inpreamble", LT_INPREAMBLE },
183 { "intitle", LT_INTITLE },
184 { "itemcommand", LT_ITEMCOMMAND },
185 { "itemsep", LT_ITEMSEP },
186 { "itemtag", LT_ITEMTAG },
187 { "keepempty", LT_KEEPEMPTY },
188 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
189 { "labelcounter", LT_LABELCOUNTER },
190 { "labelfont", LT_LABELFONT },
191 { "labelindent", LT_LABELINDENT },
192 { "labelsep", LT_LABELSEP },
193 { "labelstring", LT_LABELSTRING },
194 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
195 { "labeltag", LT_LABELTAG },
196 { "labeltype", LT_LABELTYPE },
197 { "langpreamble", LT_LANGPREAMBLE },
198 { "latexname", LT_LATEXNAME },
199 { "latexparam", LT_LATEXPARAM },
200 { "latextype", LT_LATEXTYPE },
201 { "leftdelim", LT_LEFTDELIM },
202 { "leftmargin", LT_LEFTMARGIN },
203 { "margin", LT_MARGIN },
204 { "needprotect", LT_NEED_PROTECT },
205 { "newline", LT_NEWLINE },
206 { "nextnoindent", LT_NEXTNOINDENT },
207 { "obsoletedby", LT_OBSOLETEDBY },
208 { "parbreakisnewline", LT_PARBREAK_IS_NEWLINE },
209 { "parindent", LT_PARINDENT },
210 { "parsep", LT_PARSEP },
211 { "parskip", LT_PARSKIP },
212 { "passthru", LT_PASS_THRU },
213 { "preamble", LT_PREAMBLE },
214 { "refprefix", LT_REFPREFIX },
215 { "requires", LT_REQUIRES },
216 { "resetargs", LT_RESETARGS },
217 { "rightdelim", LT_RIGHTDELIM },
218 { "rightmargin", LT_RIGHTMARGIN },
219 { "spacing", LT_SPACING },
220 { "spellcheck", LT_SPELLCHECK },
221 { "textfont", LT_TEXTFONT },
222 { "toclevel", LT_TOCLEVEL },
223 { "topsep", LT_TOPSEP }
227 bool finished = false;
228 lex.pushTable(layoutTags);
230 // parse style section
231 while (!finished && lex.isOK() && !error) {
233 // See comment in LyXRC.cpp.
235 case Lexer::LEX_FEOF:
238 case Lexer::LEX_UNDEF:
240 lex.printError("Unknown layout tag `$$Token'");
247 switch (static_cast<LayoutTags>(le)) {
259 style = subst(style, '_', ' ');
261 if (tclass.hasLayout(style)) {
262 docstring const tmpname = name_;
263 this->operator=(tclass[style]);
266 LYXERR0("Cannot copy unknown style `"
268 << "All layouts so far:");
269 DocumentClass::const_iterator lit = tclass.begin();
270 DocumentClass::const_iterator len = tclass.end();
271 for (; lit != len; ++lit)
272 LYXERR0(lit->name());
277 case LT_OBSOLETEDBY: {
280 style = subst(style, '_', ' ');
282 if (tclass.hasLayout(style)) {
283 docstring const tmpname = name_;
284 this->operator=(tclass[style]);
286 if (obsoleted_by().empty())
287 obsoleted_by_ = style;
289 LYXERR0("Cannot replace with unknown style `"
292 //lex.printError("Cannot replace with"
301 depends_on_ = subst(depends_on_, '_', ' ');
330 postcommandargs_.clear();
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)
697 LexerKeyword labelTypeTags[] = {
698 { "above", LA_ABOVE },
699 { "bibliography", LA_BIBLIO },
700 { "centered", LA_CENTERED },
701 { "enumerate", LA_ENUMERATE },
702 { "itemize", LA_ITEMIZE },
703 { "manual", LA_MANUAL },
704 { "no_label", LA_NO_LABEL },
705 { "sensitive", LA_SENSITIVE },
706 { "static", LA_STATIC }
709 PushPopHelper pph(lex, labelTypeTags);
712 case Lexer::LEX_UNDEF:
713 lex.printError("Unknown labeltype tag `$$Token'");
719 labeltype = LABEL_NO_LABEL;
722 labeltype = LABEL_MANUAL;
725 labeltype = LABEL_ABOVE;
728 labeltype = LABEL_CENTERED;
731 labeltype = LABEL_STATIC;
734 labeltype = LABEL_SENSITIVE;
737 labeltype = LABEL_ENUMERATE;
740 labeltype = LABEL_ITEMIZE;
743 labeltype = LABEL_BIBLIO;
749 void Layout::readEndLabelType(Lexer & lex)
751 static LexerKeyword endlabelTypeTags[] = {
752 { "box", END_LABEL_BOX },
753 { "filled_box", END_LABEL_FILLED_BOX },
754 { "no_label", END_LABEL_NO_LABEL },
755 { "static", END_LABEL_STATIC }
758 PushPopHelper pph(lex, endlabelTypeTags);
761 case Lexer::LEX_UNDEF:
762 lex.printError("Unknown labeltype tag `$$Token'");
764 case END_LABEL_STATIC:
766 case END_LABEL_FILLED_BOX:
767 case END_LABEL_NO_LABEL:
768 endlabeltype = static_cast<EndLabelType>(le);
771 LYXERR0("Unhandled value " << le);
777 void Layout::readMargin(Lexer & lex)
779 LexerKeyword marginTags[] = {
780 { "dynamic", MARGIN_DYNAMIC },
781 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
782 { "manual", MARGIN_MANUAL },
783 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
784 { "static", MARGIN_STATIC }
787 PushPopHelper pph(lex, marginTags);
791 case Lexer::LEX_UNDEF:
792 lex.printError("Unknown margin type tag `$$Token'");
797 case MARGIN_FIRST_DYNAMIC:
798 case MARGIN_RIGHT_ADDRESS_BOX:
799 margintype = static_cast<MarginType>(le);
802 LYXERR0("Unhandled value " << le);
808 void Layout::readLatexType(Lexer & lex)
810 LexerKeyword latexTypeTags[] = {
811 { "bib_environment", LATEX_BIB_ENVIRONMENT },
812 { "command", LATEX_COMMAND },
813 { "environment", LATEX_ENVIRONMENT },
814 { "item_environment", LATEX_ITEM_ENVIRONMENT },
815 { "list_environment", LATEX_LIST_ENVIRONMENT },
816 { "paragraph", LATEX_PARAGRAPH }
819 PushPopHelper pph(lex, latexTypeTags);
822 case Lexer::LEX_UNDEF:
823 lex.printError("Unknown latextype tag `$$Token'");
825 case LATEX_PARAGRAPH:
827 case LATEX_ENVIRONMENT:
828 case LATEX_ITEM_ENVIRONMENT:
829 case LATEX_BIB_ENVIRONMENT:
830 case LATEX_LIST_ENVIRONMENT:
831 latextype = static_cast<LatexType>(le);
834 LYXERR0("Unhandled value " << le);
840 void Layout::readSpacing(Lexer & lex)
843 ST_SPACING_SINGLE = 1,
849 LexerKeyword spacingTags[] = {
850 {"double", ST_SPACING_DOUBLE },
851 {"onehalf", ST_SPACING_ONEHALF },
852 {"other", ST_OTHER },
853 {"single", ST_SPACING_SINGLE }
856 PushPopHelper pph(lex, spacingTags);
859 case Lexer::LEX_UNDEF:
860 lex.printError("Unknown spacing token `$$Token'");
865 case ST_SPACING_SINGLE:
866 spacing.set(Spacing::Single);
868 case ST_SPACING_ONEHALF:
869 spacing.set(Spacing::Onehalf);
871 case ST_SPACING_DOUBLE:
872 spacing.set(Spacing::Double);
876 spacing.set(Spacing::Other, lex.getString());
882 void Layout::readArgument(Lexer & lex)
885 arg.mandatory = false;
886 arg.autoinsert = false;
888 bool finished = false;
889 arg.font = inherit_font;
890 arg.labelfont = inherit_font;
893 bool const itemarg = prefixIs(id, "item:");
894 bool const postcmd = prefixIs(id, "post:");
896 while (!finished && lex.isOK() && !error) {
898 string const tok = ascii_lowercase(lex.getString());
902 } else if (tok == "endargument") {
904 } else if (tok == "labelstring") {
906 arg.labelstring = lex.getDocString();
907 } else if (tok == "menustring") {
909 arg.menustring = lex.getDocString();
910 } else if (tok == "mandatory") {
912 arg.mandatory = lex.getBool();
913 } else if (tok == "autoinsert") {
915 arg.autoinsert = lex.getBool();
916 } else if (tok == "leftdelim") {
918 arg.ldelim = lex.getDocString();
919 arg.ldelim = support::subst(arg.ldelim, from_ascii("<br/>"),
921 } else if (tok == "rightdelim") {
923 arg.rdelim = lex.getDocString();
924 arg.rdelim = support::subst(arg.rdelim, from_ascii("<br/>"),
926 } else if (tok == "defaultarg") {
928 arg.defaultarg = lex.getDocString();
929 } else if (tok == "presetarg") {
931 arg.presetarg = lex.getDocString();
932 } else if (tok == "tooltip") {
934 arg.tooltip = lex.getDocString();
935 } else if (tok == "requires") {
937 arg.requires = lex.getString();
938 } else if (tok == "decoration") {
940 arg.decoration = lex.getString();
941 } else if (tok == "font") {
942 arg.font = lyxRead(lex, arg.font);
943 } else if (tok == "labelfont") {
944 arg.labelfont = lyxRead(lex, arg.labelfont);
946 lex.printError("Unknown tag");
950 if (arg.labelstring.empty())
951 LYXERR0("Incomplete Argument definition!");
955 postcommandargs_[id] = arg;
957 latexargs_[id] = arg;
961 Layout::LaTeXArgMap Layout::args() const
963 LaTeXArgMap args = latexargs_;
964 if (!postcommandargs_.empty())
965 args.insert(postcommandargs_.begin(), postcommandargs_.end());
966 if (!itemargs_.empty())
967 args.insert(itemargs_.begin(), itemargs_.end());
972 int Layout::optArgs() const
975 LaTeXArgMap::const_iterator it = latexargs_.begin();
976 for (; it != latexargs_.end(); ++it) {
977 if (!(*it).second.mandatory)
980 LaTeXArgMap::const_iterator iit = postcommandargs_.begin();
981 for (; iit != postcommandargs_.end(); ++iit) {
982 if (!(*iit).second.mandatory)
989 int Layout::requiredArgs() const
992 LaTeXArgMap::const_iterator it = latexargs_.begin();
993 for (; it != latexargs_.end(); ++it) {
994 if ((*it).second.mandatory)
997 LaTeXArgMap::const_iterator iit = postcommandargs_.begin();
998 for (; iit != postcommandargs_.end(); ++iit) {
999 if (!(*iit).second.mandatory)
1006 string const & Layout::htmltag() const
1008 if (htmltag_.empty())
1014 string const & Layout::htmlattr() const
1016 if (htmlattr_.empty())
1017 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
1022 string const & Layout::htmlitemtag() const
1024 if (htmlitemtag_.empty())
1025 htmlitemtag_ = "div";
1026 return htmlitemtag_;
1030 string const & Layout::htmlitemattr() const
1032 if (htmlitemattr_.empty())
1033 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
1034 return htmlitemattr_;
1038 string const & Layout::htmllabeltag() const
1040 if (htmllabeltag_.empty()) {
1041 if (labeltype != LABEL_ABOVE &&
1042 labeltype != LABEL_CENTERED)
1043 htmllabeltag_ = "span";
1045 htmllabeltag_ = "div";
1047 return htmllabeltag_;
1051 string const & Layout::htmllabelattr() const
1053 if (htmllabelattr_.empty())
1054 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
1055 return htmllabelattr_;
1059 docstring Layout::htmlstyle() const
1061 if (!htmlstyle_.empty() && !htmlforcecss_)
1063 if (htmldefaultstyle_.empty())
1065 docstring retval = htmldefaultstyle_;
1066 if (!htmlstyle_.empty())
1067 retval += '\n' + htmlstyle_;
1072 string Layout::defaultCSSClass() const
1074 if (!defaultcssclass_.empty())
1075 return defaultcssclass_;
1077 docstring::const_iterator it = name().begin();
1078 docstring::const_iterator en = name().end();
1079 for (; it != en; ++it) {
1080 char_type const c = *it;
1081 if (!isAlphaASCII(c)) {
1083 // make sure we don't start with an underscore,
1084 // as that sometimes causes problems.
1085 d = from_ascii("lyx_");
1088 } else if (isLower(c))
1091 // this is slow, so do it only if necessary
1094 defaultcssclass_ = to_utf8(d);
1095 return defaultcssclass_;
1101 string makeMarginValue(char const * side, double d)
1104 os << "margin-" << side << ": " << d << "ex;\n";
1111 void Layout::makeDefaultCSS() const
1113 // this never needs to be redone, since reloading layouts will
1114 // wipe out what we did before.
1115 if (!htmldefaultstyle_.empty())
1119 htmldefaultstyle_ = font.asCSS();
1124 tmp += makeMarginValue("top", topsep);
1126 tmp += makeMarginValue("bottom", bottomsep);
1127 if (!leftmargin.empty()) {
1128 // we can't really do what LyX does with the margin, so
1129 // we'll just figure out how many characters it is
1130 int const len = leftmargin.length();
1131 tmp += makeMarginValue("left", len);
1133 if (!rightmargin.empty()) {
1134 int const len = rightmargin.length();
1135 tmp += makeMarginValue("right", len);
1139 if (!htmldefaultstyle_.empty())
1140 htmldefaultstyle_ += from_ascii("\n");
1141 htmldefaultstyle_ += from_ascii(tmp);
1144 // tex2lyx does not see output_xhtml.cpp
1147 string where = alignmentToCSS(align);
1148 if (!where.empty()) {
1149 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1153 // wrap up what we have, if anything
1154 if (!htmldefaultstyle_.empty())
1156 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1157 htmldefaultstyle_ + from_ascii("\n}\n");
1159 if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1165 if (labelfont != font)
1166 labelCSS = labelfont.asCSS() + from_ascii("\n");
1167 if (labeltype == LABEL_CENTERED)
1168 labelCSS += from_ascii("text-align: center;\n");
1170 if (!labelCSS.empty())
1171 htmldefaultstyle_ +=
1172 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1173 labelCSS + from_ascii("\n}\n");
1177 bool Layout::operator==(Layout const & rhs) const
1179 // This is enough for the applications we actually make,
1180 // at least at the moment. But we could check more.
1181 return name() == rhs.name()
1182 && latexname() == rhs.latexname()
1183 && latextype == rhs.latextype;