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/regex.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]
54 LT_PARBREAK_IS_NEWLINE,
63 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;
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 { "babelpreamble", LT_BABELPREAMBLE },
158 { "bottomsep", LT_BOTTOMSEP },
159 { "category", LT_CATEGORY },
160 { "commanddepth", LT_COMMANDDEPTH },
161 { "copystyle", LT_COPYSTYLE },
162 { "dependson", LT_DEPENDSON },
164 { "endlabelstring", LT_ENDLABELSTRING },
165 { "endlabeltype", LT_ENDLABELTYPE },
167 { "freespacing", LT_FREE_SPACING },
168 { "htmlattr", LT_HTMLATTR },
169 { "htmlforcecss", LT_HTMLFORCECSS },
170 { "htmlitem", LT_HTMLITEM },
171 { "htmlitemattr", LT_HTMLITEMATTR },
172 { "htmllabel", LT_HTMLLABEL },
173 { "htmllabelattr", LT_HTMLLABELATTR },
174 { "htmllabelfirst", LT_HTMLLABELFIRST },
175 { "htmlpreamble", LT_HTMLPREAMBLE },
176 { "htmlstyle", LT_HTMLSTYLE },
177 { "htmltag", LT_HTMLTAG },
178 { "htmltitle", LT_HTMLTITLE },
179 { "innertag", LT_INNERTAG },
180 { "inpreamble", LT_INPREAMBLE },
181 { "intitle", LT_INTITLE },
182 { "itemsep", LT_ITEMSEP },
183 { "itemtag", LT_ITEMTAG },
184 { "keepempty", LT_KEEPEMPTY },
185 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
186 { "labelcounter", LT_LABELCOUNTER },
187 { "labelfont", LT_LABELFONT },
188 { "labelindent", LT_LABELINDENT },
189 { "labelsep", LT_LABELSEP },
190 { "labelstring", LT_LABELSTRING },
191 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
192 { "labeltag", LT_LABELTAG },
193 { "labeltype", LT_LABELTYPE },
194 { "langpreamble", LT_LANGPREAMBLE },
195 { "latexname", LT_LATEXNAME },
196 { "latexparam", LT_LATEXPARAM },
197 { "latextype", LT_LATEXTYPE },
198 { "leftmargin", LT_LEFTMARGIN },
199 { "margin", LT_MARGIN },
200 { "needprotect", LT_NEED_PROTECT },
201 { "newline", LT_NEWLINE },
202 { "nextnoindent", LT_NEXTNOINDENT },
203 { "obsoletedby", LT_OBSOLETEDBY },
204 { "optionalargs", LT_OPTARGS },
205 { "parbreakisnewline", LT_PARBREAK_IS_NEWLINE },
206 { "parindent", LT_PARINDENT },
207 { "parsep", LT_PARSEP },
208 { "parskip", LT_PARSKIP },
209 { "passthru", LT_PASS_THRU },
210 { "preamble", LT_PREAMBLE },
211 { "refprefix", LT_REFPREFIX },
212 { "requiredargs", LT_REQARGS },
213 { "requires", LT_REQUIRES },
214 { "rightmargin", LT_RIGHTMARGIN },
215 { "spacing", LT_SPACING },
216 { "spellcheck", LT_SPELLCHECK },
217 { "textfont", LT_TEXTFONT },
218 { "toclevel", LT_TOCLEVEL },
219 { "topsep", LT_TOPSEP }
223 bool finished = false;
224 lex.pushTable(layoutTags);
226 // parse style section
227 while (!finished && lex.isOK() && !error) {
229 // See comment in LyXRC.cpp.
231 case Lexer::LEX_FEOF:
234 case Lexer::LEX_UNDEF:
236 lex.printError("Unknown layout tag `$$Token'");
243 switch (static_cast<LayoutTags>(le)) {
255 style = subst(style, '_', ' ');
257 if (tclass.hasLayout(style)) {
258 docstring const tmpname = name_;
259 this->operator=(tclass[style]);
262 LYXERR0("Cannot copy unknown style `"
264 << "All layouts so far:");
265 DocumentClass::const_iterator lit = tclass.begin();
266 DocumentClass::const_iterator len = tclass.end();
267 for (; lit != len; ++lit)
268 LYXERR0(lit->name());
273 case LT_OBSOLETEDBY: {
276 style = subst(style, '_', ' ');
278 if (tclass.hasLayout(style)) {
279 docstring const tmpname = name_;
280 this->operator=(tclass[style]);
282 if (obsoleted_by().empty())
283 obsoleted_by_ = style;
285 LYXERR0("Cannot replace with unknown style `"
288 //lex.printError("Cannot replace with"
297 depends_on_ = subst(depends_on_, '_', ' ');
328 case LT_NEED_PROTECT:
337 font = lyxRead(lex, font);
342 font = lyxRead(lex, font);
346 labelfont = lyxRead(lex, labelfont);
349 case LT_NEXTNOINDENT:
353 case LT_COMMANDDEPTH:
363 latexparam_ = subst(latexparam_, """, "\"");
379 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
382 case LT_LANGPREAMBLE:
383 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
386 case LT_BABELPREAMBLE:
387 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
394 case LT_ENDLABELTYPE:
395 readEndLabelType(lex);
430 case LT_LABEL_BOTTOMSEP:
431 lex >> labelbottomsep;
436 labelsep = subst(labelsep, 'x', ' ');
444 lex >> newline_allowed;
451 case LT_ALIGNPOSSIBLE:
452 readAlignPossible(lex);
456 // FIXME: this means LT_ENDLABELSTRING may only
457 // occur after LT_LABELSTRING
459 labelstring_ = trim(labelstring_);
460 labelstring_appendix_ = labelstring_;
463 case LT_ENDLABELSTRING:
464 lex >> endlabelstring_;
465 endlabelstring_ = trim(endlabelstring_);
468 case LT_LABELSTRING_APPENDIX:
469 lex >> labelstring_appendix_;
470 labelstring_appendix_ = trim(labelstring_appendix_);
473 case LT_LABELCOUNTER:
475 counter = trim(counter);
478 case LT_FREE_SPACING:
486 case LT_PARBREAK_IS_NEWLINE:
487 lex >> parbreak_is_newline;
496 vector<string> const req =
497 getVectorFromString(lex.getString());
498 requires_.insert(req.begin(), req.end());
524 case LT_HTMLITEMATTR:
525 lex >> htmlitemattr_;
529 lex >> htmllabeltag_;
532 case LT_HTMLLABELATTR:
533 lex >> htmllabelattr_;
536 case LT_HTMLLABELFIRST:
537 lex >> htmllabelfirst_;
541 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
544 case LT_HTMLFORCECSS:
545 lex >> htmlforcecss_;
548 case LT_HTMLPREAMBLE:
549 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
562 // make sure we only have inpreamble = true for commands
563 if (inpreamble && latextype != LATEX_COMMAND && latextype != LATEX_PARAGRAPH) {
564 LYXERR0("InPreamble not permitted except with command and paragraph layouts.");
565 LYXERR0("Layout name: " << name());
569 return finished && !error;
582 LexerKeyword alignTags[] = {
583 { "block", AT_BLOCK },
584 { "center", AT_CENTER },
585 { "layout", AT_LAYOUT },
587 { "right", AT_RIGHT }
591 void Layout::readAlign(Lexer & lex)
593 PushPopHelper pph(lex, alignTags);
596 case Lexer::LEX_UNDEF:
597 lex.printError("Unknown alignment `$$Token'");
603 align = LYX_ALIGN_BLOCK;
606 align = LYX_ALIGN_LEFT;
609 align = LYX_ALIGN_RIGHT;
612 align = LYX_ALIGN_CENTER;
615 align = LYX_ALIGN_LAYOUT;
621 void Layout::readAlignPossible(Lexer & lex)
623 lex.pushTable(alignTags);
624 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
625 int lineno = lex.lineNumber();
629 case Lexer::LEX_UNDEF:
630 lex.printError("Unknown alignment `$$Token'");
636 alignpossible |= LYX_ALIGN_BLOCK;
639 alignpossible |= LYX_ALIGN_LEFT;
642 alignpossible |= LYX_ALIGN_RIGHT;
645 alignpossible |= LYX_ALIGN_CENTER;
648 alignpossible |= LYX_ALIGN_LAYOUT;
651 } while (lineno == lex.lineNumber());
656 void Layout::readLabelType(Lexer & lex)
662 LA_CENTERED_TOP_ENVIRONMENT,
672 LexerKeyword labelTypeTags[] = {
673 { "bibliography", LA_BIBLIO },
674 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
675 { "counter", LA_COUNTER },
676 { "enumerate", LA_ENUMERATE },
677 { "itemize", LA_ITEMIZE },
678 { "manual", LA_MANUAL },
679 { "no_label", LA_NO_LABEL },
680 { "sensitive", LA_SENSITIVE },
681 { "static", LA_STATIC },
682 { "top_environment", LA_TOP_ENVIRONMENT }
685 PushPopHelper pph(lex, labelTypeTags);
688 case Lexer::LEX_UNDEF:
689 lex.printError("Unknown labeltype tag `$$Token'");
695 labeltype = LABEL_NO_LABEL;
698 labeltype = LABEL_MANUAL;
700 case LA_TOP_ENVIRONMENT:
701 labeltype = LABEL_TOP_ENVIRONMENT;
703 case LA_CENTERED_TOP_ENVIRONMENT:
704 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
707 labeltype = LABEL_STATIC;
710 labeltype = LABEL_SENSITIVE;
713 labeltype = LABEL_COUNTER;
716 labeltype = LABEL_ENUMERATE;
719 labeltype = LABEL_ITEMIZE;
722 labeltype = LABEL_BIBLIO;
728 void Layout::readEndLabelType(Lexer & lex)
730 static LexerKeyword endlabelTypeTags[] = {
731 { "box", END_LABEL_BOX },
732 { "filled_box", END_LABEL_FILLED_BOX },
733 { "no_label", END_LABEL_NO_LABEL },
734 { "static", END_LABEL_STATIC }
737 PushPopHelper pph(lex, endlabelTypeTags);
740 case Lexer::LEX_UNDEF:
741 lex.printError("Unknown labeltype tag `$$Token'");
743 case END_LABEL_STATIC:
745 case END_LABEL_FILLED_BOX:
746 case END_LABEL_NO_LABEL:
747 endlabeltype = static_cast<EndLabelType>(le);
750 LYXERR0("Unhandled value " << le);
756 void Layout::readMargin(Lexer & lex)
758 LexerKeyword marginTags[] = {
759 { "dynamic", MARGIN_DYNAMIC },
760 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
761 { "manual", MARGIN_MANUAL },
762 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
763 { "static", MARGIN_STATIC }
766 PushPopHelper pph(lex, marginTags);
770 case Lexer::LEX_UNDEF:
771 lex.printError("Unknown margin type tag `$$Token'");
776 case MARGIN_FIRST_DYNAMIC:
777 case MARGIN_RIGHT_ADDRESS_BOX:
778 margintype = static_cast<MarginType>(le);
781 LYXERR0("Unhandled value " << le);
787 void Layout::readLatexType(Lexer & lex)
789 LexerKeyword latexTypeTags[] = {
790 { "bib_environment", LATEX_BIB_ENVIRONMENT },
791 { "command", LATEX_COMMAND },
792 { "environment", LATEX_ENVIRONMENT },
793 { "item_environment", LATEX_ITEM_ENVIRONMENT },
794 { "list_environment", LATEX_LIST_ENVIRONMENT },
795 { "paragraph", LATEX_PARAGRAPH }
798 PushPopHelper pph(lex, latexTypeTags);
801 case Lexer::LEX_UNDEF:
802 lex.printError("Unknown latextype tag `$$Token'");
804 case LATEX_PARAGRAPH:
806 case LATEX_ENVIRONMENT:
807 case LATEX_ITEM_ENVIRONMENT:
808 case LATEX_BIB_ENVIRONMENT:
809 case LATEX_LIST_ENVIRONMENT:
810 latextype = static_cast<LatexType>(le);
813 LYXERR0("Unhandled value " << le);
819 void Layout::readSpacing(Lexer & lex)
822 ST_SPACING_SINGLE = 1,
828 LexerKeyword spacingTags[] = {
829 {"double", ST_SPACING_DOUBLE },
830 {"onehalf", ST_SPACING_ONEHALF },
831 {"other", ST_OTHER },
832 {"single", ST_SPACING_SINGLE }
835 PushPopHelper pph(lex, spacingTags);
838 case Lexer::LEX_UNDEF:
839 lex.printError("Unknown spacing token `$$Token'");
844 case ST_SPACING_SINGLE:
845 spacing.set(Spacing::Single);
847 case ST_SPACING_ONEHALF:
848 spacing.set(Spacing::Onehalf);
850 case ST_SPACING_DOUBLE:
851 spacing.set(Spacing::Double);
855 spacing.set(Spacing::Other, lex.getString());
863 docstring const i18npreamble(Language const * lang, docstring const & templ, bool const polyglossia)
868 string preamble = polyglossia ?
869 subst(to_utf8(templ), "$$lang", lang->polyglossia()) :
870 subst(to_utf8(templ), "$$lang", lang->babel());
873 // tex2lyx does not have getMessages()
874 LASSERT(false, /**/);
877 // lyx::regex is not unicode-safe.
878 // Should use QRegExp or (boost::u32regex, but that requires ICU)
879 static regex const reg("_\\(([^\\)]+)\\)");
881 while (regex_search(preamble, sub, reg)) {
882 string const key = sub.str(1);
883 string translated = to_utf8(lang->translateLayout(key));
884 preamble = subst(preamble, sub.str(), translated);
887 return from_utf8(preamble);
893 docstring const Layout::langpreamble(Language const * lang, bool const polyglossia) const
895 return i18npreamble(lang, langpreamble_, polyglossia);
899 docstring const Layout::babelpreamble(Language const * lang, bool const polyglossia) const
901 return i18npreamble(lang, babelpreamble_, polyglossia);
905 string const & Layout::htmltag() const
907 if (htmltag_.empty())
913 string const & Layout::htmlattr() const
915 if (htmlattr_.empty())
916 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
921 string const & Layout::htmlitemtag() const
923 if (htmlitemtag_.empty())
924 htmlitemtag_ = "div";
929 string const & Layout::htmlitemattr() const
931 if (htmlitemattr_.empty())
932 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
933 return htmlitemattr_;
937 string const & Layout::htmllabeltag() const
939 if (htmllabeltag_.empty()) {
940 if (labeltype != LABEL_TOP_ENVIRONMENT &&
941 labeltype != LABEL_CENTERED_TOP_ENVIRONMENT)
942 htmllabeltag_ = "span";
944 htmllabeltag_ = "div";
946 return htmllabeltag_;
950 string const & Layout::htmllabelattr() const
952 if (htmllabelattr_.empty())
953 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
954 return htmllabelattr_;
958 docstring Layout::htmlstyle() const {
959 if (!htmlstyle_.empty() && !htmlforcecss_)
961 if (htmldefaultstyle_.empty())
963 docstring retval = htmldefaultstyle_;
964 if (!htmlstyle_.empty())
965 retval += '\n' + htmlstyle_;
970 string Layout::defaultCSSClass() const
972 if (!defaultcssclass_.empty())
973 return defaultcssclass_;
975 docstring::const_iterator it = name().begin();
976 docstring::const_iterator en = name().end();
977 for (; it != en; ++it) {
978 char_type const c = *it;
979 if (!isAlphaASCII(c)) {
981 // make sure we don't start with an underscore,
982 // as that sometimes causes problems.
983 d = from_ascii("lyx_");
986 } else if (islower(c))
989 // this is slow, so do it only if necessary
992 defaultcssclass_ = to_utf8(d);
993 return defaultcssclass_;
998 string makeMarginValue(char const * side, double d) {
1000 os << "margin-" << side << ": " << d << "ex;\n";
1006 void Layout::makeDefaultCSS() const {
1007 // this never needs to be redone, since reloading layouts will
1008 // wipe out what we did before.
1009 if (!htmldefaultstyle_.empty())
1013 htmldefaultstyle_ = font.asCSS();
1018 tmp += makeMarginValue("top", topsep);
1020 tmp += makeMarginValue("bottom", bottomsep);
1021 if (!leftmargin.empty()) {
1022 // we can't really do what LyX does with the margin, so
1023 // we'll just figure out how many characters it is
1024 int const len = leftmargin.length();
1025 tmp += makeMarginValue("left", len);
1027 if (!rightmargin.empty()) {
1028 int const len = rightmargin.length();
1029 tmp += makeMarginValue("right", len);
1033 if (!htmldefaultstyle_.empty())
1034 htmldefaultstyle_ += from_ascii("\n");
1035 htmldefaultstyle_ += from_ascii(tmp);
1038 // tex2lyx does not see output_xhtml.cpp
1041 string where = alignmentToCSS(align);
1042 if (!where.empty()) {
1043 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1047 // wrap up what we have, if anything
1048 if (!htmldefaultstyle_.empty())
1050 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1051 htmldefaultstyle_ + from_ascii("\n}\n");
1053 if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1059 if (labelfont != font)
1060 labelCSS = labelfont.asCSS() + from_ascii("\n");
1061 if (labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1062 labelCSS += from_ascii("text-align: center;\n");
1064 if (!labelCSS.empty())
1065 htmldefaultstyle_ +=
1066 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1067 labelCSS + from_ascii("\n}\n");
1071 bool Layout::operator==(Layout const & rhs) const
1073 // This is enough for the applications we actually make,
1074 // at least at the moment. But we could check more.
1075 return name() == rhs.name()
1076 && latexname() == rhs.latexname()
1077 && latextype == rhs.latextype;