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