-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformat_text.inc.php
More file actions
475 lines (373 loc) · 17.1 KB
/
format_text.inc.php
File metadata and controls
475 lines (373 loc) · 17.1 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
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
<?php
class FormatText extends Formatter {
private $fh; // File handle for original language text
public function to_html() {
$txt = file_get_contents(sprintf('tekst/%s%03d.txt',$this->book,$this->chapter));
if (preg_match('/"/',$txt)) {
echo "<h1>Fejl</h1>\n";
echo "<p>Tekst indeholder dobbelt citationstegn.</p>\n";
die;
}
// Handle verse restriction
if ($this->from_verse>0) {
if (preg_match("/(v$this->from_verse )/s",$txt)) {
if ($this->syntactic_layout)
$txt = preg_replace("~(===[^=]+===)(?:(?!v$this->from_verse ).)*(//[0-9]+).*(v$this->from_verse )~s",
'\1\2\3',$txt);
else
$txt = preg_replace("/(===[^=]+===).*(v$this->from_verse )/s",'\1\2',$txt);
}
else {
global $title;
$this->title = $title[$this->book] . ", kapitel " . $this->chapter;
return "Vers $this->from_verse i dette kapitel findes ikke i Den Frie Bibel.";
}
}
if ($this->to_verse>0) {
if (!preg_match("/(v$this->to_verse )/s",$txt)) {
if ($this->syntactic_layout)
$txt .= "\n//0 //0 *[Vers $this->to_verse i dette kapitel findes ikke i Den Frie Bibel.]*\n";
else
$txt .= "\n***\n\n*[Vers $this->to_verse i dette kapitel findes ikke i Den Frie Bibel.]*\n";
}
// Find first verse > $this->to_verse
$matches=array();
$offset=0;
while ($found = preg_match('/v([0-9]+)/',$txt,$matches,PREG_OFFSET_CAPTURE,$offset)) {
$offset = $matches[1][1];
if (intval($matches[1][0])>intval($this->to_verse))
break;
}
if ($found) {
$txt = substr($txt,0,$offset-1);
// Remove trailing //#
$txt = preg_replace('|(.*)//[0-9]+\s*$|','\1', $txt);
// Remove a possible final heading
if (preg_match('/[^=]==[^=]+==\s*$/s',$txt,$matches,PREG_OFFSET_CAPTURE,0)) {
$txt = substr($txt,0,$matches[0][1]);
}
}
}
preg_match_all('/!!<(.*)>!!/',$txt,$meta_matches);
$this->credit = $meta_matches[1];
$this->format_credits();
// First collection of substitutions:
$from[] = "/\u{FEFF}/"; // Byte Order Mark
$to[] = '';
$from[] = '/!!<.*>!!/'; // Credits have been handled above
$to[] = '';
$from[] = '/>>>/';
$to[] = '»›';
$from[] = '/<<</';
$to[] = '‹«';
$from[] = '/>>/';
$to[] = '»';
$from[] = '/<</';
$to[] = '«';
$from[] = '/>/';
$to[] = '›';
$from[] = '/</';
$to[] = '‹';
$from[] = '/\'/';
$to[] = '’';
$from[] = '/^ *\*\*\* *$/m';
$to[] = ' ';
$from[] = '/\*([^\*]+)\*/';
$to[] = '<i>\1</i>';
$from[] = '~//([^\d])~';
$to[] = "//0";
$txt = preg_replace($from, $to, $txt);
// Handle footnotes in titles
if (preg_match('/===(.*)\s*{T: *([^}]+)}===/',$txt,$tit)) {
$this->title = $tit[1] . '<span class="ref ref1"><span class="refnumhead" data-toggle="tooltip" data-num="1" data-placement="bottom" title="' . $tit[2]. '" data-html="true"></span></span>';
++$this->nextnumber;
}
elseif (preg_match('/===(.*)\s*{E: *([^}]+)}===/',$txt,$tit)) {
$this->title = $tit[1] . '<span class="ref refa"><span class="refnumhead" data-toggle="tooltip" data-let="a" data-placement="bottom" title="' . $tit[2] . '" data-html="true">[a]</span></span>';
++$this->nextletter;
}
else {
preg_match('/===(.*)===/',$txt,$tit);
$this->title = $tit[1];
}
// Second collection of substitutions:
$from = array();
$to = array();
$from[] = '/===(.*)===/'; // Titles have been handled above
$to[] = '';
if ($this->syntactic_layout) {
$from[] = '/==(.*)==/';
$to[] = '';
$from[] = '/\n/';
$to[] = ' ';
}
else {
$from[] = '/^\s*==/m';
$to[] = "\n@";
$from[] = '/==\s*$/m';
$to[] = "@\n";
}
$from[] = '/\s*{E: *([^}]+)}/';
$to[] = '<span class="ref refa"><span class="refnum" data-toggle="tooltip" data-let="REFALET" data-placement="bottom" title="\1" data-html="true"></span></span>';
$from[] = '/\s*{T: *([^}]+)}/';
$to[] = '<span class="ref ref1"><span class="refnum" data-toggle="tooltip" data-num="REFANUM" data-placement="bottom" title="\1" data-html="true"></span></span>';
$from[] = '/\s*{K: *([^}]+)}/';
$to[] = '';
$from[] = '/\s*{N: *([^}]+)}/';
$to[] = '<a class="explain" href="ordforklaring.php?ord=\1">°</a>';
$from[] = '/JHVHs/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERRENS</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herrens';
else
$to[] = $_SESSION['godsname'].'s';
$from[] = '/JHVHvs/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERRES</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herres';
else
$to[] = $_SESSION['godsname'].'s';
$from[] = '/JHVHv/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERRE</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herre';
else
$to[] = $_SESSION['godsname'];
$from[] = '/JHVH/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERREN</small>';
else
$to[] = $_SESSION['godsname'];
$from[] = '/HERRENS/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERRENS</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herrens';
else
$to[] = $_SESSION['godsname'].'s';
$from[] = '/HERRES/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERRES</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herres';
else
$to[] = $_SESSION['godsname'].'s';
$from[] = '/HERREN/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERREN</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herren';
else
$to[] = $_SESSION['godsname'];
$from[] = '/HERRE/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERRE</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herre';
else
$to[] = $_SESSION['godsname'];
$from[] = '/([^a-z])[vV]([0-9]+)[\n ]*/';
$to[] = '\1<span class="verseno" data-verse="\2"><span class="chapno">'.$this->chapter.':</span>\2</span>';
if (!$this->syntactic_layout) {
// Remove indentation marker
$from[] = '~//\d+[\x20\xa0\x09]*\R~m'; // Prevent that marker without text causes subsequent line feed to be removed
$to[] = '';
$from[] = '~//\d+\s*~'; // Other markers are simply removed
$to[] = '';
$from[] = '/(.)\s+:/'; // Remove space in front of colon
$to[] = '\1:';
$from[] = '/\n\s*\n/';
$to[] = 'QQ';
$from[] = '/\n/';
$to[] = ' ';
$from[] = '/QQ/';
$to[] = "\n";
$from[] = '/^ *([^\n@]+) *$/m';
$to[] = '<div class="paragraph">\1</div>';
$from[] = '/@([^@]+)@/';
$to[] = '<h2>\1</h2>';
}
$from[] = '/\$([^\$]*)\$/';
// if ($_SESSION['markadded']=='on')
// $to[] = '<span class="added">\1</span>';
// else
$to[] = '\1';
$from[] = '/--/';
$to[] = '–';
$from[] = '/(\s)-(\s)/';
$to[] = '\1–\2';
$from[] = '/\.\.\./';
$to[] = '…';
$txt = preg_replace($from, $to, $txt);
if ($this->syntactic_layout) {
if ($_SESSION['include_orig_lang']=='on') {
$orig_file = sprintf('tekst/orig/%s%03d.txt',$this->book,$this->chapter);
($this->fh = @fopen($orig_file,'r')) || die("Kan ikke finde filen $orig_file");
fgets($this->fh); // Skip identification line
if ($this->from_verse>0) {
// Skip lines from original text file
do {
$pos = ftell($this->fh);
$orig_line = fgets($this->fh);
$verse = strstr($orig_line,":",true);
} while ($verse < $this->from_verse);
fseek($this->fh,$pos);
}
}
// (Regexp (.*?) matches the minimum number of arbitrary characters
// (?= indicates a lookahead condition).
$txt = preg_replace_callback('~//(\d+)\s*(.*?)\s*(?=//\d|$)~s', // A double slash,
// digits,
// optional spaces,
// the minimal possible number of characters,
// optional spaces,
// "//\d" or end-of-line
function($matches) {
if ($_SESSION['include_orig_lang']=='on') {
$orig_line = fgets($this->fh);
$orig_line = substr(strstr($orig_line,' '),1); // Strip verse and indent number and space
return '<div class="textline"><div class="indented-number" data-indent="' . $matches[1] . '">'
. $matches[1]
. '<span style="color:transparent;font-size:0pt;">¤</span>'
. '</div><div class="indented-text" data-indent="' . $matches[1] . '">'
. $matches[2]
. '</div></div>'
.'<div class="textline"><div class="indented-number-blank"></div>'
. '<div class="indented-text" data-indent="' . $matches[1] . '"><span class="hebrew">'
. $orig_line
. '</span></div></div>';
}
else
return '<div class="textline"><div class="indented-number" data-indent="' . $matches[1] . '">'
. $matches[1]
. '<span style="color:transparent;font-size:0pt;">¤</span>'
. '</div><div class="indented-text" data-indent="' . $matches[1] . '">'
. $matches[2]
. '</div></div>';
}, $txt);
}
// Generate references
$txt = preg_replace_callback('/\s*{H: *([^}]+)}/',
function ($matches) {
$target = $matches[1];
$dest = htmlspecialchars('<span class="reflinks">'
. formatref($matches[1])
. '</span>');
return '<span class="refh" data-refs="' . $dest . '">*</span>';
}, $txt);
// Generate footnote marks
$txt = preg_replace_callback('/REFALET/',
function ($matches) {
return $this->nextletter++;
}, $txt);
$txt = preg_replace_callback('/REFANUM/',
function ($matches) {
return $this->nextnumber++;
}, $txt);
return $txt;
}
public static function replaceit_ordforkl(string $filename) {
$txt = file_get_contents($filename);
if (str_ends_with($filename,'.txt')) {
if (preg_match('/"/',$txt)) {
echo "<h1>Fejl</h1>\n";
echo "<p>Tekst indeholder dobbelt citationstegn.</p>\n";
die;
}
$from[] = '/>>>/';
$to[] = '»›';
$from[] = '/<<</';
$to[] = '‹«';
$from[] = '/>>/';
$to[] = '»';
$from[] = '/<</';
$to[] = '«';
$from[] = '/>/';
$to[] = '›';
$from[] = '/</';
$to[] = '‹';
}
// If file does not end in ".txt", it is assumed to contain HTML, although the following
// replacements still take place
$from[] = '/\'/';
$to[] = '’';
$from[] = '/^ *\*\*\* *$/m';
$to[] = ' ';
$from[] = '/\$([^\*]+)\$/';
$to[] = '<b>\1</b>';
$from[] = '/\*([^\*]+)\*/';
$to[] = '<i>\1</i>';
$from[] = '/JHVHs/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERRENS</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herrens';
else
$to[] = $_SESSION['godsname'].'s';
$from[] = '/JHVHvs/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERRES</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herres';
else
$to[] = $_SESSION['godsname'].'s';
$from[] = '/JHVHv/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERRE</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herre';
else
$to[] = $_SESSION['godsname'];
$from[] = '/JHVH/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERREN</small>';
else
$to[] = $_SESSION['godsname'];
$from[] = '/HERRENS/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERRENS</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herrens';
else
$to[] = $_SESSION['godsname'].'s';
$from[] = '/HERRES/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERRES</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herres';
else
$to[] = $_SESSION['godsname'].'s';
$from[] = '/HERREN/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERREN</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herren';
else
$to[] = $_SESSION['godsname'];
$from[] = '/HERRE/';
if ($_SESSION['godsname']=='HERREN')
$to[] = 'H<small>ERRE</small>';
elseif ($_SESSION['godsname']=='Herren')
$to[] = 'Herre';
else
$to[] = $_SESSION['godsname'];
$from[] = '/--/';
$to[] = '–';
$from[] = '/(\s)-(\s)/';
$to[] = '\1–\2';
$from[] = '/\.\.\./';
$to[] = '…';
$txt = preg_replace($from, $to, $txt);
$txt = preg_replace_callback('/{H: *([^}]+)}/',
function ($matches) {
$refs = formatref($matches[1]);
if (str_ends_with($refs,'.')) // Strip final period
$refs = substr($refs,0,-1);
return $refs;
}, $txt);
return '<div class="explain">' . $txt . '</div>';
}
}