-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp2docbook.pl
executable file
·1607 lines (1476 loc) · 56.9 KB
/
wp2docbook.pl
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl -w
# ---------------------------------------------------------------------------
# wp2docbook.pl - Perl script to convert wordpress dump file to docbook XML
# files.
# Copyright (c) 2007-2009, Jeff Hung
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# - Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# - Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# - Neither the name of the copyright holders nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------
# @see http://developer.apple.com/internet/webcontent/xmltransformations.html
# @see http://www.badgers-in-foil.co.uk/projects/docbook-css/
# @see http://www.w3.org/TR/xml-stylesheet/
# @see http://www.brainbell.com/articles/xml/Styling_XML_with_CSS/XML_CSS_and_Internet_Explorer.html
package HtmlAttrCleaner;
use strict;
use utf8;
use HTML::Filter;
our @ISA=qw(HTML::Filter);
sub output { push(@{$_[0]->{fhtml}}, $_[1]); }
sub filtered_html { join('', @{$_[0]->{fhtml}}); }
sub start
{
my $self = shift or die;
my ($tagname, $attrs, $attrseq, $text) = @_;
foreach my $name (keys %$attrs) {
my $value = $attrs->{$name};
if ($value) {
$value = xmlspecialchars_($value);
$attrs->{$name} = $value;
}
}
$text = "<$tagname";
foreach my $attr_name (@$attrseq) {
next if ($attr_name =~ m/\//o);
$text .= " $attr_name=\"$attrs->{$attr_name}\"";
}
$text .= '>';
$self->output($text);
}
sub xmlspecialchars_
{
my $s = shift or die;
$s =~ s/&/&/go;
$s =~ s/</</go;
$s =~ s/>/>/go;
$s =~ s/'/'/go;
$s =~ s/"/"/go;
return $s;
}
1;
package main;
use strict;
use utf8;
use File::Basename;
use Getopt::Long;
use XML::DOM;
use File::Path;
my ($__exe_name__) = (basename($0));
my ($__revision__) = ('$Rev: 113 $' =~ m/(\d+)/o);
my ($__rev_date__) = ('$Date: 2008-03-13 02:46:55 +0800 (Thu, 13 Mar 2008) $' =~ m/(\d{4}-\d{2}-\d{2})/o);
sub usage
{
print STDERR <<"EOF";
Usage: $__exe_name__ [ <option> ... ] <wp-dump-file> <docbook-file>
Convert wordpress dump file to DocBook format.
Options:
-h,--help Show this help message.
-v,--verbose Show verbose progress messages.
-s,--section-wrap Wrap article with at least one <section>.
-p,--post <post-id> Convert only post #<post-id>.
--split <articles-dir> Split individual articles to <articles-dir>.
--css-stylesheet <css-uri> URI, either absolute or relative, of the CSS
stylesheet for the generated DocBook XML
document. Can be specified many times, and will
be listed in the command-line order.
Revision: r$__revision__ ($__rev_date__)
EOF
exit(0);
}
sub msg_exit
{
my $ex = ((scalar(@_) > 0) ? shift @_ : 0);
foreach my $m (@_) {
print STDERR "ERROR: $m\n";
}
print STDERR <<"EOF";
Usage: $__exe_name__ [ <option> ... ] <wp-dump-file> <docbook-file>
Type '$__exe_name__ --help' for usage.
EOF
exit($ex);
}
my $opt_verbose = 0;
my $opt_section_wrap = 0;
my @opt_posts;
my @opt_css_uris;
my $opt_articles_dir = undef;
if (!GetOptions('h|help' => sub { usage; },
's|section-wrap' => \$opt_section_wrap,
'p|post=i' => \@opt_posts,
'split=s' => \$opt_articles_dir,
'css-stylesheet=s' => \@opt_css_uris,
'v|verbose' => \$opt_verbose)) {
msg_exit(0);
}
my $arg_wp_dump_file = shift @ARGV or msg_exit(1, 'Missing <wp-dump-file>');
my $arg_docbook_file = shift @ARGV or msg_exit(1, 'Missing <docbook-file>');
my $options = { posts => @opt_posts };
wordpress_dump_to_docbook($arg_wp_dump_file, $arg_docbook_file, $options);
sub value_in_list
{
my $value = shift or die;
my @list = shift or die;
foreach my $l (@list) {
if ($value eq $l) {
return 1;
}
}
return 0;
}
sub read_text_file
{
my $path = shift or die;
my ($encoding) = (shift or 'utf8');
my $fh;
if ($path ne '-') {
open($fh, "<:encoding($encoding)", $path)
or msg_exit(2, "Cannot open '$path' for read: $!");
}
else {
$fh = \*STDIN;
}
my $content = '';
while (my $line = <$fh>) {
$content .= $line;
}
if ($path ne '-') {
close($fh);
}
return $content;
}
sub write_text_file
{
my $path = shift or die;
my $content = shift or die;
my ($encoding) = (shift or 'utf8');
my $fh;
if ($path ne '-') {
open($fh, ">:encoding($encoding)", $path)
or msg_exit(2, "Cannot open '$path' for write: $!");
}
else {
$fh = \*STDOUT;
}
print $fh $content;
if ($path ne '-') {
close($fh);
}
}
##
# Remove XML DOM node $node, and move its child nodes to its parent node at
# the position of $node.
#
sub xmldom_node_remove
{
my $node = shift or die;
my $parent_node = $node->getParentNode();
while ($node->hasChildNodes()) {
my $kid = $node->removeChild($node->getFirstChild());
$parent_node->insertBefore($kid, $node);
}
$parent_node->removeChild($node);
$node->dispose();
}
##
# Purge XML DOM node $node and its all child nodes recursively.
#
sub xmldom_node_purge
{
my $node = shift or die;
$node->getParentNode()->removeChild($node);
$node->dispose();
}
sub xmldom_node_replace
{
my $old_node = shift or die;
my $new_node = shift or die;
my $parent_node = $old_node->getParentNode();
if ($parent_node) {
while ($old_node->hasChildNodes()) {
my $kid = $old_node->removeChild($old_node->getFirstChild());
$new_node->appendChild($kid);
}
$parent_node->replaceChild($new_node, $old_node);
}
}
sub in_array_
{
my $v = shift or die;
my @a = @_;
foreach my $x (@a) {
return 1 if ($x eq $v);
}
return 0;
}
sub xmldom_parent_element
{
my $node = shift or die;
while ($node = $node->getParentNode()) {
if ($node->getNodeType() == XML::DOM::ELEMENT_NODE) {
return $node;
}
}
return undef;
}
sub xmldom_next_sibling_element
{
my $elem = shift or die;
die unless ($elem->getNodeType() == XML::DOM::ELEMENT_NODE);
my $node = $elem;
while ($node = $node->getNextSibling()) {
if ($node->getNodeType() == XML::DOM::ELEMENT_NODE) {
return $node;
}
}
return undef;
}
sub xmldom_first_child_element
{
my $node = shift or die;
my $child_nodes = $node->getChildNodes();
foreach my $child_node (@$child_nodes) {
if ($child_node->getNodeType() == XML::DOM::ELEMENT_NODE) {
return $child_node;
}
}
# print STDERR "[no element found in 1st level]";
# no element node found in first level child nodes
# search recursively
foreach my $child_node (@$child_nodes) {
my $x = xmldom_first_child_element($child_node);
return $x if ($x);
}
return undef;
}
##
# Given a XML DOM element, it contains only text nodes recursively
#
sub xmldom_only_text_child_nodes
{
my $node = shift or die;
if ($node->getNodeType() == XML::DOM::TEXT_NODE) {
return 1;
}
my $is_only_text = 0;
my $child_nodes = $node->getChildNodes();
foreach my $child_node (@$child_nodes) {
$is_only_text = xmldom_only_text_child_nodes($child_node);
last if (!$is_only_text); # foreach
}
return $is_only_text;
}
sub xmldom_element_match
{
my $elem = shift or die;
my $spec = shift or die;
if ($spec->{'tag_name'}) {
if ($elem->getTagName() ne $spec->{'tag_name'}) {
return 0;
}
}
if ($spec->{'required_attributes'}) {
foreach my $required_attr (@{$spec->{'required_attributes'}}) {
my $no_required_attr = 1;
my $attr_nodes = $elem->getAttributes();
for (my $i = 0; $i < $attr_nodes->getLength(); ++$i) {
my $attr_node = $attr_nodes->item($i);
my $attr_name = $attr_node->getName();
if ($attr_name eq $required_attr) {
$no_required_attr = 0;
last; # for
}
}
return 0 if ($no_required_attr);
}
}
if ($spec->{'required_classes'}) {
my $class_attr = $elem->getAttribute('class');
my @classes = split(/\s+/, $class_attr);
foreach my $required_class (@{$spec->{'required_classes'}}) {
if (!in_array_($required_class, @classes)) {
return 0;
}
}
}
if ($spec->{'parent_spec'}) {
my $parent_elem = xmldpm_parent_element($elem);
if ($parent_elem) {
if (!xmldom_element_match($parent_elem, $spec->{'parent_spec'})) {
return 0;
}
}
else {
return 0;
}
}
return 1;
}
##
# Find a list of XML DOM elements that matching given criteria.
#
sub xmldom_element_find
{
my $base_elem = shift or die; # starting point
my $spec = shift or die;
my $found_elems = []; # type: XML::DOM::Element
my $elems = $base_elem->getElementsByTagName($spec->{'tag_name'});
for (my $i = 0; $i < $elems->getLength(); ++$i) {
my $elem = $elems->item($i);
if (xmldom_element_match($elem, $spec)) {
push(@$found_elems, $elem);
}
}
return $found_elems;
}
#sub xmlspecialchars
#{
# my $s = shift or die;
# $s =~ s/&/&/go;
# $s =~ s/</</go;
# $s =~ s/>/>/go;
# $s =~ s/'/'/go;
# $s =~ s/"/"/go;
# return $s;
#}
sub xml_clean_attr_values
{
my $incomplete_xml = shift or die;
my $hf = new HtmlAttrCleaner();
$hf->parse($incomplete_xml);
return $hf->filtered_html();
# TODO: Use HTML::Parser instead?!
# use HTML::TagFilter;
# my $tf = new HTML::TagFilter(
## log_rejects => 1,
## verbose => 1,
# on_open_tag => sub {
# my ($self, $tag, $attributes, $sequence) = @_;
# foreach my $name (@$sequence) {
# my $value = $attributes->{$name};
## print STDERR "Processing attribute '$name': $value\n";
# $value = xmlspecialchars($value);
# $attributes->{$name} = $value;
# }
# },
# );
# $tf->parse($incomplete_xml);
# # Report rejected tags/attributes/...
# # TODO: We hope not rejecting anything.
## foreach my $report ($tf->report()) {
## foreach my $k (keys %$report) {
## print STDERR "\n[HTML::TagFilter: $k => $report->{$k}]";
## }
## }
# return $tf->output();
}
sub tidy_xml_file
{
my $xml_file = shift or die;
print STDERR "Tidy XML file '$xml_file'...";
# jeffhung.20070816: Using tidy will break whitespaces in <programlisting>.
# `tidy -quiet -xml -indent -wrap 79 -utf8 -modify $xml_file`;
print STDERR " [done]\n";
}
##
# Clone DOM node $node on (maybe) different DOM document $doc.
#
sub xmldom_clone_across_doc
{
my $node = shift or die;
my $doc = shift or die;
my $new_node;
my $node_type = $node->getNodeType();
if ($node_type == XML::DOM::UNKNOWN_NODE) {
die "UNKNOWN_NODE found";
}
elsif ($node_type == XML::DOM::ELEMENT_NODE) {
$new_node = $doc->createElement($node->getTagName());
my $attr_nodes = $node->getAttributes();
for (my $i = 0; $i < $attr_nodes->getLength(); ++$i) {
my $attr_node = $attr_nodes->item($i);
$new_node->setAttribute($attr_node->getName(), $attr_node->getValue());
}
}
# elsif ($node_type == XML::DOM::ATTRIBUTE_NODE) {
# my $attr_name = $node->getName();
# my $attr_value = $node->getValue();
# print STDERR "[attribute:$attr_name=\"$attr_value\"]";
# $new_node = $doc->createAttribute($node->getName(), $node->getValue());
# }
elsif ($node_type == XML::DOM::TEXT_NODE) {
$new_node = $doc->createTextNode($node->getNodeValue());
}
elsif ($node_type == XML::DOM::CDATA_SECTION_NODE) {
$new_node = $doc->createCDATASection($node->getData());
}
elsif ($node_type == XML::DOM::ENTITY_REFERENCE_NODE) {
$new_node = $doc->createEntityReference($node->getNodeValue());
}
# elsif($node_type == XML::DOM::ENTITY_NODE) {
# $new_node = $doc->createEntity(); # ...
# }
# elsif($node_type == XML::DOM::PROCESSING_INSTRUCTION_NODE) {
# }
elsif($node_type == XML::DOM::COMMENT_NODE) {
$new_node = $doc->createComment($node->getData());
}
elsif($node_type == XML::DOM::DOCUMENT_NODE) {
die "Should not encounter document node";
}
elsif($node_type == XML::DOM::DOCUMENT_TYPE_NODE) {
die "Should not encounter document-type node";
}
# elsif($node_type == XML::DOM::DOCUMENT_FRAGMENT_NODE) {
# }
# elsif($node_type == XML::DOM::NOTATION_NODE) {
# }
# elsif($node_type == XML::DOM::ELEMENT_DECL_NODE) {
# }
# elsif($node_type == XML::DOM::ATT_DEF_NODE) {
# }
elsif($node_type == XML::DOM::XML_DECL_NODE) {
die "Should not encounter xml-decl node";
}
# elsif($node_type == XML::DOM::ATTLIST_DECL_NODE) {
# }
else {
die "Unknown node_type: $node_type";
}
# Clone child nodes
foreach my $child ($node->getChildNodes()) {
$new_node->appendChild(xmldom_clone_across_doc($child, $doc));
}
return $new_node;
}
##
# Get text value inside a XML element.
#
sub xmldom_element_text_value
{
my $elem = shift or die;
die unless ($elem->getNodeType() == XML::DOM::ELEMENT_NODE);
my $text = '';
foreach my $child ($elem->getChildNodes()) {
if ($child->getNodeType() == XML::DOM::TEXT_NODE) {
my $fragment = $child->getNodeValue();
$text .= $fragment;
}
}
return $text;
}
sub wordrpess_item_content_to_docbook_article_fragment
{
my $wordpress_item_content = shift or die;
my $docbook_document = shift or die;
write_text_file(sprintf('item-%d.xml', __LINE__), $wordpress_item_content);
my $content = "<?xml version=\"1.0\"?>\n";
$content .= '<content>';
$content .= xml_clean_attr_values($wordpress_item_content);
$content .= '</content>';
write_text_file(sprintf('content-%d.xml', __LINE__), $content);
# Deal with unmatched tags
$content =~ s/<\s*br[^>]*>/<br\/>/go;
$content =~ s/<\s*hr[^>]*>/<hr\/>/go;
$content =~ s/<\s*img([^>]*)>/<img$1\/>/go;
# http://www.w3schools.com/tags/ref_entities.asp
# http://www.digitalmediaminute.com/reference/entity/index.php
# http://www.danshort.com/HTMLentities/index.php
# http://www.w3.org/TR/xhtml-modularization/dtd_module_defs.html#a_xhtml_character_entities
$content =~ s/ / /go; # no-break space = non-breaking space, U+00A0 ISOnum
$content =~ s/¡/¡/go; # inverted exclamation mark, U+00A1 ISOnum
$content =~ s/¢/¢/go; # cent sign, U+00A2 ISOnum
$content =~ s/£/£/go; # pound sign, U+00A3 ISOnum
$content =~ s/¤/¤/go; # currency sign, U+00A4 ISOnum
$content =~ s/¥/¥/go; # yen sign = yuan sign, U+00A5 ISOnum
$content =~ s/¦/¦/go; # broken bar = broken vertical bar, U+00A6 ISOnum
$content =~ s/§/§/go; # section sign, U+00A7 ISOnum
$content =~ s/¨/¨/go; # diaeresis = spacing diaeresis, U+00A8 ISOdia
$content =~ s/©/©/go; # copyright sign, U+00A9 ISOnum
$content =~ s/ª/ª/go; # feminine ordinal indicator, U+00AA ISOnum
$content =~ s/«/«/go; # left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum
$content =~ s/¬/¬/go; # not sign, U+00AC ISOnum
$content =~ s/­/­/go; # soft hyphen = discretionary hyphen, U+00AD ISOnum
$content =~ s/®/®/go; # registered sign = registered trade mark sign, U+00AE ISOnum
$content =~ s/¯/¯/go; # macron = spacing macron = overline = APL overbar, U+00AF ISOdia
$content =~ s/°/°/go; # degree sign, U+00B0 ISOnum
$content =~ s/±/±/go; # plus-minus sign = plus-or-minus sign, U+00B1 ISOnum
$content =~ s/²/²/go; # superscript two = superscript digit two = squared, U+00B2 ISOnum
$content =~ s/³/³/go; # superscript three = superscript digit three = cubed, U+00B3 ISOnum
$content =~ s/´/´/go; # acute accent = spacing acute, U+00B4 ISOdia
$content =~ s/µ/µ/go; # micro sign, U+00B5 ISOnum
$content =~ s/¶/¶/go; # pilcrow sign = paragraph sign, U+00B6 ISOnum
$content =~ s/·/·/go; # middle dot = Georgian comma = Greek middle dot, U+00B7 ISOnum
$content =~ s/¸/¸/go; # cedilla = spacing cedilla, U+00B8 ISOdia
$content =~ s/¹/¹/go; # superscript one = superscript digit one, U+00B9 ISOnum
$content =~ s/º/º/go; # masculine ordinal indicator, U+00BA ISOnum
$content =~ s/»/»/go; # right-pointing double angle quotation mark = right pointing guillemet, U+00BB ISOnum
$content =~ s/¼/¼/go; # vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum
$content =~ s/½/½/go; # vulgar fraction one half = fraction one half, U+00BD ISOnum
$content =~ s/¾/¾/go; # vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum
$content =~ s/¿/¿/go; # inverted question mark = turned question mark, U+00BF ISOnum
$content =~ s/À/À/go; # latin capital A with grave = latin capital A grave, U+00C0 ISOlat1
$content =~ s/Á/Á/go; # latin capital A with acute, U+00C1 ISOlat1
$content =~ s/Â/Â/go; # latin capital A with circumflex, U+00C2 ISOlat1
$content =~ s/Ã/Ã/go; # latin capital A with tilde, U+00C3 ISOlat1
$content =~ s/Ä/Ä/go; # latin capital A with diaeresis, U+00C4 ISOlat1
$content =~ s/Å/Å/go; # latin capital A with ring above = latin capital A ring, U+00C5 ISOlat1
$content =~ s/Æ/Æ/go; # latin capital AE = latin capital ligature AE, U+00C6 ISOlat1
$content =~ s/Ç/Ç/go; # latin capital C with cedilla, U+00C7 ISOlat1
$content =~ s/È/È/go; # latin capital E with grave, U+00C8 ISOlat1
$content =~ s/É/É/go; # latin capital E with acute, U+00C9 ISOlat1
$content =~ s/Ê/Ê/go; # latin capital E with circumflex, U+00CA ISOlat1
$content =~ s/Ë/Ë/go; # latin capital E with diaeresis, U+00CB ISOlat1
$content =~ s/Ì/Ì/go; # latin capital I with grave, U+00CC ISOlat1
$content =~ s/Í/Í/go; # latin capital I with acute, U+00CD ISOlat1
$content =~ s/Î/Î/go; # latin capital I with circumflex, U+00CE ISOlat1
$content =~ s/Ï/Ï/go; # latin capital I with diaeresis, U+00CF ISOlat1
$content =~ s/Ð/Ð/go; # latin capital ETH, U+00D0 ISOlat1
$content =~ s/Ñ/Ñ/go; # latin capital N with tilde, U+00D1 ISOlat1
$content =~ s/Ò/Ò/go; # latin capital O with grave, U+00D2 ISOlat1
$content =~ s/Ó/Ó/go; # latin capital O with acute, U+00D3 ISOlat1
$content =~ s/Ô/Ô/go; # latin capital O with circumflex, U+00D4 ISOlat1
$content =~ s/Õ/Õ/go; # latin capital O with tilde, U+00D5 ISOlat1
$content =~ s/Ö/Ö/go; # latin capital O with diaeresis, U+00D6 ISOlat1
$content =~ s/×/×/go; # multiplication sign, U+00D7 ISOnum
$content =~ s/Ø/Ø/go; # latin capital O with stroke = latin capital O slash, U+00D8 ISOlat1
$content =~ s/Ù/Ù/go; # latin capital U with grave, U+00D9 ISOlat1
$content =~ s/Ú/Ú/go; # latin capital U with acute, U+00DA ISOlat1
$content =~ s/Û/Û/go; # latin capital U with circumflex, U+00DB ISOlat1
$content =~ s/Ü/Ü/go; # latin capital U with diaeresis, U+00DC ISOlat1
$content =~ s/Ý/Ý/go; # latin capital Y with acute, U+00DD ISOlat1
$content =~ s/Þ/Þ/go; # latin capital THORN, U+00DE ISOlat1
$content =~ s/ß/ß/go; # latin small sharp s = ess-zed, U+00DF ISOlat1
$content =~ s/à/à/go; # latin small a with grave = latin small a grave, U+00E0 ISOlat1
$content =~ s/á/á/go; # latin small a with acute, U+00E1 ISOlat1
$content =~ s/â/â/go; # latin small a with circumflex, U+00E2 ISOlat1
$content =~ s/ã/ã/go; # latin small a with tilde, U+00E3 ISOlat1
$content =~ s/ä/ä/go; # latin small a with diaeresis, U+00E4 ISOlat1
$content =~ s/å/å/go; # latin small a with ring above = latin small a ring, U+00E5 ISOlat1
$content =~ s/æ/æ/go; # latin small ae = latin small ligature ae, U+00E6 ISOlat1
$content =~ s/ç/ç/go; # latin small c with cedilla, U+00E7 ISOlat1
$content =~ s/è/è/go; # latin small e with grave, U+00E8 ISOlat1
$content =~ s/é/é/go; # latin small e with acute, U+00E9 ISOlat1
$content =~ s/ê/ê/go; # latin small e with circumflex, U+00EA ISOlat1
$content =~ s/ë/ë/go; # latin small e with diaeresis, U+00EB ISOlat1
$content =~ s/ì/ì/go; # latin small i with grave, U+00EC ISOlat1
$content =~ s/í/í/go; # latin small i with acute, U+00ED ISOlat1
$content =~ s/î/î/go; # latin small i with circumflex, U+00EE ISOlat1
$content =~ s/ï/ï/go; # latin small i with diaeresis, U+00EF ISOlat1
$content =~ s/ð/ð/go; # latin small eth, U+00F0 ISOlat1
$content =~ s/ñ/ñ/go; # latin small n with tilde, U+00F1 ISOlat1
$content =~ s/ò/ò/go; # latin small o with grave, U+00F2 ISOlat1
$content =~ s/ó/ó/go; # latin small o with acute, U+00F3 ISOlat1
$content =~ s/ô/ô/go; # latin small o with circumflex, U+00F4 ISOlat1
$content =~ s/õ/õ/go; # latin small o with tilde, U+00F5 ISOlat1
$content =~ s/ö/ö/go; # latin small o with diaeresis, U+00F6 ISOlat1
$content =~ s/÷/÷/go; # division sign, U+00F7 ISOnum
$content =~ s/ø/ø/go; # latin small o with stroke, = latin small o slash, U+00F8 ISOlat1
$content =~ s/ù/ù/go; # latin small u with grave, U+00F9 ISOlat1
$content =~ s/ú/ú/go; # latin small u with acute, U+00FA ISOlat1
$content =~ s/û/û/go; # latin small u with circumflex, U+00FB ISOlat1
$content =~ s/ü/ü/go; # latin small u with diaeresis, U+00FC ISOlat1
$content =~ s/ý/ý/go; # latin small y with acute, U+00FD ISOlat1
$content =~ s/þ/þ/go; # latin small thorn with, U+00FE ISOlat1
$content =~ s/ÿ/ÿ/go; # latin small y with diaeresis, U+00FF ISOlat1
# end of xhtml-lat1.ent
# ISO 8859-1 Symbol Entities
# $content =~ s/ / /go; # non-breaking space
# Some Other Entities supported by HTML
$content =~ s/Œ/Œ/go; # capital ligature OE
$content =~ s/œ/œ/go; # small ligature oe
$content =~ s/Š/Š/go; # capital S with caron
$content =~ s/š/š/go; # small S with caron
$content =~ s/Ÿ/Ÿ/go; # capital Y with diaeres
$content =~ s/ˆ/ˆ/go; # modifier letter circumflex accent
$content =~ s/˜/˜/go; # small tilde
$content =~ s/ / /go; # en space
$content =~ s/ / /go; # em space
$content =~ s/ / /go; # thin space
$content =~ s/‌/‌/go; # zero width non-joiner
$content =~ s/‍/‍/go; # zero width joiner
$content =~ s/‎/‎/go; # left-to-right mark
$content =~ s/‏/‏/go; # right-to-left mark
$content =~ s/–/–/go; # en dash
$content =~ s/—/—/go; # em dash
$content =~ s/‘/‘/go; # left single quotation mark
$content =~ s/’/’/go; # right single quotation mark
$content =~ s/‚/‚/go; # single low-9 quotation mark
$content =~ s/“/“/go; # left double quotation mark
$content =~ s/”/”/go; # right double quotation mark
$content =~ s/„/„/go; # double low-9 quotation mark
$content =~ s/†/†/go; # dagger
$content =~ s/‡/‡/go; # double dagger
$content =~ s/…/…/go; # horizontal ellipsis
$content =~ s/‰/‰/go; # per mille
$content =~ s/‹/‹/go; # single left-pointing angle quotation
$content =~ s/›/›/go; # single right-pointing angle quotation
# $content =~ s///go;
# Other html entities
$content =~ s/√/√/go; # radical sign
# http://www.webmasterworld.com/forum21/11238.htm
# http://www.codingforums.com/showthread.php?threadid=30181
# http://www.fileformat.info/info/unicode/char/00ab/index.htm
$content =~ s/«/«/go; # left-pointing double angle quotation mark
# http://www.fileformat.info/info/unicode/char/00bb/index.htm
$content =~ s/»/»/go; # right-pointing double angle quotation mark
# http://en.wikipedia.org/wiki/Interpunct
$content =~ s/·/·/go; # interpunct, middle dot
# Greek Letters
# http://www.danshort.com/HTMLentities/index.php?w=greek
# http://www.w3.org/TR/xhtml-modularization/dtd_module_defs.html#a_xhtml_character_entities
$content =~ s/Α/Α/go; # greek capital letter alpha, U+0391
$content =~ s/Β/Β/go; # greek capital letter beta, U+0392
$content =~ s/Γ/Γ/go; # greek capital letter gamma, U+0393 ISOgrk3
$content =~ s/Δ/Δ/go; # greek capital letter delta, U+0394 ISOgrk3
$content =~ s/Ε/Ε/go; # greek capital letter epsilon, U+0395
$content =~ s/Ζ/Ζ/go; # greek capital letter zeta, U+0396
$content =~ s/Η/Η/go; # greek capital letter eta, U+0397
$content =~ s/Θ/Θ/go; # greek capital letter theta, U+0398 ISOgrk3
$content =~ s/Ι/Ι/go; # greek capital letter iota, U+0399
$content =~ s/Κ/Κ/go; # greek capital letter kappa, U+039A
$content =~ s/Λ/Λ/go; # greek capital letter lambda, U+039B ISOgrk3
$content =~ s/Μ/Μ/go; # greek capital letter mu, U+039C
$content =~ s/Ν/Ν/go; # greek capital letter nu, U+039D
$content =~ s/Ξ/Ξ/go; # greek capital letter xi, U+039E ISOgrk3
$content =~ s/Ο/Ο/go; # greek capital letter omicron, U+039F
$content =~ s/Π/Π/go; # greek capital letter pi, U+03A0 ISOgrk3
$content =~ s/Ρ/Ρ/go; # greek capital letter rho, U+03A1
# there is no Sigmaf, and no U+03A2 character either
$content =~ s/Σ/Σ/go; # greek capital letter sigma, U+03A3 ISOgrk3
$content =~ s/Τ/Τ/go; # greek capital letter tau, U+03A4
$content =~ s/Υ/Υ/go; # greek capital letter upsilon, U+03A5 ISOgrk3
$content =~ s/Φ/Φ/go; # greek capital letter phi, U+03A6 ISOgrk3
$content =~ s/Χ/Χ/go; # greek capital letter chi, U+03A7
$content =~ s/Ψ/Ψ/go; # greek capital letter psi, U+03A8 ISOgrk3
$content =~ s/Ω/Ω/go; # greek capital letter omega, U+03A9 ISOgrk3
$content =~ s/α/α/go; # greek small letter alpha, U+03B1 ISOgrk3
$content =~ s/β/β/go; # greek small letter beta, U+03B2 ISOgrk3
$content =~ s/γ/γ/go; # greek small letter gamma, U+03B3 ISOgrk3
$content =~ s/δ/δ/go; # greek small letter delta, U+03B4 ISOgrk3
$content =~ s/ε/ε/go; # greek small letter epsilon, U+03B5 ISOgrk3
$content =~ s/ζ/ζ/go; # greek small letter zeta, U+03B6 ISOgrk3
$content =~ s/η/η/go; # greek small letter eta, U+03B7 ISOgrk3
$content =~ s/θ/θ/go; # greek small letter theta, U+03B8 ISOgrk3
$content =~ s/ι/ι/go; # greek small letter iota, U+03B9 ISOgrk3
$content =~ s/κ/κ/go; # greek small letter kappa, U+03BA ISOgrk3
$content =~ s/λ/λ/go; # greek small letter lambda, U+03BB ISOgrk3
$content =~ s/μ/μ/go; # greek small letter mu, U+03BC ISOgrk3
$content =~ s/ν/ν/go; # greek small letter nu, U+03BD ISOgrk3
$content =~ s/ξ/ξ/go; # greek small letter xi, U+03BE ISOgrk3
$content =~ s/ο/ο/go; # greek small letter omicron, U+03BF NEW
$content =~ s/π/π/go; # greek small letter pi, U+03C0 ISOgrk3
$content =~ s/ρ/ρ/go; # greek small letter rho, U+03C1 ISOgrk3
$content =~ s/ς/ς/go; # greek small letter final sigma, U+03C2 ISOgrk3
$content =~ s/σ/σ/go; # greek small letter sigma, U+03C3 ISOgrk3
$content =~ s/τ/τ/go; # greek small letter tau, U+03C4 ISOgrk3
$content =~ s/υ/υ/go; # greek small letter upsilon, U+03C5 ISOgrk3
$content =~ s/φ/φ/go; # greek small letter phi, U+03C6 ISOgrk3
$content =~ s/χ/χ/go; # greek small letter chi, U+03C7 ISOgrk3
$content =~ s/ψ/ψ/go; # greek small letter psi, U+03C8 ISOgrk3
$content =~ s/ω/ω/go; # greek small letter omega, U+03C9 ISOgrk3
$content =~ s/ϑ/ϑ/go;# greek small letter theta symbol, U+03D1 NEW
$content =~ s/ϒ/ϒ/go; # greek upsilon with hook symbol, U+03D2 NEW
$content =~ s/ϖ/ϖ/go; # greek pi symbol, U+03D6 ISOgrk3
# Letterlike Symbols
$content =~ s/℘/℘/go; # script capital P = power set = Weierstrass p, U+2118 ISOamso
$content =~ s/ℑ/ℑ/go; # blackletter capital I = imaginary part, U+2111 ISOamso
$content =~ s/ℜ/ℜ/go; # blackletter capital R = real part symbol, U+211C ISOamso
$content =~ s/™/™/go; # trade mark sign, U+2122 ISOnum
$content =~ s/ℵ/ℵ/go; # alef symbol = first transfinite cardinal, U+2135 NEW
# alef symbol is NOT the same as hebrew letter alef, U+05D0 although
# the same glyph could be used to depict both characters
# lsaquo is proposed but not yet ISO standardized
$content =~ s/‹/‹/go; # single left-pointing angle quotation mark, U+2039 ISO proposed
# rsaquo is proposed but not yet ISO standardized
$content =~ s/›/›/go; # single right-pointing angle quotation mark, U+203A ISO proposed
$content =~ s/€/€/go; # euro sign, U+20AC NEW
# Miscellaneous Symbols
# http://www.w3.org/TR/xhtml-modularization/dtd_module_defs.html#a_xhtml_character_entities
$content =~ s/♠/♠/go; # black spade suit, U+2660 ISOpub
# black here seems to mean filled as opposed to hollow
$content =~ s/♣/♣/go; # black club suit = shamrock, U+2663 ISOpub
$content =~ s/♥/♥/go; # black heart suit = valentine, U+2665 ISOpub
$content =~ s/♦/♦/go; # black diamond suit, U+2666 ISOpub
# write_text_file('content.html', $content);
# tidy_xml_file('content.html');
write_text_file(sprintf('content-%d.xml', __LINE__), $content);
my $xml_parser = new XML::DOM::Parser;
my $doc = $xml_parser->parse($content);
# write_text_file(sprintf('fixed-%d.xml', __LINE__), $doc->toString());
# replace <p> to <para>
{
my $old_elems = xmldom_element_find($doc->getDocumentElement(), {
'tag_name' => 'p',
});
foreach my $old_elem (@$old_elems) {
my $new_elem = $doc->createElement('para');
xmldom_node_replace($old_elem, $new_elem);
$old_elem->dispose();
}
}
# replace <strong> to <emphasis>
{
my $old_elems = xmldom_element_find($doc->getDocumentElement(), {
'tag_name' => 'strong',
});
foreach my $old_elem (@$old_elems) {
my $new_elem = $doc->createElement('emphasis');
xmldom_node_replace($old_elem, $new_elem);
$old_elem->dispose();
}
}
# replace <a href="..."> to <ulink url="...">
{
my $old_elems = xmldom_element_find($doc->getDocumentElement(), {
'tag_name' => 'a',
'required_attributes' => [ 'href' ],
});
foreach my $old_elem (@$old_elems) {
my $new_elem = $doc->createElement('ulink');
$new_elem->setAttribute('url', $old_elem->getAttribute('href'));
xmldom_node_replace($old_elem, $new_elem);
$old_elem->dispose();
}
}
# replace <code class="inline_code"> to <code>
{
my $old_elems = xmldom_element_find($doc->getDocumentElement(), {
'tag_name' => 'code',
'required_classes' => [ 'inline_code' ],
});
foreach my $old_elem (@$old_elems) {
my $new_elem = $doc->createElement('code');
xmldom_node_replace($old_elem, $new_elem);
$old_elem->dispose();
}
}
# replace <span class="footnote"> to <footnote>
{
my $old_elems = xmldom_element_find($doc->getDocumentElement(), {
'tag_name' => 'span',
'required_classes' => [ 'footnote' ],
});
foreach my $old_elem (@$old_elems) {
my $new_elem = $doc->createElement('footnote');
xmldom_node_replace($old_elem, $new_elem);
$old_elem->dispose();
}
}
# replace <img ...> with <mediaobject><imageobject><imagedata>
{
my $old_elems = xmldom_element_find($doc->getDocumentElement(), {
'tag_name' => 'img',
});
foreach my $old_elem (@$old_elems) {
my $img_src = $old_elem->getAttribute('src');
my $img_title = $old_elem->getAttribute('title');
my $img_alt = $old_elem->getAttribute('alt');
my $mediaobject_elem = $doc->createElement('mediaobject');
my $imageobject_elem = $doc->createElement('imageobject');
$mediaobject_elem->appendChild($imageobject_elem);
if ($img_src) {
my $imagedata_elem = $doc->createElement('imagedata');
$imagedata_elem->setAttribute('fileref', $img_src);
$imageobject_elem->appendChild($imagedata_elem);
}
my $img_text = (defined($img_title) ? $img_title : $img_alt);
if ($img_text) {
my $textobject_elem = $doc->createElement('textobject');
my $phrase_elem = $doc->createElement('phrase');
$phrase_elem->appendChild($doc->createTextNode($img_text));
$textobject_elem->appendChild($phrase_elem);
$mediaobject_elem->appendChild($textobject_elem);
}
xmldom_node_replace($old_elem, $mediaobject_elem);
$old_elem->dispose();
}
}
# replace <pre class="code"> and <pre class="docbook-programlisting"> to <programlisting>
{
my $old_elems = xmldom_element_find($doc->getDocumentElement(), {
'tag_name' => 'pre',
'required_classes' => [ 'code' ],
});
foreach my $old_elem (@$old_elems) {
my $new_elem = $doc->createElement('programlisting');
xmldom_node_replace($old_elem, $new_elem);
$old_elem->dispose();
}
}
{
my $old_elems = xmldom_element_find($doc->getDocumentElement(), {
'tag_name' => 'pre',
'required_classes' => [ 'docbook-programlisting' ],
});
foreach my $old_elem (@$old_elems) {
my $new_elem = $doc->createElement('programlisting');
xmldom_node_replace($old_elem, $new_elem);
$old_elem->dispose();
}
}
# replace <pre class="terminal_screen"> and <pre class="docbook-screen"> to <screen>
{
my $old_elems = xmldom_element_find($doc->getDocumentElement(), {
'tag_name' => 'pre',
'required_classes' => [ 'terminal_screen' ],
});
foreach my $old_elem (@$old_elems) {
my $new_elem = $doc->createElement('screen');
xmldom_node_replace($old_elem, $new_elem);
$old_elem->dispose();
}
}
{
my $old_elems = xmldom_element_find($doc->getDocumentElement(), {
'tag_name' => 'pre',
'required_classes' => [ 'docbook-screen' ],
});
foreach my $old_elem (@$old_elems) {
my $new_elem = $doc->createElement('screen');
xmldom_node_replace($old_elem, $new_elem);
$old_elem->dispose();
}
}
# replace <pre> to <literallayout>
# NOTE: This must after processing other DocBook line-specific block
# elements that has semantic associated with.
{
my $old_elems = xmldom_element_find($doc->getDocumentElement(), {
'tag_name' => 'pre',
});
foreach my $old_elem (@$old_elems) {
my $new_elem = $doc->createElement('literallayout');
xmldom_node_replace($old_elem, $new_elem);
$old_elem->dispose();
}
}
# replace <br/> to "\n"
{
my $old_elems = xmldom_element_find($doc->getDocumentElement(), {
'tag_name' => 'br',
});
foreach my $old_elem (@$old_elems) {
# xmldom_node_remove($old_elem);
my $new_node = $doc->createTextNode("\n");
xmldom_node_replace($old_elem, $new_node);
$old_elem->dispose();
}
}
# replace <para> containing only whitespaces text nodes
{
my $has_empty_para = 0;
do {
$has_empty_para = 0;
my $elems = $doc->getDocumentElement()->getElementsByTagName('para');
foreach my $elem (@$elems) {
my $elem_text = '';
my $has_child_element = 0;
my $child_nodes = $elem->getChildNodes();
foreach my $child_node (@$child_nodes) {
if ($child_node->getNodeType() == XML::DOM::ELEMENT_NODE) {
$has_child_element = 1;
last; # foreach
}
elsif ($child_node->getNodeType() == XML::DOM::TEXT_NODE) {
$elem_text .= $child_node->getData();
}
}
if (!$has_child_element && ($elem_text =~ m/^\s+$/o)) {
xmldom_node_purge($elem);
$has_empty_para = 1;
last; # foreach
}
}
} while ($has_empty_para);
}
# replace <para><strong>#TEXT with <section><title>
# - step 1: replace <para><strong>#TEXT with <title>
{
my $repeat;
do {
$repeat = 0;
my $para_elems = $doc->getDocumentElement()->getElementsByTagName('para');
foreach my $para_elem (@$para_elems) {
my $emphasis_elem = xmldom_first_child_element($para_elem);
if ($emphasis_elem && ($emphasis_elem->getTagName() eq 'emphasis')) {
# print STDERR "[<para><emphasis>]";
if (xmldom_only_text_child_nodes($emphasis_elem)) {
# print STDERR "[Can insert <section><title>]";
xmldom_node_remove($emphasis_elem);
my $title_elem = $doc->createElement('title');
xmldom_node_replace($para_elem, $title_elem);
$repeat = 1;