]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
Restore XHTML output for InsetListings.
[lyx.git] / src / insets / InsetRef.cpp
1 /**
2  * \file InsetRef.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10 #include <config.h>
11
12 #include "InsetRef.h"
13
14 #include "Buffer.h"
15 #include "Cursor.h"
16 #include "DispatchResult.h"
17 #include "FuncRequest.h"
18 #include "LaTeXFeatures.h"
19 #include "LyXFunc.h"
20 #include "OutputParams.h"
21 #include "output_xhtml.h"
22 #include "ParIterator.h"
23 #include "sgml.h"
24 #include "TocBackend.h"
25
26 #include "support/docstream.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
29
30 using namespace lyx::support;
31 using namespace std;
32
33 namespace lyx {
34
35
36 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
37         : InsetCommand(buf, p, "ref"), isLatex(buf->isLatex())
38 {}
39
40
41 InsetRef::InsetRef(InsetRef const & ir)
42         : InsetCommand(ir), isLatex(ir.isLatex)
43 {}
44
45
46 bool InsetRef::isCompatibleCommand(string const & s) {
47         //FIXME This is likely not the best way to handle this.
48         //But this stuff is hardcoded elsewhere already.
49         return s == "ref" 
50                 || s == "pageref"
51                 || s == "vref" 
52                 || s == "vpageref"
53                 || s == "prettyref"
54                 || s == "eqref";
55 }
56
57
58 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
59 {
60         static ParamInfo param_info_;
61         if (param_info_.empty()) {
62                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
63                 param_info_.add("reference", ParamInfo::LATEX_REQUIRED);
64         }
65         return param_info_;
66 }
67
68
69 docstring InsetRef::screenLabel() const
70 {
71         return screen_label_;
72 }
73
74
75 int InsetRef::latex(odocstream & os, OutputParams const &) const
76 {
77         // We don't want to output p_["name"], since that is only used 
78         // in docbook. So we construct new params, without it, and use that.
79         InsetCommandParams p(REF_CODE, getCmdName());
80         p["reference"] = getParam("reference");
81         os << escape(p.getCommand());
82         return 0;
83 }
84
85
86 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
87 {
88         docstring const str = getParam("reference");
89         os << '[' << str << ']';
90         return 2 + str.size();
91 }
92
93
94 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
95 {
96         docstring const & name = getParam("name");
97         if (name.empty()) {
98                 if (runparams.flavor == OutputParams::XML) {
99                         os << "<xref linkend=\""
100                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
101                            << "\" />";
102                 } else {
103                         os << "<xref linkend=\""
104                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
105                            << "\">";
106                 }
107         } else {
108                 os << "<link linkend=\""
109                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
110                    << "\">"
111                    << getParam("name")
112                    << "</link>";
113         }
114
115         return 0;
116 }
117
118
119 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
120 {
121         // FIXME What we'd really like to do is to be able to output some
122         // appropriate sort of text here. But to do that, we need to associate
123         // some sort of counter with the label, and we don't have that yet.
124         docstring const ref = html::cleanAttr(getParam("reference"));
125         string const attr = "href=\"#" + to_utf8(ref) + "\"";
126         xs << StartTag("a", attr);
127         xs << ref;
128         xs << EndTag("a");
129         return docstring();
130 }
131
132 void InsetRef::tocString(odocstream & os) const
133 {
134         plaintext(os, OutputParams(0));
135 }
136
137
138 void InsetRef::updateLabels(ParIterator const & it, bool)
139 {
140         docstring const & label = getParam("reference");
141         // register this inset into the buffer reference cache.
142         buffer().references(label).push_back(make_pair(this, it));
143
144         for (int i = 0; !types[i].latex_name.empty(); ++i) {
145                 if (getCmdName() == types[i].latex_name) {
146                         screen_label_ = _(types[i].short_gui_name);
147                         break;
148                 }
149         }
150         screen_label_ += label;
151
152         if (!isLatex && !getParam("name").empty()) {
153                 screen_label_ += "||";
154                 screen_label_ += getParam("name");
155         }
156 }
157
158
159 void InsetRef::addToToc(DocIterator const & cpit)
160 {
161         docstring const & label = getParam("reference");
162         if (buffer().insetLabel(label))
163                 // This InsetRef has already been taken care of in InsetLabel::addToToc().
164                 return;
165
166         // It seems that this reference does not point to any valid label.
167         screen_label_ = _("BROKEN: ") + screen_label_;
168         Toc & toc = buffer().tocBackend().toc("label");
169         toc.push_back(TocItem(cpit, 0, screen_label_));
170 }
171
172
173 void InsetRef::validate(LaTeXFeatures & features) const
174 {
175         if (getCmdName() == "vref" || getCmdName() == "vpageref")
176                 features.require("varioref");
177         else if (getCmdName() == "prettyref")
178                 features.require("prettyref");
179         else if (getCmdName() == "eqref")
180                 features.require("amsmath");
181 }
182
183
184 InsetRef::type_info InsetRef::types[] = {
185         { "ref",       N_("Standard"),              N_("Ref: ")},
186         { "eqref",     N_("Equation"),              N_("EqRef: ")},
187         { "pageref",   N_("Page Number"),           N_("Page: ")},
188         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
189         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
190         { "prettyref", N_("PrettyRef"),             N_("FormatRef: ")},
191         { "", "", "" }
192 };
193
194
195 int InsetRef::getType(string const & name)
196 {
197         for (int i = 0; !types[i].latex_name.empty(); ++i)
198                 if (name == types[i].latex_name)
199                         return i;
200         return 0;
201 }
202
203
204 string const & InsetRef::getName(int type)
205 {
206         return types[type].latex_name;
207 }
208
209
210 } // namespace lyx