]> git.lyx.org Git - features.git/blob - src/Floating.cpp
DocBook: add inner tags for layouts.
[features.git] / src / Floating.cpp
1 /**
2  * \file Floating.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Jean-Marc Lasgouttes
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14 #include <set>
15
16 #include "Floating.h"
17
18 #include "support/debug.h"
19 #include "support/lstrings.h"
20 #include "support/textutils.h"
21
22 using namespace std;
23
24
25 namespace lyx {
26
27
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_tag_(docbookTag), docbook_tag_type_(docbookTagType),
46           docbook_caption_(docbookCaption)
47 {
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.
50
51         // Determine the value of docbook_float_type_.
52         {
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"};
56
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";
70                 } else {
71                         // If nothing matches, return something that will not be valid.
72                         LYXERR0("Unrecognised float type: " + floattype_);
73                         docbook_float_type_ = "unknown";
74                 }
75         }
76
77         // Determine the value of docbook_attr_.
78         {
79                 std::set<std::string> achemso = {"chart", "graph", "scheme"};
80                 bool hasType = docbook_attr_.find("type=") != std::string::npos;
81
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_ + "'";
88         }
89 }
90
91
92 string const & Floating::htmlAttrib() const
93 {
94         if (html_attrib_.empty())
95                 html_attrib_ = "class='" + defaultCSSClass() + "'";
96         return html_attrib_;
97 }
98
99
100 string const & Floating::htmlTag() const
101 {
102         if (html_tag_.empty())
103                 html_tag_ = "div";
104         return html_tag_;
105 }
106
107
108 string Floating::defaultCSSClass() const
109 {
110         if (!defaultcssclass_.empty())
111                 return defaultcssclass_;
112         string d;
113         string n = floattype_;
114         string::const_iterator it = n.begin();
115         string::const_iterator en = n.end();
116         for (; it != en; ++it) {
117                 if (!isAlphaASCII(*it))
118                         d += "_";
119                 else if (isLower(*it))
120                         d += *it;
121                 else
122                         d += support::lowercase(*it);
123         }
124         // are there other characters we need to remove?
125         defaultcssclass_ = "float-" + d;
126         return defaultcssclass_;
127 }
128
129
130 std::string Floating::docbookFloatType() const
131 {
132         // All the work is done in the constructor.
133         return docbook_float_type_;
134 }
135
136
137 string Floating::docbookAttr() const
138 {
139         return docbook_attr_;
140 }
141
142
143 string Floating::docbookTag(bool hasTitle) const
144 {
145         // If there is a preconfigured tag, use it.
146         if (!docbook_tag_.empty())
147                 return docbook_tag_;
148
149         // Otherwise, guess it.
150         if (docbookFloatType() == "figure" || docbookFloatType() == "algorithm" || docbookFloatType() == "video") {
151                 return hasTitle ? "figure" : "informalfigure";
152         } else if (docbookFloatType() == "example") {
153                 return hasTitle ? "example" : "informalexample";
154         } else if (docbookFloatType() == "table") {
155                 return hasTitle ? "table" : "informaltable";
156         } else {
157                 // If nothing matches, return something that will not be valid.
158                 LYXERR0("Unrecognised float type: " + floattype());
159                 return "float";
160         }
161 }
162
163
164 string const & Floating::docbookTagType() const
165 {
166         if (docbook_tag_type_ != "block" && docbook_tag_type_ != "paragraph" && docbook_tag_type_ != "inline")
167                 docbook_tag_type_ = "block";
168         return docbook_tag_type_;
169 }
170
171
172 string const & Floating::docbookCaption() const
173 {
174         if (!docbook_caption_.empty())
175                 return docbook_caption_;
176
177         if (docbook_float_type_ == "figure" || docbook_float_type_ == "video" ||
178                         docbook_float_type_ == "algorithm" || docbook_float_type_ == "example")
179                 docbook_caption_ = "title";
180         else if (floattype_ == "table" || floattype_ == "tableau")
181                 docbook_caption_ = "caption";
182         return docbook_caption_;
183 }
184
185
186 } // namespace lyx