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