]> git.lyx.org Git - lyx.git/blob - src/output_plaintext.cpp
Improve error message
[lyx.git] / src / output_plaintext.cpp
1 /**
2  * \file output_plaintext.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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "output_plaintext.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "Layout.h"
18 #include "output.h"
19 #include "OutputParams.h"
20 #include "Paragraph.h"
21 #include "ParagraphList.h"
22 #include "ParagraphParameters.h"
23
24 #include "support/debug.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
27
28 using namespace std;
29 using namespace lyx::support;
30
31 namespace lyx {
32
33
34 void writePlaintextFile(Buffer const & buf, FileName const & fname,
35         OutputParams const & runparams)
36 {
37         ofdocstream ofs;
38         if (!openFileWrite(ofs, fname))
39                 return;
40
41         // make sure we are ready to export
42         buf.updateBuffer();
43         buf.updateMacroInstances(OutputUpdate);
44         buf.makeCitationLabels();
45
46         writePlaintextFile(buf, ofs, runparams);
47 }
48
49
50 void writePlaintextFile(Buffer const & buf, odocstream & os,
51         OutputParams const & runparams)
52 {
53         bool ref_printed = false;
54         ParagraphList const & par = buf.paragraphs();
55         ParagraphList::const_iterator beg = par.begin();
56         ParagraphList::const_iterator end = par.end();
57         ParagraphList::const_iterator it = beg;
58         for (; it != end; ++it) {
59                 bool const merged_par = (*it).parEndChange().deleted();
60                 writePlaintextParagraph(buf, *it, os, runparams, ref_printed);
61                 if (!merged_par)
62                         os << "\n";
63                 if (runparams.linelen > 0 && !merged_par)
64                         os << "\n";
65         }
66 }
67
68
69 static pair<int, docstring> addDepth(int depth, int ldepth)
70 {
71         int d = depth * 2;
72         if (ldepth > depth)
73                 d += (ldepth - depth) * 2;
74         return make_pair(d, docstring(d, ' '));
75 }
76
77
78 void writePlaintextParagraph(Buffer const & buf,
79                     Paragraph const & par,
80                     odocstream & ods,
81                     OutputParams const & runparams,
82                     bool & ref_printed, size_t max_length)
83 {
84         int ltype = 0;
85         depth_type ltype_depth = 0;
86         depth_type depth = par.params().depth();
87
88         // First write the layout
89         string const tmp = to_utf8(par.layout().name());
90         if (compare_ascii_no_case(tmp, "itemize") == 0) {
91                 ltype = 1;
92                 ltype_depth = depth + 1;
93         } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
94                 ltype = 2;
95                 ltype_depth = depth + 1;
96         } else if (contains(ascii_lowercase(tmp), "ection")) {
97                 ltype = 3;
98                 ltype_depth = depth + 1;
99         } else if (contains(ascii_lowercase(tmp), "aragraph")) {
100                 ltype = 4;
101                 ltype_depth = depth + 1;
102         } else if (compare_ascii_no_case(tmp, "description") == 0) {
103                 ltype = 5;
104                 ltype_depth = depth + 1;
105         } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
106                 ltype = 6;
107                 ltype_depth = 0;
108         } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
109                 ltype = 7;
110                 ltype_depth = 0;
111         } else {
112                 ltype = 0;
113                 ltype_depth = 0;
114         }
115
116         /* the labelwidthstring used in lists */
117
118         /* noindent ? */
119
120         /* what about the alignment */
121
122         // runparams.linelen == 0 is special and means we don't have paragraph breaks
123
124         string::size_type currlinelen = 0;
125
126         odocstringstream os;
127         os << docstring(depth * 2, ' ');
128         currlinelen += depth * 2;
129
130         //--
131         // we should probably change to the paragraph language in the
132         // support/gettext.here (if possible) so that strings are output in
133         // the correct language! (20012712 Jug)
134         //--
135         switch (ltype) {
136         case 0: // Standard
137         case 4: // (Sub)Paragraph
138         case 5: // Description
139                 break;
140
141         case 6: // Abstract
142                 if (runparams.linelen > 0) {
143                         os << buf.B_("Abstract") << "\n\n";
144                         currlinelen = 0;
145                 } else {
146                         docstring const abst = buf.B_("Abstract: ");
147                         os << abst;
148                         currlinelen += abst.length();
149                 }
150                 break;
151
152         case 7: // Bibliography
153                 if (!ref_printed) {
154                         if (runparams.linelen > 0) {
155                                 os << buf.B_("References") << "\n\n";
156                                 currlinelen = 0;
157                         } else {
158                                 docstring const refs = buf.B_("References: ");
159                                 os << refs;
160                                 currlinelen += refs.length();
161                         }
162                         ref_printed = true;
163                 }
164                 break;
165
166         default: {
167                 docstring const label = par.params().labelString();
168                 if (!label.empty()) {
169                         os << label << ' ';
170                         currlinelen += label.length() + 1;
171                 }
172                 break;
173         }
174
175         }
176
177         if (currlinelen == 0) {
178                 pair<int, docstring> p = addDepth(depth, ltype_depth);
179                 os << p.second;
180                 currlinelen += p.first;
181         }
182
183         docstring word;
184
185         for (pos_type i = 0; i < par.size(); ++i) {
186                 // deleted characters don't make much sense in plain text output
187                 if (par.isDeleted(i))
188                         continue;
189
190                 if (os.str().size() > max_length)
191                         break;
192
193                 char_type c = par.getUChar(buf.params(), runparams, i);
194
195                 if (par.isInset(i) || c == ' ') {
196                         if (runparams.linelen > 0 &&
197                             currlinelen + word.length() > runparams.linelen) {
198                                 os << '\n';
199                                 pair<int, docstring> p = addDepth(depth, ltype_depth);
200                                 os << p.second;
201                                 currlinelen = p.first;
202                         }
203                         os << word;
204                         currlinelen += word.length();
205                         word.erase();
206                 }
207
208                 if (par.isInset(i)) {
209                         OutputParams rp = runparams;
210                         rp.depth = par.params().depth();
211                         int len = par.getInset(i)->plaintext(os, rp, max_length);
212                         if (len >= Inset::PLAINTEXT_NEWLINE)
213                                 currlinelen = len - Inset::PLAINTEXT_NEWLINE;
214                         else
215                                 currlinelen += len;
216                         continue;
217                 }
218
219                 switch (c) {
220                 case ' ':
221                         os << ' ';
222                         ++currlinelen;
223                         break;
224
225                 case '\0':
226                         LYXERR(Debug::INFO, "writePlaintextFile: NUL char in structure.");
227                         break;
228
229                 default:
230                         word += c;
231                         break;
232                 }
233         }
234
235         // currlinelen may be greater than runparams.linelen!
236         // => check whether word is empty and do nothing in this case
237         if (!word.empty()) {
238                 if (runparams.linelen > 0 &&
239                     currlinelen + word.length() > runparams.linelen) {
240                         os << '\n';
241                         pair<int, docstring> p = addDepth(depth, ltype_depth);
242                         os << p.second;
243                         currlinelen = p.first;
244                 }
245                 os << word;
246         }
247         ods << os.str();
248 }
249
250
251 } // namespace lyx