-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreplaceit_xml.inc.php
More file actions
147 lines (116 loc) · 4.74 KB
/
Copy pathreplaceit_xml.inc.php
File metadata and controls
147 lines (116 loc) · 4.74 KB
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
<?php
class replaceit_XML {
// What to look for:
static private $book = null;
static private $chapter = null;
static private $from_verse = 0;
static private $to_verse = 0;
// Collected:
static private $to_print = '';
// Auxiliary info:
static private $collect = ''; // Currently collected text
static private $in_chapter = false; // True if we are currently in a chapter
static private $collecting = 0; // 0 = not collecting, 1 = not collecting but in correct chapter, 2 = collecting
static public $refnum = 0; // Footnote number
static private function tagStart($parser, $tagname, $attributes = null) {
global $title;
switch ($tagname) {
case 'title':
self::$collect = '';
break;
case 'chapter':
if (isset($attributes['sID'])) {
$c = explode('.',$attributes['sID']);
self::$in_chapter = true;
if ($c[0]==self::$book && $c[1]==self::$chapter) {
self::$collecting = self::$from_verse==0 ? 2 : 1;
}
else
self::$collecting = 0;
}
else if (isset($attributes['eID'])) {
self::$in_chapter = false;
}
break;
case 'verse':
if (isset($attributes['sID'])) {
$v = explode('.',$attributes['sID']);
if (self::$collecting==1 && $v[2]==self::$from_verse) {
self::$collecting = 2;
self::$to_print .= "<div class=\"paragraph\">"; // Simulate <p>
}
if (self::$collecting==2) {
self::$to_print .= '<span class="verseno"><span class="chapno">'.$v[1].':</span>'.$v[2].'</span>';
}
self::$collect = '';
}
else if (isset($attributes['eID'])) {
$v = explode('.',$attributes['eID']);
if (self::$collecting==2) {
self::$to_print .= self::$collect."\n";
if ($v[2]==self::$to_verse) {
self::$collecting = 0;
self::$to_print .= "</div>"; // Simulate </p>
}
}
}
break;
case 'note':
if (self::$collecting==2) {
self::$collect .= "<span class=\"ref ref1\"><span class=\"refnum\" data-toggle=\"tooltip\" data-num=\""
. ++self::$refnum . "\" data-placement=\"bottom\" data-html=\"true\" title=\"";
}
break;
case 'p':
if (self::$collecting==2)
self::$to_print .= "<div class=\"paragraph\">";
break;
}
}
static private function tagEnd($parser, $tagname) {
switch ($tagname) {
case 'title':
if (self::$in_chapter) {
if (self::$collecting==2)
self::$to_print .= "<h2>" . self::$collect . "</h2>\n";
}
self::$collect = '';
break;
case 'p':
if (self::$collecting==2)
self::$to_print .= "</div>";
break;
case 'note':
if (self::$collecting==2) {
self::$collect .= "\"></span></span>";
}
break;
}
}
static private function tagContent($parser, string $content) {
self::$collect .= $content;
}
static public function replaceit($filename, $book, $chapter, $from_verse, $to_verse) {
self::$book = $book;
self::$chapter = $chapter;
self::$from_verse = $from_verse;
self::$to_verse = $to_verse;
$parser = xml_parser_create('UTF-8');
// xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
xml_set_element_handler($parser, 'replaceit_XML::tagStart', 'replaceit_XML::tagEnd');
xml_set_character_data_handler($parser, 'replaceit_XML::tagContent');
$xml=file_get_contents($filename);
$xml = str_replace("</note> ", "</note>", $xml);
xml_parse($parser, $xml);
// Replacements
$from = array();
$to = array();
$from[] = "/'/";
$to[] = "’";
$from[] = '/(\s)-(\s)/';
$to[] = '\1–\2';
return preg_replace($from,$to,self::$to_print);
}
}