3 * \file InsetLayout.cpp
4 * This file is part of LyX, the document processor.
5 * Licence details can be found in the file COPYING.
7 * \author Martin Vermeer
10 * Full author contact details are available in file CREDITS.
15 #include "InsetLayout.h"
20 #include "TextClass.h"
22 #include "support/debug.h"
23 #include "support/lstrings.h"
33 InsetLayout::InsetLayout() :
34 name_(from_ascii("undefined")), lyxtype_(STANDARD),
35 labelstring_(from_ascii("UNDEFINED")), decoration_(DEFAULT),
36 latextype_(NOLATEXTYPE), font_(sane_font),
37 labelfont_(sane_font), bgcolor_(Color_error),
38 htmlforcecss_ (false), htmlisblock_(true),
39 multipar_(false), custompars_(true), forceplain_(false),
40 passthru_(false), needprotect_(false), freespacing_(false),
41 keepempty_(false), forceltr_(false), intoc_(false)
43 labelfont_.setColor(Color_error);
49 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
51 if (support::compare_ascii_no_case(str, "classic") == 0)
52 return InsetLayout::CLASSIC;
53 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
54 return InsetLayout::MINIMALISTIC;
55 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
56 return InsetLayout::CONGLOMERATE;
57 return InsetLayout::DEFAULT;
60 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
62 if (support::compare_ascii_no_case(str, "command") == 0)
63 return InsetLayout::COMMAND;
64 if (support::compare_ascii_no_case(str, "environment") == 0)
65 return InsetLayout::ENVIRONMENT;
66 if (support::compare_ascii_no_case(str, "none") == 0)
67 return InsetLayout::NOLATEXTYPE;
68 return InsetLayout::ILT_ERROR;
74 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
112 LexerKeyword elementTags[] = {
113 { "bgcolor", IL_BGCOLOR },
114 { "copystyle", IL_COPYSTYLE },
115 { "counter", IL_COUNTER},
116 { "custompars", IL_CUSTOMPARS },
117 { "decoration", IL_DECORATION },
120 { "forceltr", IL_FORCELTR },
121 { "forceplain", IL_FORCEPLAIN },
122 { "freespacing", IL_FREESPACING },
123 { "htmlattr", IL_HTMLATTR },
124 { "htmlforcecss", IL_HTMLFORCECSS },
125 { "htmlinnerattr", IL_HTMLINNERATTR},
126 { "htmlinnertag", IL_HTMLINNERTAG},
127 { "htmlisblock", IL_HTMLISBLOCK},
128 { "htmllabel", IL_HTMLLABEL },
129 { "htmlpreamble", IL_HTMLPREAMBLE },
130 { "htmlstyle", IL_HTMLSTYLE },
131 { "htmltag", IL_HTMLTAG },
132 { "intoc", IL_INTOC },
133 { "keepempty", IL_KEEPEMPTY },
134 { "labelfont", IL_LABELFONT },
135 { "labelstring", IL_LABELSTRING },
136 { "latexname", IL_LATEXNAME },
137 { "latexparam", IL_LATEXPARAM },
138 { "latextype", IL_LATEXTYPE },
139 { "lyxtype", IL_LYXTYPE },
140 { "multipar", IL_MULTIPAR },
141 { "needprotect", IL_NEEDPROTECT },
142 { "passthru", IL_PASSTHRU },
143 { "preamble", IL_PREAMBLE },
144 { "requires", IL_REQUIRES }
147 lex.pushTable(elementTags);
149 FontInfo font = inherit_font;
150 labelfont_ = inherit_font;
151 bgcolor_ = Color_none;
153 // whether we've read the CustomPars or ForcePlain tag
154 // for issuing a warning in case MultiPars comes later
155 bool readCustomOrPlain = false;
158 while (!getout && lex.isOK()) {
161 case Lexer::LEX_UNDEF:
162 lex.printError("Unknown InsetLayout tag");
169 // Perhaps a more elegant way to deal with the next two would be the
170 // way this sort of thing is handled in Layout::read(), namely, by
175 lyxtype_ = translateLyXType(lt);
176 if (lyxtype_ == NOLYXTYPE)
177 LYXERR0("Unknown LyXType `" << lt << "'.");
183 latextype_ = translateLaTeXType(lt);
184 if (latextype_ == ILT_ERROR)
185 LYXERR0("Unknown LaTeXType `" << lt << "'.");
193 decoration_ = translateDecoration(tmp);
200 latexparam_ = support::subst(tmp, """, "\"");
203 labelfont_ = lyxRead(lex, inherit_font);
213 // the defaults for these depend upon multipar_
214 if (readCustomOrPlain)
215 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
216 "Previous value may be overwritten!");
217 readCustomOrPlain = false;
218 custompars_ = multipar_;
219 forceplain_ = !multipar_;
226 readCustomOrPlain = true;
233 readCustomOrPlain = true;
244 case IL_COPYSTYLE: { // initialize with a known style
247 style = support::subst(style, '_', ' ');
249 // We don't want to apply the algorithm in DocumentClass::insetLayout()
250 // here. So we do it the long way.
251 TextClass::InsetLayouts::const_iterator it =
252 tclass.insetLayouts().find(style);
253 if (it != tclass.insetLayouts().end()) {
254 docstring const tmpname = name_;
255 this->operator=(it->second);
258 LYXERR0("Cannot copy unknown InsetLayout `"
260 << "All InsetLayouts so far:");
261 TextClass::InsetLayouts::const_iterator lit =
262 tclass.insetLayouts().begin();
263 TextClass::InsetLayouts::const_iterator len =
264 tclass.insetLayouts().end();
265 for (; lit != len; ++lit)
266 lyxerr << lit->second.name() << "\n";
272 font_ = lyxRead(lex, inherit_font);
273 // If you want to define labelfont, you need to do so after
280 bgcolor_ = lcolor.getFromLyXName(tmp);
283 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
291 case IL_HTMLFORCECSS:
292 lex >> htmlforcecss_;
294 case IL_HTMLINNERTAG:
295 lex >> htmlinnertag_;
297 case IL_HTMLINNERATTR:
298 lex >> htmlinnerattr_;
307 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
309 case IL_HTMLPREAMBLE:
310 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
314 vector<string> const req
315 = support::getVectorFromString(lex.getString());
316 requires_.insert(req.begin(), req.end());
325 // Here add element to list if getout == true
329 // The label font is generally used as-is without
330 // any realization against a given context.
331 labelfont_.realize(sane_font);
338 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
341 if (support::compare_ascii_no_case(str, "charstyle") == 0)
342 return InsetLayout::CHARSTYLE;
343 if (support::compare_ascii_no_case(str, "custom") == 0)
344 return InsetLayout::CUSTOM;
345 if (support::compare_ascii_no_case(str, "element") == 0)
346 return InsetLayout::ELEMENT;
347 if (support::compare_ascii_no_case(str, "end") == 0)
348 return InsetLayout::END;
349 if (support::compare_ascii_no_case(str, "standard") == 0)
350 return InsetLayout::STANDARD;
351 return InsetLayout::NOLYXTYPE;
355 string InsetLayout::defaultCSSClass() const
357 if (!defaultcssclass_.empty())
358 return defaultcssclass_;
360 docstring::const_iterator it = name().begin();
361 docstring::const_iterator en = name().end();
362 for (; it != en; ++it) {
368 d += support::lowercase(*it);
370 // are there other characters we need to remove?
371 defaultcssclass_ = to_utf8(d);
372 return defaultcssclass_;
376 void InsetLayout::makeDefaultCSS() const
379 // tex2lyx does not have FontInfo::asCSS()
382 if (!htmldefaultstyle_.empty())
384 if (!htmltag_.empty()) {
385 docstring const mainfontCSS = font_.asCSS();
386 if (!mainfontCSS.empty())
388 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
389 mainfontCSS + from_ascii("\n}\n");
392 At present, we do not have default tags, etc, for the label.
393 if (labelfont_ == font_)
395 docstring const labelfontCSS = labelfont_.asCSS();
396 if (!labelfontCSS.empty())
398 . from_ascii(htmllabeltag() + "." + defaultCSSLabelClass() + " {\n") +
399 labelfontCSS + from_ascii("\n}\n");
404 docstring InsetLayout::htmlstyle() const
406 if (!htmlstyle_.empty() && !htmlforcecss_)
408 if (htmldefaultstyle_.empty())
410 docstring retval = htmldefaultstyle_;
411 if (!htmlstyle_.empty())
412 retval += '\n' + htmlstyle_;