2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include "TxtBookReader.h"
23 #include "../../bookmodel/BookModel.h"
25 TxtBookReader::TxtBookReader(BookModel
&model
, const PlainTextFormat
&format
, const std::string
&encoding
) : TxtReader(encoding
), BookReader(model
), myFormat(format
) {
28 void TxtBookReader::internalEndParagraph() {
29 if (!myLastLineIsEmpty
) {
30 myLineFeedCounter
= 0;
32 myLastLineIsEmpty
= true;
36 bool TxtBookReader::characterDataHandler(std::string
&str
) {
37 const char *ptr
= str
.data();
38 const char *end
= ptr
+ str
.length();
39 for (; ptr
!= end
; ++ptr
) {
40 if (isspace((unsigned char)*ptr
)) {
43 myLastLineIsEmpty
= false;
48 if ((myFormat
.breakType() & PlainTextFormat::BREAK_PARAGRAPH_AT_LINE_WITH_INDENT
) &&
49 myNewLine
&& (mySpaceCounter
> myFormat
.ignoredIndent())) {
50 internalEndParagraph();
54 if (myInsideContentsParagraph
) {
62 bool TxtBookReader::newLineHandler() {
63 if (!myLastLineIsEmpty
) {
64 myLineFeedCounter
= -1;
66 myLastLineIsEmpty
= true;
71 (myFormat
.breakType() & PlainTextFormat::BREAK_PARAGRAPH_AT_NEW_LINE
) ||
72 ((myFormat
.breakType() & PlainTextFormat::BREAK_PARAGRAPH_AT_EMPTY_LINE
) && (myLineFeedCounter
> 0));
74 if (myFormat
.createContentsTable()) {
75 if (!myInsideContentsParagraph
&& (myLineFeedCounter
== myFormat
.emptyLinesBeforeNewSection() + 1)) {
76 myInsideContentsParagraph
= true;
77 internalEndParagraph();
78 insertEndOfSectionParagraph();
79 beginContentsParagraph();
81 pushKind(SECTION_TITLE
);
83 paragraphBreak
= false;
85 if (myInsideContentsParagraph
&& (myLineFeedCounter
== 1)) {
87 endContentsParagraph();
89 myInsideContentsParagraph
= false;
90 paragraphBreak
= true;
95 internalEndParagraph();
101 void TxtBookReader::startDocumentHandler() {
105 myLineFeedCounter
= 0;
106 myInsideContentsParagraph
= false;
108 myLastLineIsEmpty
= true;
113 void TxtBookReader::endDocumentHandler() {
114 internalEndParagraph();