-
Notifications
You must be signed in to change notification settings - Fork 2
/
SyntaxLaw.pm
executable file
·1836 lines (1619 loc) · 67.5 KB
/
SyntaxLaw.pm
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
# vim: shiftwidth=4 tabstop=4 noexpandtab
package SyntaxLaw;
use Exporter;
our @ISA = qw(Exporter);
our $VERSION = "1.0";
our @EXPORT = qw(convert);
use v5.14;
no if ($]>=5.018), warnings => 'experimental';
use strict;
no strict 'refs';
# no warnings 'misc::replacement_list_length';
use utf8;
use English;
use Encode;
# use Roman;
use Time::HiRes 'time';
use Getopt::Long;
use Data::Dumper;
$Data::Dumper::Useperl = 1;
eval { require Roman; Roman->import(); };
my $use_roman = defined $Roman::VERSION;
use constant { true => 1, false => 0 };
my $do_expand = 0;
my $div_table = 0;
# \bו?כ?ש?מ?[בהל]?(חוק|פקוד[הת]|תקנות|צו|חלק|פרק|סימן(?: משנה|)|תוספו?ת|טופס|לוח)
our $pre_sig = 'ו?כ?ש?מ?[בהל]?-?';
our $extref_sig = $pre_sig . '(חוק(?:[ -]ה?יסוד:?|)|פקוד[הת]|תקנות(?: שעת[ -]ה?חי?רום)?|צו|דברי?[ -]ה?מלך|(?:החלט|הכרז|אכרז|הודע)(?:ה|ו?ת)|תקנון|הוראו?ת|הסכם|רשימת|מנשר|כללים?|נוהל|רי?שיון|קביעו?ת|נורמו?ת|דריש[הת]|חוק[הת]|אמנ[הת]|הנחי[יו]ת|קווים מנחים|אמות מידה|היתר|(?:כתבי? )?מי?נוי)';
our $date_sig = '(?:\,? ה?תש.?["״].[-–]\d{4}|, *[^ ]*\d{4}|, (חוק |פקודה |צו |)מס([\'׳]|פר) \d+ לשנת \d{4}| [-–] ה?תש.?["״]. מיום \d+.*|\, *\d+ עד \d+|\ \(\d{4}\)|\ [-–] \d{4})';
our $type_sig = $pre_sig . '(סעי(?:ף|פים)|תקנ(?:ה|ות)|אמו?ת[ -]ה?מידה|כלל|חלק|פרק|סימן(?: משנה|)|לוח(?:ות|) השוואה|נספח|תוספת|טופס|לוח|טבל[הא]|מפ(?:ה|ות)|الفصل)';
our $chp_sig = '\d+(?:[^ ,.:;"״\n\[\]()]{0,3}?\.|(?:\.\d+)+[א-י]?\.?)';
our $heb_num2 = '(?:[א-ט]|טו|טז|[יכלמנסעפצ][א-ט]?)';
our $heb_num3 = '(?:[א-ט]|טו|טז|[יכלמנסעפצ][א-ט]?|[קרש](?:טו|טז|[יכלמנסעפצ]?[א-ט]?))';
our $roman = '(?:[IVX]+|[ivx]+)';
our $EN = '[A-Za-z]';
our $HE = '(?:[א-ת][\x{05B0}-\x{05BD}]*+)';
our $nikkud = '[\x{05B0}-\x{05BD}]';
our $nochar = '(?:\'\'\'|\<\/?(?:[bis]|ins|del|qq)\>|\x00|[␚␡␛])*+';
our $nochar2 = '(?:\'\'\'|\<\/?(?:[bis]|ins|del|qq)\>|\x00|[␚␡␛])++';
# our $nochar = '(?:\(\(|\)\)|\'\'\'|\<\/?(?:[bis]|ins|del|qq)\>)*+';
sub main() {
GetOptions( "expand" => \$do_expand);
if ($#ARGV>=0) {
my $fin = $ARGV[0];
my $fout = $fin;
$fout =~ s/\.[^.]*$/.txt2/;
open(my $FIN,"<:utf8",$fin) || die "Cannot open file \"$fin\"!\n";
open(STDOUT, ">$fout") || die "Cannot open file \"$fout\"!\n";
local $/;
$_ = <$FIN>;
} else {
binmode STDIN, "utf8";
local $/;
$_ = <STDIN>;
}
binmode STDOUT, "utf8";
binmode STDERR, "utf8";
print convert($_);
exit;
}
sub convert {
local $_ = shift;
cleanup();
$div_table = 1 if (s/<מידע נוסף:? טבל(אות|א|ה) שקופ(ות|ה)>\n?//);
# Remove HTML comments
# s/<!--((?:(?!<!--|-->).)*)-->//sg;
s/<!--/⟪/g; s/-->/⟫/g;
s/\n( *⟪[^⟪⟫]*?⟫ *\n)+/\n/sg;
s/^ *(⟪[^⟪⟫]*?⟫ *)+//mg;
s/( *)⟪[^⟪⟫]*?⟫( *)/$1$2\x00/sg;
s/\x00( *\x00)++/\x00/g;
s/⟪/<!--/g; s/⟫/-->/g;
# Easier parsing using single characeter (⸨⸩⟪⟫⟦⟧⦃⦄ for various elelments
# s/<!--/⟪/g; s/-->/⟫/g;
# s/(?<!\()\(\(/⸨/g; s/\)\)(?!\))/⸩/g;
# s/(?<!\[)\[\[/⟦/g; s/\]\](?!\])/⟧/g;
# s/(?<!\{)\{\{/⦃/g; s/\}\}(?!\})/⦄/g;
# General cleanup
s/\r//g; # Unix style, no CR
s/[\t\xA0]/ /g; # tab and hardspace are whitespaces
s/^[ ]+//mg; # Remove redundant whitespaces
s/[ ]+$//mg; # Remove redundant whitespaces
s/$/\n/s; # add last linefeed
s/\n{3,}/\n\n/sg; # remove extra linefeeds
s/\n\n$/\n/sg; # Remove last linefeed
if (/[\x{202A}-\x{202E}]/) {
# Throw away BIDI characters if LRE/RLE/PDF exists
tr/\x{200E}\x{200F}\x{202A}-\x{202E}\x{2066}-\x{2069}//d;
}
# Replace vulgar fractions
s/([½⅓⅔¼¾⅕⅖⅗⅘⅙⅚⅐⅛⅜⅝⅞⅑⅒↉])(\d+)/$2$1/g;
$_ = s_lut($_, {
'½'=>'¹⁄₂', '⅓'=>'¹⁄₃', '⅔'=>'²⁄₃', '¼'=>'¹⁄₄', '¾'=>'³⁄₄', '⅕'=>'¹⁄₅', '⅖'=>'²⁄₅', '⅗'=>'³⁄₅', '⅘'=>'⁴⁄₅',
'⅙'=>'¹⁄₆', '⅚' =>'⁵⁄₆', '⅐'=>'¹⁄₇', '⅛'=>'¹⁄₈', '⅜'=>'³⁄₈', '⅝'=>'⁵⁄₈', '⅞'=>'⁷⁄₈', '⅑'=>'¹⁄₉', '⅒'=>'¹⁄₁₀', '↉'=>'⁰⁄₃'
});
# Convert Roman numerals
$_ = s_lut($_, {
'Ⅰ'=>'I', 'Ⅱ'=>'II', 'Ⅲ'=>'III', 'Ⅳ'=>'IV', 'Ⅴ'=>'V', 'Ⅵ'=>'VI', 'Ⅶ'=>'VII', 'Ⅷ'=>'VIII', 'Ⅸ'=>'IX', 'Ⅹ'=>'X', 'Ⅺ'=>'XI', 'Ⅻ'=>'XII',
'ⅰ'=>'i', 'ⅱ'=>'ii', 'ⅲ'=>'iii', 'ⅳ'=>'iv', 'ⅴ'=>'v', 'ⅵ'=>'vi', 'ⅶ'=>'vii', 'ⅷ'=>'viii', 'ⅸ'=>'ix', 'ⅹ'=>'x', 'ⅺ'=>'xi', 'ⅻ'=>'xii',
});
s/(?<=[0-9₀-₉])′/′/g; # Keep prime [feet/minutes], double prime [inch/seconds] and triple prime
s/(?<=[0-9₀-₉])″/″/g; # (will be restored later by unescape_text)
s/(?<=[0-9₀-₉])‴/‴/g; #
# s/[»«⌸]/"&#".ord($1).";"/ge; # Escape special markers
# Replace subscript/superscript numerals
s/([₀-₉₊₋₌₍₎]+)/'<sub>' . $1=~tr|₀-₉₊₋₌₍₎|0-9+\-=()|r . '<\/sub>'/ge;
s/([⁰¹²³⁴-⁹⁺⁻⁼⁽⁾]+)/'<sup>' . $1=~tr|⁰¹²³⁴-⁹⁺⁻⁼⁽⁾|0-9+\-=()|r . '<\/sup>'/ge;
s/(\[\[(?:קובץ|תמונה|[Ff]ile|[Ii]mage):.*?\]\])/ $1 =~ tr| |_|r =~ s|(["'–\-])|"&#".ord($1).";"|gre /ge;
s/($HE['׳]?(?:\(\()?)–(?=(?:\)\))?$HE|\d)/$1--/g; # Keep en-dash between Hebrew words
s/(\d$HE*)–(?=\d)/$1--/g; # Keep en-dash between numerals
tr/\x{FEFF}//d; # Unicode marker
tr/\x{2000}-\x{200A}\x{202F}\x{205F}\x{2060}/ /; # Typographic spaces
tr/\x{200B}-\x{200D}\xAD//d; # Remove zero-width spaces and soft-hyphen
tr/־–—‒―/-/; # typographic dashes
tr/\x96\x97/-/; # more typographic dashes
tr/‑/–/; # typographic en-dash
tr/״”“„‟″‶/"/; # typographic double quotes
tr/`׳’‘‚‛′‵/'/; # typographic single quotes
s/(?<=[ \n\x00])-{2,4}(?=[ \n\x00])/—/g; # en-dash
s/[ ]{2,}/ /g; # remove extra spaces
s/…/.../g;
s/\n+(=[^\n]*=)\n+/\n\n$1\n\n/g;
s/(?<!\n)\n(?=@)/\n\n/gm;
s/(?<![=\n])\n(\=)/\n\n/gm;
# s/(\d+(?:[.,]\d+)*(?:̸[23];)?) ([×\\\/—+−±]) (?=\d+(?:[.,]\d+)*)/$1\x{202F}$2\x{202F}/g;
# Unescape HTML characters
$_ = unescape_text($_);
$_ = process_diffs($_) if /<(ins|del)>/;
s/(?<=\<ויקי\>)\s*(.*?)\s*(?=\<\/(ויקי)?\>)/&escape_text($1)/egs;
s/\[\[קטגוריה:.*?\]\] *\n?//g; # Ignore categories (for now)
s/(?<!\[)\[(https?:\/\/[^ |\[\]\n]++) ([^|\[\]\n]++)\](?!\])/[[$1|$2]]/g;
s/(?<!\[)\[(https?:\/\/[^ |\[\]\n]++)\](?!\])/[[$1]]/g;
s/\[\[([ws]:)([^\[\]\|]*)\|\]\]/[[$1$2|$2]]/g;
s/(\[\[[ws]:.*?(?:\||\]\]))/ $1 =~ tr| |_|r /ge;
s/(\[\[(?:קובץ|תמונה|[Ff]ile|[Ii]mage):.*?\]\])/ $1 =~ tr| |_|r =~ s|(["'–\-])|"&#".ord($1).";"|gre /ge;
s/([ :]\x00?)-(\x00?[ \n])/$1–$2/g;
s/(?<=[ |])([A-Za-z0-9+−\-±,.:()\[\]\'′″‴{}<>\/α-ε]{20,})(?=[:;]|\s|$)/&wbr_chemicals($1)/ge;
# Replace with “Smart quotes”
$_ = convert_quotes($_);
# Parse wikitables
$_ = parse_wikitable($_) if /\{\|/;
# s/(\{\|(?:(?R)|.*?)*\n\|\}) *\n?/&parse_wikitable($1)/egs;
# [---] as span float left
unless ($div_table) {
s/ \[(?:—|-{2,4})\] *([^\n]*) *$/ <span style="float: left;">$1<\/span><div style="clear: left;"><\/div>/gm;
s/ \[(?:—|-{2,4})\] *([^\n]*) *$/ <span style="float: left;">$1<\/span><div style="clear: left;"><\/div>/gm;
s/ \[(?:—|-{2,4})\] */ /gm;
}
# Parse various elements
s/^(?|<שם> *\n?(.*)|=([^=].*)=)\n*/&parse_title($1)/em; # Once!
s/<שם[^>\n]*> .*\n*//g;
s/<מאגר (\d*) תיקון (.*?)>\n?/<מאגר מזהה="$1" תיקון="$2"\/>\n/;
s/<מאגר תיקון (.*?)>\n?/<מאגר מזהה="0" תיקון="$1"\/>\n/;
s/<מאגר (\d*)>\n?/<מאגר מזהה="$1" תיקון="0"\/>\n/;
s/^<פרסום> *\n?(.*)\n/&parse_pubdate($1)/egm;
s/^<חתימ(?:ות|ה)> *\n?(((\*.*\n)+)|(.*\n))/&parse_signatures($1)/egm;
s/^<מקור> *\n?(.*)\n/\n<מקור>\n$1\n<\/מקור>\n/m;
s/^<(?:מבוא|הקדמה)> *\n?(.*)\n/<הקדמה>\n$1\n<\/הקדמה>\n\n/m;
s/^<סיום> *\n?(.*)\n/<מפריד\/>\n<הקדמה>\n$1\n<\/הקדמה>\n\n/m;
s/^(-{3,}|—)$/<מפריד\/>/gm;
s/(<(?:td|th)[^>]*>|\|+)([\x00 ]*)(:)/$1$2␊\n$3/g;
s/(<\/(?:td|th)[^>]*>)/␊\n$1/g;
# Parse structured elements
s/^(=+)(.*?)\1\n+/&parse_section(length($1),$2)/egm;
s/^<סעיף *(.*?)>(.*?)\n/&parse_chapter($1,$2,"סעיף")/egm;
s/^(@.*?) +($nochar:+?[-–]?$nochar .*)$/$1\n$2/gm;
s/^(?|($nochar2)(:+[–-]?)($nochar)\ |()(:+[–-]?)($nochar2))/$2 $1$3/gm;
s/^@ *($nochar\(תיקון: .*?)\n/&parse_chapter("",$1,"סעיף*")/egm;
s/^@ *($nochar\d\S*) *\n/&parse_chapter($1,"","סעיף")/egm;
s/^@ *($nochar$chp_sig) +(.*?)\n/&parse_chapter($1,$2,"סעיף")/egm;
s/^@ *($nochar\d[^ .]*\.$nochar) *(.*?)\n/&parse_chapter($1,$2,"סעיף")/egm;
s/^@ *($nochar[^ \n.]+\.$nochar) *(.*?)\n/&parse_chapter($1,$2,"סעיף")/egm;
s/^@ *($nochar\([^()]*?\)$nochar) *(.*?)\n/&parse_chapter($1,$2,"סעיף*")/egm;
s/^@ *(.*?)\n/&parse_chapter("",$1,"סעיף*")/egm;
# s/^(:+) *(\([^( ]+\)|[א-י]\. |\d+\. |[^ <>]* \(\(\)\)) *(\([^( ]{1,2}\)|[א-י]\. |[^ <>]* \(\(\)\)) *(\([^( ]{1,2}\)|[א-י]\. |[^ |]* (?:[-–] )?\(\(\)\))/$1 $2\n$1: $3\n$1:: $4/gm;
# s/^(:+) *(\([^( ]+\)|[א-י]\. |\d+\. |[^ <>]* \(\(\)\)) *(\([^( ]{1,2}\)|[א-י]\. |[^ <>]* (?:[-–] )?\(\(\)\))/$1 $2\n$1: $3/gm;
s/^(:+) *(\([^( ]+\)|[א-י]\. |\d+\. ) *(\([^( ]{1,2}\)|[א-י]\. ) *(\([^( ]{1,2}\)|[א-י]\. )/$1 $2\n$1: $3\n$1:: $4/gm;
s/^(:+) *(\([^( ]+\)|[א-י]\. |\d+\. ) *(\([^( ]{1,2}\)|[א-י]\. )/$1 $2\n$1: $3/gm;
s/^:+-? *(␊?)$/$1/gm;
# s/^(:+) *("?\([^( ]+\)|\[[^[ ]+\]|\d[^ .]*\.|)(?| +(.*?)|([-–].*?)|())\n/&parse_line($1,$2,$3)/egm;
s/^(:+[-–]?+) *($nochar["”“]?(?:(?:\([^( \n]+\)|\[[A-Za-z0-9א-ת][^\[\] \n]*\]|[A-Za-z0-9א-ת.]*\)? *\(\(\.?\)\)|\(\(\(\d+.?\)\)\)|\(\(\([א-י]\d?\)\)\)|\d+(?:\.\d+)+\.?|\d[^ .\n]*\.|$heb_num2\d?\.|[A-Za-z]\.|$roman\.?|[•■□-◿*❏☒√✔☑✅☐\-–]|<sup>[0-9א-ת]{1,2}<\/sup>|\( \)|\[ \])\*?|(?:[^ \n]+ |)(?:[-–] |)\(\(\)\)|)$nochar)(?| +(.*?)|(␊?))\n/&parse_line($1,$2,$3)/egm;
# Move container tags if needed
my $barrier = '<\/?(?:מקור|הקדמה|ת+|קטע|סעיף|חתימות|מידע נוסף|td|tr|table)|__TOC__|$';
s/(\n?<\/(?:הקדמה|מקור)>)(.*?)(?=\s*($barrier))/$2$1/sg;
s/(\n?<\/ת+>)(.*?)(?=\s*(<מפריד.*?>\s*)?($barrier))/$2$1/sg;
# Add <סעיף> marker after <קטע> if not found
s/(<\/קטע.*?>\s*+)(?!<(קטע|סעיף|חתימות))/$1<סעיף><\/סעיף>\n/g;
s/␊([^\n]*?)\n/$1/g;
s/␊//g;
# s/(<(qq|ins|del)>.*?<\/\2>)/&parse_spans($2,$1)/egs;
# Parse links and remarks
s/(\[\[[ws]:.*?\]\])/ $1 =~ tr|_| |r /ge;
s/\[\[(?:קובץ:|תמונה:|[Ff]ile:|[Ii]mage:)(.*?)\]\]/"<תמונה>" . (unescape_text($1) =~ tr|_| |r) . "<\/תמונה>"/gem;
# s/\[\[(?:קובץ:|תמונה:|[Ff]ile:|[Ii]mage:)(.*?)\]\]/"<תמונה>" . $1 =~ tr|_| |r =~ s|&#(\d+);|chr($1)|gre . "<\/תמונה>"/gem;
# s/(?<!\[)(\[\[(?:(?!\]\]|\[\[).)*\]\]\]?(?:(?:,|או|) \[\[(?:(?!\]\]|\[\[).)*\]\]\]?)++)/&mark_linkset($1)/egm;
s/(?<!\[)\[\[((?:(?!\]\]|\[\[).)*?\]?)\|((?:(?!\]\]|\[\[).)*)\]\](\]?)/&parse_link($1,"$2$3")/egm;
s/(?<!\[)\[\[((?:(?!\]\]|\[\[).)*)\]\](\]?)/&parse_link('',"$1$2")/egm;
# s/(?<!\()(\(\(([^\n]*?)\)\)([^(\n]*?\)\))?)(?!\))/&parse_remark($1)/egs;
s/(?<!\()(\(\(([^()\n]++|(?R)|[()])*?\)\)([^(\n]*?\)\))?)(?!\))/&parse_remark($1)/egs;
# Parse file linearly, constructing all ankors and links
$_ = linear_parser($_);
s/\n+<סעיף><\/סעיף>\n+(?=(\<מקור|\<מאגר|$))/\n\n/sg;
s/^(<(?:שם|קטע)[^\n]+)\n\n(?=\<קטע)/$1\n/mg;
s/__TOC__/&insert_TOC()/e;
s/ *__NOTOC__//g;
s/ *__NOSUB__//g;
s/(?<=\<ויקי\>)\s*(.*?)\s*(\<\/(ויקי)?\>)/&unescape_text($1) . "<\/ויקי>"/egs;
# s/\<תמונה\>\s*(.*?)\s*\<\/(תמונה)?\>/&unescape_text($1)/egs;
s/<לוח_השוואה>\s*(.*?)<\/(לוח_השוואה|)>\n?/&parse_comparetable($1)/egs;
s/(\<math.*?\>.*?\<\/math\>)/&fix_tags($1)/egs;
s/(<(?:div|span|table|td|th|tr|מידע נוסף) [^>]+>)/&fix_tags($1)/egs;
s/( ["”“]?(?:[A-Za-zא-ת0-9]{3,}[\\\/]){2,}[A-Za-zא-ת0-9]{3,}[,;."”“]?(?: |\n))/ $1 =~ s|(?<=[\\\/])|<wbr>|gr /eg;
# Use thin spaces in dotted lines
# s/(\.{21,})/'<span style="word-break: break-all;">' . '. 'x(length($1)-1) . '.<\/span>'/ge;
s/(\.{21,})/'. ' x 10 . ('<wbr>' . '. ' x 10) x (int((length($1)-20)\/10)) . '<wbr>' . '. ' x (length($1) % 10) . (length($1) % 10 ? '<wbr>' : '') . '. 'x9 . '.'/ge;
s/(\.{4,20})/'. ' x (length($1)-1) . '.'/ge;
# Styling for long underscore spacers
s/(_{3,})/<span style="font-family: Arial; font-size: 80%;">$1<\/span>/g;
# Use Arial font for fraction slash (U+2044) [bug of ALEF font]
s/⁄/<span style="font-family: Arial;">⁄<\/span>/g;
# Compact form for comments mark
s/(\*{2,})/<span style="letter-spacing: -2pt; padding-inline-end: 2pt;">$1<\/span>/g;
# LTR wrapper for "300.-" and similar
s/ (?|[-–]\.([0-9]+)|\x{200E}([0-9]+)\.[-–]\x{200E}) / <span dir="ltr">$1.-<\/span> /g;
# This syntax will be used to link booklets, but not supported yet.
s/(\d+) \[(\d{4,5})\]/$1/g;
if ($do_expand) {
# Replace "=" within templates with {{=}}
s/(\{\{(?:[^{}\n]++|(?R))*\}\})/ &escape_eqsign($1) /eg;
$_ = expand_templates($_);
}
s/␚([^␚␡␛]*)␛/<ins>$1<\/ins>/g;
s/␡([^␚␡␛]*)␛/<del>$1<\/del>/g;
s/\x00//g; # Remove nulls
s/\n{3,}/\n\n/g;
s/ {2,}/ /g;
cleanup();
return $_;
}
# Allow usage as a module and as a executable script
__PACKAGE__->main() unless (caller);
######################################################################
sub process_diffs {
local $_ = shift;
my $c = '[^␚␡␛]'; my $d = '[0-9A-Za-zא-ת;:\-%,.]';
s/<ins>/␚/g;
s/<del>/␡/g;
s/<\/(ins|del)>/␛/g;
# my @str = split(/([␚␡␛]|<!--|-->)/, $_);
# my $state = 0;
# foreach (@str) {
# if ($state<10) {
# when (/␛/) { state = 0; }
# when (/␚/) { state = 1; }
# when (/␡/) { state = 2; }
# when (/<!--/) { state += 10; }
# } else {
# }
# }
# $_ = join(@str);
while (s/([␚␡])($c+)($c*)(␛)\2/$2$1$3$2$4/g) {};
s/([␚␡])($c+) (␛)/$1$2$3 /g;
s/␚($c+)␛␡\1␛/$1/g;
s/␡($c+)␛␚\1␛/$1/g;
s/␚($c+)␛␡\1($c+)␛/$1␡$2␛/g;
s/␡($c+)␛␚\1($c+)␛/$1␚$2␛/g;
s/([␚␡])([.,;:\-])\n($c+)(␛)([.,;:\-])\n/$2\n$1$3$5$4\n/g;
# ␡57␛␚2003␛8␡858␛␚99␛ => ␡578␛␚20038␛␡858␛␚99␛ => ␡578␛␡858␛␚20038␛␚99␛ => ␡578858␛␚2003899␛
while (s/([␡␚]$d+␛)($d+)/$1␡$2␛␚$2␛/g || s/($d+)([␡␚]$d+␛)/␡$1␛␚$1␛$2/g || s/[␚␡]␛//g || s/(␚$d+␛)(␡$d+␛)/$2$1/g || s/([␚␡])($c+)␛\1($c+)(␛)/$1$2$3$4/g ) {};
s/(␡$c+␛)(␚$c+␛)/$1 $2/g;
s/([\n ]+)(␛)/$2$1/g;
s/^([␚␡])(@|:+[-–]?+) *($nochar["”“]?(?:\([^( ]+\)|\[[A-Za-z0-9א-ת][^\[\] ]*\]|[A-Za-z0-9א-ת.]*\)? *\(\(\.?\)\)|\(\(\(\d+.?\)\)\)|\(\(\([א-י]\d?\)\)\)|\d+(?:\.\d+)+|\d[^ .]*\.|$heb_num2\d?\.|$roman\.?|[•■□-◿*❏☒√✔☑✅☐\-–]|<sup>[0-9א-ת]{1,2}<\/sup>|\( \)|\[ \]|[^ ]+ (?:[-–] )?\(\(\)\)|)$nochar)(?| +(.*?)|(␊?))\n/$2 $1$3␛ $1$4\n/gm;
while (s/^([␚␡])([\@:]$c*)\n/$1$2␛\n$1/gm) {};
s/^([␚␡])(@|:+[-–]?+) (\($d+\)|$d\.) ($c*)/$2 $1$3␛ $1$4/gm;
s/^([␚␡])(@|:+[-–]?+) ($c*)/$2 $1$3/gm;
s/^([␚␡])(=+ *)($c*?)( *=+)(␛)$/$2$1$3$5$4/gm;
s/\n{3,}/\n\n/g;
# s/␚($c*)␛/<ins>$1<\/ins>/g;
# s/␡($c*)␛/<del>$1<\/del>/g;
return $_;
}
######################################################################
sub parse_title {
local $_ = shift;
my ($fix, $str);
$_ = unquote($_);
($_, $fix) = get_fixstr($_);
$str = "<שם>";
$str .= "<תיקון>$fix</תיקון>\n" if ($fix);
$str .= "$_</שם>\n\n";
return $str;
}
sub parse_section {
my $level = shift;
local $_ = shift;
my ($fix, $extra, $str, $ankor);
$level = 2 unless defined $level;
$_ = unquote($_);
($_, $fix) = get_fixstr($_);
($_, $extra) = get_extrastr($_);
($_, $ankor) = get_ankor($_);
$str = $_;
# print STDERR "parse_section with |$_|\n";
s/\(\([^()]*?\[\[[^()]*?\)\)//g;
s/(?|\(\(\(([^()]*?)\)\)\)|\(\(([^()]*?)\)\))/$1/g;
if ($ankor) {
($ankor,undef) = find_href($ankor);
} elsif (/^\((.*?)\)$/) {
$ankor = '';
} elsif (/^(?|(.+?)(?: *:| +[-])|($type_sig *(?:[^ (]+( +|$)){0,2}))/) {
($ankor,undef) = find_href($1);
$ankor = '' if ($ankor =~ /סעיף/);
} else {
$ankor = '';
}
my $str2 = $str;
$str = "<קטע";
$str .= " דרגה=\"$level\"" if ($level);
$str .= " עוגן=\"$ankor\"" if ($ankor);
$str .= ">";
$str .= "<תיקון>$fix</תיקון>" if ($fix);
$str .= "<אחר>[$extra]</אחר>" if ($extra);
$str .= $str2;
$str .= "</קטע>\n\n";
return $str;
}
sub parse_chapter {
my ($num, $desc, $type) = @_;
my ($id, $fix, $extra, $ankor);
$desc = unquote($desc);
($desc, $fix) = get_fixstr($desc);
($desc, $extra) = get_extrastr($desc);
($desc, $ankor) = get_ankor($desc);
if ($num eq '' && $ankor eq '' && $desc =~ /^(סעיף|תקנה|פרט|المادة) (\d+[^ .:\-]*)(?::| [-–]|$)/) {
$num = $2;
$type =~ s/\*$//;
}
$id = get_numeral($num);
$desc =~ s/(?<=[־–])(?! |$)/<wbr>/g;
$desc =~ s/(?<=[ |].)<wbr>//g;
$extra =~ s/(?=\()/<wbr>/g if ($extra);
# [Don't] Use Roman symbols for numerals
# $num = roman_ligatures($num);
$type =~ s/\*$//;
$ankor = ((!$ankor && $num) ? $id : $ankor);
my $str = "<$type";
$str .= " עוגן=\"$ankor\"" if ($ankor);
$str .= ">";
$str .= "<מספר>$num</מספר>" if ($num);
$str .= "<תיאור>$desc</תיאור>" if ($desc);
$str .= "<תיקון>$fix</תיקון>" if ($fix);
$str .= "<אחר>[$extra]</אחר>" if ($extra);
$str .= "</$type>\n";
return $str;
}
sub parse_line {
my ($type,$num,$line) = @_;
$type =~ /(:+)([-–]?)/;
my $len = length($1);
my $def = length($2)>0;
my $id;
$num =~ s/"/"/g;
$num =~ s/ *\(\(\)\)$//;
$num =~ s/(?|(\[) (\])|(\() (\)))/$1\x{2002}$2/; # ensp
if (($num =~ /'''/)%2) {
$num =~ s/$/'''/;
$num = '' if ($num =~ /^''' *'''$/);
$line =~ s/^ */'''/;
}
$id = unparent($num);
$id = '' if $num =~ /\(\(.*\)\)/;
$len++ if ($num);
$type = "ת" x $len;
$line =~ s/^ *(.*?) *$/$1/;
if ($div_table) {
my $local_align = ($line =~ /\[(?:—|-{2,4})\] .*\<u\>/);
$line =~ s/(?|(.*) |^())\[(?:—|-{2,4})\] ?(.*) ?\[(?:—|-{2,4})\] ?(.*)$/{{טור לשמאל|$1|$2|$3}}/m;
$line =~ s/(?|(.*) |^())\[(?:—|-{2,4})\] ?(.*)$/{{טור לשמאל|$1|$2}}/m;
$line =~ s/(\{\{טור לשמאל)(?=\|)/$1|יישור=מרכז/m if $local_align;
}
my $str;
$str = "<$type";
$str .= " מספר=\"$id\"" if ($id);
$str .= " סוג=\"הגדרה\"" if ($def);
$str .= ">";
$str .= "<מספר>$num</מספר> " if ($num);
$str .= "$line" if (length($line)>0);
$str .= "</$type>\n";
return $str;
}
sub parse_link {
my ($helper,$txt) = @_;
my $str;
my $pre = ''; my $post = '';
if ($helper =~ /\{\{/ and $txt =~ /^[^{]*\}\}/) { ($helper, $txt) = ('', "$helper|$txt"); }
$helper = unquote($helper);
if ($helper =~ /^\[/ and $txt =~ /\]$/) { $pre = '['; $helper =~ s/^\[//; $txt =~ s/\]$//; $post = ']'; }
elsif ($txt =~ s/^(\[)(.*)(\])$/$2/) { ($pre, $post) = ($1, $3); }
elsif ($txt =~ s/(?<=\d{4})(\])$//) { $post = $1; }
($helper,$txt) = ($txt,$1) if ($txt =~ /^[ws]:(?:[a-z]{2}:)?(.*)$/ && !$helper);
$helper =~ s/<\/?.*?>//g;
$str = "$pre<קישור" . ($helper ? " $helper" : '') . ">$txt</קישור>$post";
$str =~ s/([()])\1/$1\x00$1/g unless ($str =~ /\(\(.*\)\)/); # Avoid splitted comments
push_lookahead($2) if ($helper =~ /^([^a-zA-Z]*)\=([^a-zA-Z]+)$/);
push_lookahead($txt) if ($helper =~ /^([^a-zA-Z]*)\=$/);
return $str;
}
sub parse_remark {
local $_ = shift;
return $1 . parse_remark($2) if /^(?|(\()(\(\(\[[^\[\]]*?\]\)\))|(\()(\(\(\(.*?[^)]\)\)\)))$/;
s/^\(\((.*?)\)\)$/$1/s;
my ($text,$tip,$url) = ( /((?:\{\{.*?\}\}|\[\[.*?\]\]|[^|])+)/g );
return $_ unless defined $text;
my $str;
$text =~ s/^ *(.*?) *$/$1/;
$text =~ s/(?<!\()\(\((.*?)\)\)(?!\))/{{מוקטן|$1}}/g;
if ($tip) {
$tip =~ s/^ *(.*?) *$/$1/;
$tip = escape_quote($tip);
$str = "<תיבה טקסט=\"$tip\"";
if ($url) {
$url = '' if ($url =~ /^\d+[אב]?(_\d+)?$/);
$url = find_reshumot_href($url);
$str .= " קישור=\"$url\"";
}
$str .= ">$text</תיבה>";
} else {
$str = "<הערה>$text</הערה>";
}
return $str;
}
my $pubdate = '';
sub parse_signatures {
local $_ = shift;
chomp;
# print STDERR "Signatures = |$_|\n";
my $str = "<חתימות>\n";
$str .= "<פרסום>$pubdate</פרסום>\n" if ($pubdate);
s/;/\n/g;
foreach (split("\n")) {
s/^\*? *(.*?) *$/$1/;
if (/\|/) {}
elsif (/,/) { tr/,/|/; }
elsif (s/ +(?=ה?(שר[הת]?|נשיאת?|ראש|יושבת?[\-־ ]ראש|נציבת?)\b)/\|/) {}
else { s/^([א-ת\-–־"״]{2,} (?:[א-ת]]['.] |\([א-ת\-–־]{2,}\) |)[א-ת\-–־"״]{2,}) ([^ ]+ +.+)$/$1 | $2/; }
s/ *\| */ | /g;
$str .= "* $_\n";
# /^\*? *([^,|]*?)(?: *[,|] *(.*?) *)?$/;
# $str .= ($2 ? "* $1 | $2\n" : "* $1\n");
}
$str .= "</חתימות>\n";
return $str;
}
sub parse_pubdate {
$pubdate = shift;
return "";
}
sub mark_linkset {
local $_ = shift;
s/\[\[(?|(.*?)\|(.*?)|()(.*?))\]\]/[[»$1|$2]]/g;
s/»([^»]+)$/«$1/;
# print STDERR "$_\n";
return $_;
}
#---------------------------------------------------------------------
sub parse_wikitable {
# Based on [mediawiki/core.git]/includes/parser/Parser.php doTableStuff()
local $_ = shift;
s/^(\|-.*)\n(.*?)\<עוגן (.*?)\>/$1 id="$3"\n$2/gm;
s/^(\|-.*?) *\<עוגן (.*?)\>/$1 id="$2"/gm;
my @lines = split(/\n/,$_);
my $out = '';
my ($last_tag, $previous, $indent_level, $attributes);
my (@td_history, @last_tag_history, @tr_history, @tr_attributes, @has_opened_tr);
for (@lines) {
s/^ *(.*?) *$/$1/;
if ($_ eq '') {
$out .= "\n";
next;
}
if (/^(.*)\{\|(.*)$/) {
$previous = $1;
$attributes = $2;
$attributes =~ s/wikitable *//; $attributes =~ s/ ?class=([“”]{2}|""|'')//;
$_ = "$previous<table$attributes>\n";
push @td_history, false;
push @last_tag_history, '';
push @tr_history, false;
push @tr_attributes, '';
push @has_opened_tr, false;
} elsif ( scalar(@td_history) == 0 ) {
# Don't do any of the following
$out .= "$_\n";
next;
} elsif (/^\|\}(.*)$/ ) {
# We are ending a table
$_ = "</table>\n$1";
$last_tag = pop @last_tag_history;
$_ = "<tr><td></td></tr>\n$_" if (!(pop @has_opened_tr));
$_ = "</tr>\n$_" if (pop @tr_history);
$_ = "</$last_tag>$_" if (pop @td_history);
pop @tr_attributes;
# $_ .= "</dd></dl>" x $indent_level;
} elsif ( /^\|-(.*)/ ) {
# Now we have a table row
# Whats after the tag is now only attributes
$attributes = $1;
pop @tr_attributes;
push @tr_attributes, $attributes;
$_ = '';
$last_tag = pop @last_tag_history;
pop @has_opened_tr;
push @has_opened_tr, true;
$_ = "</tr>\n" if (pop @tr_history);
$_ = "</$last_tag>$_" if (pop @td_history);
push @tr_history, false;
push @td_history, false;
push @last_tag_history, '';
} elsif (/^\!\! *(.*)$/) {
my @cells = split( / *\|\| */, $1 );
s/(.*)/<col>$1<\/col>/ for (@cells);
$_ = join('', @cells);
$_ = "<colgroup>$_</colgroup>";
} elsif (/^(?|\|(\+)|(\|)|(\!)) *(.*)$/) {
# This might be cell elements, td, th or captions
my $type = $1; $_ = $2;
s/!!/||/g if ( $type eq '!' );
my @cells = split( /[ \x00]*\|\|[ \x00]*/, $_ , -1);
@cells = ('') if (!@cells);
$_ = '';
# print STDERR "Cell is |" . join('|',@cells) . "|\n";
# Loop through each table cell
foreach my $cell (@cells) {
$previous = '';
if ($type ne '+') {
my $tr_after = pop @tr_attributes;
if ( !(pop @tr_history) ) {
# $previous = "<tr " . (pop @tr_attributes) . ">\n";
$previous = "<tr$tr_after>";
}
push @tr_history, true;
push @tr_attributes, '';
pop @has_opened_tr;
push @has_opened_tr, true;
}
$last_tag = pop @last_tag_history;
$previous = "</$last_tag>$previous" if (pop @td_history);
if ( $type eq '|' ) {
$last_tag = 'td';
} elsif ( $type eq '!' ) {
$last_tag = 'th';
} elsif ( $type eq '+' ) {
$last_tag = 'caption';
} else {
$last_tag = '';
}
push @last_tag_history, $last_tag;
# A cell could contain both parameters and data
my @cell_data = split( / *\| */, $cell, 2 );
if (!defined $cell_data[0]) {
$cell = "$previous<$last_tag> ";
} elsif ( $cell_data[0] =~ /\[\[|\{\{/ ) {
$cell = "$previous<$last_tag>$cell";
} elsif ( @cell_data < 2 ) {
$cell = "$previous<$last_tag>" . $cell_data[0] || " ";
} else {
$attributes = $cell_data[0];
$cell = $cell_data[1] || " ";
$cell = "$previous<$last_tag $attributes>$cell";
}
$_ .= $cell;
push @td_history, true;
}
$_ .= "␊\n";
} else {
$_ .= "\n";
$out =~ s/ ␊?\n?$//s;
}
$out .= $_;
}
# Closing open td, tr && table
while ( @td_history ) {
$out .= "</td>" if (pop @td_history);
$out .= "</tr>\n" if (pop @tr_history);
$out .= "<tr><td></td></tr>\n" if (!(pop @has_opened_tr));
$out .= "</table>\n";
}
# # Remove trailing line-ending (b/c)
# $out =~ s/\n$//s;
# special case: don't return empty table
if ( $out eq "<table>\n<tr><td></td></tr>\n</table>" ) {
$out = '';
}
# $out =~ s/\n\n/\n/g;
return $out;
}
#---------------------------------------------------------------------
sub parse_comparetable {
my @lines = split(/\n/,shift);
my $col = 0;
my @table;
for (@lines) {
if (/^ *(.*?) *\| *(.*?) *$/) {
$table[$col][0] = $1;
$table[$col][1] = $2;
$col++;
}
}
$col = int(($col+1)/2);
my $str = '<table border="0" cellpadding="1" cellspacing="0" dir="rtl" align="center">
<tr><th width="120">הסעיף הקודם</th><th width="120">הסעיף החדש</th><th width="120">הסעיף הקודם</th><th width="120">הסעיף החדש</th></tr>
';
for (my $i=0; $i<$col; $i++) {
$str .= "<tr>" .
"<td>" . ($table[$i][0] || " ") . "</td>" .
"<td>" . ($table[$i][1] || " ") . "</td>" .
"<td>" . ($table[$i+$col][0] || " ") . "</td>" .
"<td>" . ($table[$i+$col][1] || " ") . "</td>" .
"</tr>\n";
}
$str .= "</table>\n";
return $str;
}
#---------------------------------------------------------------------
sub expand_templates {
local $_ = shift;
# Convert to single character brackets for simpler regexps
s/\{\{/⦃/g;
s/\}\}/⦄/g;
s/(⦃(?:[^⦃⦄]++|(?R))*⦄)/ &expand_templates_2($1) /eg;
s/⦃/\{\{/g;
s/⦄/\}\}/g;
return $_;
}
sub expand_templates_2 {
local $_ = shift;
# s/^⦃ *([^ |])(\|//;
# s/ *⦄$//;
# s/((?:[^⦃⦄]++|(?R))*⦄)/ &expand_templates_2($1) /eg;
return $_;
}
#---------------------------------------------------------------------
sub get_fixstr {
local $_ = shift;
my @fix = ();
my $fix_sig = '(?:תיקון|תקון|תיקונים):';
push @fix, unquote($1) while (s/(?| *\($fix_sig *(([^()]++|\(.*?\))+) *\)| *\[$fix_sig *(.*?) *\](?!\]))//);
s/^ *(.*?) *$/$1/;
my $fix = join(', ',@fix);
$fix =~ s/\bה(תש[א-ת"]+)\b/$1/g;
$fix =~ s/(^| )(\*+|-+|\?+),?( |$)/ /g;
return ($_, $fix);
}
sub get_extrastr {
local $_ = shift;
my @extra = ();
s/\[(נוסח $HE+)\]/⟦$1⟧/g;
s/(\(\(.*?\)\))/ $1 =~ tr|[]|⟦⟧|r /eg; # Terrible way to avoid [...] match within ((...)).
push @extra, unquote($1) while (s/(?<=[^\[\(])\[ *([^\[\]]+) *\] *//) || (s/^\[ *([^\[\]]+) *\] *//);
s/^ *(.*?) *$/$1/;
tr/⟦⟧/[]/;
return ($_, join(', ',@extra));
}
sub get_ankor {
local $_ = shift;
my @ankor = ();
push @ankor, unquote($1) while (s/ *<עוגן:? *(.*?)>//);
s/^ *(.*?) *$/$1/;
return ($_, join(', ',@ankor));
}
sub get_numeral {
local $_ = shift;
return '' if (!defined($_));
my $num = '';
my $token = '';
s/"/"/g;
s/[,"'״׳]//g;
s/$nochar//g;
s/־/-/g;
s/([א-ת]{3})(\d)/$1-$2/;
$_ = unparent($_);
while ($_ ne '') {
$token = '';
given ($_) {
($num,$token) = ("0",$1) when /^(מקדמית?|אפס)\b/;
($num,$token) = ("11",$1) when /^(אחד[- ]עשר|אחת[- ]עשרה|(ال)?حادي عشر|احد عشر)\b/;
($num,$token) = ("12",$1) when /^(שניי?ם[- ]עשר|שתיי?ם[- ]עשרה|(ال)?ثاني عشر|اثنا عشر)\b/;
($num,$token) = ("13",$1) when /^(שלושה[- ]עשר|שלוש[- ]עשרה|(ال)?ثالث عشر|ثلاثة عشر)\b/;
($num,$token) = ("14",$1) when /^(ארבעה[- ]עשר|ארבע[- ]עשרה|(ال)?رابع عشر|اربعة عشر)\b/;
($num,$token) = ("15",$1) when /^(חמי?שה[- ]עשר|חמש[- ]עשרה|(ال)?خامس عشر|خمسة عشر)\b/;
($num,$token) = ("16",$1) when /^(שי?שה[- ]עשר|שש[- ]עשרה|(ال)?سادس عشر|ستة عشر)\b/;
($num,$token) = ("17",$1) when /^(שבעה[- ]עשר|שבע[- ]עשרה|(ال)?سابع عشر|سبعة عشر)\b/;
($num,$token) = ("18",$1) when /^(שמונה[- ]עשרה?|(ال)?ثامن عشر|ثمانية عشر)\b/;
($num,$token) = ("19",$1) when /^(תשעה[- ]עשר|תשע[- ]עשרה|(ال)?تاسع عشر|تسعة عشر)\b/;
($num,$token) = ("21",$1) when /^(עשרים[- ]ו?אח[דת]|واحد وعشرون)\b/;
($num,$token) = ("22",$1) when /^(עשרים[- ]ו?ש[נת]יי?ם|اثنان وعشرون)\b/;
($num,$token) = ("23",$1) when /^(עשרים[- ]ו?שלושה?|ثلاثة وعشرون)\b/;
($num,$token) = ("24",$1) when /^(עשרים[- ]ו?ארבעה?|أربعة وعشرون)\b/;
($num,$token) = ("25",$1) when /^(עשרים[- ]ו?חמי?שה?|خمسة وعشرون)\b/;
($num,$token) = ("26",$1) when /^(עשרים[- ]ו?שי?שה?|ستة وعشرون)\b/;
($num,$token) = ("27",$1) when /^(עשרים[- ]ו?שבעה|سبعة وعشرون)\b/;
($num,$token) = ("28",$1) when /^(עשרים[- ]ו?שמונה|ثمانية وعشرون)\b/;
($num,$token) = ("29",$1) when /^(עשרים[- ]ו?תשעה?|تسعة وعشرون)\b/;
($num,$token) = ("1",$1) when /^(ראשו(ן|נה)|אחד|אחת|(ال)?أول|واحد)\b/;
($num,$token) = ("2",$1) when /^(שניי?ה?|ש[תנ]יי?ם|(ال)?(ثاني|ثانيا)|إثنان)\b/;
($num,$token) = ("3",$1) when /^(שלישית?|שלושה?|(ال)?ثالث|ثلاثة)\b/;
($num,$token) = ("4",$1) when /^(רביעית?|ארבעה?|(ال)?(رابعة|رابع|أربع))\b/;
($num,$token) = ("5",$1) when /^(חמי?שית?|חמש|חמי?שה|(ال)?خامس|خمسة)\b/;
($num,$token) = ("6",$1) when /^(שי?שית?|שש|שי?שה|(ال)?سادس|ستة)\b/;
($num,$token) = ("7",$1) when /^(שביעית?|שבעה?|(ال)?سابع|سبعة)\b/;
($num,$token) = ("8",$1) when /^(שמינית?|שמונה|(ال)?ثامن|ثمانية)\b/;
($num,$token) = ("9",$1) when /^(תשיעית?|תשעה?|(ال)?تاسع|تسعة)\b/;
($num,$token) = ("10",$1) when /^(עשירית?|עשרה?|(ال)?عاشر|عشرة)\b/;
($num,$token) = ("20",$1) when /^(עשרים|(ال)?عشرون)\b/;
($num,$token) = ("30",$1) when /^(שלושים|(ال)?ثلاثون)\b/;
($num,$token) = ($use_roman ? arabic($1) : uc($1), $1) when /^($roman)\b/;
($num,$token) = ("$1-2","$1$2") when /^(\d+)([- ]?bis)\b/i;
($num,$token) = ("$1-3","$1$2") when /^(\d+)([- ]?ter)\b/i;
($num,$token) = ("$1-4","$1$2") when /^(\d+)([- ]?quater)\b/i;
($num,$token) = ($2,"$1$2") when /^($pre_sig-|)(\d\d+([._]\d*[א-ט]?)*)\b/; # At least two digits.
($num,$token) = ($1,$1) when /^(\d+([._]\d+[א-ט]?)+)\b/;
($num,$token) = ($1,$1) when /^(\d+$heb_num2?\.$heb_num2\d?)\b/;
($num,$token) = ($1,$1) when /^(\d+($heb_num2?\d*|))\b/;
($num,$token) = ($1,$1) when /^(\d+$heb_num2\d+$heb_num2)\b/;
($num,$token) = ($1,$1) when /^($heb_num3(\d+[א-י]*|))\b/;
}
# print STDERR "get_numeral: got \$_=\"$_\"; \$num=\"$num\" and \$token=\"$token\".\n";
if ($num ne '') {
# Remove token from rest of string
$_ =~ s/^$token//;
last;
} else {
# Fetch next token
$_ =~ s/^[הו]// || $_ =~ s/^\<[^>]*\>// || $_ =~ s/^[^ ()|]*[ ()|]+// || last;
}
}
$num .= "-$1$2$3" if (s/^[- ]([א-י])[- ]?(\d)[- ]?([א-י])\b//);
$num .= "-$1$2" if (s/^[- ]([א-י])[- ]?(\d)\b//);
$num .= "-$1" if (s/^[- ]([א-י])\b//);
$num .= "-$1" if ($num =~ /^\d/ and $token !~ /^\d/ and /^[- ]?(\d[א-י]?)\b/);
$num =~ s/(?<=\d)-(?=[א-ת])//;
return $num;
}
sub unquote {
my $s = shift;
# my $Q1 = '["״”“„‟″‶]';
# my $Q2 = '[\'`׳’‘‚‛′‵]';
my $Q1 = qr/"/;
my $Q2 = qr/'/;
my $sp = qr/[ \x00]*/;
$s =~ s/'''/‴/g;
$s =~ s/^$sp(?|$Q1$sp(.*?)$sp$Q1|$Q2$sp(.*?)$sp$Q2|$sp(.*?)$sp)$sp$/$1/;
$s =~ s/‴/'''/g;
return $s;
}
sub unparent {
my $s = unquote(shift);
$s =~ s/^\(\((.*?)\)\)$/$1/;
$s =~ s/^<([a-z]+)>(.*)<\/\1>/$2/g;
$s =~ s/^(?|\((.*?)\)|\[(.*?)\]|\{(.*?)\})$/$1/;
$s =~ s/^ *(.*?) *$/$1/;
return $s;
}
sub escape_quote {
my $s = shift;
$s =~ s/^ *(.*?) *$/$1/;
$s =~ s/&/\&/g;
$s =~ s/"/"/g;
return $s;
}
sub escape_text {
my $s = unquote(shift);
$s =~ s/&/\&/g;
$s =~ s/</</g;
$s =~ s/>/>/g;
$s =~ s/([(){}"'\[\]<>\|])/"&#" . ord($1) . ";"/ge;
return $s;
}
sub unescape_text {
local $_ = shift;
my %table = ( 'quot' => '"', 'lt' => '<', 'gt' => '>', 'ndash' => '–', 'nbsp' => ' ', 'apos' => "'", # & subs later
'lrm' => "\x{200E}", 'rlm' => "\x{200F}", 'thinsp' => "\x{2009}", 'shy' => '&null;',
'deg' => '°', 'minus' => '−', 'pm' => '±', 'plusmn' => '±', 'mnplus' => '∓', 'mp' => '∓', 'times' => '×', 'sup1' => '¹', 'sup2' => '²', 'sup3' => '³',
'frac14' => '¼', 'frac12' => '½', 'frac34' => '¾', 'alpha' => 'α', 'beta' => 'β', 'gamma' => 'γ', 'delta' => 'δ', 'epsilon' => 'ε', 'micro' => 'µ',
'percnt' => '%', 'permil' => '‰', 'pertenk' => '‱',
);
s/&#(\d+);/chr($1)/ge;
s/(&([a-z]+);)/($table{$2} || $1)/ge;
s/&null;//g;
s/&/&/g;
return $_;
}
sub s_lut {
my ($str, $regexp, $table);
if (scalar(@_)==2) {
($str,$table) = @_;
$regexp = '([' . join('', keys(%{$table})) . '])';
} else {
($str, $regexp, $table) = @_;
}
$str =~ s|$regexp| $table->{$1} // $1 |ge;
return $str;
}
sub roman_ligatures {
my $str = shift;
$str = s_lut($str, '([ivx]+|[IVX]+)', {
'I'=>'Ⅰ', 'II'=>'Ⅱ', 'III'=>'Ⅲ', 'IV'=>'Ⅳ', 'V'=>'Ⅴ', 'VI'=>'Ⅵ', 'VII'=>'Ⅶ', 'VIII'=>'Ⅷ', 'IX'=>'Ⅸ', 'X'=>'Ⅹ',
'XI'=>'Ⅺ', 'XII'=>'Ⅻ', 'XIII'=>'ⅩⅢ', 'XIV'=>'ⅩⅣ', 'XV'=>'ⅩⅤ', 'XVI'=>'ⅩⅥ', 'XVII'=>'ⅩⅦ', 'XVIII'=>'ⅩⅧ', 'XIX'=>'ⅩⅨ', 'XX'=>'ⅩⅩ',
'i'=>'ⅰ', 'ii'=>'ⅱ', 'iii'=>'ⅲ', 'iv'=>'ⅳ', 'v'=>'ⅴ', 'vi'=>'ⅵ', 'vii'=>'ⅶ', 'viii'=>'ⅷ', 'ix'=>'ⅸ', 'x'=>'ⅹ',
'xi'=>'ⅺ', 'xii'=>'ⅻ', 'xiii'=>'ⅹⅲ', 'xiv'=>'ⅹⅳ', 'xv'=>'ⅹⅴ', 'xvi'=>'ⅹⅵ', 'xvii'=>'ⅹⅶ', 'xviii'=>'ⅹⅷ', 'xix'=>'ⅹⅸ', 'xx'=>'ⅹⅹ',
});
return $str;
}
sub canonic_name {
local $_ = shift;
tr/–־/-/;
tr/״”“„/"/;
tr/׳‘’/'/;
tr/\x{05B0}-\x{05BD}//d;
tr/אּ-תּ/א-ת/;
s/ - / – /g;
s/^ *(.*?) *$/$1/;
s/ {2,}/ /g;
# s/\(\((?|\[(.*?)\]|(.*?))\)\)/$1/g;
# s/<הערה>(?|\[(.*?)\]|(.*?))<\/הערה>\)\)/$1/g;
return $_;
}
sub fix_tags {
local $_ = shift;
tr/–/-/;
tr/“”״/"/;
s/\{\{==?\}\}/=/g;
return $_;
}
sub escape_eqsign {
local $_ = shift;
s/\{\{==?\}\}/⌸/g;
s/(\<[^>]+\>)/ $1 =~ s|=|⌸|gr /eg;
s/⌸/\{\{=\}\}/g;
tr/⌸/=/;
return $_;
}
sub dump_hash {
my $h = shift;
return join('; ', map("$_ => '" . ($h->{$_} // "[undef]") . "'", keys(%{$h})));
}
#---------------------------------------------------------------------
our %glob;
our %hrefs;
our %sections;
our @lookahead;
our (@line, $idx);
sub cleanup {
$do_expand = 0;
$div_table = 0;
undef %glob; undef %hrefs; undef %sections; undef @line;
undef $pubdate;
$glob{context} = '';
$glob{href}{last_class} = '';
$glob{level} = ['','','',''];
$glob{part_type} = $glob{sect_type} = $glob{subs_type} = 0;
}
sub linear_parser {