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);
45 writePlaintextFile(buf, ofs, runparams);
49 void writePlaintextFile(Buffer const & buf, odocstream & os,
50 OutputParams const & runparams)
52 bool ref_printed = false;
53 ParagraphList const & par = buf.paragraphs();
54 ParagraphList::const_iterator beg = par.begin();
55 ParagraphList::const_iterator end = par.end();
56 ParagraphList::const_iterator it = beg;
57 for (; it != end; ++it) {
58 writePlaintextParagraph(buf, *it, os, runparams, ref_printed);
60 if (runparams.linelen > 0)
66 static pair<int, docstring> addDepth(int depth, int ldepth)
70 d += (ldepth - depth) * 2;
71 return make_pair(d, docstring(d, ' '));
75 void writePlaintextParagraph(Buffer const & buf,
76 Paragraph const & par,
78 OutputParams const & runparams,
82 depth_type ltype_depth = 0;
83 depth_type depth = par.params().depth();
85 // First write the layout
86 string const tmp = to_utf8(par.layout().name());
87 if (compare_ascii_no_case(tmp, "itemize") == 0) {
89 ltype_depth = depth + 1;
90 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
92 ltype_depth = depth + 1;
93 } else if (contains(ascii_lowercase(tmp), "ection")) {
95 ltype_depth = depth + 1;
96 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
98 ltype_depth = depth + 1;
99 } else if (compare_ascii_no_case(tmp, "description") == 0) {
101 ltype_depth = depth + 1;
102 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
105 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
113 /* the labelwidthstring used in lists */
117 /* what about the alignment */
119 // runparams.linelen == 0 is special and means we don't have paragraph breaks
121 string::size_type currlinelen = 0;
123 os << docstring(depth * 2, ' ');
124 currlinelen += depth * 2;
127 // we should probably change to the paragraph language in the
128 // support/gettext.here (if possible) so that strings are output in
129 // the correct language! (20012712 Jug)
133 case 4: // (Sub)Paragraph
134 case 5: // Description
138 if (runparams.linelen > 0) {
139 os << buf.B_("Abstract") << "\n\n";
142 docstring const abst = buf.B_("Abstract: ");
144 currlinelen += abst.length();
148 case 7: // Bibliography
150 if (runparams.linelen > 0) {
151 os << buf.B_("References") << "\n\n";
154 docstring const refs = buf.B_("References: ");
156 currlinelen += refs.length();
163 docstring const label = par.params().labelString();
164 if (!label.empty()) {
166 currlinelen += label.length() + 1;
173 if (currlinelen == 0) {
174 pair<int, docstring> p = addDepth(depth, ltype_depth);
176 currlinelen += p.first;
181 for (pos_type i = 0; i < par.size(); ++i) {
182 // deleted characters don't make much sense in plain text output
183 if (par.isDeleted(i))
186 char_type c = par.getUChar(buf.params(), i);
188 if (par.isInset(i) || c == ' ') {
189 if (runparams.linelen > 0 &&
190 currlinelen + word.length() > runparams.linelen) {
192 pair<int, docstring> p = addDepth(depth, ltype_depth);
194 currlinelen = p.first;
197 currlinelen += word.length();
201 if (par.isInset(i)) {
202 OutputParams rp = runparams;
203 rp.depth = par.params().depth();
204 int len = par.getInset(i)->plaintext(os, rp);
205 if (len >= Inset::PLAINTEXT_NEWLINE)
206 currlinelen = len - Inset::PLAINTEXT_NEWLINE;
219 LYXERR(Debug::INFO, "writePlaintextFile: NUL char in structure.");
228 // currlinelen may be greater than runparams.linelen!
229 // => check whether word is empty and do nothing in this case
231 if (runparams.linelen > 0 &&
232 currlinelen + word.length() > runparams.linelen) {
234 pair<int, docstring> p = addDepth(depth, ltype_depth);
236 currlinelen = p.first;