]> git.lyx.org Git - lyx.git/blob - src/output_plaintext.C
hopefully fix tex2lyx linking.
[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
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, string const & fname,
43         OutputParams const & runparams)
44 {
45         odocfstream ofs;
46         if (!openFileWrite(ofs, fname))
47                 return;
48         writeFileAscii(buf, ofs, runparams);
49 }
50
51
52 void writeFileAscii(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                 asciiParagraph(buf, *it, os, runparams, ref_printed);
62         }
63         os << "\n";
64 }
65
66
67 namespace {
68
69 pair<int, docstring> 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, docstring(d, ' '));
75 }
76
77 }
78
79
80 void asciiParagraph(Buffer const & buf,
81                     Paragraph const & par,
82                     odocstream & os,
83                     OutputParams const & runparams,
84                     bool & ref_printed)
85 {
86         int ltype = 0;
87         depth_type ltype_depth = 0;
88         depth_type depth = par.params().depth();
89
90         // First write the layout
91         string const & tmp = par.layout()->name();
92         if (compare_no_case(tmp, "itemize") == 0) {
93                 ltype = 1;
94                 ltype_depth = depth + 1;
95         } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
96                 ltype = 2;
97                 ltype_depth = depth + 1;
98         } else if (contains(ascii_lowercase(tmp), "ection")) {
99                 ltype = 3;
100                 ltype_depth = depth + 1;
101         } else if (contains(ascii_lowercase(tmp), "aragraph")) {
102                 ltype = 4;
103                 ltype_depth = depth + 1;
104         } else if (compare_ascii_no_case(tmp, "description") == 0) {
105                 ltype = 5;
106                 ltype_depth = depth + 1;
107         } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
108                 ltype = 6;
109                 ltype_depth = 0;
110         } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
111                 ltype = 7;
112                 ltype_depth = 0;
113         } else {
114                 ltype = 0;
115                 ltype_depth = 0;
116         }
117
118         /* maybe some vertical spaces */
119
120         /* the labelwidthstring used in lists */
121
122         /* some lines? */
123
124         /* some pagebreaks? */
125
126         /* noindent ? */
127
128         /* what about the alignment */
129
130         // runparams.linelen <= 0 is special and means we don't have paragraph breaks
131
132         string::size_type currlinelen = 0;
133
134         if (runparams.linelen > 0)
135                 os << "\n\n";
136
137         os << docstring(depth * 2, ' ');
138         currlinelen += depth * 2;
139
140         //--
141         // we should probably change to the paragraph language in the
142         // gettext here (if possible) so that strings are output in
143         // the correct language! (20012712 Jug)
144         //--
145         switch (ltype) {
146         case 0: // Standard
147         case 4: // (Sub)Paragraph
148         case 5: // Description
149                 break;
150
151         case 6: // Abstract
152                 if (runparams.linelen > 0) {
153                         os << _("Abstract") << "\n\n";
154                         currlinelen = 0;
155                 } else {
156                         docstring const abst = _("Abstract: ");
157                         os << abst;
158                         currlinelen += abst.length();
159                 }
160                 break;
161
162         case 7: // Bibliography
163                 if (!ref_printed) {
164                         if (runparams.linelen > 0) {
165                                 os << _("References") << "\n\n";
166                                 currlinelen = 0;
167                         } else {
168                                 docstring const refs = _("References: ");
169                                 os << refs;
170                                 currlinelen += refs.length();
171                         }
172                         ref_printed = true;
173                 }
174                 break;
175
176         default: {
177                 docstring const label = par.params().labelString();
178                 os << label << ' ';
179                 currlinelen += label.length() + 1;
180                 break;
181         }
182
183         }
184
185         if (!currlinelen) {
186                 pair<int, docstring> p = addDepth(depth, ltype_depth);
187                 os << p.second;
188                 currlinelen += p.first;
189         }
190
191         // this is to change the linebreak to do it by word a bit more
192         // intelligent hopefully! (only in the case where we have a
193         // max runparams.linelength!) (Jug)
194
195         docstring word;
196
197         for (pos_type i = 0; i < par.size(); ++i) {
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                         if (runparams.linelen > 0) {
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 - 10) {
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                                 "writeAsciiFile: NULL char in structure." << endl;
232                         break;
233
234                 default:
235                         word += c;
236                         if (runparams.linelen > 0 &&
237                             currlinelen + word.length() > runparams.linelen)
238                         {
239                                 os << '\n';
240                                 pair<int, docstring> p = addDepth(depth, ltype_depth);
241                                 os << p.second;
242                                 currlinelen = p.first;
243                         }
244                         break;
245                 }
246         }
247         os << word;
248 }
249
250
251 } // namespace lyx