2 * \file output_plaintext.C
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"
26 #include "support/unicode.h"
30 using lyx::support::ascii_lowercase;
31 using lyx::support::compare_ascii_no_case;
32 using lyx::support::compare_no_case;
33 using lyx::support::contains;
43 void writeFileAscii(Buffer const & buf,
45 OutputParams const & runparams)
48 if (!::openFileWrite(ofs, fname))
50 writeFileAscii(buf, ofs, runparams);
54 void writeFileAscii(Buffer const & buf, ostream & os,
55 OutputParams const & runparams)
57 bool ref_printed = false;
58 ParagraphList const par = buf.paragraphs();
59 ParagraphList::const_iterator beg = par.begin();
60 ParagraphList::const_iterator end = par.end();
61 ParagraphList::const_iterator it = beg;
62 for (; it != end; ++it) {
63 asciiParagraph(buf, *it, os, runparams, ref_printed);
71 pair<int, string> const addDepth(int depth, int ldepth)
75 d += (ldepth - depth) * 2;
76 return make_pair(d, string(d, ' '));
82 void asciiParagraph(Buffer const & buf,
83 Paragraph const & par,
85 OutputParams const & runparams,
89 Paragraph::depth_type ltype_depth = 0;
90 Paragraph::depth_type depth = par.params().depth();
92 // First write the layout
93 string const & tmp = par.layout()->name();
94 if (compare_no_case(tmp, "itemize") == 0) {
96 ltype_depth = depth + 1;
97 } else if (compare_ascii_no_case(tmp, "enumerate") == 0) {
99 ltype_depth = depth + 1;
100 } else if (contains(ascii_lowercase(tmp), "ection")) {
102 ltype_depth = depth + 1;
103 } else if (contains(ascii_lowercase(tmp), "aragraph")) {
105 ltype_depth = depth + 1;
106 } else if (compare_ascii_no_case(tmp, "description") == 0) {
108 ltype_depth = depth + 1;
109 } else if (compare_ascii_no_case(tmp, "abstract") == 0) {
112 } else if (compare_ascii_no_case(tmp, "bibliography") == 0) {
120 /* maybe some vertical spaces */
122 /* the labelwidthstring used in lists */
126 /* some pagebreaks? */
130 /* what about the alignment */
132 // runparams.linelen <= 0 is special and means we don't have paragraph breaks
134 string::size_type currlinelen = 0;
136 if (runparams.linelen > 0)
139 os << string(depth * 2, ' ');
140 currlinelen += depth * 2;
143 // we should probably change to the paragraph language in the
144 // gettext here (if possible) so that strings are output in
145 // the correct language! (20012712 Jug)
149 case 4: // (Sub)Paragraph
150 case 5: // Description
154 if (runparams.linelen > 0) {
155 os << lyx::to_utf8(_("Abstract")) << "\n\n";
158 string const abst = lyx::to_utf8(_("Abstract: "));
160 currlinelen += abst.length();
164 case 7: // Bibliography
166 if (runparams.linelen > 0) {
167 os << lyx::to_utf8(_("References")) << "\n\n";
170 string const refs = lyx::to_utf8(_("References: "));
172 currlinelen += refs.length();
179 string const label = par.params().labelString();
181 currlinelen += label.length() + 1;
188 pair<int, string> p = addDepth(depth, ltype_depth);
190 currlinelen += p.first;
193 // this is to change the linebreak to do it by word a bit more
194 // intelligent hopefully! (only in the case where we have a
195 // max runparams.linelength!) (Jug)
199 for (pos_type i = 0; i < par.size(); ++i) {
200 lyx::char_type c = par.getUChar(buf.params(), i);
202 case Paragraph::META_INSET: {
203 InsetBase const * inset = par.getInset(i);
204 if (runparams.linelen > 0) {
206 currlinelen += word.length();
209 OutputParams rp = runparams;
210 rp.depth = par.params().depth();
211 if (inset->plaintext(buf, os, rp)) {
212 // to be sure it breaks paragraph
213 currlinelen += runparams.linelen;
219 if (runparams.linelen > 0 &&
220 currlinelen + word.length() > runparams.linelen - 10) {
222 pair<int, string> p = addDepth(depth, ltype_depth);
224 currlinelen = p.first;
227 currlinelen += word.length() + 1;
232 lyxerr[Debug::INFO] <<
233 "writeAsciiFile: NULL char in structure." << endl;
237 std::vector<char> const tmp = ucs4_to_utf8(c);
238 word.append(tmp.begin(), tmp.end());
239 if (runparams.linelen > 0 &&
240 currlinelen + word.length() > runparams.linelen)
243 pair<int, string> p = addDepth(depth, ltype_depth);
245 currlinelen = p.first;