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
8 * \author Angus Leeming
10 * Full author contact details are available in file CREDITS.
18 #include "support/debug.h"
19 #include "support/lstrings.h"
20 #include "support/textutils.h"
28 Floating::Floating(string const & type, string const & placement,
29 string const & ext, string const & within,
30 string const & style, string const & name,
31 string const & listName, std::string const & listCmd,
32 string const & refPrefix, std::string const & allowedplacement,
33 string const & htmlTag, string const & htmlAttrib,
34 docstring const & htmlStyle, std::string const & docbookTag,
35 string const & docbookAttr, string const & docbookTagType,
36 std::string const & docbookFloatType, std::string const & docbookCaption,
37 string const & required, bool usesfloat, bool ispredefined,
38 bool allowswide, bool allowssideways)
39 : floattype_(type), placement_(placement), ext_(ext), within_(within),
40 style_(style), name_(name), listname_(listName), listcommand_(listCmd),
41 refprefix_(refPrefix), allowedplacement_(allowedplacement), required_(required),
42 usesfloatpkg_(usesfloat), ispredefined_(ispredefined),
43 allowswide_(allowswide), allowssideways_(allowssideways),
44 html_tag_(htmlTag), html_attrib_(htmlAttrib), html_style_(htmlStyle),
45 docbook_caption_(docbookCaption), docbook_tag_(docbookTag),
46 docbook_tag_type_(docbookTagType)
48 // Implement some edge cases for DocBook. Both docbook_float_type_ and docbook_attr_ must be computed
49 // based on the given value of docbookFloatType; docbook_tag_ can still be guessed without correlation.
51 // Determine the value of docbook_float_type_.
53 // These are the allowed values for docbook_float_type_. Both docbook_attr_ and docbook_tag_type_
54 // depend on this list.
55 static std::set<std::string> allowedFloatTypes{"figure", "table", "algorithm", "video", "example"};
57 // If some type is predetermined in the layout, use it.
58 if (!docbookFloatType.empty() && allowedFloatTypes.find(docbookFloatType) != allowedFloatTypes.end())
59 docbook_float_type_ = docbookFloatType;
60 // Otherwise, try to guess the type.
61 else if (floattype_ == "figure" || floattype_ == "graph" ||
62 floattype_ == "chart" || floattype_ == "scheme") {
63 docbook_float_type_ = "figure";
64 } else if (floattype_ == "table" || floattype_ == "tableau") {
65 docbook_float_type_ = "table";
66 } else if (floattype_ == "algorithm") {
67 docbook_float_type_ = "algorithm";
68 } else if (floattype_ == "video") {
69 docbook_float_type_ = "video";
71 // If nothing matches, return something that will not be valid.
72 LYXERR0("Unrecognised float type: " + floattype_);
73 docbook_float_type_ = "unknown";
77 // Determine the value of docbook_attr_.
79 std::set<std::string> achemso = {"chart", "graph", "scheme"};
80 bool hasType = docbook_attr_.find("type=") != std::string::npos;
82 // For algorithms, a type attribute must be mentioned, if not already present in docbook_attr_.
83 if (docbook_float_type_ == "algorithm" && !hasType)
84 docbook_attr_ += " type='algorithm'";
85 // Specific floats for achemso.
86 else if (docbook_float_type_ == "figure" && achemso.find(floattype_) != achemso.end())
87 docbook_attr_ += " type='" + floattype_ + "'";
89 // Finally, merge in the attributes given in argument.
90 if (!docbookAttr.empty())
91 docbook_attr_ += " " + docbookAttr;
96 string const & Floating::htmlAttrib() const
98 if (html_attrib_.empty())
99 html_attrib_ = "class='" + defaultCSSClass() + "'";
104 string const & Floating::htmlTag() const
106 if (html_tag_.empty())
112 string Floating::defaultCSSClass() const
114 if (!defaultcssclass_.empty())
115 return defaultcssclass_;
117 string n = floattype_;
118 string::const_iterator it = n.begin();
119 string::const_iterator en = n.end();
120 for (; it != en; ++it) {
121 if (!isAlphaASCII(*it))
123 else if (isLower(*it))
126 d += support::lowercase(*it);
128 // are there other characters we need to remove?
129 defaultcssclass_ = "float-" + d;
130 return defaultcssclass_;
134 std::string Floating::docbookFloatType() const
136 // All the work is done in the constructor.
137 return docbook_float_type_;
141 string Floating::docbookAttr() const
143 return docbook_attr_;
147 string Floating::docbookTag(bool hasTitle) const
149 // If there is a preconfigured tag, use it.
150 if (!docbook_tag_.empty())
153 // Otherwise, guess it.
154 if (docbookFloatType() == "figure" || docbookFloatType() == "algorithm" || docbookFloatType() == "video") {
155 return hasTitle ? "figure" : "informalfigure";
156 } else if (docbookFloatType() == "example") {
157 return hasTitle ? "example" : "informalexample";
158 } else if (docbookFloatType() == "table") {
159 return hasTitle ? "table" : "informaltable";
161 // If nothing matches, return something that will not be valid.
162 LYXERR0("Unrecognised float type: " + floattype());
168 string const & Floating::docbookTagType() const
170 if (docbook_tag_type_ != "block" && docbook_tag_type_ != "paragraph" && docbook_tag_type_ != "inline")
171 docbook_tag_type_ = "block";
172 return docbook_tag_type_;
176 string const & Floating::docbookCaption() const
178 if (!docbook_caption_.empty())
179 return docbook_caption_;
181 if (docbook_float_type_ == "figure" || docbook_float_type_ == "video" ||
182 docbook_float_type_ == "algorithm" || docbook_float_type_ == "example")
183 docbook_caption_ = "title";
184 else if (floattype_ == "table" || floattype_ == "tableau")
185 docbook_caption_ = "caption";
186 return docbook_caption_;