/src/libe-book/src/lib/FictionBook2TextContext.cpp
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* |
3 | | * This file is part of the libe-book project. |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | */ |
9 | | |
10 | | #include "FictionBook2Collector.h" |
11 | | #include "FictionBook2TextContext.h" |
12 | | #include "FictionBook2Token.h" |
13 | | |
14 | | namespace libebook |
15 | | { |
16 | | |
17 | | FictionBook2ParaContextBase::FictionBook2ParaContextBase(std::shared_ptr<FictionBook2ParserContext> parentContext, const FictionBook2BlockFormat &format) |
18 | 122k | : FictionBook2StyleContextBase(parentContext, FictionBook2Style(format)) |
19 | 122k | { |
20 | 122k | } |
21 | | |
22 | | void FictionBook2ParaContextBase::startOfElement() |
23 | 122k | { |
24 | 122k | getCollector()->openParagraph(getStyle().getBlockFormat()); |
25 | 122k | } |
26 | | |
27 | | void FictionBook2ParaContextBase::endOfElement() |
28 | 121k | { |
29 | 121k | getCollector()->closeParagraph(); |
30 | 121k | } |
31 | | |
32 | | void FictionBook2ParaContextBase::attribute(const FictionBook2TokenData &name, const FictionBook2TokenData *ns, const char *value) |
33 | 4.01k | { |
34 | 4.01k | FictionBook2StyleContextBase::attribute(name, ns, value); |
35 | | |
36 | 4.01k | if (FictionBook2_NO_NAMESPACE(ns)) |
37 | 343 | { |
38 | 343 | switch (getFictionBook2TokenID(name)) |
39 | 343 | { |
40 | 233 | case FictionBook2Token::id : |
41 | 233 | getCollector()->defineID(value); |
42 | 233 | break; |
43 | 0 | case FictionBook2Token::style : |
44 | | // ignore |
45 | 0 | break; |
46 | 110 | default : |
47 | 110 | break; |
48 | 343 | } |
49 | 343 | } |
50 | 4.01k | } |
51 | | |
52 | | FictionBook2AContext::FictionBook2AContext(std::shared_ptr<FictionBook2ParserContext> parentContext, const FictionBook2Style &style) |
53 | 400k | : FictionBook2StyleContextBase(parentContext, style) |
54 | 400k | , m_href() |
55 | 400k | , m_valid(true) |
56 | 400k | , m_note(false) |
57 | 400k | { |
58 | 400k | } |
59 | | |
60 | | std::shared_ptr<FictionBook2XMLParserContext> FictionBook2AContext::element(const FictionBook2TokenData &name, const FictionBook2TokenData &ns) |
61 | 19.5k | { |
62 | 19.5k | if (m_note) |
63 | 4.05k | return std::make_shared<FictionBook2SkipElementContext>(shared_from_this()); |
64 | 15.5k | else |
65 | 15.5k | return FictionBook2StyleContextBase::element(name, ns); |
66 | 19.5k | } |
67 | | |
68 | | void FictionBook2AContext::startOfElement() |
69 | 400k | { |
70 | 400k | } |
71 | | |
72 | | void FictionBook2AContext::endOfElement() |
73 | 399k | { |
74 | 399k | if (m_note) |
75 | 13.3k | getCollector()->insertFootnote(m_href.c_str()); |
76 | 399k | } |
77 | | |
78 | | void FictionBook2AContext::endOfAttributes() |
79 | 400k | { |
80 | 400k | if (!m_valid) |
81 | 16 | m_note = false; |
82 | | |
83 | 400k | if (m_note) |
84 | 16.3k | { |
85 | 16.3k | if ('#' == m_href[0]) |
86 | 13.3k | m_href = m_href.substr(1); |
87 | 3.01k | else |
88 | 3.01k | m_note = false; |
89 | 16.3k | } |
90 | 400k | } |
91 | | |
92 | | void FictionBook2AContext::attribute(const FictionBook2TokenData &name, const FictionBook2TokenData *ns, const char *value) |
93 | 34.2k | { |
94 | 34.2k | if (FictionBook2Token::NS_XLINK == getFictionBook2TokenID(ns)) |
95 | 14.9k | { |
96 | 14.9k | switch (getFictionBook2TokenID(name)) |
97 | 14.9k | { |
98 | 14.8k | case FictionBook2Token::href : |
99 | 14.8k | m_href = value; |
100 | 14.8k | break; |
101 | 16 | case FictionBook2Token::type : |
102 | 16 | m_valid = FictionBook2Token::simple == getFictionBook2TokenID(value); |
103 | 16 | break; |
104 | 84 | default : |
105 | 84 | break; |
106 | 14.9k | } |
107 | 14.9k | } |
108 | 19.2k | else if (FictionBook2_NO_NAMESPACE(ns) && (FictionBook2Token::type == getFictionBook2TokenID(name))) |
109 | 17.8k | m_note = FictionBook2Token::note == getFictionBook2TokenID(value); |
110 | 34.2k | } |
111 | | |
112 | | void FictionBook2AContext::text(const char *value) |
113 | 61.7k | { |
114 | 61.7k | if (!m_note) |
115 | 49.5k | { |
116 | 49.5k | getCollector()->openSpan(getStyle()); |
117 | 49.5k | getCollector()->insertText(value); |
118 | 49.5k | getCollector()->closeSpan(); |
119 | 49.5k | } |
120 | 61.7k | } |
121 | | |
122 | | FictionBook2CodeContext::FictionBook2CodeContext(std::shared_ptr<FictionBook2ParserContext> parentContext, FictionBook2Style &style) |
123 | 316 | : FictionBook2StyleContextBase(parentContext, style) |
124 | 316 | { |
125 | 316 | } |
126 | | |
127 | | void FictionBook2CodeContext::startOfElement() |
128 | 316 | { |
129 | 316 | getTextFormat().code = true; |
130 | 316 | } |
131 | | |
132 | | FictionBook2EmphasisContext::FictionBook2EmphasisContext(std::shared_ptr<FictionBook2ParserContext> parentContext, FictionBook2Style &style) |
133 | 2.68k | : FictionBook2StyleContextBase(parentContext, style) |
134 | 2.68k | { |
135 | 2.68k | } |
136 | | |
137 | | void FictionBook2EmphasisContext::startOfElement() |
138 | 2.68k | { |
139 | 2.68k | getTextFormat().emphasis = true; |
140 | 2.68k | } |
141 | | |
142 | | FictionBook2InlineImageContext::FictionBook2InlineImageContext(std::shared_ptr<FictionBook2ParserContext> parentContext, const FictionBook2Style &style) |
143 | 16.6k | : FictionBook2DataContextBase(parentContext) |
144 | 16.6k | , m_style(style) |
145 | 16.6k | , m_href() |
146 | 16.6k | , m_altText() |
147 | 16.6k | , m_valid(true) |
148 | 16.6k | { |
149 | 16.6k | } |
150 | | |
151 | | void FictionBook2InlineImageContext::startOfElement() |
152 | 16.6k | { |
153 | 16.6k | } |
154 | | |
155 | | void FictionBook2InlineImageContext::endOfElement() |
156 | 16.5k | { |
157 | 16.5k | if (m_valid && ('#' != m_href[0])) |
158 | 3.54k | m_valid = false; |
159 | | |
160 | 16.5k | if (m_valid) |
161 | 8.67k | getCollector()->insertBitmap(m_href.substr(1).c_str()); |
162 | 7.91k | else |
163 | 7.91k | { |
164 | 7.91k | getCollector()->openSpan(m_style); |
165 | 7.91k | const std::string altText("[Image: " + m_altText + "]"); |
166 | 7.91k | getCollector()->insertText(altText.c_str()); |
167 | 7.91k | getCollector()->closeSpan(); |
168 | 7.91k | } |
169 | 16.5k | } |
170 | | |
171 | | void FictionBook2InlineImageContext::endOfAttributes() |
172 | 16.6k | { |
173 | 16.6k | } |
174 | | |
175 | | void FictionBook2InlineImageContext::attribute(const FictionBook2TokenData &name, const FictionBook2TokenData *ns, const char *value) |
176 | 37.4k | { |
177 | 37.4k | if (FictionBook2_NO_NAMESPACE(ns) && (FictionBook2Token::alt == getFictionBook2TokenID(name))) |
178 | 6.61k | m_altText = value; |
179 | 30.8k | else if (FictionBook2Token::NS_XLINK == getFictionBook2TokenID(ns)) |
180 | 19.4k | { |
181 | 19.4k | switch (getFictionBook2TokenID(name)) |
182 | 19.4k | { |
183 | 10.8k | case FictionBook2Token::href : |
184 | 10.8k | m_href = value; |
185 | 10.8k | break; |
186 | 8.55k | case FictionBook2Token::type : |
187 | 8.55k | m_valid = FictionBook2Token::simple == getFictionBook2TokenID(value); |
188 | 8.55k | break; |
189 | 72 | default : |
190 | 72 | break; |
191 | 19.4k | } |
192 | 19.4k | } |
193 | 37.4k | } |
194 | | |
195 | | FictionBook2PContext::FictionBook2PContext(std::shared_ptr<FictionBook2ParserContext> parentContext, const FictionBook2BlockFormat &format) |
196 | 119k | : FictionBook2ParaContextBase(parentContext, makeBlockFormat(format)) |
197 | 119k | { |
198 | 119k | } |
199 | | |
200 | | FictionBook2BlockFormat FictionBook2PContext::makeBlockFormat(const FictionBook2BlockFormat &format) |
201 | 119k | { |
202 | 119k | FictionBook2BlockFormat outFormat(format); |
203 | 119k | outFormat.p = true; |
204 | 119k | return outFormat; |
205 | 119k | } |
206 | | |
207 | | FictionBook2StrikethroughContext::FictionBook2StrikethroughContext(std::shared_ptr<FictionBook2ParserContext> parentContext, FictionBook2Style &style) |
208 | 22 | : FictionBook2StyleContextBase(parentContext, style) |
209 | 22 | { |
210 | 22 | } |
211 | | |
212 | | void FictionBook2StrikethroughContext::startOfElement() |
213 | 22 | { |
214 | 22 | getTextFormat().strikethrough = true; |
215 | 22 | } |
216 | | |
217 | | FictionBook2StrongContext::FictionBook2StrongContext(std::shared_ptr<FictionBook2ParserContext> parentContext, FictionBook2Style &style) |
218 | 116 | : FictionBook2StyleContextBase(parentContext, style) |
219 | 116 | { |
220 | 116 | } |
221 | | |
222 | | void FictionBook2StrongContext::startOfElement() |
223 | 116 | { |
224 | 116 | getTextFormat().strong = true; |
225 | 116 | } |
226 | | |
227 | | FictionBook2StyleContext::FictionBook2StyleContext(std::shared_ptr<FictionBook2ParserContext> parentContext, FictionBook2Style &style) |
228 | 1.85k | : FictionBook2StyleContextBase(parentContext, style) |
229 | 1.85k | { |
230 | 1.85k | } |
231 | | |
232 | | void FictionBook2StyleContext::startOfElement() |
233 | 1.85k | { |
234 | | // TODO: implement me |
235 | 1.85k | } |
236 | | |
237 | | FictionBook2SubContext::FictionBook2SubContext(std::shared_ptr<FictionBook2ParserContext> parentContext, FictionBook2Style &style) |
238 | 81 | : FictionBook2StyleContextBase(parentContext, style) |
239 | 81 | { |
240 | 81 | } |
241 | | |
242 | | void FictionBook2SubContext::startOfElement() |
243 | 81 | { |
244 | 81 | getTextFormat().sub = true; |
245 | 81 | } |
246 | | |
247 | | FictionBook2SubtitleContext::FictionBook2SubtitleContext(std::shared_ptr<FictionBook2ParserContext> parentContext, const FictionBook2BlockFormat &format) |
248 | 2.52k | : FictionBook2ParaContextBase(parentContext, makeBlockFormat(format)) |
249 | 2.52k | { |
250 | 2.52k | } |
251 | | |
252 | | FictionBook2BlockFormat FictionBook2SubtitleContext::makeBlockFormat(const FictionBook2BlockFormat &format) |
253 | 2.52k | { |
254 | 2.52k | FictionBook2BlockFormat outFormat(format); |
255 | 2.52k | outFormat.subtitle = true; |
256 | 2.52k | return outFormat; |
257 | 2.52k | } |
258 | | |
259 | | FictionBook2SupContext::FictionBook2SupContext(std::shared_ptr<FictionBook2ParserContext> parentContext, FictionBook2Style &style) |
260 | 1.79k | : FictionBook2StyleContextBase(parentContext, style) |
261 | 1.79k | { |
262 | 1.79k | } |
263 | | |
264 | | void FictionBook2SupContext::startOfElement() |
265 | 1.79k | { |
266 | 1.79k | getTextFormat().sup = true; |
267 | 1.79k | } |
268 | | |
269 | | FictionBook2TextAuthorContext::FictionBook2TextAuthorContext(std::shared_ptr<FictionBook2ParserContext> parentContext, const FictionBook2BlockFormat &format) |
270 | 3 | : FictionBook2PContext(parentContext, makeBlockFormat(format)) |
271 | 3 | { |
272 | 3 | } |
273 | | |
274 | | FictionBook2BlockFormat FictionBook2TextAuthorContext::makeBlockFormat(const FictionBook2BlockFormat &format) |
275 | 3 | { |
276 | 3 | FictionBook2BlockFormat outFormat(format); |
277 | 3 | outFormat.textAuthor = true; |
278 | 3 | return outFormat; |
279 | 3 | } |
280 | | |
281 | | FictionBook2VContext::FictionBook2VContext(std::shared_ptr<FictionBook2ParserContext> parentContext, const FictionBook2BlockFormat &format) |
282 | 78 | : FictionBook2ParaContextBase(parentContext, makeBlockFormat(format)) |
283 | 78 | { |
284 | 78 | } |
285 | | |
286 | | FictionBook2BlockFormat FictionBook2VContext::makeBlockFormat(const FictionBook2BlockFormat &format) |
287 | 78 | { |
288 | 78 | FictionBook2BlockFormat outFormat(format); |
289 | 78 | outFormat.v = true; |
290 | 78 | return outFormat; |
291 | 78 | } |
292 | | |
293 | | } |
294 | | |
295 | | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |