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"
20 #include "OutputParams.h"
21 #include "Paragraph.h"
22 #include "ParagraphList.h"
23 #include "ParagraphParameters.h"
25 #include "support/lstrings.h"
30 using support::ascii_lowercase;
31 using support::compare_ascii_no_case;
32 using support::contains;
33 using support::FileName;
41 void writePlaintextFile(Buffer const & buf, FileName const & fname,
42 OutputParams const & runparams)
45 if (!openFileWrite(ofs, fname))
47 writePlaintextFile(buf, ofs, runparams);
51 void writePlaintextFile(Buffer const & buf, odocstream & os,
52 OutputParams const & runparams)
54 bool ref_printed = false;
55 ParagraphList const par = buf.paragraphs();
56 ParagraphList::const_iterator beg = par.begin();
57 ParagraphList::const_iterator end = par.end();
58 ParagraphList::const_iterator it = beg;
59 for (; it != end; ++it) {
60 writePlaintextParagraph(buf, *it, os, runparams, ref_printed);
62 if (runparams.linelen > 0)
70 pair<int, docstring> const addDepth(int depth, int ldepth)
74 d += (ldepth - depth) * 2;
75 return make_pair(d, docstring(d, ' '));
81 void writePlaintextParagraph(Buffer const & buf,
82 Paragraph const & par,
84 OutputParams const & runparams,
88 depth_type ltype_depth = 0;
89 depth_type depth = par.params().depth();
91 // First write the layout
92 string const tmp = to_utf8(par.layout()->name());
93 if (compare_ascii_no_case(tmp, "itemize") == 0) {
95 ltype_depth = depth + 1;
96 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
98 ltype_depth = depth + 1;
99 } else if (contains(ascii_lowercase(tmp), "ection")) {
101 ltype_depth = depth + 1;
102 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
104 ltype_depth = depth + 1;
105 } else if (compare_ascii_no_case(tmp, "description") == 0) {
107 ltype_depth = depth + 1;
108 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
111 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
119 /* the labelwidthstring used in lists */
123 /* what about the alignment */
125 // runparams.linelen == 0 is special and means we don't have paragraph breaks
127 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 // 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 char_type c = par.getUChar(buf.params(), i);
194 if (c == Paragraph::META_INSET || c == ' ') {
195 if (runparams.linelen > 0 &&
196 currlinelen + word.length() > runparams.linelen) {
198 pair<int, docstring> p = addDepth(depth, ltype_depth);
200 currlinelen = p.first;
203 currlinelen += word.length();
208 case Paragraph::META_INSET: {
209 OutputParams rp = runparams;
210 rp.depth = par.params().depth();
211 int len = par.getInset(i)->plaintext(buf, os, rp);
212 if (len >= Inset::PLAINTEXT_NEWLINE)
213 currlinelen = len - Inset::PLAINTEXT_NEWLINE;
225 LYXERR(Debug::INFO) <<
226 "writePlaintextFile: NULL char in structure." << endl;
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;