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,
60 LT_PARBREAK_IS_NEWLINE,
70 LT_LABELSTRING_APPENDIX,
113 LT_INTITLE // keep this last!
116 /////////////////////
121 margintype = MARGIN_STATIC;
122 latextype = LATEX_PARAGRAPH;
128 labelfont = inherit_font;
130 reslabelfont = sane_font;
131 nextnoindent = false;
136 labelbottomsep = 0.0;
138 align = LYX_ALIGN_BLOCK;
139 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
140 labeltype = LABEL_NO_LABEL;
141 endlabeltype = END_LABEL_NO_LABEL;
142 // Should or should not. That is the question.
143 // spacing.set(Spacing::OneHalf);
146 newline_allowed = true;
147 free_spacing = false;
149 parbreak_is_newline = false;
150 toclevel = NOT_IN_TOC;
152 htmllabelfirst_ = false;
153 htmlforcecss_ = false;
161 bool Layout::read(Lexer & lex, TextClass const & tclass)
163 // This table is sorted alphabetically [asierra 30March96]
164 LexerKeyword layoutTags[] = {
165 { "align", LT_ALIGN },
166 { "alignpossible", LT_ALIGNPOSSIBLE },
167 { "babelpreamble", LT_BABELPREAMBLE },
168 { "bottomsep", LT_BOTTOMSEP },
169 { "category", LT_CATEGORY },
170 { "commanddepth", LT_COMMANDDEPTH },
171 { "copystyle", LT_COPYSTYLE },
172 { "dependson", LT_DEPENDSON },
174 { "endlabelstring", LT_ENDLABELSTRING },
175 { "endlabeltype", LT_ENDLABELTYPE },
176 { "fill_bottom", LT_FILL_BOTTOM },
177 { "fill_top", LT_FILL_TOP },
179 { "freespacing", LT_FREE_SPACING },
180 { "htmlattr", LT_HTMLATTR },
181 { "htmlforcecss", LT_HTMLFORCECSS },
182 { "htmlitem", LT_HTMLITEM },
183 { "htmlitemattr", LT_HTMLITEMATTR },
184 { "htmllabel", LT_HTMLLABEL },
185 { "htmllabelattr", LT_HTMLLABELATTR },
186 { "htmllabelfirst", LT_HTMLLABELFIRST },
187 { "htmlpreamble", LT_HTMLPREAMBLE },
188 { "htmlstyle", LT_HTMLSTYLE },
189 { "htmltag", LT_HTMLTAG },
190 { "htmltitle", LT_HTMLTITLE },
191 { "innertag", LT_INNERTAG },
192 { "inpreamble", LT_INPREAMBLE },
193 { "intitle", LT_INTITLE },
194 { "itemsep", LT_ITEMSEP },
195 { "itemtag", LT_ITEMTAG },
196 { "keepempty", LT_KEEPEMPTY },
197 { "labelbottomsep", LT_LABEL_BOTTOMSEP },
198 { "labelcounter", LT_LABELCOUNTER },
199 { "labelfont", LT_LABELFONT },
200 { "labelindent", LT_LABELINDENT },
201 { "labelsep", LT_LABELSEP },
202 { "labelstring", LT_LABELSTRING },
203 { "labelstringappendix", LT_LABELSTRING_APPENDIX },
204 { "labeltag", LT_LABELTAG },
205 { "labeltype", LT_LABELTYPE },
206 { "langpreamble", LT_LANGPREAMBLE },
207 { "latexname", LT_LATEXNAME },
208 { "latexparam", LT_LATEXPARAM },
209 { "latextype", LT_LATEXTYPE },
210 { "leftmargin", LT_LEFTMARGIN },
211 { "margin", LT_MARGIN },
212 { "needprotect", LT_NEED_PROTECT },
213 { "newline", LT_NEWLINE },
214 { "nextnoindent", LT_NEXTNOINDENT },
215 { "obsoletedby", LT_OBSOLETEDBY },
216 { "optionalargs", LT_OPTARGS },
217 { "parbreakisnewline", LT_PARBREAK_IS_NEWLINE },
218 { "parindent", LT_PARINDENT },
219 { "parsep", LT_PARSEP },
220 { "parskip", LT_PARSKIP },
221 { "passthru", LT_PASS_THRU },
222 { "preamble", LT_PREAMBLE },
223 { "refprefix", LT_REFPREFIX },
224 { "requiredargs", LT_REQARGS },
225 { "requires", LT_REQUIRES },
226 { "rightmargin", LT_RIGHTMARGIN },
227 { "spacing", LT_SPACING },
228 { "spellcheck", LT_SPELLCHECK },
229 { "textfont", LT_TEXTFONT },
230 { "toclevel", LT_TOCLEVEL },
231 { "topsep", LT_TOPSEP }
235 bool finished = false;
236 lex.pushTable(layoutTags);
237 // parse style section
238 while (!finished && lex.isOK() && !error) {
240 // See comment in LyXRC.cpp.
242 case Lexer::LEX_FEOF:
245 case Lexer::LEX_UNDEF: // parse error
246 lex.printError("Unknown layout tag `$$Token'");
253 switch (static_cast<LayoutTags>(le)) {
254 case LT_END: // end of structure
262 case LT_COPYSTYLE: { // initialize with a known style
265 style = subst(style, '_', ' ');
267 if (tclass.hasLayout(style)) {
268 docstring const tmpname = name_;
269 this->operator=(tclass[style]);
272 LYXERR0("Cannot copy unknown style `"
274 << "All layouts so far:");
275 DocumentClass::const_iterator lit = tclass.begin();
276 DocumentClass::const_iterator len = tclass.end();
277 for (; lit != len; ++lit)
278 LYXERR0(lit->name());
283 case LT_OBSOLETEDBY: { // replace with a known style
286 style = subst(style, '_', ' ');
288 if (tclass.hasLayout(style)) {
289 docstring const tmpname = name_;
290 this->operator=(tclass[style]);
292 if (obsoleted_by().empty())
293 obsoleted_by_ = style;
295 LYXERR0("Cannot replace with unknown style `"
298 //lex.printError("Cannot replace with"
307 depends_on_ = subst(depends_on_, '_', ' ');
310 case LT_MARGIN: // margin style definition.
314 case LT_LATEXTYPE: // LaTeX style definition.
338 case LT_NEED_PROTECT:
347 font = lyxRead(lex, font);
352 font = lyxRead(lex, font);
356 labelfont = lyxRead(lex, labelfont);
359 case LT_NEXTNOINDENT: // Indent next paragraph?
363 case LT_COMMANDDEPTH:
373 latexparam_ = subst(latexparam_, """, "\"");
389 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
392 case LT_LANGPREAMBLE:
393 langpreamble_ = from_utf8(lex.getLongString("EndLangPreamble"));
396 case LT_BABELPREAMBLE:
397 babelpreamble_ = from_utf8(lex.getLongString("EndBabelPreamble"));
404 case LT_ENDLABELTYPE:
405 readEndLabelType(lex);
408 case LT_LEFTMARGIN: // left margin type
412 case LT_RIGHTMARGIN: // right margin type
416 case LT_LABELINDENT: // label indenting flag
420 case LT_PARINDENT: // paragraph indent. flag
424 case LT_PARSKIP: // paragraph skip size
428 case LT_ITEMSEP: // item separation size
432 case LT_TOPSEP: // top separation size
436 case LT_BOTTOMSEP: // bottom separation size
440 case LT_LABEL_BOTTOMSEP: // label bottom separation size
441 lex >> labelbottomsep;
444 case LT_LABELSEP: // label separator
446 labelsep = subst(labelsep, 'x', ' ');
449 case LT_PARSEP: // par. separation size
453 case LT_FILL_TOP: // fill top flag
457 case LT_FILL_BOTTOM: // fill bottom flag
461 case LT_NEWLINE: // newlines allowed?
462 lex >> newline_allowed;
465 case LT_ALIGN: // paragraph align
468 case LT_ALIGNPOSSIBLE: // paragraph allowed align
469 readAlignPossible(lex);
472 case LT_LABELSTRING: // label string definition
473 // FIXME: this means LT_ENDLABELSTRING may only
474 // occur after LT_LABELSTRING
476 labelstring_ = trim(labelstring_);
477 labelstring_appendix_ = labelstring_;
480 case LT_ENDLABELSTRING: // endlabel string definition
481 lex >> endlabelstring_;
482 endlabelstring_ = trim(endlabelstring_);
485 case LT_LABELSTRING_APPENDIX: // label string appendix definition
486 lex >> labelstring_appendix_;
487 labelstring_appendix_ = trim(labelstring_appendix_);
490 case LT_LABELCOUNTER: // name of counter to use
492 counter = trim(counter);
495 case LT_FREE_SPACING: // Allow for free spacing.
499 case LT_PASS_THRU: // Allow for pass thru.
503 case LT_PARBREAK_IS_NEWLINE:
504 lex >> parbreak_is_newline;
507 case LT_SPACING: // setspace.sty
513 vector<string> const req =
514 getVectorFromString(lex.getString());
515 requires_.insert(req.begin(), req.end());
541 case LT_HTMLITEMATTR:
542 lex >> htmlitemattr_;
546 lex >> htmllabeltag_;
549 case LT_HTMLLABELATTR:
550 lex >> htmllabelattr_;
553 case LT_HTMLLABELFIRST:
554 lex >> htmllabelfirst_;
558 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
561 case LT_HTMLFORCECSS:
562 lex >> htmlforcecss_;
565 case LT_HTMLPREAMBLE:
566 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
579 // make sure we only have inpreamble = true for commands
580 if (inpreamble && latextype != LATEX_COMMAND && latextype != LATEX_PARAGRAPH) {
581 LYXERR0("InPreamble not permitted except with command and paragraph layouts.");
582 LYXERR0("Layout name: " << name());
586 return finished && !error;
599 LexerKeyword alignTags[] = {
600 { "block", AT_BLOCK },
601 { "center", AT_CENTER },
602 { "layout", AT_LAYOUT },
604 { "right", AT_RIGHT }
608 void Layout::readAlign(Lexer & lex)
610 PushPopHelper pph(lex, alignTags);
613 case Lexer::LEX_UNDEF:
614 lex.printError("Unknown alignment `$$Token'");
620 align = LYX_ALIGN_BLOCK;
623 align = LYX_ALIGN_LEFT;
626 align = LYX_ALIGN_RIGHT;
629 align = LYX_ALIGN_CENTER;
632 align = LYX_ALIGN_LAYOUT;
638 void Layout::readAlignPossible(Lexer & lex)
640 lex.pushTable(alignTags);
641 alignpossible = LYX_ALIGN_NONE | LYX_ALIGN_LAYOUT;
642 int lineno = lex.lineNumber();
646 case Lexer::LEX_UNDEF:
647 lex.printError("Unknown alignment `$$Token'");
653 alignpossible |= LYX_ALIGN_BLOCK;
656 alignpossible |= LYX_ALIGN_LEFT;
659 alignpossible |= LYX_ALIGN_RIGHT;
662 alignpossible |= LYX_ALIGN_CENTER;
665 alignpossible |= LYX_ALIGN_LAYOUT;
668 } while (lineno == lex.lineNumber());
673 void Layout::readLabelType(Lexer & lex)
679 LA_CENTERED_TOP_ENVIRONMENT,
689 LexerKeyword labelTypeTags[] = {
690 { "bibliography", LA_BIBLIO },
691 { "centered_top_environment", LA_CENTERED_TOP_ENVIRONMENT },
692 { "counter", LA_COUNTER },
693 { "enumerate", LA_ENUMERATE },
694 { "itemize", LA_ITEMIZE },
695 { "manual", LA_MANUAL },
696 { "no_label", LA_NO_LABEL },
697 { "sensitive", LA_SENSITIVE },
698 { "static", LA_STATIC },
699 { "top_environment", LA_TOP_ENVIRONMENT }
702 PushPopHelper pph(lex, labelTypeTags);
705 case Lexer::LEX_UNDEF:
706 lex.printError("Unknown labeltype tag `$$Token'");
712 labeltype = LABEL_NO_LABEL;
715 labeltype = LABEL_MANUAL;
717 case LA_TOP_ENVIRONMENT:
718 labeltype = LABEL_TOP_ENVIRONMENT;
720 case LA_CENTERED_TOP_ENVIRONMENT:
721 labeltype = LABEL_CENTERED_TOP_ENVIRONMENT;
724 labeltype = LABEL_STATIC;
727 labeltype = LABEL_SENSITIVE;
730 labeltype = LABEL_COUNTER;
733 labeltype = LABEL_ENUMERATE;
736 labeltype = LABEL_ITEMIZE;
739 labeltype = LABEL_BIBLIO;
745 void Layout::readEndLabelType(Lexer & lex)
747 static LexerKeyword endlabelTypeTags[] = {
748 { "box", END_LABEL_BOX },
749 { "filled_box", END_LABEL_FILLED_BOX },
750 { "no_label", END_LABEL_NO_LABEL },
751 { "static", END_LABEL_STATIC }
754 PushPopHelper pph(lex, endlabelTypeTags);
757 case Lexer::LEX_UNDEF:
758 lex.printError("Unknown labeltype tag `$$Token'");
760 case END_LABEL_STATIC:
762 case END_LABEL_FILLED_BOX:
763 case END_LABEL_NO_LABEL:
764 endlabeltype = static_cast<EndLabelType>(le);
767 LYXERR0("Unhandled value " << le);
773 void Layout::readMargin(Lexer & lex)
775 LexerKeyword marginTags[] = {
776 { "dynamic", MARGIN_DYNAMIC },
777 { "first_dynamic", MARGIN_FIRST_DYNAMIC },
778 { "manual", MARGIN_MANUAL },
779 { "right_address_box", MARGIN_RIGHT_ADDRESS_BOX },
780 { "static", MARGIN_STATIC }
783 PushPopHelper pph(lex, marginTags);
787 case Lexer::LEX_UNDEF:
788 lex.printError("Unknown margin type tag `$$Token'");
793 case MARGIN_FIRST_DYNAMIC:
794 case MARGIN_RIGHT_ADDRESS_BOX:
795 margintype = static_cast<MarginType>(le);
798 LYXERR0("Unhandled value " << le);
804 void Layout::readLatexType(Lexer & lex)
806 LexerKeyword latexTypeTags[] = {
807 { "bib_environment", LATEX_BIB_ENVIRONMENT },
808 { "command", LATEX_COMMAND },
809 { "environment", LATEX_ENVIRONMENT },
810 { "item_environment", LATEX_ITEM_ENVIRONMENT },
811 { "list_environment", LATEX_LIST_ENVIRONMENT },
812 { "paragraph", LATEX_PARAGRAPH }
815 PushPopHelper pph(lex, latexTypeTags);
818 case Lexer::LEX_UNDEF:
819 lex.printError("Unknown latextype tag `$$Token'");
821 case LATEX_PARAGRAPH:
823 case LATEX_ENVIRONMENT:
824 case LATEX_ITEM_ENVIRONMENT:
825 case LATEX_BIB_ENVIRONMENT:
826 case LATEX_LIST_ENVIRONMENT:
827 latextype = static_cast<LatexType>(le);
830 LYXERR0("Unhandled value " << le);
836 void Layout::readSpacing(Lexer & lex)
839 ST_SPACING_SINGLE = 1,
845 LexerKeyword spacingTags[] = {
846 {"double", ST_SPACING_DOUBLE },
847 {"onehalf", ST_SPACING_ONEHALF },
848 {"other", ST_OTHER },
849 {"single", ST_SPACING_SINGLE }
852 PushPopHelper pph(lex, spacingTags);
855 case Lexer::LEX_UNDEF:
856 lex.printError("Unknown spacing token `$$Token'");
861 case ST_SPACING_SINGLE:
862 spacing.set(Spacing::Single);
864 case ST_SPACING_ONEHALF:
865 spacing.set(Spacing::Onehalf);
867 case ST_SPACING_DOUBLE:
868 spacing.set(Spacing::Double);
872 spacing.set(Spacing::Other, lex.getString());
880 docstring const i18npreamble(Language const * lang, docstring const & templ)
885 string preamble = subst(to_utf8(templ), "$$lang", lang->babel());
888 // tex2lyx does not have getMessages()
889 LASSERT(false, /**/);
892 // lyx::regex is not unicode-safe.
893 // Should use QRegExp or (boost::u32regex, but that requires ICU)
894 static regex const reg("_\\(([^\\)]+)\\)");
896 while (regex_search(preamble, sub, reg)) {
897 string const key = sub.str(1);
900 translated = to_utf8(getMessages(lang->code()).get(key));
902 lyxerr << "Warning: not translating `" << key
903 << "' because it is not pure ASCII." << endl;
906 preamble = subst(preamble, sub.str(), translated);
909 return from_utf8(preamble);
915 docstring const Layout::langpreamble(Language const * lang) const
917 return i18npreamble(lang, langpreamble_);
921 docstring const Layout::babelpreamble(Language const * lang) const
923 return i18npreamble(lang, babelpreamble_);
927 string const & Layout::htmltag() const
929 if (htmltag_.empty())
935 string const & Layout::htmlattr() const
937 if (htmlattr_.empty())
938 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
943 string const & Layout::htmlitemtag() const
945 if (htmlitemtag_.empty())
946 htmlitemtag_ = "div";
951 string const & Layout::htmlitemattr() const
953 if (htmlitemattr_.empty())
954 htmlitemattr_ = "class=\"" + defaultCSSItemClass() + "\"";
955 return htmlitemattr_;
959 string const & Layout::htmllabeltag() const
961 if (htmllabeltag_.empty()) {
962 if (labeltype != LABEL_TOP_ENVIRONMENT &&
963 labeltype != LABEL_CENTERED_TOP_ENVIRONMENT)
964 htmllabeltag_ = "span";
966 htmllabeltag_ = "div";
968 return htmllabeltag_;
972 string const & Layout::htmllabelattr() const
974 if (htmllabelattr_.empty())
975 htmllabelattr_ = "class=\"" + defaultCSSLabelClass() + "\"";
976 return htmllabelattr_;
980 docstring Layout::htmlstyle() const {
981 if (!htmlstyle_.empty() && !htmlforcecss_)
983 if (htmldefaultstyle_.empty())
985 docstring retval = htmldefaultstyle_;
986 if (!htmlstyle_.empty())
987 retval += '\n' + htmlstyle_;
992 string Layout::defaultCSSClass() const
994 if (!defaultcssclass_.empty())
995 return defaultcssclass_;
997 docstring::const_iterator it = name().begin();
998 docstring::const_iterator en = name().end();
999 for (; it != en; ++it) {
1000 char_type const c = *it;
1001 if (!isAlphaASCII(c)) {
1003 // make sure we don't start with an underscore,
1004 // as that sometimes causes problems.
1005 d = from_ascii("lyx_");
1008 } else if (islower(c))
1011 // this is slow, so do it only if necessary
1014 defaultcssclass_ = to_utf8(d);
1015 return defaultcssclass_;
1020 string makeMarginValue(char const * side, double d) {
1022 os << "margin-" << side << ": " << d << "ex;\n";
1028 void Layout::makeDefaultCSS() const {
1029 // this never needs to be redone, since reloading layouts will
1030 // wipe out what we did before.
1031 if (!htmldefaultstyle_.empty())
1035 htmldefaultstyle_ = font.asCSS();
1040 tmp += makeMarginValue("top", topsep);
1042 tmp += makeMarginValue("bottom", bottomsep);
1043 if (!leftmargin.empty()) {
1044 // we can't really do what LyX does with the margin, so
1045 // we'll just figure out how many characters it is
1046 int const len = leftmargin.length();
1047 tmp += makeMarginValue("left", len);
1049 if (!rightmargin.empty()) {
1050 int const len = rightmargin.length();
1051 tmp += makeMarginValue("right", len);
1055 if (!htmldefaultstyle_.empty())
1056 htmldefaultstyle_ += from_ascii("\n");
1057 htmldefaultstyle_ += from_ascii(tmp);
1060 // tex2lyx does not see output_xhtml.cpp
1063 string where = alignmentToCSS(align);
1064 if (!where.empty()) {
1065 htmldefaultstyle_ += from_ascii("text-align: " + where + ";\n");
1069 // wrap up what we have, if anything
1070 if (!htmldefaultstyle_.empty())
1072 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
1073 htmldefaultstyle_ + from_ascii("\n}\n");
1075 if (labeltype == LABEL_NO_LABEL || htmllabeltag() == "NONE")
1081 if (labelfont != font)
1082 labelCSS = labelfont.asCSS() + from_ascii("\n");
1083 if (labeltype == LABEL_CENTERED_TOP_ENVIRONMENT)
1084 labelCSS += from_ascii("text-align: center;\n");
1086 if (!labelCSS.empty())
1087 htmldefaultstyle_ +=
1088 from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
1089 labelCSS + from_ascii("\n}\n");
1093 bool Layout::operator==(Layout const & rhs) const
1095 // This is enough for the applications we actually make,
1096 // at least at the moment. But we could check more.
1097 return name() == rhs.name()
1098 && latexname() == rhs.latexname()
1099 && latextype == rhs.latextype;