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"
27 #include "support/regex.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]
51 //LT_ENVIRONMENT_DEFAULT,
59 LT_PARBREAK_IS_NEWLINE,
69 LT_LABELSTRING_APPENDIX,
112 LT_INTITLE // keep this last!
115 /////////////////////
120 margintype = MARGIN_STATIC;
121 latextype = LATEX_PARAGRAPH;
127 labelfont = inherit_font;
129 reslabelfont = sane_font;
130 nextnoindent = false;
135 labelbottomsep = 0.0;
137 align = LYX_ALIGN_BLOCK;
138 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
139 labeltype = LABEL_NO_LABEL;
140 endlabeltype = END_LABEL_NO_LABEL;
141 // Should or should not. That is the question.
142 // spacing.set(Spacing::OneHalf);
145 newline_allowed = true;
146 free_spacing = false;
148 parbreak_is_newline = false;
149 toclevel = NOT_IN_TOC;
151 htmllabelfirst_ = false;
152 htmlforcecss_ = false;
160 bool Layout::read(Lexer & lex, TextClass const & tclass)
162 // This table is sorted alphabetically [asierra 30March96]
163 LexerKeyword layoutTags[] = {
164 { "align", LT_ALIGN },
165 { "alignpossible", LT_ALIGNPOSSIBLE },
166 { "babelpreamble", LT_BABELPREAMBLE },
167 { "bottomsep", LT_BOTTOMSEP },
168 { "category", LT_CATEGORY },
169 { "commanddepth", LT_COMMANDDEPTH },
170 { "copystyle", LT_COPYSTYLE },
171 { "dependson", LT_DEPENDSON },
173 { "endlabelstring", LT_ENDLABELSTRING },
174 { "endlabeltype", LT_ENDLABELTYPE },
175 { "fill_bottom", LT_FILL_BOTTOM },
176 { "fill_top", LT_FILL_TOP },
178 { "freespacing", LT_FREE_SPACING },
179 { "htmlattr", LT_HTMLATTR },
180 { "htmlforcecss", LT_HTMLFORCECSS },
181 { "htmlitem", LT_HTMLITEM },
182 { "htmlitemattr", LT_HTMLITEMATTR },
183 { "htmllabel", LT_HTMLLABEL },
184 { "htmllabelattr", LT_HTMLLABELATTR },
185 { "htmllabelfirst", LT_HTMLLABELFIRST },
186 { "htmlpremable", LT_HTMLPREAMBLE },
187 { "htmlstyle", LT_HTMLSTYLE },
188 { "htmltag", LT_HTMLTAG },
189 { "htmltitle", LT_HTMLTITLE },
190 { "innertag", LT_INNERTAG },
191 { "inpreamble", LT_INPREAMBLE },
192 { "intitle", LT_INTITLE },
193 { "itemsep", LT_ITEMSEP },
194 { "itemtag", LT_ITEMTAG },
195 { "keepempty", LT_KEEPEMPTY },
196 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
197 { "labelcounter", LT_LABELCOUNTER },
198 { "labelfont", LT_LABELFONT },
199 { "labelindent", LT_LABELINDENT },
200 { "labelsep", LT_LABELSEP },
201 { "labelstring", LT_LABELSTRING },
202 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
203 { "labeltag", LT_LABELTAG },
204 { "labeltype", LT_LABELTYPE },
205 { "langpreamble", LT_LANGPREAMBLE },
206 { "latexname", LT_LATEXNAME },
207 { "latexparam", LT_LATEXPARAM },
208 { "latextype", LT_LATEXTYPE },
209 { "leftmargin", LT_LEFTMARGIN },
210 { "margin", LT_MARGIN },
211 { "needprotect", LT_NEED_PROTECT },
212 { "newline", LT_NEWLINE },
213 { "nextnoindent", LT_NEXTNOINDENT },
214 { "obsoletedby", LT_OBSOLETEDBY },
215 { "optionalargs", LT_OPTARGS },
216 { "parbreakisnewline", LT_PARBREAK_IS_NEWLINE },
217 { "parindent", LT_PARINDENT },
218 { "parsep", LT_PARSEP },
219 { "parskip", LT_PARSKIP },
220 { "passthru", LT_PASS_THRU },
221 { "preamble", LT_PREAMBLE },
222 { "refprefix", LT_REFPREFIX },
223 { "requiredargs", LT_REQARGS },
224 { "requires", LT_REQUIRES },
225 { "rightmargin", LT_RIGHTMARGIN },
226 { "spacing", LT_SPACING },
227 { "spellcheck", LT_SPELLCHECK },
228 { "textfont", LT_TEXTFONT },
229 { "toclevel", LT_TOCLEVEL },
230 { "topsep", LT_TOPSEP }
234 bool finished = false;
235 lex.pushTable(layoutTags);
236 // parse style section
237 while (!finished && lex.isOK() && !error) {
239 // See comment in LyXRC.cpp.
241 case Lexer::LEX_FEOF:
244 case Lexer::LEX_UNDEF: // parse error
245 lex.printError("Unknown layout tag `$$Token'");
252 switch (static_cast<LayoutTags>(le)) {
253 case LT_END: // end of structure
261 case LT_COPYSTYLE: { // initialize with a known style
264 style = subst(style, '_', ' ');
266 if (tclass.hasLayout(style)) {
267 docstring const tmpname = name_;
268 this->operator=(tclass[style]);
271 LYXERR0("Cannot copy unknown style `"
273 << "All layouts so far:");
274 DocumentClass::const_iterator lit = tclass.begin();
275 DocumentClass::const_iterator len = tclass.end();
276 for (; lit != len; ++lit)
277 LYXERR0(lit->name());
282 case LT_OBSOLETEDBY: { // replace with a known style
285 style = subst(style, '_', ' ');
287 if (tclass.hasLayout(style)) {
288 docstring const tmpname = name_;
289 this->operator=(tclass[style]);
291 if (obsoleted_by().empty())
292 obsoleted_by_ = style;
294 LYXERR0("Cannot replace with unknown style `"
297 //lex.printError("Cannot replace with"
306 depends_on_ = subst(depends_on_, '_', ' ');
309 case LT_MARGIN: // margin style definition.
313 case LT_LATEXTYPE: // LaTeX style definition.
337 case LT_NEED_PROTECT:
346 font = lyxRead(lex, font);
351 font = lyxRead(lex, font);
355 labelfont = lyxRead(lex, labelfont);
358 case LT_NEXTNOINDENT: // Indent next paragraph?
362 case LT_COMMANDDEPTH:
372 latexparam_ = subst(latexparam_, """, "\"");
388 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
391 case LT_LANGPREAMBLE:
392 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
395 case LT_BABELPREAMBLE:
396 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
403 case LT_ENDLABELTYPE:
404 readEndLabelType(lex);
407 case LT_LEFTMARGIN: // left margin type
411 case LT_RIGHTMARGIN: // right margin type
415 case LT_LABELINDENT: // label indenting flag
419 case LT_PARINDENT: // paragraph indent. flag
423 case LT_PARSKIP: // paragraph skip size
427 case LT_ITEMSEP: // item separation size
431 case LT_TOPSEP: // top separation size
435 case LT_BOTTOMSEP: // bottom separation size
439 case LT_LABEL_BOTTOMSEP: // label bottom separation size
440 lex >> labelbottomsep;
443 case LT_LABELSEP: // label separator
445 labelsep = subst(labelsep, 'x', ' ');
448 case LT_PARSEP: // par. separation size
452 case LT_FILL_TOP: // fill top flag
456 case LT_FILL_BOTTOM: // fill bottom flag
460 case LT_NEWLINE: // newlines allowed?
461 lex >> newline_allowed;
464 case LT_ALIGN: // paragraph align
467 case LT_ALIGNPOSSIBLE: // paragraph allowed align
468 readAlignPossible(lex);
471 case LT_LABELSTRING: // label string definition
472 // FIXME: this means LT_ENDLABELSTRING may only
473 // occur after LT_LABELSTRING
475 labelstring_ = trim(labelstring_);
476 labelstring_appendix_ = labelstring_;
479 case LT_ENDLABELSTRING: // endlabel string definition
480 lex >> endlabelstring_;
481 endlabelstring_ = trim(endlabelstring_);
484 case LT_LABELSTRING_APPENDIX: // label string appendix definition
485 lex >> labelstring_appendix_;
486 labelstring_appendix_ = trim(labelstring_appendix_);
489 case LT_LABELCOUNTER: // name of counter to use
491 counter = trim(counter);
494 case LT_FREE_SPACING: // Allow for free spacing.
498 case LT_PASS_THRU: // Allow for pass thru.
502 case LT_PARBREAK_IS_NEWLINE:
503 lex >> parbreak_is_newline;
506 case LT_SPACING: // setspace.sty
512 vector<string> const req =
513 getVectorFromString(lex.getString());
514 requires_.insert(req.begin(), req.end());
540 case LT_HTMLITEMATTR:
541 lex >> htmlitemattr_;
545 lex >> htmllabeltag_;
548 case LT_HTMLLABELATTR:
549 lex >> htmllabelattr_;
552 case LT_HTMLLABELFIRST:
553 lex >> htmllabelfirst_;
557 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
560 case LT_HTMLFORCECSS:
561 lex >> htmlforcecss_;
564 case LT_HTMLPREAMBLE:
565 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
578 // make sure we only have inpreamble = true for commands
579 if (inpreamble && latextype != LATEX_COMMAND) {
580 LYXERR0("InPreamble not permitted except with Command-type layouts.");
581 LYXERR0("Layout name: " << name());
585 return finished && !error;
598 LexerKeyword alignTags[] = {
599 { "block", AT_BLOCK },
600 { "center", AT_CENTER },
601 { "layout", AT_LAYOUT },
603 { "right", AT_RIGHT }
607 void Layout::readAlign(Lexer & lex)
609 PushPopHelper pph(lex, alignTags);
612 case Lexer::LEX_UNDEF:
613 lex.printError("Unknown alignment `$$Token'");
619 align = LYX_ALIGN_BLOCK;
622 align = LYX_ALIGN_LEFT;
625 align = LYX_ALIGN_RIGHT;
628 align = LYX_ALIGN_CENTER;
631 align = LYX_ALIGN_LAYOUT;
637 void Layout::readAlignPossible(Lexer & lex)
639 lex.pushTable(alignTags);
640 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
641 int lineno = lex.lineNumber();
645 case Lexer::LEX_UNDEF:
646 lex.printError("Unknown alignment `$$Token'");
652 alignpossible |= LYX_ALIGN_BLOCK;
655 alignpossible |= LYX_ALIGN_LEFT;
658 alignpossible |= LYX_ALIGN_RIGHT;
661 alignpossible |= LYX_ALIGN_CENTER;
664 alignpossible |= LYX_ALIGN_LAYOUT;
667 } while (lineno == lex.lineNumber());
672 void Layout::readLabelType(Lexer & lex)
678 LA_CENTERED_TOP_ENVIRONMENT,
688 LexerKeyword labelTypeTags[] = {
689 { "bibliography", LA_BIBLIO },
690 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
691 { "counter", LA_COUNTER },
692 { "enumerate", LA_ENUMERATE },
693 { "itemize", LA_ITEMIZE },
694 { "manual", LA_MANUAL },
695 { "no_label", LA_NO_LABEL },
696 { "sensitive", LA_SENSITIVE },
697 { "static", LA_STATIC },
698 { "top_environment", LA_TOP_ENVIRONMENT }
701 PushPopHelper pph(lex, labelTypeTags);
704 case Lexer::LEX_UNDEF:
705 lex.printError("Unknown labeltype tag `$$Token'");
711 labeltype = LABEL_NO_LABEL;
714 labeltype = LABEL_MANUAL;
716 case LA_TOP_ENVIRONMENT:
717 labeltype = LABEL_TOP_ENVIRONMENT;
719 case LA_CENTERED_TOP_ENVIRONMENT:
720 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
723 labeltype = LABEL_STATIC;
726 labeltype = LABEL_SENSITIVE;
729 labeltype = LABEL_COUNTER;
732 labeltype = LABEL_ENUMERATE;
735 labeltype = LABEL_ITEMIZE;
738 labeltype = LABEL_BIBLIO;
744 void Layout::readEndLabelType(Lexer & lex)
746 static LexerKeyword endlabelTypeTags[] = {
747 { "box", END_LABEL_BOX },
748 { "filled_box", END_LABEL_FILLED_BOX },
749 { "no_label", END_LABEL_NO_LABEL },
750 { "static", END_LABEL_STATIC }
753 PushPopHelper pph(lex, endlabelTypeTags);
756 case Lexer::LEX_UNDEF:
757 lex.printError("Unknown labeltype tag `$$Token'");
759 case END_LABEL_STATIC:
761 case END_LABEL_FILLED_BOX:
762 case END_LABEL_NO_LABEL:
763 endlabeltype = static_cast<EndLabelType>(le);
766 LYXERR0("Unhandled value " << le);
772 void Layout::readMargin(Lexer & lex)
774 LexerKeyword marginTags[] = {
775 { "dynamic", MARGIN_DYNAMIC },
776 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
777 { "manual", MARGIN_MANUAL },
778 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
779 { "static", MARGIN_STATIC }
782 PushPopHelper pph(lex, marginTags);
786 case Lexer::LEX_UNDEF:
787 lex.printError("Unknown margin type tag `$$Token'");
792 case MARGIN_FIRST_DYNAMIC:
793 case MARGIN_RIGHT_ADDRESS_BOX:
794 margintype = static_cast<MarginType>(le);
797 LYXERR0("Unhandled value " << le);
803 void Layout::readLatexType(Lexer & lex)
805 LexerKeyword latexTypeTags[] = {
806 { "bib_environment", LATEX_BIB_ENVIRONMENT },
807 { "command", LATEX_COMMAND },
808 { "environment", LATEX_ENVIRONMENT },
809 { "item_environment", LATEX_ITEM_ENVIRONMENT },
810 { "list_environment", LATEX_LIST_ENVIRONMENT },
811 { "paragraph", LATEX_PARAGRAPH }
814 PushPopHelper pph(lex, latexTypeTags);
817 case Lexer::LEX_UNDEF:
818 lex.printError("Unknown latextype tag `$$Token'");
820 case LATEX_PARAGRAPH:
822 case LATEX_ENVIRONMENT:
823 case LATEX_ITEM_ENVIRONMENT:
824 case LATEX_BIB_ENVIRONMENT:
825 case LATEX_LIST_ENVIRONMENT:
826 latextype = static_cast<LatexType>(le);
829 LYXERR0("Unhandled value " << le);
835 void Layout::readSpacing(Lexer & lex)
838 ST_SPACING_SINGLE = 1,
844 LexerKeyword spacingTags[] = {
845 {"double", ST_SPACING_DOUBLE },
846 {"onehalf", ST_SPACING_ONEHALF },
847 {"other", ST_OTHER },
848 {"single", ST_SPACING_SINGLE }
851 PushPopHelper pph(lex, spacingTags);
854 case Lexer::LEX_UNDEF:
855 lex.printError("Unknown spacing token `$$Token'");
860 case ST_SPACING_SINGLE:
861 spacing.set(Spacing::Single);
863 case ST_SPACING_ONEHALF:
864 spacing.set(Spacing::Onehalf);
866 case ST_SPACING_DOUBLE:
867 spacing.set(Spacing::Double);
871 spacing.set(Spacing::Other, lex.getString());
879 docstring const i18npreamble(Language const * lang, docstring const & templ)
884 string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
887 // tex2lyx does not have getMessages()
888 LASSERT(false, /**/);
891 // lyx::regex is not unicode-safe.
892 // Should use QRegExp or (boost::u32regex, but that requires ICU)
893 static regex const reg("_\\(([^\\)]+)\\)");
895 while (regex_search(preamble, sub, reg)) {
896 string const key = sub.str(1);
899 translated = to_utf8(getMessages(lang->code()).get(key));
901 lyxerr << "Warning: not translating `" << key
902 << "' because it is not pure ASCII." << endl;
905 preamble = subst(preamble, sub.str(), translated);
908 return from_utf8(preamble);
914 docstring const Layout::langpreamble(Language const * lang) const
916 return i18npreamble(lang, langpreamble_);
920 docstring const Layout::babelpreamble(Language const * lang) const
922 return i18npreamble(lang, babelpreamble_);
926 string const & Layout::htmltag() const
928 if (htmltag_.empty())
934 string const & Layout::htmlattr() const
936 if (htmlattr_.empty())
937 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
942 string const & Layout::htmlitemtag() const
944 if (htmlitemtag_.empty())
945 htmlitemtag_ = "div";
950 string const & Layout::htmlitemattr() const
952 if (htmlitemattr_.empty())
953 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
954 return htmlitemattr_;
958 string const & Layout::htmllabeltag() const
960 if (htmllabeltag_.empty()) {
961 if (labeltype != LABEL_TOP_ENVIRONMENT &&
962 labeltype != LABEL_CENTERED_TOP_ENVIRONMENT)
963 htmllabeltag_ = "span";
965 htmllabeltag_ = "div";
967 return htmllabeltag_;
971 string const & Layout::htmllabelattr() const
973 if (htmllabelattr_.empty())
974 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
975 return htmllabelattr_;
979 docstring Layout::htmlstyle() const {
980 if (!htmlstyle_.empty() && !htmlforcecss_)
982 if (htmldefaultstyle_.empty())
984 docstring retval = htmldefaultstyle_;
985 if (!htmlstyle_.empty())
986 retval += '\n' + htmlstyle_;
991 string Layout::defaultCSSClass() const
993 if (!defaultcssclass_.empty())
994 return defaultcssclass_;
996 docstring::const_iterator it = name().begin();
997 docstring::const_iterator en = name().end();
998 for (; it != en; ++it) {
1004 d += lowercase(*it);
1006 // are there other characters we need to remove?
1007 defaultcssclass_ = to_utf8(d);
1008 return defaultcssclass_;
1013 string makeMarginValue(char const * side, double d) {
1015 os << "margin-" << side << ": " << d << "ex;\n";
1021 void Layout::makeDefaultCSS() const {
1022 // this never needs to be redone, since reloading layouts will
1023 // wipe out what we did before.
1024 if (!htmldefaultstyle_.empty())
1028 htmldefaultstyle_ = font.asCSS();
1033 tmp += makeMarginValue("top", topsep);
1035 tmp += makeMarginValue("bottom", bottomsep);
1036 if (!leftmargin.empty()) {
1037 // we can't really do what LyX does with the margin, so
1038 // we'll just figure out how many characters it is
1039 int const len = leftmargin.length();
1040 tmp += makeMarginValue("left", len);
1042 if (!rightmargin.empty()) {
1043 int const len = rightmargin.length();
1044 tmp += makeMarginValue("right", len);
1048 if (!htmldefaultstyle_.empty())
1049 htmldefaultstyle_ += from_ascii("\n");
1050 htmldefaultstyle_ += from_ascii(tmp);
1053 // tex2lyx does not see output_xhtml.cpp
1056 string where = alignmentToCSS(align);
1057 if (!where.empty()) {
1058 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1062 // wrap up what we have, if anything
1063 if (!htmldefaultstyle_.empty())
1065 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1066 htmldefaultstyle_ + from_ascii("\n}\n");
1068 if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1074 if (labelfont != font)
1075 labelCSS = labelfont.asCSS() + from_ascii("\n");
1076 if (labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1077 labelCSS += from_ascii("text-align: center;\n");
1079 if (!labelCSS.empty())
1080 htmldefaultstyle_ +=
1081 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1082 labelCSS + from_ascii("\n}\n");
1086 bool Layout::operator==(Layout const & rhs) const
1088 // This is enough for the applications we actually make,
1089 // at least at the moment. But we could check more.
1090 return name() == rhs.name()
1091 && latexname() == rhs.latexname()
1092 && latextype == rhs.latextype;