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 == "presetarg") {
928 arg.presetarg = lex.getDocString();
929 } else if (tok == "tooltip") {
931 arg.tooltip = lex.getDocString();
932 } else if (tok == "requires") {
934 arg.requires = lex.getString();
935 } else if (tok == "decoration") {
937 arg.decoration = lex.getString();
938 } else if (tok == "font") {
939 arg.font = lyxRead(lex, arg.font);
940 } else if (tok == "labelfont") {
941 arg.labelfont = lyxRead(lex, arg.labelfont);
943 lex.printError("Unknown tag");
947 if (arg.labelstring.empty())
948 LYXERR0("Incomplete Argument definition!");
952 postcommandargs_[id] = arg;
954 latexargs_[id] = arg;
958 Layout::LaTeXArgMap Layout::args() const
960 LaTeXArgMap args = latexargs_;
961 if (!postcommandargs_.empty())
962 args.insert(postcommandargs_.begin(), postcommandargs_.end());
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)
977 LaTeXArgMap::const_iterator iit = postcommandargs_.begin();
978 for (; iit != postcommandargs_.end(); ++iit) {
979 if (!(*iit).second.mandatory)
986 int Layout::requiredArgs() const
989 LaTeXArgMap::const_iterator it = latexargs_.begin();
990 for (; it != latexargs_.end(); ++it) {
991 if ((*it).second.mandatory)
994 LaTeXArgMap::const_iterator iit = postcommandargs_.begin();
995 for (; iit != postcommandargs_.end(); ++iit) {
996 if (!(*iit).second.mandatory)
1003 string const & Layout::htmltag() const
1005 if (htmltag_.empty())
1011 string const & Layout::htmlattr() const
1013 if (htmlattr_.empty())
1014 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
1019 string const & Layout::htmlitemtag() const
1021 if (htmlitemtag_.empty())
1022 htmlitemtag_ = "div";
1023 return htmlitemtag_;
1027 string const & Layout::htmlitemattr() const
1029 if (htmlitemattr_.empty())
1030 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
1031 return htmlitemattr_;
1035 string const & Layout::htmllabeltag() const
1037 if (htmllabeltag_.empty()) {
1038 if (labeltype != LABEL_ABOVE &&
1039 labeltype != LABEL_CENTERED)
1040 htmllabeltag_ = "span";
1042 htmllabeltag_ = "div";
1044 return htmllabeltag_;
1048 string const & Layout::htmllabelattr() const
1050 if (htmllabelattr_.empty())
1051 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
1052 return htmllabelattr_;
1056 docstring Layout::htmlstyle() const
1058 if (!htmlstyle_.empty() && !htmlforcecss_)
1060 if (htmldefaultstyle_.empty())
1062 docstring retval = htmldefaultstyle_;
1063 if (!htmlstyle_.empty())
1064 retval += '\n' + htmlstyle_;
1069 string Layout::defaultCSSClass() const
1071 if (!defaultcssclass_.empty())
1072 return defaultcssclass_;
1074 docstring::const_iterator it = name().begin();
1075 docstring::const_iterator en = name().end();
1076 for (; it != en; ++it) {
1077 char_type const c = *it;
1078 if (!isAlphaASCII(c)) {
1080 // make sure we don't start with an underscore,
1081 // as that sometimes causes problems.
1082 d = from_ascii("lyx_");
1085 } else if (isLower(c))
1088 // this is slow, so do it only if necessary
1091 defaultcssclass_ = to_utf8(d);
1092 return defaultcssclass_;
1098 string makeMarginValue(char const * side, double d)
1101 os << "margin-" << side << ": " << d << "ex;\n";
1108 void Layout::makeDefaultCSS() const
1110 // this never needs to be redone, since reloading layouts will
1111 // wipe out what we did before.
1112 if (!htmldefaultstyle_.empty())
1116 htmldefaultstyle_ = font.asCSS();
1121 tmp += makeMarginValue("top", topsep);
1123 tmp += makeMarginValue("bottom", bottomsep);
1124 if (!leftmargin.empty()) {
1125 // we can't really do what LyX does with the margin, so
1126 // we'll just figure out how many characters it is
1127 int const len = leftmargin.length();
1128 tmp += makeMarginValue("left", len);
1130 if (!rightmargin.empty()) {
1131 int const len = rightmargin.length();
1132 tmp += makeMarginValue("right", len);
1136 if (!htmldefaultstyle_.empty())
1137 htmldefaultstyle_ += from_ascii("\n");
1138 htmldefaultstyle_ += from_ascii(tmp);
1141 // tex2lyx does not see output_xhtml.cpp
1144 string where = alignmentToCSS(align);
1145 if (!where.empty()) {
1146 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1150 // wrap up what we have, if anything
1151 if (!htmldefaultstyle_.empty())
1153 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1154 htmldefaultstyle_ + from_ascii("\n}\n");
1156 if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1162 if (labelfont != font)
1163 labelCSS = labelfont.asCSS() + from_ascii("\n");
1164 if (labeltype == LABEL_CENTERED)
1165 labelCSS += from_ascii("text-align: center;\n");
1167 if (!labelCSS.empty())
1168 htmldefaultstyle_ +=
1169 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1170 labelCSS + from_ascii("\n}\n");
1174 bool Layout::operator==(Layout const & rhs) const
1176 // This is enough for the applications we actually make,
1177 // at least at the moment. But we could check more.
1178 return name() == rhs.name()
1179 && latexname() == rhs.latexname()
1180 && latextype == rhs.latextype;