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