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_, '_', ' ');
333 case LT_NEED_PROTECT:
342 font = lyxRead(lex, font);
347 font = lyxRead(lex, font);
351 labelfont = lyxRead(lex, labelfont);
354 case LT_NEXTNOINDENT:
358 case LT_COMMANDDEPTH:
368 latexparam_ = subst(latexparam_, """, "\"");
392 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
395 case LT_LANGPREAMBLE:
396 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
399 case LT_BABELPREAMBLE:
400 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
407 case LT_ENDLABELTYPE:
408 readEndLabelType(lex);
443 case LT_LABEL_BOTTOMSEP:
444 lex >> labelbottomsep;
449 labelsep = subst(labelsep, 'x', ' ');
457 lex >> newline_allowed;
464 case LT_ALIGNPOSSIBLE:
465 readAlignPossible(lex);
469 // FIXME: this means LT_ENDLABELSTRING may only
470 // occur after LT_LABELSTRING
472 labelstring_ = trim(labelstring_);
473 labelstring_appendix_ = labelstring_;
476 case LT_ENDLABELSTRING:
477 lex >> endlabelstring_;
478 endlabelstring_ = trim(endlabelstring_);
481 case LT_LABELSTRING_APPENDIX:
482 lex >> labelstring_appendix_;
483 labelstring_appendix_ = trim(labelstring_appendix_);
486 case LT_LABELCOUNTER:
488 counter = trim(counter);
491 case LT_FREE_SPACING:
499 case LT_PARBREAK_IS_NEWLINE:
500 lex >> parbreak_is_newline;
509 vector<string> const req =
510 getVectorFromString(lex.getString());
511 requires_.insert(req.begin(), req.end());
537 case LT_HTMLITEMATTR:
538 lex >> htmlitemattr_;
542 lex >> htmllabeltag_;
545 case LT_HTMLLABELATTR:
546 lex >> htmllabelattr_;
549 case LT_HTMLLABELFIRST:
550 lex >> htmllabelfirst_;
554 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
557 case LT_HTMLFORCECSS:
558 lex >> htmlforcecss_;
561 case LT_HTMLPREAMBLE:
562 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
575 // make sure we only have inpreamble = true for commands
576 if (inpreamble && latextype != LATEX_COMMAND && latextype != LATEX_PARAGRAPH) {
577 LYXERR0("InPreamble not permitted except with command and paragraph layouts.");
578 LYXERR0("Layout name: " << name());
582 return finished && !error;
595 LexerKeyword alignTags[] = {
596 { "block", AT_BLOCK },
597 { "center", AT_CENTER },
598 { "layout", AT_LAYOUT },
600 { "right", AT_RIGHT }
604 void Layout::readAlign(Lexer & lex)
606 PushPopHelper pph(lex, alignTags);
609 case Lexer::LEX_UNDEF:
610 lex.printError("Unknown alignment `$$Token'");
616 align = LYX_ALIGN_BLOCK;
619 align = LYX_ALIGN_LEFT;
622 align = LYX_ALIGN_RIGHT;
625 align = LYX_ALIGN_CENTER;
628 align = LYX_ALIGN_LAYOUT;
634 void Layout::readAlignPossible(Lexer & lex)
636 lex.pushTable(alignTags);
637 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
638 int lineno = lex.lineNumber();
642 case Lexer::LEX_UNDEF:
643 lex.printError("Unknown alignment `$$Token'");
649 alignpossible |= LYX_ALIGN_BLOCK;
652 alignpossible |= LYX_ALIGN_LEFT;
655 alignpossible |= LYX_ALIGN_RIGHT;
658 alignpossible |= LYX_ALIGN_CENTER;
661 alignpossible |= LYX_ALIGN_LAYOUT;
664 } while (lineno == lex.lineNumber());
669 void Layout::readLabelType(Lexer & lex)
675 LA_CENTERED_TOP_ENVIRONMENT,
685 LexerKeyword labelTypeTags[] = {
686 { "bibliography", LA_BIBLIO },
687 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
688 { "counter", LA_COUNTER },
689 { "enumerate", LA_ENUMERATE },
690 { "itemize", LA_ITEMIZE },
691 { "manual", LA_MANUAL },
692 { "no_label", LA_NO_LABEL },
693 { "sensitive", LA_SENSITIVE },
694 { "static", LA_STATIC },
695 { "top_environment", LA_TOP_ENVIRONMENT }
698 PushPopHelper pph(lex, labelTypeTags);
701 case Lexer::LEX_UNDEF:
702 lex.printError("Unknown labeltype tag `$$Token'");
708 labeltype = LABEL_NO_LABEL;
711 labeltype = LABEL_MANUAL;
713 case LA_TOP_ENVIRONMENT:
714 labeltype = LABEL_TOP_ENVIRONMENT;
716 case LA_CENTERED_TOP_ENVIRONMENT:
717 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
720 labeltype = LABEL_STATIC;
723 labeltype = LABEL_SENSITIVE;
726 labeltype = LABEL_COUNTER;
729 labeltype = LABEL_ENUMERATE;
732 labeltype = LABEL_ITEMIZE;
735 labeltype = LABEL_BIBLIO;
741 void Layout::readEndLabelType(Lexer & lex)
743 static LexerKeyword endlabelTypeTags[] = {
744 { "box", END_LABEL_BOX },
745 { "filled_box", END_LABEL_FILLED_BOX },
746 { "no_label", END_LABEL_NO_LABEL },
747 { "static", END_LABEL_STATIC }
750 PushPopHelper pph(lex, endlabelTypeTags);
753 case Lexer::LEX_UNDEF:
754 lex.printError("Unknown labeltype tag `$$Token'");
756 case END_LABEL_STATIC:
758 case END_LABEL_FILLED_BOX:
759 case END_LABEL_NO_LABEL:
760 endlabeltype = static_cast<EndLabelType>(le);
763 LYXERR0("Unhandled value " << le);
769 void Layout::readMargin(Lexer & lex)
771 LexerKeyword marginTags[] = {
772 { "dynamic", MARGIN_DYNAMIC },
773 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
774 { "manual", MARGIN_MANUAL },
775 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
776 { "static", MARGIN_STATIC }
779 PushPopHelper pph(lex, marginTags);
783 case Lexer::LEX_UNDEF:
784 lex.printError("Unknown margin type tag `$$Token'");
789 case MARGIN_FIRST_DYNAMIC:
790 case MARGIN_RIGHT_ADDRESS_BOX:
791 margintype = static_cast<MarginType>(le);
794 LYXERR0("Unhandled value " << le);
800 void Layout::readLatexType(Lexer & lex)
802 LexerKeyword latexTypeTags[] = {
803 { "bib_environment", LATEX_BIB_ENVIRONMENT },
804 { "command", LATEX_COMMAND },
805 { "environment", LATEX_ENVIRONMENT },
806 { "item_environment", LATEX_ITEM_ENVIRONMENT },
807 { "list_environment", LATEX_LIST_ENVIRONMENT },
808 { "paragraph", LATEX_PARAGRAPH }
811 PushPopHelper pph(lex, latexTypeTags);
814 case Lexer::LEX_UNDEF:
815 lex.printError("Unknown latextype tag `$$Token'");
817 case LATEX_PARAGRAPH:
819 case LATEX_ENVIRONMENT:
820 case LATEX_ITEM_ENVIRONMENT:
821 case LATEX_BIB_ENVIRONMENT:
822 case LATEX_LIST_ENVIRONMENT:
823 latextype = static_cast<LatexType>(le);
826 LYXERR0("Unhandled value " << le);
832 void Layout::readSpacing(Lexer & lex)
835 ST_SPACING_SINGLE = 1,
841 LexerKeyword spacingTags[] = {
842 {"double", ST_SPACING_DOUBLE },
843 {"onehalf", ST_SPACING_ONEHALF },
844 {"other", ST_OTHER },
845 {"single", ST_SPACING_SINGLE }
848 PushPopHelper pph(lex, spacingTags);
851 case Lexer::LEX_UNDEF:
852 lex.printError("Unknown spacing token `$$Token'");
857 case ST_SPACING_SINGLE:
858 spacing.set(Spacing::Single);
860 case ST_SPACING_ONEHALF:
861 spacing.set(Spacing::Onehalf);
863 case ST_SPACING_DOUBLE:
864 spacing.set(Spacing::Double);
868 spacing.set(Spacing::Other, lex.getString());
874 void Layout::readArgument(Lexer & lex)
877 arg.mandatory = false;
879 bool finished = false;
880 arg.font = inherit_font;
881 arg.labelfont = inherit_font;
884 while (!finished && lex.isOK() && !error) {
886 string const tok = ascii_lowercase(lex.getString());
890 } else if (tok == "endargument") {
892 } else if (tok == "labelstring") {
894 arg.labelstring = lex.getDocString();
895 } else if (tok == "mandatory") {
897 arg.mandatory = lex.getBool();
898 } else if (tok == "leftdelim") {
900 arg.ldelim = lex.getDocString();
901 } else if (tok == "rightdelim") {
903 arg.rdelim = lex.getDocString();
904 } else if (tok == "tooltip") {
906 arg.tooltip = lex.getDocString();
907 } else if (tok == "requires") {
909 arg.requires = lex.getString();
910 } else if (tok == "decoration") {
912 arg.decoration = lex.getString();
913 } else if (tok == "font") {
914 arg.font = lyxRead(lex, arg.font);
915 } else if (tok == "labelfont") {
916 arg.labelfont = lyxRead(lex, arg.labelfont);
918 lex.printError("Unknown tag");
922 if (arg.labelstring.empty())
923 LYXERR0("Incomplete Argument definition!");
925 latexargs_[nr] = arg;
929 int Layout::optArgs() const
932 LaTeXArgMap::const_iterator it = latexargs_.begin();
933 for (; it != latexargs_.end(); ++it) {
934 if (!(*it).second.mandatory)
941 int Layout::requiredArgs() const
944 LaTeXArgMap::const_iterator it = latexargs_.begin();
945 for (; it != latexargs_.end(); ++it) {
946 if ((*it).second.mandatory)
953 string const & Layout::htmltag() const
955 if (htmltag_.empty())
961 string const & Layout::htmlattr() const
963 if (htmlattr_.empty())
964 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
969 string const & Layout::htmlitemtag() const
971 if (htmlitemtag_.empty())
972 htmlitemtag_ = "div";
977 string const & Layout::htmlitemattr() const
979 if (htmlitemattr_.empty())
980 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
981 return htmlitemattr_;
985 string const & Layout::htmllabeltag() const
987 if (htmllabeltag_.empty()) {
988 if (labeltype != LABEL_TOP_ENVIRONMENT &&
989 labeltype != LABEL_CENTERED_TOP_ENVIRONMENT)
990 htmllabeltag_ = "span";
992 htmllabeltag_ = "div";
994 return htmllabeltag_;
998 string const & Layout::htmllabelattr() const
1000 if (htmllabelattr_.empty())
1001 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
1002 return htmllabelattr_;
1006 docstring Layout::htmlstyle() const
1008 if (!htmlstyle_.empty() && !htmlforcecss_)
1010 if (htmldefaultstyle_.empty())
1012 docstring retval = htmldefaultstyle_;
1013 if (!htmlstyle_.empty())
1014 retval += '\n' + htmlstyle_;
1019 string Layout::defaultCSSClass() const
1021 if (!defaultcssclass_.empty())
1022 return defaultcssclass_;
1024 docstring::const_iterator it = name().begin();
1025 docstring::const_iterator en = name().end();
1026 for (; it != en; ++it) {
1027 char_type const c = *it;
1028 if (!isAlphaASCII(c)) {
1030 // make sure we don't start with an underscore,
1031 // as that sometimes causes problems.
1032 d = from_ascii("lyx_");
1035 } else if (isLower(c))
1038 // this is slow, so do it only if necessary
1041 defaultcssclass_ = to_utf8(d);
1042 return defaultcssclass_;
1048 string makeMarginValue(char const * side, double d)
1051 os << "margin-" << side << ": " << d << "ex;\n";
1058 void Layout::makeDefaultCSS() const
1060 // this never needs to be redone, since reloading layouts will
1061 // wipe out what we did before.
1062 if (!htmldefaultstyle_.empty())
1066 htmldefaultstyle_ = font.asCSS();
1071 tmp += makeMarginValue("top", topsep);
1073 tmp += makeMarginValue("bottom", bottomsep);
1074 if (!leftmargin.empty()) {
1075 // we can't really do what LyX does with the margin, so
1076 // we'll just figure out how many characters it is
1077 int const len = leftmargin.length();
1078 tmp += makeMarginValue("left", len);
1080 if (!rightmargin.empty()) {
1081 int const len = rightmargin.length();
1082 tmp += makeMarginValue("right", len);
1086 if (!htmldefaultstyle_.empty())
1087 htmldefaultstyle_ += from_ascii("\n");
1088 htmldefaultstyle_ += from_ascii(tmp);
1091 // tex2lyx does not see output_xhtml.cpp
1094 string where = alignmentToCSS(align);
1095 if (!where.empty()) {
1096 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1100 // wrap up what we have, if anything
1101 if (!htmldefaultstyle_.empty())
1103 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1104 htmldefaultstyle_ + from_ascii("\n}\n");
1106 if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1112 if (labelfont != font)
1113 labelCSS = labelfont.asCSS() + from_ascii("\n");
1114 if (labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1115 labelCSS += from_ascii("text-align: center;\n");
1117 if (!labelCSS.empty())
1118 htmldefaultstyle_ +=
1119 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1120 labelCSS + from_ascii("\n}\n");
1124 bool Layout::operator==(Layout const & rhs) const
1126 // This is enough for the applications we actually make,
1127 // at least at the moment. But we could check more.
1128 return name() == rhs.name()
1129 && latexname() == rhs.latexname()
1130 && latextype == rhs.latextype;