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"
17 #include "support/debug.h"
18 #include "support/gettext.h"
21 #include "OutputParams.h"
22 #include "Paragraph.h"
23 #include "ParagraphList.h"
24 #include "ParagraphParameters.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))
40 writePlaintextFile(buf, ofs, runparams);
44 void writePlaintextFile(Buffer const & buf, odocstream & os,
45 OutputParams const & runparams)
47 bool ref_printed = false;
48 ParagraphList const par = buf.paragraphs();
49 ParagraphList::const_iterator beg = par.begin();
50 ParagraphList::const_iterator end = par.end();
51 ParagraphList::const_iterator it = beg;
52 for (; it != end; ++it) {
53 writePlaintextParagraph(buf, *it, os, runparams, ref_printed);
55 if (runparams.linelen > 0)
63 pair<int, docstring> const addDepth(int depth, int ldepth)
67 d += (ldepth - depth) * 2;
68 return make_pair(d, docstring(d, ' '));
74 void writePlaintextParagraph(Buffer const & buf,
75 Paragraph const & par,
77 OutputParams const & runparams,
81 depth_type ltype_depth = 0;
82 depth_type depth = par.params().depth();
84 // First write the layout
85 string const tmp = to_utf8(par.layout()->name());
86 if (compare_ascii_no_case(tmp, "itemize") == 0) {
88 ltype_depth = depth + 1;
89 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
91 ltype_depth = depth + 1;
92 } else if (contains(ascii_lowercase(tmp), "ection")) {
94 ltype_depth = depth + 1;
95 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
97 ltype_depth = depth + 1;
98 } else if (compare_ascii_no_case(tmp, "description") == 0) {
100 ltype_depth = depth + 1;
101 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
104 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
112 /* the labelwidthstring used in lists */
116 /* what about the alignment */
118 // runparams.linelen == 0 is special and means we don't have paragraph breaks
120 string::size_type currlinelen = 0;
122 os << docstring(depth * 2, ' ');
123 currlinelen += depth * 2;
126 // we should probably change to the paragraph language in the
127 // support/gettext.here (if possible) so that strings are output in
128 // the correct language! (20012712 Jug)
132 case 4: // (Sub)Paragraph
133 case 5: // Description
137 if (runparams.linelen > 0) {
138 os << buf.B_("Abstract") << "\n\n";
141 docstring const abst = buf.B_("Abstract: ");
143 currlinelen += abst.length();
147 case 7: // Bibliography
149 if (runparams.linelen > 0) {
150 os << buf.B_("References") << "\n\n";
153 docstring const refs = buf.B_("References: ");
155 currlinelen += refs.length();
162 docstring const label = par.params().labelString();
163 if (!label.empty()) {
165 currlinelen += label.length() + 1;
172 if (currlinelen == 0) {
173 pair<int, docstring> p = addDepth(depth, ltype_depth);
175 currlinelen += p.first;
180 for (pos_type i = 0; i < par.size(); ++i) {
181 // deleted characters don't make much sense in plain text output
182 if (par.isDeleted(i))
185 char_type c = par.getUChar(buf.params(), i);
187 if (par.isInset(i) || c == ' ') {
188 if (runparams.linelen > 0 &&
189 currlinelen + word.length() > runparams.linelen) {
191 pair<int, docstring> p = addDepth(depth, ltype_depth);
193 currlinelen = p.first;
196 currlinelen += word.length();
200 if (par.isInset(i)) {
201 OutputParams rp = runparams;
202 rp.depth = par.params().depth();
203 int len = par.getInset(i)->plaintext(buf, os, rp);
204 if (len >= Inset::PLAINTEXT_NEWLINE)
205 currlinelen = len - Inset::PLAINTEXT_NEWLINE;
218 LYXERR(Debug::INFO, "writePlaintextFile: NULL char in structure.");
227 // currlinelen may be greater than runparams.linelen!
228 // => check whether word is empty and do nothing in this case
230 if (runparams.linelen > 0 &&
231 currlinelen + word.length() > runparams.linelen) {
233 pair<int, docstring> p = addDepth(depth, ltype_depth);
235 currlinelen = p.first;