]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNomencl.cpp
Implement XHTML output for InsetPrintNomencl.
[lyx.git] / src / insets / InsetNomencl.cpp
1 /**
2  * \file InsetNomencl.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 O. U. Baran
8  * \author Uwe Stöhr
9  * \author Jürgen Spitzmüller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13 #include <config.h>
14
15 #include "InsetNomencl.h"
16 #include "InsetNote.h"
17
18 #include "Buffer.h"
19 #include "Cursor.h"
20 #include "DispatchResult.h"
21 #include "Font.h"
22 #include "Encoding.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetIterator.h"
26 #include "InsetList.h"
27 #include "Language.h"
28 #include "LaTeXFeatures.h"
29 #include "Length.h"
30 #include "LyX.h"
31 #include "OutputParams.h"
32 #include "output_xhtml.h"
33 #include "sgml.h"
34 #include "TocBackend.h"
35
36 #include "frontends/FontMetrics.h"
37
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/gettext.h"
41 #include "support/lstrings.h"
42
43 using namespace std;
44 using namespace lyx::support;
45
46 namespace lyx {
47
48
49 /////////////////////////////////////////////////////////////////////
50 //
51 // InsetNomencl
52 //
53 /////////////////////////////////////////////////////////////////////
54
55 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
56         : InsetCommand(buf, p),
57           nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
58 {}
59
60
61 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
62 {
63         static ParamInfo param_info_;
64         if (param_info_.empty()) {
65                 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
66                 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
67                                 ParamInfo::HANDLING_LATEXIFY);
68                 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
69                                 ParamInfo::HANDLING_LATEXIFY);
70         }
71         return param_info_;
72 }
73
74
75 docstring InsetNomencl::screenLabel() const
76 {
77         size_t const maxLabelChars = 25;
78
79         docstring label = _("Nom: ") + getParam("symbol");
80         if (label.size() > maxLabelChars) {
81                 label.erase(maxLabelChars - 3);
82                 label += "...";
83         }
84         return label;
85 }
86
87
88 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
89 {
90         docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
91         tip += _("Description: ") + "\t"
92                 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
93         if (!getParam("prefix").empty())
94                 tip += "\n" + _("Sorting: ") + getParam("prefix");
95         return tip;
96 }
97
98
99
100 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
101 {
102         os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
103            << sgml::escapeString(getParam("symbol"))
104            << "</glossterm>";
105         return 0;
106 }
107
108
109 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
110 {
111         return docstring();
112 }
113
114
115 int InsetNomencl::docbookGlossary(odocstream & os) const
116 {
117         os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
118            << "<glossterm>"
119            << sgml::escapeString(getParam("symbol"))
120            << "</glossterm>\n"
121            << "<glossdef><para>"
122            << sgml::escapeString(getParam("description"))
123            << "</para></glossdef>\n"
124            <<"</glossentry>\n";
125         return 4;
126 }
127
128
129 void InsetNomencl::validate(LaTeXFeatures & features) const
130 {
131         features.require("nomencl");
132 }
133
134
135 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active) const
136 {
137         docstring const str = getParam("symbol");
138         buffer().tocBackend().toc("nomencl").push_back(TocItem(cpit, 0, str, output_active));
139 }
140
141
142 /////////////////////////////////////////////////////////////////////
143 //
144 // InsetPrintNomencl
145 //
146 /////////////////////////////////////////////////////////////////////
147
148 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
149         : InsetCommand(buf, p)
150 {}
151
152
153 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
154 {
155         // The symbol width is set via nomencl's \nomlabelwidth in 
156         // InsetPrintNomencl::latex and not as optional parameter of
157         // \printnomenclature
158         static ParamInfo param_info_;
159         if (param_info_.empty()) {
160                 // how is the width set?
161                 // values: none|auto|custom
162                 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
163                 // custom width
164                 param_info_.add("width", ParamInfo::LYX_INTERNAL);
165         }
166         return param_info_;
167 }
168
169
170 docstring InsetPrintNomencl::screenLabel() const
171 {
172         return _("Nomenclature");
173 }
174
175
176 struct NomenclEntry {
177         NomenclEntry() {}
178         NomenclEntry(docstring s, docstring d, Paragraph const * p)
179           : symbol(s), desc(d), par(p)
180         {}
181
182         docstring symbol;
183         docstring desc;
184         Paragraph const * par;
185 };
186
187
188 typedef map<docstring, NomenclEntry > EntryMap;
189
190
191 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const & op) const
192 {
193         Toc const & toc = buffer().tocBackend().toc("nomencl");
194
195         EntryMap entries;
196         Toc::const_iterator it = toc.begin();
197         Toc::const_iterator const en = toc.end();
198         for (; it != en; ++it) {
199                 DocIterator dit = it->dit();
200                 Paragraph const & par = dit.innerParagraph();
201                 Inset const * inset = par.getInset(dit.top().pos());
202                 if (!inset)
203                         return docstring();
204                 InsetCommand const * ic = inset->asInsetCommand();
205                 if (!ic)
206                         return docstring();
207                 
208                 // FIXME We need a link to the paragraph here, so we
209                 // need some kind of struct.
210                 docstring const symbol = ic->getParam("symbol");
211                 docstring const desc = ic->getParam("description");
212                 docstring const prefix = ic->getParam("prefix");
213                 docstring const sortas = prefix.empty() ? symbol : prefix;
214                 
215                 entries[sortas] = NomenclEntry(symbol, desc, &par);
216         }
217         
218         if (entries.empty())
219                 return docstring();
220         
221         // we'll use our own stream, because we are going to defer everything.
222         // that's how we deal with the fact that we're probably inside a standard
223         // paragraph, and we don't want to be.
224         odocstringstream ods;
225         XHTMLStream xs(ods);
226         
227         InsetLayout const & il = getLayout();
228         string const & tag = il.htmltag();
229         docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
230                 op.local_font->language()->lang());
231
232         xs << html::StartTag("div", "class='nomencl'")
233            << html::StartTag(tag, "class='nomencl'")
234                  << toclabel 
235                  << html::EndTag(tag)
236            << html::CR()
237            << html::StartTag("dl")
238            << html::CR();
239         
240         EntryMap::const_iterator eit = entries.begin();
241         EntryMap::const_iterator const een = entries.end();
242         for (; eit != een; ++eit) {
243                 NomenclEntry const & ne = eit->second;
244                 string const parid = ne.par->magicLabel();
245                 xs << html::StartTag("dt")
246                    << html::StartTag("a", "href='#" + parid + "' class='nomencl'")
247                    << ne.symbol
248                    << html::EndTag("a")
249                    << html::EndTag("dt")
250                    << html::CR()
251                    << html::StartTag("dd")
252                    << ne.desc
253                    << html::EndTag("dd")
254                    << html::CR();
255         }
256
257         xs << html::EndTag("dl")
258            << html::CR()
259            << html::EndTag("div")
260            << html::CR();
261
262         return ods.str();
263 }
264
265
266 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
267 {
268         switch (cmd.action()) {
269
270         case LFUN_INSET_MODIFY: {
271                 InsetCommandParams p(NOMENCL_PRINT_CODE);
272                 // FIXME UNICODE
273                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
274                 if (p.getCmdName().empty()) {
275                         cur.noScreenUpdate();
276                         break;
277                 }
278
279                 cur.recordUndo();
280                 setParams(p);
281                 break;
282         }
283
284         default:
285                 InsetCommand::doDispatch(cur, cmd);
286                 break;
287         }
288 }
289
290
291 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
292         FuncStatus & status) const
293 {
294         switch (cmd.action()) {
295
296         case LFUN_INSET_DIALOG_UPDATE:
297         case LFUN_INSET_MODIFY:
298                 status.setEnabled(true);
299                 return true;
300
301         default:
302                 return InsetCommand::getStatus(cur, cmd, status);
303         }
304 }
305
306
307 // FIXME This should be changed to use the TOC. Perhaps
308 // that could be done when XHTML output is added.
309 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
310 {
311         os << "<glossary>\n";
312         int newlines = 2;
313         InsetIterator it = inset_iterator_begin(buffer().inset());
314         while (it) {
315                 if (it->lyxCode() == NOMENCL_CODE) {
316                         newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
317                         ++it;
318                 } else if (!it->producesOutput()) {
319                         // Ignore contents of insets that are not in output
320                         size_t const depth = it.depth();
321                         ++it;
322                         while (it.depth() > depth)
323                                 ++it;
324                 } else {
325                         ++it;
326                 }
327         }
328         os << "</glossary>\n";
329         return newlines;
330 }
331
332
333 namespace {
334 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
335 {
336         // nomenclWidest() determines and returns the widest used
337         // nomenclature symbol in the document
338
339         int w = 0;
340         docstring symb;
341         InsetNomencl const * nomencl = 0;
342         ParagraphList::const_iterator it = buffer.paragraphs().begin();
343         ParagraphList::const_iterator end = buffer.paragraphs().end();
344
345         for (; it != end; ++it) {
346                 if (it->insetList().empty())
347                         continue;
348                 InsetList::const_iterator iit = it->insetList().begin();
349                 InsetList::const_iterator eend = it->insetList().end();
350                 for (; iit != eend; ++iit) {
351                         Inset * inset = iit->inset;
352                         if (inset->lyxCode() != NOMENCL_CODE)
353                                 continue;
354                         nomencl = static_cast<InsetNomencl const *>(inset);
355                         docstring const symbol =
356                                 nomencl->getParam("symbol");
357                         // This is only an approximation,
358                         // but the best we can get.
359                         int const wx = use_gui ?
360                                 theFontMetrics(Font()).width(symbol) :
361                                 symbol.size();
362                         if (wx > w) {
363                                 w = wx;
364                                 symb = symbol;
365                         }
366                 }
367         }
368         // return the widest (or an empty) string
369         if (symb.empty())
370                 return symb;
371
372         // we have to encode the string properly
373         pair<docstring, docstring> latex_symb =
374                 runparams.encoding->latexString(symb, runparams.dryrun);
375         if (!latex_symb.second.empty())
376                 LYXERR0("Omitting uncodable characters '"
377                         << latex_symb.second
378                         << "' in nomencl widest string!");
379         return latex_symb.first;
380 }
381 } // namespace anon
382
383
384 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
385 {
386         OutputParams runparams = runparams_in;
387         if (getParam("set_width") == "auto") {
388                 docstring widest = nomenclWidest(buffer(), runparams);
389                 // Set the label width via nomencl's command \nomlabelwidth.
390                 // This must be output before the command \printnomenclature
391                 if (!widest.empty()) {
392                         os << "\\settowidth{\\nomlabelwidth}{"
393                            << widest
394                            << "}\n";
395                 }
396         } else if (getParam("set_width") == "custom") {
397                 // custom length as optional arg of \printnomenclature
398                 string const width =
399                         Length(to_ascii(getParam("width"))).asLatexString();
400                 os << '\\'
401                    << from_ascii(getCmdName())
402                    << '['
403                    << from_ascii(width)
404                    << "]{}";
405                 return;
406         }
407         // output the command \printnomenclature
408         os << getCommand(runparams);
409 }
410
411
412 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
413 {
414         features.require("nomencl");
415 }
416
417
418 InsetCode InsetPrintNomencl::lyxCode() const
419 {
420         return NOMENCL_PRINT_CODE;
421 }
422
423
424 string InsetPrintNomencl::contextMenuName() const
425 {
426         return "context-nomenclprint";
427 }
428
429
430 } // namespace lyx