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
))
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)
61 static pair
<int, docstring
> addDepth(int depth
, int ldepth
)
65 d
+= (ldepth
- depth
) * 2;
66 return make_pair(d
, docstring(d
, ' '));
69 void writePlaintextParagraph(Buffer
const & buf
,
70 Paragraph
const & par
,
72 OutputParams
const & runparams
,
76 depth_type ltype_depth
= 0;
77 depth_type depth
= par
.params().depth();
79 // First write the layout
80 string
const tmp
= to_utf8(par
.layout().name());
81 if (compare_ascii_no_case(tmp
, "itemize") == 0) {
83 ltype_depth
= depth
+ 1;
84 } else if (compare_ascii_no_case(tmp
, "enumerate") == 0) {
86 ltype_depth
= depth
+ 1;
87 } else if (contains(ascii_lowercase(tmp
), "ection")) {
89 ltype_depth
= depth
+ 1;
90 } else if (contains(ascii_lowercase(tmp
), "aragraph")) {
92 ltype_depth
= depth
+ 1;
93 } else if (compare_ascii_no_case(tmp
, "description") == 0) {
95 ltype_depth
= depth
+ 1;
96 } else if (compare_ascii_no_case(tmp
, "abstract") == 0) {
99 } else if (compare_ascii_no_case(tmp
, "bibliography") == 0) {
107 /* the labelwidthstring used in lists */
111 /* what about the alignment */
113 // runparams.linelen == 0 is special and means we don't have paragraph breaks
115 string::size_type currlinelen
= 0;
117 os
<< docstring(depth
* 2, ' ');
118 currlinelen
+= depth
* 2;
121 // we should probably change to the paragraph language in the
122 // support/gettext.here (if possible) so that strings are output in
123 // the correct language! (20012712 Jug)
127 case 4: // (Sub)Paragraph
128 case 5: // Description
132 if (runparams
.linelen
> 0) {
133 os
<< buf
.B_("Abstract") << "\n\n";
136 docstring
const abst
= buf
.B_("Abstract: ");
138 currlinelen
+= abst
.length();
142 case 7: // Bibliography
144 if (runparams
.linelen
> 0) {
145 os
<< buf
.B_("References") << "\n\n";
148 docstring
const refs
= buf
.B_("References: ");
150 currlinelen
+= refs
.length();
157 docstring
const label
= par
.params().labelString();
158 if (!label
.empty()) {
160 currlinelen
+= label
.length() + 1;
167 if (currlinelen
== 0) {
168 pair
<int, docstring
> p
= addDepth(depth
, ltype_depth
);
170 currlinelen
+= p
.first
;
175 for (pos_type i
= 0; i
< par
.size(); ++i
) {
176 // deleted characters don't make much sense in plain text output
177 if (par
.isDeleted(i
))
180 char_type c
= par
.getUChar(buf
.params(), i
);
182 if (par
.isInset(i
) || c
== ' ') {
183 if (runparams
.linelen
> 0 &&
184 currlinelen
+ word
.length() > runparams
.linelen
) {
186 pair
<int, docstring
> p
= addDepth(depth
, ltype_depth
);
188 currlinelen
= p
.first
;
191 currlinelen
+= word
.length();
195 if (par
.isInset(i
)) {
196 OutputParams rp
= runparams
;
197 rp
.depth
= par
.params().depth();
198 int len
= par
.getInset(i
)->plaintext(os
, rp
);
199 if (len
>= Inset::PLAINTEXT_NEWLINE
)
200 currlinelen
= len
- Inset::PLAINTEXT_NEWLINE
;
213 LYXERR(Debug::INFO
, "writePlaintextFile: NUL char in structure.");
222 // currlinelen may be greater than runparams.linelen!
223 // => check whether word is empty and do nothing in this case
225 if (runparams
.linelen
> 0 &&
226 currlinelen
+ word
.length() > runparams
.linelen
) {
228 pair
<int, docstring
> p
= addDepth(depth
, ltype_depth
);
230 currlinelen
= p
.first
;