]> git.lyx.org Git - lyx.git/blob - src/output_plaintext.C
minimal effort implementation of:
[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 #include "support/unicode.h"
27
28 #include <fstream>
29
30 using lyx::support::ascii_lowercase;
31 using lyx::support::compare_ascii_no_case;
32 using lyx::support::compare_no_case;
33 using lyx::support::contains;
34
35 using lyx::pos_type;
36 using std::endl;
37 using std::ostream;
38 using std::ofstream;
39 using std::pair;
40 using std::string;
41
42
43 void writeFileAscii(Buffer const & buf,
44                     string const & fname,
45                     OutputParams const & runparams)
46 {
47         ofstream ofs;
48         if (!::openFileWrite(ofs, fname))
49                 return;
50         writeFileAscii(buf, ofs, runparams);
51 }
52
53
54 void writeFileAscii(Buffer const & buf, ostream & os,
55         OutputParams const & runparams)
56 {
57         bool ref_printed = false;
58         ParagraphList const par = buf.paragraphs();
59         ParagraphList::const_iterator beg = par.begin();
60         ParagraphList::const_iterator end = par.end();
61         ParagraphList::const_iterator it = beg;
62         for (; it != end; ++it) {
63                 asciiParagraph(buf, *it, os, runparams, ref_printed);
64         }
65         os << "\n";
66 }
67
68
69 namespace {
70
71 pair<int, string> 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, string(d, ' '));
77 }
78
79 }
80
81
82 void asciiParagraph(Buffer const & buf,
83                     Paragraph const & par,
84                     ostream & os,
85                     OutputParams const & runparams,
86                     bool & ref_printed)
87 {
88         int ltype = 0;
89         Paragraph::depth_type ltype_depth = 0;
90         Paragraph::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         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         if (!currlinelen) {
188                 pair<int, string> p = addDepth(depth, ltype_depth);
189                 os << p.second;
190                 currlinelen += p.first;
191         }
192
193         // this is to change the linebreak to do it by word a bit more
194         // intelligent hopefully! (only in the case where we have a
195         // max runparams.linelength!) (Jug)
196
197         string word;
198
199         for (pos_type i = 0; i < par.size(); ++i) {
200                 lyx::char_type c = par.getUChar(buf.params(), i);
201                 switch (c) {
202                 case Paragraph::META_INSET: {
203                         InsetBase const * inset = par.getInset(i);
204                         if (runparams.linelen > 0) {
205                                 os << word;
206                                 currlinelen += word.length();
207                                 word.erase();
208                         }
209                         OutputParams rp = runparams;
210                         rp.depth = par.params().depth();
211                         if (inset->plaintext(buf, os, rp)) {
212                                 // to be sure it breaks paragraph
213                                 currlinelen += runparams.linelen;
214                         }
215                         break;
216                 }
217
218                 case ' ':
219                         if (runparams.linelen > 0 &&
220                             currlinelen + word.length() > runparams.linelen - 10) {
221                                 os << "\n";
222                                 pair<int, string> p = addDepth(depth, ltype_depth);
223                                 os << p.second;
224                                 currlinelen = p.first;
225                         }
226                         os << word << ' ';
227                         currlinelen += word.length() + 1;
228                         word.erase();
229                         break;
230
231                 case '\0':
232                         lyxerr[Debug::INFO] <<
233                                 "writeAsciiFile: NULL char in structure." << endl;
234                         break;
235
236                 default: {
237                         std::vector<char> const tmp = ucs4_to_utf8(c);
238                         word.append(tmp.begin(), tmp.end());
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         }
251         os << word;
252 }