forked from goldendict/goldendict
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdxf2html.cc
652 lines (537 loc) · 19.9 KB
/
xdxf2html.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/* This file is (c) 2008-2012 Konstantin Isakov <[email protected]>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "xdxf2html.hh"
#include <QtXml>
#include "gddebug.hh"
#include "utf8.hh"
#include "wstring_qt.hh"
#include "folding.hh"
#include "fsencoding.hh"
#include "audiolink.hh"
#include "file.hh"
#include "filetype.hh"
#include "htmlescape.hh"
#include "qt4x5.hh"
#include <QDebug>
namespace Xdxf2Html {
static void fixLink( QDomElement & el, string const & dictId, const char *attrName )
{
QUrl url;
url.setScheme( "bres" );
url.setHost( QString::fromStdString(dictId) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( el.attribute(attrName) ) );
el.setAttribute( attrName, url.toEncoded().data() );
}
// converting a number into roman representation
string convertToRoman( int input, int lower_case )
{
string romanvalue = "";
if( input >= 4000 )
{
int x = ( input - input % 4000 ) / 1000;
romanvalue = "(" + convertToRoman( x, lower_case ) + ")" ;
input %= 4000;
}
const string roman[26] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I",
"m", "cm", "d", "cd", "c", "xc", "l", "xl", "x", "ix", "v", "iv", "i"};
const int decimal[13] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
for( int i = 0; i < 13; i++ )
{
while( input >= decimal[ i ] )
{
input -= decimal[ i ];
if ( lower_case == 1 )
romanvalue += roman[ i + 13 ];
else
romanvalue += roman[ i ];
}
}
return romanvalue;
}
QDomElement fakeElement( QDomDocument & dom )
{
// Create element which will be removed after
// We will insert it to empty elements to avoid output ones in <xxx/> form
return dom.createElement( "b" );
}
string convert( string const & in, DICT_TYPE type, map < string, string > const * pAbrv,
Dictionary::Class *dictPtr, IndexedZip * resourceZip,
bool isLogicalFormat, unsigned revisionNumber, QString * headword )
{
// DPRINTF( "Source>>>>>>>>>>: %s\n\n\n", in.c_str() );
// Convert spaces after each end of line to s, and then each end of
// line to a <br>
string inConverted;
inConverted.reserve( in.size() );
bool afterEol = false;
for( string::const_iterator i = in.begin(), j = in.end(); i != j; ++i )
{
switch( *i )
{
case '\n':
afterEol = true;
if( !isLogicalFormat )
inConverted.append( "<br/>" );
break;
case '\r':
break;
case ' ':
if ( afterEol )
{
if( !isLogicalFormat )
inConverted.append( " " );
break;
}
// Fall-through
default:
inConverted.push_back( *i );
afterEol = false;
}
}
// Strip "<nu />" tags - QDomDocument don't handle it correctly
string::size_type n;
while( ( n = inConverted.find( "<nu />" ) ) != string::npos )
inConverted.erase( n, 6 );
// We build a dom representation of the given xml, then do some transforms
QDomDocument dd;
QString errorStr;
int errorLine, errorColumn;
string in_data;
if( type == XDXF )
{
in_data = "<div class=\"xdxf\"";
if( dictPtr->isToLanguageRTL() )
in_data += " dir=\"rtl\"";
in_data += ">";
}
else
in_data = "<div class=\"sdct_x\">";
in_data += inConverted + "</div>";
if( !dd.setContent( QByteArray( in_data.c_str() ), false, &errorStr, &errorLine, &errorColumn ) )
{
qWarning( "Xdxf2html error, xml parse failed: %s at %d,%d\n", errorStr.toLocal8Bit().constData(), errorLine, errorColumn );
gdWarning( "The input was: %s\n", in.c_str() );
return in;
}
QDomNodeList nodes = dd.elementsByTagName( "ex" ); // Example
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
el.setTagName( "span" );
if( isLogicalFormat )
el.setAttribute( "class", "xdxf_ex" );
else
el.setAttribute( "class", "xdxf_ex_old" );
}
nodes = dd.elementsByTagName( "mrkd" ); // marked out words in tranlations/examples of usage
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
el.setTagName( "span" );
el.setAttribute( "class", "xdxf_ex_markd" );
}
nodes = dd.elementsByTagName( "k" ); // Key
if( headword )
headword->clear();
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
if( type == STARDICT )
{
el.setTagName( "span" );
el.setAttribute( "class", "xdxf_k" );
}
else
{
if( headword && headword->isEmpty() )
*headword = el.text();
el.setTagName( "div" );
el.setAttribute( "class", "xdxf_headwords" );
if( dictPtr->isFromLanguageRTL() != dictPtr->isToLanguageRTL() )
el.setAttribute( "dir", dictPtr->isFromLanguageRTL() ? "rtl" : "ltr" );
}
}
// processing of nested <def>s
if( isLogicalFormat ) // in articles with visual format <def> tags do not effect the formatting.
{
nodes = dd.elementsByTagName( "def" );
// this is a logical type of XDXF, so we need to render proper numbering
// we will do it this way:
// 1. we compute the maximum nesting depth of the article
int maxNestingDepth = 1; // maximum nesting depth of the article
for( int i = 0; i < nodes.size(); i++ )
{
QDomElement el = nodes.at( i ).toElement();
QDomElement nestingNode = el;
int nestingCount = 0;
while ( nestingNode.parentNode().toElement().tagName() == "def" )
{
nestingCount++;
nestingNode = nestingNode.parentNode().toElement();
}
if ( nestingCount > maxNestingDepth )
maxNestingDepth = nestingCount;
}
// 2. in this loop we go layer-by-layer through all <def> and insert proper numbers according to its structure
for( int j = maxNestingDepth; j > 0; j-- ) // j symbolizes special depth to be processed at this iteration
{
int siblingCount = 0; // this that counts the number of among all siblings of this depth
QString numberText = ""; // the number to be inserted into the beginning of <def> (I,II,IV,1,2,3,a),b),c)...)
for( int i = 0; i < nodes.size(); i++ )
{
QDomElement el = nodes.at( i ).toElement();
QDomElement nestingNode = el;
// computing the depth @nestingDepth of a current node @el
int nestingDepth = 0;
while( nestingNode.parentNode().toElement().tagName() == "def" )
{
nestingDepth++;
nestingNode=nestingNode.parentNode().toElement();
}
// we process nodes on of current depth @j
// we do this in order not to break the numbering at this depth level
if (nestingDepth == j)
{
siblingCount++;
if( maxNestingDepth == 1 )
{
numberText = numberText.setNum( siblingCount ) + ". ";
}
else if( maxNestingDepth == 2 )
{
if( nestingDepth == 1 )
numberText = numberText.setNum( siblingCount ) + ". ";
if( nestingDepth == 2 )
numberText = numberText.setNum( siblingCount ) + ") ";
}
else
{
if( nestingDepth == 1 )
numberText = QString::fromStdString( convertToRoman(siblingCount,0) + ". " );
if( nestingDepth == 2 )
numberText = numberText.setNum( siblingCount ) + ". ";
if( nestingDepth == 3 )
numberText = numberText.setNum( siblingCount ) + ") ";
if( nestingDepth == 4 )
numberText = QString::fromStdString( convertToRoman(siblingCount,1) + ") " );
}
QDomElement numberNode = dd.createElement( "span" );
numberNode.setAttribute( "class", "xdxf_num" );
QDomText text_num = dd.createTextNode( numberText );
numberNode.appendChild( text_num );
el.insertBefore( numberNode, el.firstChild() );
if ( el.hasAttribute( "cmt" ) )
{
QDomElement cmtNode = dd.createElement( "span" );
cmtNode.setAttribute( "class", "xdxf_co" );
QDomText text_num = dd.createTextNode( el.attribute( "cmt" ) );
cmtNode.appendChild( text_num );
el.insertAfter( cmtNode, el.firstChild() );
}
}
else if( nestingDepth < j ) // if it goes one level up @siblingCount needs to be reset
siblingCount = 0;
}
}
// we finally change all <def> tags into 'xdxf_def' <span>s
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
el.setTagName( "span" );
el.setAttribute( "class", "xdxf_def" );
}
}
nodes = dd.elementsByTagName( "opt" ); // Optional headword part
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
el.setTagName( "span" );
el.setAttribute( "class", "xdxf_opt" );
}
nodes = dd.elementsByTagName( "kref" ); // Reference to another word
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
el.setTagName( "a" );
el.setAttribute( "href", QString( "bword:" ) + el.text() );
el.setAttribute( "class", "xdxf_kref" );
if ( el.hasAttribute( "idref" ) )
{
// todo implement support for referencing only specific parts of the article
el.setAttribute( "href", QString( "bword:" ) + el.text() + "#" + el.attribute( "idref" ));
}
if ( el.hasAttribute( "kcmt" ) )
{
QDomText kcmtText = dd.createTextNode( " " + el.attribute( "kcmt" ) );
el.parentNode().insertAfter( kcmtText, el );
}
}
nodes = dd.elementsByTagName( "iref" ); // Reference to internet site
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
QString ref = el.attribute( "href" );
if( ref.isEmpty() )
ref = el.text();
el.setAttribute( "href", ref );
el.setTagName( "a" );
}
// Abbreviations
if( revisionNumber < 29 )
nodes = dd.elementsByTagName( "abr" );
else
nodes = dd.elementsByTagName( "abbr" );
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
el.setTagName( "span" );
el.setAttribute( "class", "xdxf_abbr" );
if( type == XDXF && pAbrv != NULL )
{
string val = Utf8::encode( Folding::trimWhitespace( gd::toWString( el.text() ) ) );
// If we have such a key, display a title
map< string, string >::const_iterator i = pAbrv->find( val );
if ( i != pAbrv->end() )
{
string title;
if ( Utf8::decode( i->second ).size() < 70 )
{
// Replace all spaces with non-breakable ones, since that's how Lingvo shows tooltips
title.reserve( i->second.size() );
for( char const * c = i->second.c_str(); *c; ++c )
{
if ( *c == ' ' || *c == '\t' )
{
// u00A0 in utf8
title.push_back( 0xC2 );
title.push_back( 0xA0 );
}
else
if( *c == '-' ) // Change minus to non-breaking hyphen (uE28091 in utf8)
{
title.push_back( 0xE2 );
title.push_back( 0x80 );
title.push_back( 0x91 );
}
else
title.push_back( *c );
}
}
else
title = i->second;
el.setAttribute( "title", gd::toQString( Utf8::decode( title ) ) );
}
}
}
nodes = dd.elementsByTagName( "dtrn" ); // Direct translation
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
el.setTagName( "span" );
el.setAttribute( "class", "xdxf_dtrn" );
}
nodes = dd.elementsByTagName( "c" ); // Color
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
el.setTagName( "span" );
if ( el.hasAttribute( "c" ) )
{
el.setAttribute( "style", "color:" + el.attribute( "c" ) );
el.removeAttribute( "c" );
}
else
el.setAttribute( "style", "color:blue" );
}
nodes = dd.elementsByTagName( "co" ); // Editorial comment
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
el.setTagName( "span" );
if( isLogicalFormat )
el.setAttribute( "class", "xdxf_co" );
else
el.setAttribute( "class", "xdxf_co_old" );
}
/* grammar information */
nodes = dd.elementsByTagName( "gr" ); // proper grammar tag
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
el.setTagName( "span" );
if( isLogicalFormat )
el.setAttribute( "class", "xdxf_gr" );
else
el.setAttribute( "class", "xdxf_gr_old" );
}
nodes = dd.elementsByTagName( "pos" ); // deprecated grammar tag
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
el.setTagName( "span" );
if( isLogicalFormat )
el.setAttribute( "class", "xdxf_gr" );
else
el.setAttribute( "class", "xdxf_gr_old" );
}
nodes = dd.elementsByTagName( "tense" ); // deprecated grammar tag
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
el.setTagName( "span" );
if( isLogicalFormat )
el.setAttribute( "class", "xdxf_gr" );
else
el.setAttribute( "class", "xdxf_gr_old" );
}
/* end of grammar generation */
nodes = dd.elementsByTagName( "tr" ); // Transcription
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
el.setTagName( "span" );
if( isLogicalFormat )
el.setAttribute( "class", "xdxf_tr" );
else
el.setAttribute( "class", "xdxf_tr_old" );
}
// Ensure that ArticleNetworkAccessManager can deal with XDXF images.
// We modify the URL by using the dictionary ID as the hostname.
// This is necessary to determine from which dictionary a requested
// image originates.
nodes = dd.elementsByTagName( "img" );
for( int i = 0; i < nodes.size(); i++ )
{
QDomElement el = nodes.at( i ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
if ( el.hasAttribute( "src" ) )
{
fixLink( el, dictPtr->getId(), "src" );
}
if ( el.hasAttribute( "losrc" ) )
{
fixLink( el, dictPtr->getId(), "losrc" );
}
if ( el.hasAttribute( "hisrc" ) )
{
fixLink( el, dictPtr->getId(), "hisrc" );
}
}
nodes = dd.elementsByTagName( "rref" ); // Resource reference
while( nodes.size() )
{
QDomElement el = nodes.at( 0 ).toElement();
if( el.text().isEmpty() && el.childNodes().isEmpty() )
el.appendChild( fakeElement( dd ) );
// if( type == XDXF && dictPtr != NULL && !el.hasAttribute( "start" ) )
if( dictPtr != NULL && !el.hasAttribute( "start" ) )
{
string filename = Utf8::encode( gd::toWString( el.text() ) );
if ( Filetype::isNameOfPicture( filename ) )
{
QUrl url;
url.setScheme( "bres" );
url.setHost( QString::fromUtf8( dictPtr->getId().c_str() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
QDomElement newEl = dd.createElement( "img" );
newEl.setAttribute( "src", url.toEncoded().data() );
newEl.setAttribute( "alt", Html::escape( filename ).c_str() );
QDomNode parent = el.parentNode();
if( !parent.isNull() )
{
parent.replaceChild( newEl, el );
continue;
}
}
else if( Filetype::isNameOfSound( filename ) )
{
QDomElement el_script = dd.createElement( "script" );
QDomNode parent = el.parentNode();
if( !parent.isNull() )
{
bool search = false;
if( type == STARDICT )
{
string n = FsEncoding::dirname( dictPtr->getDictionaryFilenames()[ 0 ] ) +
FsEncoding::separator() + string( "res" ) + FsEncoding::separator() +
FsEncoding::encode( filename );
search = !File::exists( n ) &&
( !resourceZip ||
!resourceZip->isOpen() ||
!resourceZip->hasFile( Utf8::decode( filename ) ) );
}
else
{
string n = dictPtr->getDictionaryFilenames()[ 0 ] + ".files" +
FsEncoding::separator() +
FsEncoding::encode( filename );
search = !File::exists( n ) && !File::exists( FsEncoding::dirname( dictPtr->getDictionaryFilenames()[ 0 ] ) +
FsEncoding::separator() +
FsEncoding::encode( filename ) ) &&
( !resourceZip ||
!resourceZip->isOpen() ||
!resourceZip->hasFile( Utf8::decode( filename ) ) );
}
QUrl url;
url.setScheme( "gdau" );
url.setHost( QString::fromUtf8( search ? "search" : dictPtr->getId().c_str() ) );
url.setPath( Qt4x5::Url::ensureLeadingSlash( QString::fromUtf8( filename.c_str() ) ) );
el_script.setAttribute( "type", "text/javascript" );
parent.replaceChild( el_script, el );
QDomText el_txt = dd.createTextNode( makeAudioLinkScript( string( "\"" ) + url.toEncoded().data() + "\"",
dictPtr->getId() ).c_str() );
el_script.appendChild( el_txt );
QDomElement el_span = dd.createElement( "span" );
el_span.setAttribute( "class", "xdxf_wav" );
parent.insertAfter( el_span, el_script );
QDomElement el_a = dd.createElement( "a" );
el_a.setAttribute( "href", url.toEncoded().data() );
el_span.appendChild( el_a );
QDomElement el_img = dd.createElement( "img");
el_img.setAttribute( "src", "qrcx://localhost/icons/playsound.png" );
el_img.setAttribute( "border", "0" );
el_img.setAttribute( "align", "absmiddle" );
el_img.setAttribute( "alt", "Play" );
el_a.appendChild( el_img );
continue;
}
}
}
// We don't really know how to handle this at the moment, so we'll just
// convert it to a span and leave it as is for now.
el.setTagName( "span" );
el.setAttribute( "class", "xdxf_rref" );
}
// GD_DPRINTF( "Result>>>>>>>>>>: %s\n\n\n", dd.toByteArray( 0 ).data() );
return dd.toString( 1 ).remove('\n').remove( QRegExp( "<(b|i)/>" ) ).toUtf8().data();
}
}