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]
52 //LT_ENVIRONMENT_DEFAULT,
58 LT_PARBREAK_IS_NEWLINE,
68 LT_LABELSTRING_APPENDIX,
111 LT_INTITLE // keep this last!
114 /////////////////////
119 margintype = MARGIN_STATIC;
120 latextype = LATEX_PARAGRAPH;
126 labelfont = inherit_font;
128 reslabelfont = sane_font;
129 nextnoindent = false;
134 labelbottomsep = 0.0;
136 align = LYX_ALIGN_BLOCK;
137 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
138 labeltype = LABEL_NO_LABEL;
139 endlabeltype = END_LABEL_NO_LABEL;
140 // Should or should not. That is the question.
141 // spacing.set(Spacing::OneHalf);
142 newline_allowed = true;
143 free_spacing = false;
145 parbreak_is_newline = false;
146 toclevel = NOT_IN_TOC;
148 htmllabelfirst_ = false;
149 htmlforcecss_ = false;
157 bool Layout::read(Lexer & lex, TextClass const & tclass)
159 // This table is sorted alphabetically [asierra 30March96]
160 LexerKeyword layoutTags[] = {
161 { "align", LT_ALIGN },
162 { "alignpossible", LT_ALIGNPOSSIBLE },
163 { "babelpreamble", LT_BABELPREAMBLE },
164 { "bottomsep", LT_BOTTOMSEP },
165 { "category", LT_CATEGORY },
166 { "commanddepth", LT_COMMANDDEPTH },
167 { "copystyle", LT_COPYSTYLE },
168 { "dependson", LT_DEPENDSON },
170 { "endlabelstring", LT_ENDLABELSTRING },
171 { "endlabeltype", LT_ENDLABELTYPE },
173 { "freespacing", LT_FREE_SPACING },
174 { "htmlattr", LT_HTMLATTR },
175 { "htmlforcecss", LT_HTMLFORCECSS },
176 { "htmlitem", LT_HTMLITEM },
177 { "htmlitemattr", LT_HTMLITEMATTR },
178 { "htmllabel", LT_HTMLLABEL },
179 { "htmllabelattr", LT_HTMLLABELATTR },
180 { "htmllabelfirst", LT_HTMLLABELFIRST },
181 { "htmlpreamble", LT_HTMLPREAMBLE },
182 { "htmlstyle", LT_HTMLSTYLE },
183 { "htmltag", LT_HTMLTAG },
184 { "htmltitle", LT_HTMLTITLE },
185 { "innertag", LT_INNERTAG },
186 { "inpreamble", LT_INPREAMBLE },
187 { "intitle", LT_INTITLE },
188 { "itemsep", LT_ITEMSEP },
189 { "itemtag", LT_ITEMTAG },
190 { "keepempty", LT_KEEPEMPTY },
191 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
192 { "labelcounter", LT_LABELCOUNTER },
193 { "labelfont", LT_LABELFONT },
194 { "labelindent", LT_LABELINDENT },
195 { "labelsep", LT_LABELSEP },
196 { "labelstring", LT_LABELSTRING },
197 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
198 { "labeltag", LT_LABELTAG },
199 { "labeltype", LT_LABELTYPE },
200 { "langpreamble", LT_LANGPREAMBLE },
201 { "latexname", LT_LATEXNAME },
202 { "latexparam", LT_LATEXPARAM },
203 { "latextype", LT_LATEXTYPE },
204 { "leftmargin", LT_LEFTMARGIN },
205 { "margin", LT_MARGIN },
206 { "needprotect", LT_NEED_PROTECT },
207 { "newline", LT_NEWLINE },
208 { "nextnoindent", LT_NEXTNOINDENT },
209 { "obsoletedby", LT_OBSOLETEDBY },
210 { "optionalargs", LT_OPTARGS },
211 { "parbreakisnewline", LT_PARBREAK_IS_NEWLINE },
212 { "parindent", LT_PARINDENT },
213 { "parsep", LT_PARSEP },
214 { "parskip", LT_PARSKIP },
215 { "passthru", LT_PASS_THRU },
216 { "preamble", LT_PREAMBLE },
217 { "refprefix", LT_REFPREFIX },
218 { "requiredargs", LT_REQARGS },
219 { "requires", LT_REQUIRES },
220 { "rightmargin", LT_RIGHTMARGIN },
221 { "spacing", LT_SPACING },
222 { "spellcheck", LT_SPELLCHECK },
223 { "textfont", LT_TEXTFONT },
224 { "toclevel", LT_TOCLEVEL },
225 { "topsep", LT_TOPSEP }
229 bool finished = false;
230 lex.pushTable(layoutTags);
231 // parse style section
232 while (!finished && lex.isOK() && !error) {
234 // See comment in LyXRC.cpp.
236 case Lexer::LEX_FEOF:
239 case Lexer::LEX_UNDEF: // parse error
240 lex.printError("Unknown layout tag `$$Token'");
247 switch (static_cast<LayoutTags>(le)) {
248 case LT_END: // end of structure
256 case LT_COPYSTYLE: { // initialize with a known style
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: { // replace with a known style
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_, '_', ' ');
304 case LT_MARGIN: // margin style definition.
308 case LT_LATEXTYPE: // LaTeX style definition.
332 case LT_NEED_PROTECT:
341 font = lyxRead(lex, font);
346 font = lyxRead(lex, font);
350 labelfont = lyxRead(lex, labelfont);
353 case LT_NEXTNOINDENT: // Indent next paragraph?
357 case LT_COMMANDDEPTH:
367 latexparam_ = subst(latexparam_, """, "\"");
383 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
386 case LT_LANGPREAMBLE:
387 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
390 case LT_BABELPREAMBLE:
391 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
398 case LT_ENDLABELTYPE:
399 readEndLabelType(lex);
402 case LT_LEFTMARGIN: // left margin type
406 case LT_RIGHTMARGIN: // right margin type
410 case LT_LABELINDENT: // label indenting flag
414 case LT_PARINDENT: // paragraph indent. flag
418 case LT_PARSKIP: // paragraph skip size
422 case LT_ITEMSEP: // item separation size
426 case LT_TOPSEP: // top separation size
430 case LT_BOTTOMSEP: // bottom separation size
434 case LT_LABEL_BOTTOMSEP: // label bottom separation size
435 lex >> labelbottomsep;
438 case LT_LABELSEP: // label separator
440 labelsep = subst(labelsep, 'x', ' ');
443 case LT_PARSEP: // par. separation size
447 case LT_NEWLINE: // newlines allowed?
448 lex >> newline_allowed;
451 case LT_ALIGN: // paragraph align
454 case LT_ALIGNPOSSIBLE: // paragraph allowed align
455 readAlignPossible(lex);
458 case LT_LABELSTRING: // label string definition
459 // FIXME: this means LT_ENDLABELSTRING may only
460 // occur after LT_LABELSTRING
462 labelstring_ = trim(labelstring_);
463 labelstring_appendix_ = labelstring_;
466 case LT_ENDLABELSTRING: // endlabel string definition
467 lex >> endlabelstring_;
468 endlabelstring_ = trim(endlabelstring_);
471 case LT_LABELSTRING_APPENDIX: // label string appendix definition
472 lex >> labelstring_appendix_;
473 labelstring_appendix_ = trim(labelstring_appendix_);
476 case LT_LABELCOUNTER: // name of counter to use
478 counter = trim(counter);
481 case LT_FREE_SPACING: // Allow for free spacing.
485 case LT_PASS_THRU: // Allow for pass thru.
489 case LT_PARBREAK_IS_NEWLINE:
490 lex >> parbreak_is_newline;
493 case LT_SPACING: // setspace.sty
499 vector<string> const req =
500 getVectorFromString(lex.getString());
501 requires_.insert(req.begin(), req.end());
527 case LT_HTMLITEMATTR:
528 lex >> htmlitemattr_;
532 lex >> htmllabeltag_;
535 case LT_HTMLLABELATTR:
536 lex >> htmllabelattr_;
539 case LT_HTMLLABELFIRST:
540 lex >> htmllabelfirst_;
544 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
547 case LT_HTMLFORCECSS:
548 lex >> htmlforcecss_;
551 case LT_HTMLPREAMBLE:
552 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
565 // make sure we only have inpreamble = true for commands
566 if (inpreamble && latextype != LATEX_COMMAND && latextype != LATEX_PARAGRAPH) {
567 LYXERR0("InPreamble not permitted except with command and paragraph layouts.");
568 LYXERR0("Layout name: " << name());
572 return finished && !error;
585 LexerKeyword alignTags[] = {
586 { "block", AT_BLOCK },
587 { "center", AT_CENTER },
588 { "layout", AT_LAYOUT },
590 { "right", AT_RIGHT }
594 void Layout::readAlign(Lexer & lex)
596 PushPopHelper pph(lex, alignTags);
599 case Lexer::LEX_UNDEF:
600 lex.printError("Unknown alignment `$$Token'");
606 align = LYX_ALIGN_BLOCK;
609 align = LYX_ALIGN_LEFT;
612 align = LYX_ALIGN_RIGHT;
615 align = LYX_ALIGN_CENTER;
618 align = LYX_ALIGN_LAYOUT;
624 void Layout::readAlignPossible(Lexer & lex)
626 lex.pushTable(alignTags);
627 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
628 int lineno = lex.lineNumber();
632 case Lexer::LEX_UNDEF:
633 lex.printError("Unknown alignment `$$Token'");
639 alignpossible |= LYX_ALIGN_BLOCK;
642 alignpossible |= LYX_ALIGN_LEFT;
645 alignpossible |= LYX_ALIGN_RIGHT;
648 alignpossible |= LYX_ALIGN_CENTER;
651 alignpossible |= LYX_ALIGN_LAYOUT;
654 } while (lineno == lex.lineNumber());
659 void Layout::readLabelType(Lexer & lex)
665 LA_CENTERED_TOP_ENVIRONMENT,
675 LexerKeyword labelTypeTags[] = {
676 { "bibliography", LA_BIBLIO },
677 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
678 { "counter", LA_COUNTER },
679 { "enumerate", LA_ENUMERATE },
680 { "itemize", LA_ITEMIZE },
681 { "manual", LA_MANUAL },
682 { "no_label", LA_NO_LABEL },
683 { "sensitive", LA_SENSITIVE },
684 { "static", LA_STATIC },
685 { "top_environment", LA_TOP_ENVIRONMENT }
688 PushPopHelper pph(lex, labelTypeTags);
691 case Lexer::LEX_UNDEF:
692 lex.printError("Unknown labeltype tag `$$Token'");
698 labeltype = LABEL_NO_LABEL;
701 labeltype = LABEL_MANUAL;
703 case LA_TOP_ENVIRONMENT:
704 labeltype = LABEL_TOP_ENVIRONMENT;
706 case LA_CENTERED_TOP_ENVIRONMENT:
707 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
710 labeltype = LABEL_STATIC;
713 labeltype = LABEL_SENSITIVE;
716 labeltype = LABEL_COUNTER;
719 labeltype = LABEL_ENUMERATE;
722 labeltype = LABEL_ITEMIZE;
725 labeltype = LABEL_BIBLIO;
731 void Layout::readEndLabelType(Lexer & lex)
733 static LexerKeyword endlabelTypeTags[] = {
734 { "box", END_LABEL_BOX },
735 { "filled_box", END_LABEL_FILLED_BOX },
736 { "no_label", END_LABEL_NO_LABEL },
737 { "static", END_LABEL_STATIC }
740 PushPopHelper pph(lex, endlabelTypeTags);
743 case Lexer::LEX_UNDEF:
744 lex.printError("Unknown labeltype tag `$$Token'");
746 case END_LABEL_STATIC:
748 case END_LABEL_FILLED_BOX:
749 case END_LABEL_NO_LABEL:
750 endlabeltype = static_cast<EndLabelType>(le);
753 LYXERR0("Unhandled value " << le);
759 void Layout::readMargin(Lexer & lex)
761 LexerKeyword marginTags[] = {
762 { "dynamic", MARGIN_DYNAMIC },
763 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
764 { "manual", MARGIN_MANUAL },
765 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
766 { "static", MARGIN_STATIC }
769 PushPopHelper pph(lex, marginTags);
773 case Lexer::LEX_UNDEF:
774 lex.printError("Unknown margin type tag `$$Token'");
779 case MARGIN_FIRST_DYNAMIC:
780 case MARGIN_RIGHT_ADDRESS_BOX:
781 margintype = static_cast<MarginType>(le);
784 LYXERR0("Unhandled value " << le);
790 void Layout::readLatexType(Lexer & lex)
792 LexerKeyword latexTypeTags[] = {
793 { "bib_environment", LATEX_BIB_ENVIRONMENT },
794 { "command", LATEX_COMMAND },
795 { "environment", LATEX_ENVIRONMENT },
796 { "item_environment", LATEX_ITEM_ENVIRONMENT },
797 { "list_environment", LATEX_LIST_ENVIRONMENT },
798 { "paragraph", LATEX_PARAGRAPH }
801 PushPopHelper pph(lex, latexTypeTags);
804 case Lexer::LEX_UNDEF:
805 lex.printError("Unknown latextype tag `$$Token'");
807 case LATEX_PARAGRAPH:
809 case LATEX_ENVIRONMENT:
810 case LATEX_ITEM_ENVIRONMENT:
811 case LATEX_BIB_ENVIRONMENT:
812 case LATEX_LIST_ENVIRONMENT:
813 latextype = static_cast<LatexType>(le);
816 LYXERR0("Unhandled value " << le);
822 void Layout::readSpacing(Lexer & lex)
825 ST_SPACING_SINGLE = 1,
831 LexerKeyword spacingTags[] = {
832 {"double", ST_SPACING_DOUBLE },
833 {"onehalf", ST_SPACING_ONEHALF },
834 {"other", ST_OTHER },
835 {"single", ST_SPACING_SINGLE }
838 PushPopHelper pph(lex, spacingTags);
841 case Lexer::LEX_UNDEF:
842 lex.printError("Unknown spacing token `$$Token'");
847 case ST_SPACING_SINGLE:
848 spacing.set(Spacing::Single);
850 case ST_SPACING_ONEHALF:
851 spacing.set(Spacing::Onehalf);
853 case ST_SPACING_DOUBLE:
854 spacing.set(Spacing::Double);
858 spacing.set(Spacing::Other, lex.getString());
866 docstring const i18npreamble(Language const * lang, docstring const & templ, bool const polyglossia)
871 string preamble = polyglossia ?
872 subst(to_utf8(templ), "$$lang", lang->polyglossia()) :
873 subst(to_utf8(templ), "$$lang", lang->babel());
876 // tex2lyx does not have getMessages()
877 LASSERT(false, /**/);
880 // lyx::regex is not unicode-safe.
881 // Should use QRegExp or (boost::u32regex, but that requires ICU)
882 static regex const reg("_\\(([^\\)]+)\\)");
884 while (regex_search(preamble, sub, reg)) {
885 string const key = sub.str(1);
886 string translated = to_utf8(lang->translateLayout(key));
887 preamble = subst(preamble, sub.str(), translated);
890 return from_utf8(preamble);
896 docstring const Layout::langpreamble(Language const * lang, bool const polyglossia) const
898 return i18npreamble(lang, langpreamble_, polyglossia);
902 docstring const Layout::babelpreamble(Language const * lang, bool const polyglossia) const
904 return i18npreamble(lang, babelpreamble_, polyglossia);
908 string const & Layout::htmltag() const
910 if (htmltag_.empty())
916 string const & Layout::htmlattr() const
918 if (htmlattr_.empty())
919 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
924 string const & Layout::htmlitemtag() const
926 if (htmlitemtag_.empty())
927 htmlitemtag_ = "div";
932 string const & Layout::htmlitemattr() const
934 if (htmlitemattr_.empty())
935 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
936 return htmlitemattr_;
940 string const & Layout::htmllabeltag() const
942 if (htmllabeltag_.empty()) {
943 if (labeltype != LABEL_TOP_ENVIRONMENT &&
944 labeltype != LABEL_CENTERED_TOP_ENVIRONMENT)
945 htmllabeltag_ = "span";
947 htmllabeltag_ = "div";
949 return htmllabeltag_;
953 string const & Layout::htmllabelattr() const
955 if (htmllabelattr_.empty())
956 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
957 return htmllabelattr_;
961 docstring Layout::htmlstyle() const {
962 if (!htmlstyle_.empty() && !htmlforcecss_)
964 if (htmldefaultstyle_.empty())
966 docstring retval = htmldefaultstyle_;
967 if (!htmlstyle_.empty())
968 retval += '\n' + htmlstyle_;
973 string Layout::defaultCSSClass() const
975 if (!defaultcssclass_.empty())
976 return defaultcssclass_;
978 docstring::const_iterator it = name().begin();
979 docstring::const_iterator en = name().end();
980 for (; it != en; ++it) {
981 char_type const c = *it;
982 if (!isAlphaASCII(c)) {
984 // make sure we don't start with an underscore,
985 // as that sometimes causes problems.
986 d = from_ascii("lyx_");
989 } else if (islower(c))
992 // this is slow, so do it only if necessary
995 defaultcssclass_ = to_utf8(d);
996 return defaultcssclass_;
1001 string makeMarginValue(char const * side, double d) {
1003 os << "margin-" << side << ": " << d << "ex;\n";
1009 void Layout::makeDefaultCSS() const {
1010 // this never needs to be redone, since reloading layouts will
1011 // wipe out what we did before.
1012 if (!htmldefaultstyle_.empty())
1016 htmldefaultstyle_ = font.asCSS();
1021 tmp += makeMarginValue("top", topsep);
1023 tmp += makeMarginValue("bottom", bottomsep);
1024 if (!leftmargin.empty()) {
1025 // we can't really do what LyX does with the margin, so
1026 // we'll just figure out how many characters it is
1027 int const len = leftmargin.length();
1028 tmp += makeMarginValue("left", len);
1030 if (!rightmargin.empty()) {
1031 int const len = rightmargin.length();
1032 tmp += makeMarginValue("right", len);
1036 if (!htmldefaultstyle_.empty())
1037 htmldefaultstyle_ += from_ascii("\n");
1038 htmldefaultstyle_ += from_ascii(tmp);
1041 // tex2lyx does not see output_xhtml.cpp
1044 string where = alignmentToCSS(align);
1045 if (!where.empty()) {
1046 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1050 // wrap up what we have, if anything
1051 if (!htmldefaultstyle_.empty())
1053 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1054 htmldefaultstyle_ + from_ascii("\n}\n");
1056 if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1062 if (labelfont != font)
1063 labelCSS = labelfont.asCSS() + from_ascii("\n");
1064 if (labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1065 labelCSS += from_ascii("text-align: center;\n");
1067 if (!labelCSS.empty())
1068 htmldefaultstyle_ +=
1069 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1070 labelCSS + from_ascii("\n}\n");
1074 bool Layout::operator==(Layout const & rhs) const
1076 // This is enough for the applications we actually make,
1077 // at least at the moment. But we could check more.
1078 return name() == rhs.name()
1079 && latexname() == rhs.latexname()
1080 && latextype == rhs.latextype;