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.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "output_plaintext.h"
16 #include "BufferParams.h"
19 #include "OutputParams.h"
20 #include "Paragraph.h"
21 #include "ParagraphList.h"
22 #include "ParagraphParameters.h"
24 #include "support/debug.h"
25 #include "support/gettext.h"
26 #include "support/lstrings.h"
29 using namespace lyx::support;
34 void writePlaintextFile(Buffer const & buf, FileName const & fname,
35 OutputParams const & runparams)
38 if (!openFileWrite(ofs, fname))
41 // make sure we are ready to export
43 buf.updateMacroInstances(OutputUpdate);
44 buf.makeCitationLabels();
46 writePlaintextFile(buf, ofs, runparams);
50 void writePlaintextFile(Buffer const & buf, odocstream & os,
51 OutputParams const & runparams)
53 bool ref_printed = false;
54 ParagraphList const & par = buf.paragraphs();
55 ParagraphList::const_iterator beg = par.begin();
56 ParagraphList::const_iterator end = par.end();
57 ParagraphList::const_iterator it = beg;
58 for (; it != end; ++it) {
59 bool const merged_par = (*it).parEndChange().deleted();
60 writePlaintextParagraph(buf, *it, os, runparams, ref_printed);
63 if (runparams.linelen > 0 && !merged_par)
69 static pair<int, docstring> addDepth(int depth, int ldepth)
73 d += (ldepth - depth) * 2;
74 return make_pair(d, docstring(d, ' '));
78 void writePlaintextParagraph(Buffer const & buf,
79 Paragraph const & par,
81 OutputParams const & runparams,
82 bool & ref_printed, size_t max_length)
85 depth_type ltype_depth = 0;
86 depth_type depth = par.params().depth();
88 // First write the layout
89 string const tmp = to_utf8(par.layout().name());
90 if (compare_ascii_no_case(tmp, "itemize") == 0) {
92 ltype_depth = depth + 1;
93 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
95 ltype_depth = depth + 1;
96 } else if (contains(ascii_lowercase(tmp), "ection")) {
98 ltype_depth = depth + 1;
99 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
101 ltype_depth = depth + 1;
102 } else if (compare_ascii_no_case(tmp, "description") == 0) {
104 ltype_depth = depth + 1;
105 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
108 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
116 /* the labelwidthstring used in lists */
120 /* what about the alignment */
122 // runparams.linelen == 0 is special and means we don't have paragraph breaks
124 string::size_type currlinelen = 0;
127 os << docstring(depth * 2, ' ');
128 currlinelen += depth * 2;
131 // we should probably change to the paragraph language in the
132 // support/gettext.here (if possible) so that strings are output in
133 // the correct language! (20012712 Jug)
137 case 4: // (Sub)Paragraph
138 case 5: // Description
142 if (runparams.linelen > 0) {
143 os << buf.B_("Abstract") << "\n\n";
146 docstring const abst = buf.B_("Abstract: ");
148 currlinelen += abst.length();
152 case 7: // Bibliography
154 if (runparams.linelen > 0) {
155 os << buf.B_("References") << "\n\n";
158 docstring const refs = buf.B_("References: ");
160 currlinelen += refs.length();
167 docstring const label = par.params().labelString();
168 if (!label.empty()) {
170 currlinelen += label.length() + 1;
177 if (currlinelen == 0) {
178 pair<int, docstring> p = addDepth(depth, ltype_depth);
180 currlinelen += p.first;
185 for (pos_type i = 0; i < par.size(); ++i) {
186 // deleted characters don't make much sense in plain text output
187 if (par.isDeleted(i))
190 if (os.str().size() > max_length)
193 char_type c = par.getUChar(buf.params(), runparams, i);
195 if (par.isInset(i) || c == ' ') {
196 if (runparams.linelen > 0 &&
197 currlinelen + word.length() > runparams.linelen) {
199 pair<int, docstring> p = addDepth(depth, ltype_depth);
201 currlinelen = p.first;
204 currlinelen += word.length();
208 if (par.isInset(i)) {
209 OutputParams rp = runparams;
210 rp.depth = par.params().depth();
211 int len = par.getInset(i)->plaintext(os, rp, max_length);
212 if (len >= Inset::PLAINTEXT_NEWLINE)
213 currlinelen = len - Inset::PLAINTEXT_NEWLINE;
226 LYXERR(Debug::INFO, "writePlaintextFile: NUL char in structure.");
235 // currlinelen may be greater than runparams.linelen!
236 // => check whether word is empty and do nothing in this case
238 if (runparams.linelen > 0 &&
239 currlinelen + word.length() > runparams.linelen) {
241 pair<int, docstring> p = addDepth(depth, ltype_depth);
243 currlinelen = p.first;