-
Notifications
You must be signed in to change notification settings - Fork 1
/
reverse_xml.php
151 lines (121 loc) · 4.11 KB
/
reverse_xml.php
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
<?php
include("functions.php");
//load the rules text file
//this functin define in functions.php
$rules = get_the_rules("ottoman_arabic_rules.txt");
//get metadata information from the $_POST
$title = $_POST['title'];
if(!$title) $htitle = "untitled"; else $htitle = $title;
$author = $_POST['author'];
$date = $_POST['date'];
$responsibility = $_POST['responsibility'];
//check and see if a file was uploaded
if ($_FILES["file"]["error"] > 0) {
$file = false;
} else {
$file = $_FILES["file"]["tmp_name"];
}
//if a file was uploaded, append it to the text area content
if($file != false) {
$input = $_POST['area'] . file_get_contents($file);
} else {
$input = $_POST['area'];
}
//insert a leadind space
$input = " " . $input;
//count the number of tags like <stuff> and store them for later
preg_match_all('/<(.*?)>/', $input, $matches);
//replace all tags with a numbered tag like <1>
for($i = 0; $i < sizeof($matches[0]); $i++) {
#$input = preg_replace('/<(.*?)>/', "<>", $input);
$input = str_replace($matches[0][$i], "<".$i.">", $input);
}
//count the entity references and store them for later
preg_match_all('/&#(.*?);/', $input, $matches2);
//replace the entity references with bracketed number like [1]
for($i = 0; $i < sizeof($matches2[0]); $i++) {
$input = str_replace($matches2[0][$i], "[".$i."]", $input);
}
//remove extraneous space
$input = preg_replace('([[:space:]]+)', " ", $input);
//retrieve the keys from the rules file
$keys = array_keys($rules);
//iterate over the rules and apply them
foreach ($keys as $key) {
$input = str_replace($key, $rules[$key], $input);
}
//get the font size
$size = $_POST['size'];
//the markers for line group breaks and line breaks
$pbreak = strpos($input, "**");
$lbreak = strpos($input, "*");
//if there are both lines and line groups, put <p> around the line <div>
if($pbreak !== false && $lbreak !== false) {
$input = str_replace("**", "</l>\n</lg>\n<lg>\n<l>", $input);
}
//if just lines, put the line in a <div>
if($lbreak !== false) {
$input = str_replace("*", "</l>\n<l>", $input);
}
//replace the + with &# to correct the entity references
$input = str_replace("+", "&#", $input);
//put the stored tags back into place
for($i = 0; $i < sizeof($matches[0]); $i++) {
$input = str_replace("<".$i.">", $matches[0][$i], $input);
}
//put the stored entity references back into place
for($i = 0; $i < sizeof($matches2[0]); $i++) {
$input = str_replace("[".$i."]", $matches2[0][$i], $input);
}
//html stuff
echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
echo "<?xml-stylesheet type=\"text/css\" href=\"mystyle.css\" ?>\n";
echo "<!DOCTYPE TEI PUBLIC \"-//OTAP//DTD TEI Transitional//EN\" \"http://www.tei-c.org/release/xml/tei/custom/schema/dtd/tei_lite.dtd\">\n";
echo "<tei>\n<teiHeader>\n";
echo "<fileDesc>\n";
//header stuff with some fields taken from the Metadata fields
echo "<titleStmt>\n";
echo "<title>\n";
echo $title . "\n";
echo "</title>\n";
echo "<principal>\n";
echo $author . "\n";
echo "</principal>\n";
echo "</titleStmt>\n";
echo "<editionStmt>\n";
echo "</editionStmt>\n";
echo "<extent>\n";
echo "</extent>\n";
echo "<publicationStmt>\n";
echo "<authority>\n";
echo "OTAP\n";
echo "</authority>\n";
echo "<address>\n";
echo "Near Eastern Languages\n";
echo "</address>\n";
echo "</publicationStmt>\n";
echo "<sourceDesc>\n";
echo "<biblStruct>\n";
echo "</biblStruct>\n";
echo "<msDesc>\n";
echo "</msDesc>\n";
echo "</sourceDesc>\n";
echo "</fileDesc>\n";
echo "<encodingDesc>\n";
echo "Generated by the Revesible Transcription System.\n";
echo "</encodingDesc>\n";
echo "</teiHeader>\n";
echo "<text>\n<body>\n<div>\n";
//take care of the various possibilities of line or line group
if($prose !== false) { echo "<lg>\n"; }
if($verse !== false && $prose === false) { echo "<lg>\n"; }
if($verse === false && $prose === false) { echo "<lg>\n<l>"; }
if($verse !== false) { echo "<l>"; }
echo $input;
if($verse !== false) { echo "</l>\n"; }
if($verse !== false && $prose === false) { echo "</lg>\n"; }
if($verse === false && $prose === false) { echo "</l>\n</lg>\n"; }
if($prose !== false) { echo "</lg>\n"; }
//close up
echo "</div>\n</body>\n</text>\n</tei>";
?>