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 "insets/Inset.h"
26 #include "support/debug.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
31 using namespace lyx::support;
36 void writePlaintextFile(Buffer const & buf, FileName const & fname,
37 OutputParams const & runparams)
40 if (!openFileWrite(ofs, fname))
43 // make sure we are ready to export
45 buf.updateMacroInstances(OutputUpdate);
46 buf.makeCitationLabels();
48 writePlaintextFile(buf, ofs, runparams);
52 void writePlaintextFile(Buffer const & buf, odocstream & os,
53 OutputParams const & runparams)
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 bool const merged_par = (*it).parEndChange().deleted();
62 writePlaintextParagraph(buf, *it, os, runparams, ref_printed);
65 if (runparams.linelen > 0 && !merged_par)
71 static pair<int, docstring> addDepth(int depth, int ldepth)
75 d += (ldepth - depth) * 2;
76 return make_pair(d, docstring(d, ' '));
80 void writePlaintextParagraph(Buffer const & buf,
81 Paragraph const & par,
83 OutputParams const & runparams,
84 bool & ref_printed, size_t max_length)
87 depth_type ltype_depth = 0;
88 depth_type depth = par.params().depth();
90 // First write the layout
91 string const tmp = to_utf8(par.layout().name());
92 if (compare_ascii_no_case(tmp, "itemize") == 0) {
94 ltype_depth = depth + 1;
95 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
97 ltype_depth = depth + 1;
98 } else if (contains(ascii_lowercase(tmp), "ection")) {
100 ltype_depth = depth + 1;
101 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
103 ltype_depth = depth + 1;
104 } else if (compare_ascii_no_case(tmp, "description") == 0) {
106 ltype_depth = depth + 1;
107 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
110 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
118 /* the labelwidthstring used in lists */
122 /* what about the alignment */
124 // runparams.linelen == 0 is special and means we don't have paragraph breaks
126 string::size_type currlinelen = 0;
129 os << docstring(depth * 2, ' ');
130 currlinelen += depth * 2;
133 // we should probably change to the paragraph language in the
134 // support/gettext.here (if possible) so that strings are output in
135 // the correct language! (20012712 Jug)
139 case 4: // (Sub)Paragraph
140 case 5: // Description
144 if (runparams.linelen > 0) {
145 os << buf.B_("Abstract") << "\n\n";
148 docstring const abst = buf.B_("Abstract: ");
150 currlinelen += abst.length();
154 case 7: // Bibliography
156 if (runparams.linelen > 0) {
157 os << buf.B_("References") << "\n\n";
160 docstring const refs = buf.B_("References: ");
162 currlinelen += refs.length();
169 docstring const label = par.params().labelString();
170 if (!label.empty()) {
172 currlinelen += label.length() + 1;
179 if (currlinelen == 0) {
180 pair<int, docstring> p = addDepth(depth, ltype_depth);
182 currlinelen += p.first;
187 for (pos_type i = 0; i < par.size(); ++i) {
188 // deleted characters don't make much sense in plain text output
189 if (par.isDeleted(i))
192 if (os.str().size() > max_length)
195 char_type c = par.getUChar(buf.params(), runparams, i);
197 if (par.isInset(i) || c == ' ') {
198 if (runparams.linelen > 0 &&
199 currlinelen + word.length() > runparams.linelen) {
201 pair<int, docstring> p = addDepth(depth, ltype_depth);
203 currlinelen = p.first;
206 currlinelen += word.length();
210 if (par.isInset(i)) {
211 OutputParams rp = runparams;
212 rp.depth = par.params().depth();
213 int len = par.getInset(i)->plaintext(os, rp, max_length);
214 if (len >= Inset::PLAINTEXT_NEWLINE)
215 currlinelen = len - Inset::PLAINTEXT_NEWLINE;
228 LYXERR(Debug::INFO, "writePlaintextFile: NUL char in structure.");
237 // currlinelen may be greater than runparams.linelen!
238 // => check whether word is empty and do nothing in this case
240 if (runparams.linelen > 0 &&
241 currlinelen + word.length() > runparams.linelen) {
243 pair<int, docstring> p = addDepth(depth, ltype_depth);
245 currlinelen = p.first;