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,
105 LT_INTITLE // keep this last!
108 /////////////////////
113 margintype = MARGIN_STATIC;
114 latextype = LATEX_PARAGRAPH;
120 labelfont = inherit_font;
122 reslabelfont = sane_font;
123 nextnoindent = false;
128 labelbottomsep = 0.0;
130 align = LYX_ALIGN_BLOCK;
131 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
132 labeltype = LABEL_NO_LABEL;
133 endlabeltype = END_LABEL_NO_LABEL;
134 // Should or should not. That is the question.
135 // spacing.set(Spacing::OneHalf);
136 newline_allowed = true;
137 free_spacing = false;
139 parbreak_is_newline = false;
140 toclevel = NOT_IN_TOC;
142 htmllabelfirst_ = false;
143 htmlforcecss_ = false;
149 bool Layout::read(Lexer & lex, TextClass const & tclass)
151 // This table is sorted alphabetically [asierra 30March96]
152 LexerKeyword layoutTags[] = {
153 { "align", LT_ALIGN },
154 { "alignpossible", LT_ALIGNPOSSIBLE },
155 { "argument", LT_ARGUMENT },
156 { "babelpreamble", LT_BABELPREAMBLE },
157 { "bottomsep", LT_BOTTOMSEP },
158 { "category", LT_CATEGORY },
159 { "commanddepth", LT_COMMANDDEPTH },
160 { "copystyle", LT_COPYSTYLE },
161 { "dependson", LT_DEPENDSON },
163 { "endlabelstring", LT_ENDLABELSTRING },
164 { "endlabeltype", LT_ENDLABELTYPE },
166 { "freespacing", LT_FREE_SPACING },
167 { "htmlattr", LT_HTMLATTR },
168 { "htmlforcecss", LT_HTMLFORCECSS },
169 { "htmlitem", LT_HTMLITEM },
170 { "htmlitemattr", LT_HTMLITEMATTR },
171 { "htmllabel", LT_HTMLLABEL },
172 { "htmllabelattr", LT_HTMLLABELATTR },
173 { "htmllabelfirst", LT_HTMLLABELFIRST },
174 { "htmlpreamble", LT_HTMLPREAMBLE },
175 { "htmlstyle", LT_HTMLSTYLE },
176 { "htmltag", LT_HTMLTAG },
177 { "htmltitle", LT_HTMLTITLE },
178 { "innertag", LT_INNERTAG },
179 { "inpreamble", LT_INPREAMBLE },
180 { "intitle", LT_INTITLE },
181 { "itemsep", LT_ITEMSEP },
182 { "itemtag", LT_ITEMTAG },
183 { "keepempty", LT_KEEPEMPTY },
184 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
185 { "labelcounter", LT_LABELCOUNTER },
186 { "labelfont", LT_LABELFONT },
187 { "labelindent", LT_LABELINDENT },
188 { "labelsep", LT_LABELSEP },
189 { "labelstring", LT_LABELSTRING },
190 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
191 { "labeltag", LT_LABELTAG },
192 { "labeltype", LT_LABELTYPE },
193 { "langpreamble", LT_LANGPREAMBLE },
194 { "latexname", LT_LATEXNAME },
195 { "latexparam", LT_LATEXPARAM },
196 { "latextype", LT_LATEXTYPE },
197 { "leftmargin", LT_LEFTMARGIN },
198 { "margin", LT_MARGIN },
199 { "needprotect", LT_NEED_PROTECT },
200 { "newline", LT_NEWLINE },
201 { "nextnoindent", LT_NEXTNOINDENT },
202 { "obsoletedby", LT_OBSOLETEDBY },
203 { "parbreakisnewline", LT_PARBREAK_IS_NEWLINE },
204 { "parindent", LT_PARINDENT },
205 { "parsep", LT_PARSEP },
206 { "parskip", LT_PARSKIP },
207 { "passthru", LT_PASS_THRU },
208 { "preamble", LT_PREAMBLE },
209 { "refprefix", LT_REFPREFIX },
210 { "requires", LT_REQUIRES },
211 { "resetargs", LT_RESETARGS },
212 { "rightmargin", LT_RIGHTMARGIN },
213 { "spacing", LT_SPACING },
214 { "spellcheck", LT_SPELLCHECK },
215 { "textfont", LT_TEXTFONT },
216 { "toclevel", LT_TOCLEVEL },
217 { "topsep", LT_TOPSEP }
221 bool finished = false;
222 lex.pushTable(layoutTags);
224 // parse style section
225 while (!finished && lex.isOK() && !error) {
227 // See comment in LyXRC.cpp.
229 case Lexer::LEX_FEOF:
232 case Lexer::LEX_UNDEF:
234 lex.printError("Unknown layout tag `$$Token'");
241 switch (static_cast<LayoutTags>(le)) {
253 style = subst(style, '_', ' ');
255 if (tclass.hasLayout(style)) {
256 docstring const tmpname = name_;
257 this->operator=(tclass[style]);
260 LYXERR0("Cannot copy unknown style `"
262 << "All layouts so far:");
263 DocumentClass::const_iterator lit = tclass.begin();
264 DocumentClass::const_iterator len = tclass.end();
265 for (; lit != len; ++lit)
266 LYXERR0(lit->name());
271 case LT_OBSOLETEDBY: {
274 style = subst(style, '_', ' ');
276 if (tclass.hasLayout(style)) {
277 docstring const tmpname = name_;
278 this->operator=(tclass[style]);
280 if (obsoleted_by().empty())
281 obsoleted_by_ = style;
283 LYXERR0("Cannot replace with unknown style `"
286 //lex.printError("Cannot replace with"
295 depends_on_ = subst(depends_on_, '_', ' ');
329 case LT_NEED_PROTECT:
338 font = lyxRead(lex, font);
343 font = lyxRead(lex, font);
347 labelfont = lyxRead(lex, labelfont);
350 case LT_NEXTNOINDENT:
354 case LT_COMMANDDEPTH:
364 latexparam_ = subst(latexparam_, """, "\"");
380 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
383 case LT_LANGPREAMBLE:
384 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
387 case LT_BABELPREAMBLE:
388 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
395 case LT_ENDLABELTYPE:
396 readEndLabelType(lex);
431 case LT_LABEL_BOTTOMSEP:
432 lex >> labelbottomsep;
437 labelsep = subst(labelsep, 'x', ' ');
445 lex >> newline_allowed;
452 case LT_ALIGNPOSSIBLE:
453 readAlignPossible(lex);
457 // FIXME: this means LT_ENDLABELSTRING may only
458 // occur after LT_LABELSTRING
460 labelstring_ = trim(labelstring_);
461 labelstring_appendix_ = labelstring_;
464 case LT_ENDLABELSTRING:
465 lex >> endlabelstring_;
466 endlabelstring_ = trim(endlabelstring_);
469 case LT_LABELSTRING_APPENDIX:
470 lex >> labelstring_appendix_;
471 labelstring_appendix_ = trim(labelstring_appendix_);
474 case LT_LABELCOUNTER:
476 counter = trim(counter);
479 case LT_FREE_SPACING:
487 case LT_PARBREAK_IS_NEWLINE:
488 lex >> parbreak_is_newline;
497 vector<string> const req =
498 getVectorFromString(lex.getString());
499 requires_.insert(req.begin(), req.end());
525 case LT_HTMLITEMATTR:
526 lex >> htmlitemattr_;
530 lex >> htmllabeltag_;
533 case LT_HTMLLABELATTR:
534 lex >> htmllabelattr_;
537 case LT_HTMLLABELFIRST:
538 lex >> htmllabelfirst_;
542 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
545 case LT_HTMLFORCECSS:
546 lex >> htmlforcecss_;
549 case LT_HTMLPREAMBLE:
550 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
563 // make sure we only have inpreamble = true for commands
564 if (inpreamble && latextype != LATEX_COMMAND && latextype != LATEX_PARAGRAPH) {
565 LYXERR0("InPreamble not permitted except with command and paragraph layouts.");
566 LYXERR0("Layout name: " << name());
570 return finished && !error;
583 LexerKeyword alignTags[] = {
584 { "block", AT_BLOCK },
585 { "center", AT_CENTER },
586 { "layout", AT_LAYOUT },
588 { "right", AT_RIGHT }
592 void Layout::readAlign(Lexer & lex)
594 PushPopHelper pph(lex, alignTags);
597 case Lexer::LEX_UNDEF:
598 lex.printError("Unknown alignment `$$Token'");
604 align = LYX_ALIGN_BLOCK;
607 align = LYX_ALIGN_LEFT;
610 align = LYX_ALIGN_RIGHT;
613 align = LYX_ALIGN_CENTER;
616 align = LYX_ALIGN_LAYOUT;
622 void Layout::readAlignPossible(Lexer & lex)
624 lex.pushTable(alignTags);
625 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
626 int lineno = lex.lineNumber();
630 case Lexer::LEX_UNDEF:
631 lex.printError("Unknown alignment `$$Token'");
637 alignpossible |= LYX_ALIGN_BLOCK;
640 alignpossible |= LYX_ALIGN_LEFT;
643 alignpossible |= LYX_ALIGN_RIGHT;
646 alignpossible |= LYX_ALIGN_CENTER;
649 alignpossible |= LYX_ALIGN_LAYOUT;
652 } while (lineno == lex.lineNumber());
657 void Layout::readLabelType(Lexer & lex)
663 LA_CENTERED_TOP_ENVIRONMENT,
673 LexerKeyword labelTypeTags[] = {
674 { "bibliography", LA_BIBLIO },
675 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
676 { "counter", LA_COUNTER },
677 { "enumerate", LA_ENUMERATE },
678 { "itemize", LA_ITEMIZE },
679 { "manual", LA_MANUAL },
680 { "no_label", LA_NO_LABEL },
681 { "sensitive", LA_SENSITIVE },
682 { "static", LA_STATIC },
683 { "top_environment", LA_TOP_ENVIRONMENT }
686 PushPopHelper pph(lex, labelTypeTags);
689 case Lexer::LEX_UNDEF:
690 lex.printError("Unknown labeltype tag `$$Token'");
696 labeltype = LABEL_NO_LABEL;
699 labeltype = LABEL_MANUAL;
701 case LA_TOP_ENVIRONMENT:
702 labeltype = LABEL_TOP_ENVIRONMENT;
704 case LA_CENTERED_TOP_ENVIRONMENT:
705 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
708 labeltype = LABEL_STATIC;
711 labeltype = LABEL_SENSITIVE;
714 labeltype = LABEL_COUNTER;
717 labeltype = LABEL_ENUMERATE;
720 labeltype = LABEL_ITEMIZE;
723 labeltype = LABEL_BIBLIO;
729 void Layout::readEndLabelType(Lexer & lex)
731 static LexerKeyword endlabelTypeTags[] = {
732 { "box", END_LABEL_BOX },
733 { "filled_box", END_LABEL_FILLED_BOX },
734 { "no_label", END_LABEL_NO_LABEL },
735 { "static", END_LABEL_STATIC }
738 PushPopHelper pph(lex, endlabelTypeTags);
741 case Lexer::LEX_UNDEF:
742 lex.printError("Unknown labeltype tag `$$Token'");
744 case END_LABEL_STATIC:
746 case END_LABEL_FILLED_BOX:
747 case END_LABEL_NO_LABEL:
748 endlabeltype = static_cast<EndLabelType>(le);
751 LYXERR0("Unhandled value " << le);
757 void Layout::readMargin(Lexer & lex)
759 LexerKeyword marginTags[] = {
760 { "dynamic", MARGIN_DYNAMIC },
761 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
762 { "manual", MARGIN_MANUAL },
763 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
764 { "static", MARGIN_STATIC }
767 PushPopHelper pph(lex, marginTags);
771 case Lexer::LEX_UNDEF:
772 lex.printError("Unknown margin type tag `$$Token'");
777 case MARGIN_FIRST_DYNAMIC:
778 case MARGIN_RIGHT_ADDRESS_BOX:
779 margintype = static_cast<MarginType>(le);
782 LYXERR0("Unhandled value " << le);
788 void Layout::readLatexType(Lexer & lex)
790 LexerKeyword latexTypeTags[] = {
791 { "bib_environment", LATEX_BIB_ENVIRONMENT },
792 { "command", LATEX_COMMAND },
793 { "environment", LATEX_ENVIRONMENT },
794 { "item_environment", LATEX_ITEM_ENVIRONMENT },
795 { "list_environment", LATEX_LIST_ENVIRONMENT },
796 { "paragraph", LATEX_PARAGRAPH }
799 PushPopHelper pph(lex, latexTypeTags);
802 case Lexer::LEX_UNDEF:
803 lex.printError("Unknown latextype tag `$$Token'");
805 case LATEX_PARAGRAPH:
807 case LATEX_ENVIRONMENT:
808 case LATEX_ITEM_ENVIRONMENT:
809 case LATEX_BIB_ENVIRONMENT:
810 case LATEX_LIST_ENVIRONMENT:
811 latextype = static_cast<LatexType>(le);
814 LYXERR0("Unhandled value " << le);
820 void Layout::readSpacing(Lexer & lex)
823 ST_SPACING_SINGLE = 1,
829 LexerKeyword spacingTags[] = {
830 {"double", ST_SPACING_DOUBLE },
831 {"onehalf", ST_SPACING_ONEHALF },
832 {"other", ST_OTHER },
833 {"single", ST_SPACING_SINGLE }
836 PushPopHelper pph(lex, spacingTags);
839 case Lexer::LEX_UNDEF:
840 lex.printError("Unknown spacing token `$$Token'");
845 case ST_SPACING_SINGLE:
846 spacing.set(Spacing::Single);
848 case ST_SPACING_ONEHALF:
849 spacing.set(Spacing::Onehalf);
851 case ST_SPACING_DOUBLE:
852 spacing.set(Spacing::Double);
856 spacing.set(Spacing::Other, lex.getString());
862 void Layout::readArgument(Lexer & lex)
865 arg.mandatory = false;
867 bool finished = false;
870 while (!finished && lex.isOK() && !error) {
872 string const tok = ascii_lowercase(lex.getString());
876 } else if (tok == "endargument") {
878 } else if (tok == "labelstring") {
880 arg.labelstring = lex.getDocString();
881 } else if (tok == "mandatory") {
883 arg.mandatory = lex.getBool();
884 } else if (tok == "leftdelim") {
886 arg.ldelim = lex.getDocString();
887 } else if (tok == "rightdelim") {
889 arg.rdelim = lex.getDocString();
890 } else if (tok == "tooltip") {
892 arg.tooltip = lex.getDocString();
893 } else if (tok == "shortcut") {
895 arg.shortcut = lex.getString();
896 } else if (tok == "requires") {
898 arg.requires = lex.getString();
900 lex.printError("Unknown tag");
904 if (arg.labelstring.empty())
905 LYXERR0("Incomplete Argument definition!");
907 latexargs_[nr] = arg;
911 int Layout::optArgs() const
914 LaTeXArgMap::const_iterator it = latexargs_.begin();
915 for (; it != latexargs_.end(); ++it) {
916 if (!(*it).second.mandatory)
923 int Layout::requiredArgs() const
926 LaTeXArgMap::const_iterator it = latexargs_.begin();
927 for (; it != latexargs_.end(); ++it) {
928 if ((*it).second.mandatory)
935 string const & Layout::htmltag() const
937 if (htmltag_.empty())
943 string const & Layout::htmlattr() const
945 if (htmlattr_.empty())
946 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
951 string const & Layout::htmlitemtag() const
953 if (htmlitemtag_.empty())
954 htmlitemtag_ = "div";
959 string const & Layout::htmlitemattr() const
961 if (htmlitemattr_.empty())
962 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
963 return htmlitemattr_;
967 string const & Layout::htmllabeltag() const
969 if (htmllabeltag_.empty()) {
970 if (labeltype != LABEL_TOP_ENVIRONMENT &&
971 labeltype != LABEL_CENTERED_TOP_ENVIRONMENT)
972 htmllabeltag_ = "span";
974 htmllabeltag_ = "div";
976 return htmllabeltag_;
980 string const & Layout::htmllabelattr() const
982 if (htmllabelattr_.empty())
983 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
984 return htmllabelattr_;
988 docstring Layout::htmlstyle() const
990 if (!htmlstyle_.empty() && !htmlforcecss_)
992 if (htmldefaultstyle_.empty())
994 docstring retval = htmldefaultstyle_;
995 if (!htmlstyle_.empty())
996 retval += '\n' + htmlstyle_;
1001 string Layout::defaultCSSClass() const
1003 if (!defaultcssclass_.empty())
1004 return defaultcssclass_;
1006 docstring::const_iterator it = name().begin();
1007 docstring::const_iterator en = name().end();
1008 for (; it != en; ++it) {
1009 char_type const c = *it;
1010 if (!isAlphaASCII(c)) {
1012 // make sure we don't start with an underscore,
1013 // as that sometimes causes problems.
1014 d = from_ascii("lyx_");
1017 } else if (isLower(c))
1020 // this is slow, so do it only if necessary
1023 defaultcssclass_ = to_utf8(d);
1024 return defaultcssclass_;
1030 string makeMarginValue(char const * side, double d)
1033 os << "margin-" << side << ": " << d << "ex;\n";
1040 void Layout::makeDefaultCSS() const
1042 // this never needs to be redone, since reloading layouts will
1043 // wipe out what we did before.
1044 if (!htmldefaultstyle_.empty())
1048 htmldefaultstyle_ = font.asCSS();
1053 tmp += makeMarginValue("top", topsep);
1055 tmp += makeMarginValue("bottom", bottomsep);
1056 if (!leftmargin.empty()) {
1057 // we can't really do what LyX does with the margin, so
1058 // we'll just figure out how many characters it is
1059 int const len = leftmargin.length();
1060 tmp += makeMarginValue("left", len);
1062 if (!rightmargin.empty()) {
1063 int const len = rightmargin.length();
1064 tmp += makeMarginValue("right", len);
1068 if (!htmldefaultstyle_.empty())
1069 htmldefaultstyle_ += from_ascii("\n");
1070 htmldefaultstyle_ += from_ascii(tmp);
1073 // tex2lyx does not see output_xhtml.cpp
1076 string where = alignmentToCSS(align);
1077 if (!where.empty()) {
1078 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1082 // wrap up what we have, if anything
1083 if (!htmldefaultstyle_.empty())
1085 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1086 htmldefaultstyle_ + from_ascii("\n}\n");
1088 if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1094 if (labelfont != font)
1095 labelCSS = labelfont.asCSS() + from_ascii("\n");
1096 if (labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1097 labelCSS += from_ascii("text-align: center;\n");
1099 if (!labelCSS.empty())
1100 htmldefaultstyle_ +=
1101 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1102 labelCSS + from_ascii("\n}\n");
1106 bool Layout::operator==(Layout const & rhs) const
1108 // This is enough for the applications we actually make,
1109 // at least at the moment. But we could check more.
1110 return name() == rhs.name()
1111 && latexname() == rhs.latexname()
1112 && latextype == rhs.latextype;