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"
19 #include "TextClass.h"
21 #include "support/debug.h"
22 #include "support/lstrings.h"
32 InsetLayout::InsetLayout() :
33 name_(from_ascii("undefined")), lyxtype_(STANDARD),
34 labelstring_(from_ascii("UNDEFINED")), contentaslabel_(false),
35 decoration_(DEFAULT), latextype_(NOLATEXTYPE), font_(sane_font),
36 labelfont_(sane_font), bgcolor_(Color_error),
37 htmlforcecss_ (false), htmlisblock_(true),
38 multipar_(true), custompars_(true), forceplain_(false),
39 passthru_(false), needprotect_(false), freespacing_(false),
40 keepempty_(false), forceltr_(false), intoc_(false), spellcheck_(true)
42 labelfont_.setColor(Color_error);
48 InsetLayout::InsetDecoration translateDecoration(std::string const & str)
50 if (support::compare_ascii_no_case(str, "classic") == 0)
51 return InsetLayout::CLASSIC;
52 if (support::compare_ascii_no_case(str, "minimalistic") == 0)
53 return InsetLayout::MINIMALISTIC;
54 if (support::compare_ascii_no_case(str, "conglomerate") == 0)
55 return InsetLayout::CONGLOMERATE;
56 return InsetLayout::DEFAULT;
59 InsetLayout::InsetLaTeXType translateLaTeXType(std::string const & str)
61 if (support::compare_ascii_no_case(str, "command") == 0)
62 return InsetLayout::COMMAND;
63 if (support::compare_ascii_no_case(str, "environment") == 0)
64 return InsetLayout::ENVIRONMENT;
65 if (support::compare_ascii_no_case(str, "none") == 0)
66 return InsetLayout::NOLATEXTYPE;
67 return InsetLayout::ILT_ERROR;
73 bool InsetLayout::read(Lexer & lex, TextClass const & tclass)
113 LexerKeyword elementTags[] = {
114 { "bgcolor", IL_BGCOLOR },
115 { "contentaslabel", IL_CONTENTASLABEL },
116 { "copystyle", IL_COPYSTYLE },
117 { "counter", IL_COUNTER},
118 { "custompars", IL_CUSTOMPARS },
119 { "decoration", IL_DECORATION },
122 { "forceltr", IL_FORCELTR },
123 { "forceplain", IL_FORCEPLAIN },
124 { "freespacing", IL_FREESPACING },
125 { "htmlattr", IL_HTMLATTR },
126 { "htmlforcecss", IL_HTMLFORCECSS },
127 { "htmlinnerattr", IL_HTMLINNERATTR},
128 { "htmlinnertag", IL_HTMLINNERTAG},
129 { "htmlisblock", IL_HTMLISBLOCK},
130 { "htmllabel", IL_HTMLLABEL },
131 { "htmlpreamble", IL_HTMLPREAMBLE },
132 { "htmlstyle", IL_HTMLSTYLE },
133 { "htmltag", IL_HTMLTAG },
134 { "intoc", IL_INTOC },
135 { "keepempty", IL_KEEPEMPTY },
136 { "labelfont", IL_LABELFONT },
137 { "labelstring", IL_LABELSTRING },
138 { "latexname", IL_LATEXNAME },
139 { "latexparam", IL_LATEXPARAM },
140 { "latextype", IL_LATEXTYPE },
141 { "lyxtype", IL_LYXTYPE },
142 { "multipar", IL_MULTIPAR },
143 { "needprotect", IL_NEEDPROTECT },
144 { "passthru", IL_PASSTHRU },
145 { "preamble", IL_PREAMBLE },
146 { "requires", IL_REQUIRES },
147 { "spellcheck", IL_SPELLCHECK }
150 lex.pushTable(elementTags);
152 FontInfo font = inherit_font;
153 labelfont_ = inherit_font;
154 bgcolor_ = Color_none;
156 // whether we've read the CustomPars or ForcePlain tag
157 // for issuing a warning in case MultiPars comes later
158 bool readCustomOrPlain = false;
161 while (!getout && lex.isOK()) {
164 case Lexer::LEX_UNDEF:
165 lex.printError("Unknown InsetLayout tag");
172 // Perhaps a more elegant way to deal with the next two would be the
173 // way this sort of thing is handled in Layout::read(), namely, by
178 lyxtype_ = translateLyXType(lt);
179 if (lyxtype_ == NOLYXTYPE)
180 LYXERR0("Unknown LyXType `" << lt << "'.");
181 if (lyxtype_ == CHARSTYLE)
188 latextype_ = translateLaTeXType(lt);
189 if (latextype_ == ILT_ERROR)
190 LYXERR0("Unknown LaTeXType `" << lt << "'.");
198 decoration_ = translateDecoration(tmp);
205 latexparam_ = support::subst(tmp, """, "\"");
208 labelfont_ = lyxRead(lex, inherit_font);
218 // the defaults for these depend upon multipar_
219 if (readCustomOrPlain)
220 LYXERR0("Warning: Read MultiPar after CustomPars or ForcePlain. "
221 "Previous value may be overwritten!");
222 readCustomOrPlain = false;
223 custompars_ = multipar_;
224 forceplain_ = !multipar_;
231 readCustomOrPlain = true;
238 readCustomOrPlain = true;
249 case IL_CONTENTASLABEL:
250 lex >> contentaslabel_;
252 case IL_COPYSTYLE: { // initialize with a known style
255 style = support::subst(style, '_', ' ');
257 // We don't want to apply the algorithm in DocumentClass::insetLayout()
258 // here. So we do it the long way.
259 TextClass::InsetLayouts::const_iterator it =
260 tclass.insetLayouts().find(style);
261 if (it != tclass.insetLayouts().end()) {
262 docstring const tmpname = name_;
263 this->operator=(it->second);
266 LYXERR0("Cannot copy unknown InsetLayout `"
268 << "All InsetLayouts so far:");
269 TextClass::InsetLayouts::const_iterator lit =
270 tclass.insetLayouts().begin();
271 TextClass::InsetLayouts::const_iterator len =
272 tclass.insetLayouts().end();
273 for (; lit != len; ++lit)
274 lyxerr << lit->second.name() << "\n";
280 font_ = lyxRead(lex, inherit_font);
281 // If you want to define labelfont, you need to do so after
288 bgcolor_ = lcolor.getFromLyXName(tmp);
291 preamble_ = from_utf8(lex.getLongString("EndPreamble"));
299 case IL_HTMLFORCECSS:
300 lex >> htmlforcecss_;
302 case IL_HTMLINNERTAG:
303 lex >> htmlinnertag_;
305 case IL_HTMLINNERATTR:
306 lex >> htmlinnerattr_;
315 htmlstyle_ = from_utf8(lex.getLongString("EndHTMLStyle"));
317 case IL_HTMLPREAMBLE:
318 htmlpreamble_ = from_utf8(lex.getLongString("EndPreamble"));
322 vector<string> const req
323 = support::getVectorFromString(lex.getString());
324 requires_.insert(req.begin(), req.end());
336 // Here add element to list if getout == true
340 // The label font is generally used as-is without
341 // any realization against a given context.
342 labelfont_.realize(sane_font);
349 InsetLayout::InsetLyXType translateLyXType(std::string const & str)
352 if (support::compare_ascii_no_case(str, "charstyle") == 0)
353 return InsetLayout::CHARSTYLE;
354 if (support::compare_ascii_no_case(str, "custom") == 0)
355 return InsetLayout::CUSTOM;
356 if (support::compare_ascii_no_case(str, "element") == 0)
357 return InsetLayout::ELEMENT;
358 if (support::compare_ascii_no_case(str, "end") == 0)
359 return InsetLayout::END;
360 if (support::compare_ascii_no_case(str, "standard") == 0)
361 return InsetLayout::STANDARD;
362 return InsetLayout::NOLYXTYPE;
366 string const & InsetLayout::htmltag() const
368 if (htmltag_.empty())
369 htmltag_ = multipar_ ? "div" : "span";
374 string const & InsetLayout::htmlattr() const
376 if (htmlattr_.empty())
377 htmlattr_ = "class=\"" + defaultCSSClass() + "\"";
382 string const & InsetLayout::htmlinnerattr() const
384 if (htmlinnerattr_.empty())
385 htmlinnerattr_ = "class=\"" + defaultCSSClass() + "_inner\"";
386 return htmlinnerattr_;
390 string InsetLayout::defaultCSSClass() const
392 if (!defaultcssclass_.empty())
393 return defaultcssclass_;
395 string n = to_utf8(name());
396 string::const_iterator it = n.begin();
397 string::const_iterator en = n.end();
398 for (; it != en; ++it) {
401 else if (islower(*it))
404 d += support::lowercase(*it);
406 // are there other characters we need to remove?
407 defaultcssclass_ = d;
408 return defaultcssclass_;
412 void InsetLayout::makeDefaultCSS() const
414 if (!htmldefaultstyle_.empty())
416 docstring const mainfontCSS = font_.asCSS();
417 if (!mainfontCSS.empty())
419 from_ascii(htmltag() + "." + defaultCSSClass() + " {\n") +
420 mainfontCSS + from_ascii("\n}\n");
424 docstring InsetLayout::htmlstyle() const
426 if (!htmlstyle_.empty() && !htmlforcecss_)
428 if (htmldefaultstyle_.empty())
430 docstring retval = htmldefaultstyle_;
431 if (!htmlstyle_.empty())
432 retval += '\n' + htmlstyle_ + '\n';