-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathformat_sfm.inc.php
More file actions
547 lines (461 loc) · 20.6 KB
/
format_sfm.inc.php
File metadata and controls
547 lines (461 loc) · 20.6 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
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
<?php
class SfmTokenizer {
public function __construct(string $text) {
$this->text = $text;
$this->pos = 0;
}
// Get the next token
public function get_token(): string {
return $this->get_token2($this->pos);
}
// Retrieve the next token, but don't update the current position
public function peek_token(): string {
$pos = $this->pos;
return $this->get_token2($pos);
}
// Get the token at position $pos. Update $pos to next token.
private function get_token2(&$pos): string {
while (true) {
if ($pos==strlen($this->text))
return "";
// Use \G instead of ^ because the latter doesn't work with an offset!=0
// Look for \xx or \+xx or \xx* (we include \Z, which is a pseudo code added by this class
if (preg_match('/\G\\\\\\+?[Za-z0-9]+\*?/',$this->text,$matches,0,$pos)) {
$pos += strlen($matches[0]);
return $matches[0];
}
// Look for sequence of non-backslash and non-space characters
elseif (preg_match('/\G[^\\\\\s]+/',$this->text,$matches,0,$pos)) {
$pos += strlen($matches[0]);
return $matches[0];
}
// Look for spaces, but don't return them to caller
elseif (preg_match('/\G\s+/',$this->text,$matches,0,$pos)) {
$pos += strlen($matches[0]);
}
else
throw new ParserException("Error in SfmTokenizer. Text is '" . substr($this->text,$this->pos) . "'");
}
}
private $text;
private $pos;
}
class FormatSfm extends Formatter {
private $output = ''; // HTML string is generated here
private $synindent; // Syntactic indentation value
private $read_chapter; // Chapter number read from file
private $fh; // File handle for original language text
private $do_indent = false; // Indent the next paragraph
private function finish($building, string $buffer) {
switch ($building) {
case 'MT1':
$this->title = rtrim($buffer) . ", kapitel $this->read_chapter";
break;
case 'HEADER':
if (!$this->syntactic_layout) {
$this->output .= "<h2>$buffer</h2>\n";
$this->do_indent = false;
}
break;
case 'BLANK':
$this->output .= "<div class=\"paragraph\"> </div>\n";
break;
case 'PARAGRAPH':
if (!$this->do_indent) {
$this->output .= "<div class=\"paragraph paragraph1\">$buffer</div>\n";
$this->do_indent = true;
}
else
$this->output .= "<div class=\"paragraph\">$buffer</div>\n";
break;
case 'PARAGRAPHM':
$this->output .= "<div class=\"paragraph paragraphm\">$buffer</div>\n";
$this->do_indent = true;
break;
case 'POETRY1':
$this->output .= "<div class=\"poetry poetry1\">$buffer</div>\n";
break;
case 'POETRY2':
$this->output .= "<div class=\"poetry poetry2\">$buffer</div>\n";
break;
case 'SYNINDENT':
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
$this->output .= '<div class="textline"><div class="indented-number" data-indent="' . $this->synindent . '">'
. $this->synindent
. '<span style="color:transparent;font-size:0pt;">¤</span>'
. '</div><div class="indented-text" data-indent="' . $this->synindent . '">'
. $buffer
. '</div></div>'
.'<div class="textline"><div class="indented-number-blank"></div>'
. '<div class="indented-text" data-indent="' . $this->synindent . '"><span class="hebrew">'
. $orig_line
. '</span></div></div>';
}
else {
$this->output .= '<div class="textline"><div class="indented-number" data-indent="' . $this->synindent . '">'
. $this->synindent
. '<span style="color:transparent;font-size:0pt;">¤</span>'
. '</div><div class="indented-text" data-indent="' . $this->synindent . '">'
. $buffer
. '</div></div>';
}
break;
}
}
public function to_html() {
global $filetype;
$txt = file_get_contents('sfm/' . $filetype[$this->book]);
if (strstr($txt,"\"")!==false)
throw new ParserException("Double quotation mark in text");
// Remove following chapters
$chapter1 = $this->chapter+1;
$txt = preg_replace("/\\\\c +{$chapter1}[^0-9].*/s",'',$txt);
// Remove preceding chapters, if any
if ($this->chapter>1)
$txt = preg_replace("/\\\\c +1[^0-9].*(\\\\c +{$this->chapter}[^0-9].*)/s",'\1',$txt);
// Handle verse restriction
if ($this->from_verse>0) {
if (preg_match("/(\\\\v\s+$this->from_verse\s)/s",$txt)) {
if ($this->syntactic_layout)
$txt = preg_replace("/(\\\\c +{$this->chapter})\s(?:(?!\\\\v +$this->from_verse ).)*(\\\\zei +[0-9]+\\\\zei\\*).*(\\\\v +{$this->from_verse} )/s",'\1 \2 \3',$txt);
else
$txt = preg_replace("/(\\\\c +{$this->chapter})\s.*(\\\\v +{$this->from_verse} )/s",'\1 \p \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\s+$this->to_verse\s)/s",$txt)) {
if ($this->syntactic_layout)
$txt .= "\n\\b\n\\Z 0\n\\em [Vers $this->to_verse i dette kapitel findes ikke i Den Frie Bibel.]\\em*\n";
else
$txt .= "\n\\b\n\\m\n\\em [Vers $this->to_verse i dette kapitel findes ikke i Den Frie Bibel.]\\em*\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,$matches[0][1]);
// Remove trailing \zei...\zei*, \m, \p, and \q
$txt = preg_replace(['/(.*)(\\\\zei\s+[0-9]+\s*\\\\zei\\*)\s*$/s',
'/(.*)(\\\\[mpq][0-9]?)\s*$/s'],
['\1','\1'],
$txt);
// Remove a possible final heading
// Search for last \s or \m, \p, or \q. If it is \s, the previous verse ends in a heading.
if (preg_match('/.*\\\\([smpq])/s',$txt,$matches,PREG_OFFSET_CAPTURE,0) && $matches[1][0]=='s')
$txt = substr($txt,0,$matches[1][1]-1);
}
}
if ($this->syntactic_layout && $_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);
}
}
// Read credits
$creditfile = sprintf('sfm/%s%03d.cre', substr($filetype[$this->book],0,-4), $this->chapter);
if (file_exists($creditfile)) {
preg_match_all('/!!<(.*)>!!/',
file_get_contents($creditfile),
$meta_matches);
$this->credit = $meta_matches[1];
}
else
$this->credit = ["Ingen statusoplysninger."];
$this->format_credits();
// Substitutions:
$from[] = "/\u{FEFF}/"; // Byte Order Mark
$to[] = '';
$from[] = '/>>>/';
$to[] = '»›';
$from[] = '/<<</';
$to[] = '‹«';
$from[] = '/>>/';
$to[] = '»';
$from[] = '/<</';
$to[] = '«';
$from[] = '/>/';
$to[] = '›';
$from[] = '/</';
$to[] = '‹';
$from[] = '/\'/';
$to[] = '’';
$from[] = '/--/';
$to[] = '–';
$from[] = '/(\s)-(\s)/';
$to[] = '\1–\2';
$from[] = '/\.\.\./';
$to[] = '…';
$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'];
// Contract "\w aaa | bbb\w*" to "\w aaa|bbb\w*"
$from[] = '/\\\\w ([^\\|\\s]+)\\s*\\|\\s*(.*\\\\w\\*)/';
$to[] = '\\w \1|\2';
if ($this->syntactic_layout) {
$from[] = '/\\\\[pmq][0-9]*\s+/'; // Remove \p, \m, \q, and \q1, \q2...
$to[] = '';
$from[] = '/\\\\zei\s*([0-9]+)\s*\\\\zei\\*/'; // Remove \zei...\zei*
$to[] = '\\Z \1 ';
}
else {
$from[] = '/\\\\zei[ 0-9]+\\\\zei\\*/';
$to[] = '';
$from[] = '/(.)\s+:/'; // Remove space in front of colon
$to[] = '\1:';
}
$txt = preg_replace($from, $to, $txt);
// A state-event machine handles further processing
$tokenizer = new SfmTokenizer($txt);
$buffer = '';
$building = null;
$in_footnote = false;
// Markers:
while (($token = $tokenizer->get_token())!=='') {
switch ($token) {
// Paragraph starters
case '\id':
$this->finish($building,$buffer);
$building = 'ID';
$buffer = '';
break;
case '\toc':
case '\toc1':
$this->finish($building,$buffer);
$building = 'TOC1';
$buffer = '';
break;
case '\mt':
case '\mt1':
$this->finish($building,$buffer);
$building = 'MT1';
$buffer = '';
break;
case '\s':
$this->finish($building,$buffer);
$building = 'HEADER';
$buffer = '';
break;
case '\p':
$this->finish($building,$buffer);
$building = 'PARAGRAPH';
$buffer = '';
break;
case '\m':
$this->finish($building,$buffer);
$building = 'PARAGRAPHM';
$buffer = '';
break;
case '\b':
$this->finish($building,$buffer);
$building = 'BLANK';
$buffer = '';
break;
case '\q':
case '\q1':
$this->finish($building,$buffer);
$building = 'POETRY1';
$buffer = '';
break;
case '\q2':
$this->finish($building,$buffer);
$building = 'POETRY2';
$buffer = '';
break;
case '\Z': // Syntactic indent
$this->finish($building,$buffer);
$this->synindent = $tokenizer->get_token();
$building = 'SYNINDENT';
$buffer = '';
break;
case '\c':
$this->read_chapter = $tokenizer->get_token();
break;
// Modifiers
case '\v':
$verseno = $tokenizer->get_token();
$buffer .= " <span class=\"verseno\" data-verse=\"$verseno\">"
. "<span class=\"chapno\">$this->read_chapter:</span>$verseno</span>";
break;
case '\w': // Glossary
$word = $tokenizer->get_token();
if (!preg_match('/(.*)\|(.*)/',$word,$mat))
throw new ParserException("Badly formed glossary string");
$buffer .= "$mat[1]<a class=\"explain\" href=\"ordforklaring.php?ord=$mat[2]\">°</a>";
if ($tokenizer->get_token()!='\w*')
throw new ParserException("\w* not found");
$next_token = $tokenizer->peek_token();
if (!empty($next_token) && IntlChar::isalnum($next_token[0]))
$buffer .= ' ';
break;
case '\add':
if ($_SESSION['markadded']=='on')
$buffer .= '<span class="added">';
break;
case '\add*':
if ($_SESSION['markadded']=='on') {
if ($buffer[-1]==' ')
$buffer = rtrim($buffer) . '</span> ';
else
$buffer .= '</span>';
}
break;
case '\em': // Emphasis text
case '\+tl': // Transliterated text
if ($in_footnote)
$buffer .= '<i>';
else
$buffer .= '<i>';
break;
case '\em*': // Emphasis text
case '\+tl*': // Transliterated text ends
if ($in_footnote)
$buffer .= '</i>';
else
$buffer .= '</i>';
break;
case '\f': // Normal footnote
if ($in_footnote)
throw new ParserException('Footnote within footnote');
$buffer = rtrim($buffer) . '<span class="ref ref1"><span class="refnum" data-toggle="tooltip" data-num="'
. $this->nextnumber++
. '" data-placement="bottom" title="';
if ($tokenizer->get_token()!='+')
throw new ParserException('No + after \f');
$in_footnote = true;
$in_fq = false;
break;
case '\f*': // Normal footnote ends
if (!$in_footnote)
throw new ParserException('Misplaced \f*');
if ($in_fq) {
$buffer .= '</i>';
$in_fq = false;
}
$buffer = rtrim($buffer) . '" data-html="true"></span></span>';
$next_token = $tokenizer->peek_token();
if (!empty($next_token) && IntlChar::isalnum($next_token[0]))
$buffer .= ' ';
$in_footnote = false;
break;
case '\fe': // Exegetic footnote
if ($in_footnote)
throw new ParserException('Footnote within footnote');
$buffer = rtrim($buffer) . '<span class="ref refa"><span class="refnum" data-toggle="tooltip" data-let="'
. $this->nextletter++
. '" data-placement="bottom" title="';
if ($tokenizer->get_token()!='+')
throw new ParserException('No + after \\fe');
$in_footnote = true;
$in_fq = false;
break;
case '\fe*': // Exegetic footnote ends
if (!$in_footnote)
throw new ParserException('Misplaced \fe*');
if ($in_fq) {
$buffer .= '</i>';
$in_fq = false;
}
$buffer = rtrim($buffer) . '" data-html="true"></span></span>';
$next_token = $tokenizer->peek_token();
if (!empty($next_token) && IntlChar::isalnum($next_token[0]))
$buffer .= ' ';
$in_footnote = false;
break;
case '\fr': // Reference in footnote
if (!$in_footnote)
throw new ParserException('Misplaced \fr');
while (($tok = $tokenizer->peek_token())[0]!='\\')
$tokenizer->get_token(); // Ignored, for now
break;
case '\ft': // Start of footnote text
if (!$in_footnote)
throw new ParserException('Misplaced \ft');
if ($in_fq) {
$buffer = rtrim($buffer) . ':</i> ';
$in_fq = false;
}
break;
case '\fq': // Start footnote quote
if (!$in_footnote)
throw new ParserException('Misplaced \fq');
$in_fq = true;
$buffer .= '<i>';
break;
case '\x': // Start of cross reference
if ($tokenizer->get_token()!='+')
throw new ParserException('No + after \\x');
break;
case '\xo': // Source for reference
while (($tok = $tokenizer->peek_token())!='\xt' && $tok!='\x*')
$tokenizer->get_token(); // Ignored, for now
break;
case '\xt':
$target = '';
while (($tok = $tokenizer->get_token())!='\x*')
$target .= ' ' . $tok;
$dest = htmlspecialchars('<span class="reflinks">'
. formatref($target)
. '</span>');
$buffer = rtrim($buffer) . '<span class="refh" data-refs="' . $dest . '">*</span>';
$next_token = $tokenizer->peek_token();
if (!empty($next_token) && IntlChar::isalnum($next_token[0]))
$buffer .= ' ';
break;
default:
if ($token[0]=='\\') {
throw new ParserException("Unknown token '$token'");
die;
}
$buffer .= $token . ' ';
break;
}
}
$this->finish($building,$buffer);
return $this->output;
}
}