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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
|
-*- mode: org -*-
#+TITLE: sisu sst
#+DESCRIPTION: documents - structuring, various output representations & search
#+FILETAGS: :sisu:sst:
#+AUTHOR: Ralph Amissah
#+EMAIL: [[mailto:ralph.amissah@gmail.com][ralph.amissah@gmail.com]]
#+COPYRIGHT: Copyright (C) 2015 - 2021 Ralph Amissah
#+LANGUAGE: en
#+STARTUP: content hideblocks hidestars noindent entitiespretty
#+OPTIONS: H:3 num:nil toc:t \n:nil @:t ::t |:t ^:nil _:nil -:t f:t *:t <:t
#+PROPERTY: header-args :exports code
#+PROPERTY: header-args+ :noweb yes
#+PROPERTY: header-args+ :eval no
#+PROPERTY: header-args+ :results no
#+PROPERTY: header-args+ :cache no
#+PROPERTY: header-args+ :padline no
#+PROPERTY: header-args+ :mkdirp yes
* sst_from_xml.rb
#+BEGIN_SRC ruby :tangle "../lib/sisu/sst_from_xml.rb"
<<sisu_document_header>>
module SiSU_sstFromXML
require_relative 'se' # se.rb
class Convert
begin
require 'rexml/document'
include REXML
rescue LoadError
SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
error('rexml/document NOT FOUND (LoadError)')
end
def initialize(opt)
@opt=opt
@sisu,@sisu_base=[],[]
@ver=SiSU_Env::InfoVersion.instance.get_version
end
def tell(filename,type)
SiSU_Screen::Ansi.new(
@opt.act[:color_state][:set],
"XML #{type} to SiSU sst",
"#{filename} --> #{filename}.sst"
).green_hi_blue
end
def read
xml_to_sisu
end
def markup_head(text)
text.strip!
text.gsub!(/(?:\s*\n|\s\s+)/,' ')
text.gsub!(/<header class=['"]\S+?['"]>(.+?)<\/header>/,'\1')
text.gsub!(/<(\w+)>(.+?)<\/\w+>/,'@\1: \2')
text.gsub!(/<header class=['"]\S+?['"]><(\w+)>(.+?)<\/\w+><\/header>/,'@\1: \2')
text.gsub!(/\s +/,' ')
text.strip!
text + "\n\n"
end
def markup(text)
text.strip!
text.gsub!(/(?:\s*\n|\s\s+)/,' ')
text.gsub!(/<text class='h1'>(.+?)<\/text>/,':A~ \1')
text.gsub!(/<text class='h2'>(.+?)<\/text>/,':B~ \1')
text.gsub!(/<text class='h3'>(.+?)<\/text>/,':C~ \1')
text.gsub!(/<text class='h4'>(.+?)<\/text>/,'1~ \1')
text.gsub!(/<text class='h5'>(.+?)<\/text>/,'2~ \1')
text.gsub!(/<text class='h6'>(.+?)<\/text>/,'3~ \1')
text.gsub!(/<text class='norm'>(.+?)<\/text>/,'\1')
text.gsub!(/<endnote symbol='norm'>(.+?)<\/endnote>/,'~{ \1 }~')
text.gsub!(/<br ?\/>/,'<br>')
text.gsub!(/<i>(.+?)<\/i>/,'/{\1}/')
text.gsub!(/<b>(.+?)<\/b>/,'*{\1}*')
text.gsub!(/<u>(.+?)<\/u>/,'_{\1}_')
text.gsub!(/<sem:([a-z_]+)\s+depth=['"]zero['"]>(\s*.+?\s*)<\/sem:\1>/,';{ \2 };\1')
text.gsub!(/<sem:([a-z_]+)\s+depth=['"]one['"]>(\s*.+?\s*)<\/sem:\1>/,':{ \2 }:\1')
text.gsub!(/<sem:([a-z_]+)\s+depth=['"]many['"]>(\s*.+?\s*)<\/sem:\1>/,'\1:{ \2 }:\1')
text.gsub!(/<sem:([a-z_]+)>(\s*.+?\s*)<\/sem:\1>/,'\1:{ \2 }:\1')
text.gsub!(/\s +/,' ')
text.strip!
text + "\n\n"
end
def sax
out_file=File.new(@output_file_name,'w')
head=@doc.root.get_elements('//head/header')
body=@doc.root.get_elements('//object/text')
out_file.puts "% SiSU text #{@ver.version} (generated from a SiSU XML markup representation)\n\n"
head.each do |x|
if x.name=='header'
head=markup_head(x.to_s)
out_file.puts head
end
end
body.each do |x|
if x.name=='text'
body=markup(x.to_s)
out_file.puts body
end
end
end
def node
sax
end
def dom
raise "#{__FILE__}::#{__LINE__} xml dom representation to sst not yet implemented (experimental simple xml representations sax and node to sst are in place)."
end
def xml_to_sisu
unless @opt.files.empty?
@opt.files.each do |xml|
@sisu_base=[]
if xml =~/\.sx[sdn]\.xml$/
begin
@doc_str=IO.readlines(xml,'').join("\n")
@output=File.new("#{xml}.sst",'w')
@doc=REXML::Document.new(@doc_str)
@output_file_name="#{Dir.pwd}/#{xml}.sst"
@el=[]
rescue REXML::ParseException
end
end
if xml =~/\.sxs\.xml$/
unless @opt.act[:quiet][:set]==:on
tell(xml,'sax')
end
sax
elsif xml =~/\.sxd\.xml$/
unless @opt.act[:quiet][:set]==:on
tell(xml,'dom')
end
dom
elsif xml =~/\.sxn\.xml$/
unless @opt.act[:quiet][:set]==:on
tell(xml,'node')
end
node
else puts "filename not recognised: << #{xml} >>"
end
@output << @sisu_base
end
else puts '.xml file for conversion to sisu expected'
end
puts @opt.files.inspect
end
end
end
__END__
#+END_SRC
* sst_to_s_xml_sax.rb
#+BEGIN_SRC ruby :tangle "../lib/sisu/sst_to_s_xml_sax.rb"
<<sisu_document_header>>
module SiSU_SimpleXML_ModelSax
require_relative 'se_hub_particulars' # se_hub_particulars.rb
include SiSU_Particulars
require_relative 'dp' # dp.rb
include SiSU_Param
require_relative 'se' # se.rb
include SiSU_Env
require_relative 'ao_doc_str' # ao_doc_str.rb
require_relative 'xml_shared' # xml_shared.rb
include SiSU_XML_Munge
require_relative 'shared_sem' # shared_sem.rb
require_relative 'xml_format' # xml_format.rb
include SiSU_XML_Format
require_relative 'rexml' # rexml.rb
include SiSU_Rexml
@@alt_id_count=0
@@tablefoot=''
class Convert
@@fns=nil
def initialize(opt)
@opt=opt
@particulars=SiSU_Particulars::CombinedSingleton.instance.get_env_md(opt)
end
def read
begin
@md=@particulars.md #bug, relies on info persistence, assumes -m has previously been run
@env=@particulars.env
SiSU_Screen::Ansi.new(
@opt.act[:color_state][:set],
'invert',
'XML SAX',
"#{@md.fns} -> #{@md.fn[:sxs]}"
).colorize unless @opt.act[:quiet][:set]==:on
if (@opt.act[:verbose_plus][:set]==:on \
|| @opt.act[:maintenance][:set]==:on)
SiSU_Screen::Ansi.new(
@opt.act[:color_state][:set],
@opt.fns,
"#{Dir.pwd}/#{@md.fn[:sxs]}"
).flow
end
unless @@fns==@opt.fns
@@fns=@opt.fns
@@fns_array=[]
end
@fns_array=if @@fns_array.empty?; read_fnm
else @@fns_array.dup #check
end
SiSU_SimpleXML_ModelSax::Convert::Songsheet.new(@fns_array,@particulars).songsheet
rescue
SiSU_Errors::Rescued.new($!,$@,@opt.cmd,@opt.fns).location do
__LINE__.to_s + ':' + __FILE__
end
ensure #file closed in songsheet
end
end
def read_fnm
ao=[]
if FileTest.file?("#{Dir.pwd}/#{@opt.fns}")
ao=IO.readlines("#{Dir.pwd}/#{@opt.fns}","\n\n")
else STDERR.puts 'Error'
end
end
private
class Songsheet
def initialize(data,particulars)
@data,@particulars,@env,@md=data,particulars,particulars.env,particulars.md
end
def songsheet
begin
SiSU_SimpleXML_ModelSax::Convert::Scroll.new(@data,@particulars).songsheet
if (@md.opt.act[:verbose][:set]==:on \
|| @md.opt.act[:verbose_plus][:set]==:on \
|| @md.opt.act[:maintenance][:set]==:on)
SiSU_SimpleXML_ModelSax::Convert::Tidy.new(@md,@env).xml # test wellformedness, comment out when not in use
end
SiSU_Rexml::Rexml.new(@md,@md.fn[:sxs]).xml if @md.opt.act[:maintenance][:set]==:on # test rexml parsing, comment out when not in use #debug
rescue
SiSU_Errors::Rescued.new($!,$@,@md.opt.cmd,@md.fns).location do
__LINE__.to_s + ':' + __FILE__
end
ensure
end
end
end
class Scroll
require_relative 'txt_shared' # txt_shared.rb
require_relative 'css' # css.rb
include SiSU_TextUtils
@@xml={ body: [], open: [], close: [], head: [] }
def initialize(data='',particulars='')
@data,@env,@md=data,particulars.env,particulars.md
@regx=/^(?:#{Mx[:mk_o]}:p[bn]#{Mx[:mk_c]}\s*)?(?:#{Mx[:lv_o]}[1-9]:(\S*)#{Mx[:lv_c]})?(.+)/
@tab="\t"
if @md
@trans=SiSU_XML_Munge::Trans.new(@md)
end
@sys=SiSU_Env::SystemCall.new
end
def songsheet
pre
markup
post
publish
end
protected
def embedded_endnotes(para='')
para.gsub!(/~\{(.+?)\}~/,'<endnote symbol="norm">\1</endnote> ')
para.gsub!(/~\[([*+])\s+(.+?)\]~/,'<endnote symbol="\1">\2</endnote> ')
end
def xml_head(meta)
txt=meta.text
txt.gsub!(/\/{(.+?)}\//,'<i>\1</i>')
txt.gsub!(/[*!]{(.+?)}[*!]/,'<b>\1</b>')
txt.gsub!(/_{(.+?)}_/,'<u>\1</u>')
txt.gsub!(/-{(.+?)}-/,'<del>\1</del>')
txt.gsub!(/<br(?: \/)?>/,'<br />')
txt.gsub!(/ & /,' and ')
@@xml[:head] <<<<WOK
#{@tab}<header class="#{meta.attrib}">
#{@tab*2}<#{meta.el}>
#{@tab*3}#{txt}
#{@tab*2}</#{meta.el}>
#{@tab}</header>
WOK
end
def xml_sc(md='')
sc=if @md.sc_info
<<WOK
<source_control>
<sc class="sourcefile">
#{@md.sc_filename}
</sc>
<sc class="number">
#{@md.sc_number}
</sc>
<sc class="date">
#{@md.sc_date}
</sc>
</source_control>
WOK
else ''
end
@@xml[:sc]=sc
end
def xml_structure(para='',lv='',hname='') #extracted endnotes
lv=lv.to_i
lv=nil if lv==0
embedded_endnotes(para)
if para[@regx]
paragraph="#{para[@regx,2]}"
util=SiSU_TextUtils::Wrap.new(paragraph,70)
wrapped=util.line_wrap
end
@@xml[:body] << "#{@tab*0}<object>" if para[@regx]
@@xml[:body] << "#{@tab*1}" << "\n" if para[@regx]
@@xml[:body] << if lv; %{#{@tab*1}<text class="h#{lv}">\n#{@tab*2}#{wrapped}\n#{@tab*1}</text>\n} << "\n"
elsif wrapped =~/\A%%?\s+/; %{<!--\n#{@tab*1}<text class="comment">\n#{@tab*2}#{wrapped}\n#{@tab*1}</text>\n-->\n} # comments
else %{#{@tab*1}<text class="norm">\n#{@tab*2}#{wrapped}\n#{@tab*1}</text>\n} # main text, contents, body KEEP
end
@@xml[:body] << "#{@endnotes}" if @endnotes # main text, endnotes KEEP
@@xml[:body] << "#{@tab*0}</object>" << "\n" if para[@regx]
@endnotes=[]
end
def block_structure(para='')
para.gsub!(/<:block(?:-end)?>/,'')
para.strip!
@@xml[:body] << %{#{@tab*0}<object>}
@@xml[:body] << %{#{@tab*1}<text class="block">#{@tab*1}\n}
@@xml[:body] << %{#{@tab*2}#{para}#{@tab*1}\n}
@@xml[:body] << %{#{@tab*1}</text>\n}
@@xml[:body] << "#{@tab*0}</object>"
end
def group_structure(para='')
para.gsub!(/<:group(?:-end)?>/,'')
para.strip!
@@xml[:body] << %{#{@tab*0}<object>}
@@xml[:body] << %{#{@tab*1}<text class="group">#{@tab*1}\n}
@@xml[:body] << %{#{@tab*2}#{para}#{@tab*1}\n}
@@xml[:body] << %{#{@tab*1}</text>\n}
@@xml[:body] << "#{@tab*0}</object>"
end
def poem_structure(para='')
para.gsub!(/<:verse(?:-end)?>/,'')
para.strip!
@@xml[:body] << %{#{@tab*0}<object>}
@@xml[:body] << %{#{@tab*1}<text class="verse">#{@tab*1}\n}
@@xml[:body] << %{#{@tab*2}#{para}#{@tab*1}\n}
@@xml[:body] << %{#{@tab*1}</text>\n}
@@xml[:body] << "#{@tab*0}</object>" << "\n"
end
def code_structure(para='')
para.gsub!(/<:code(?:-end)?>/,'')
para.strip!
@@xml[:body] << %{#{@tab*0}<object>}
@@xml[:body] << %{#{@tab*1}<text class="code">#{@tab*1}\n}
@@xml[:body] << %{#{@tab*2}#{para}#{@tab*1}\n}
@@xml[:body] << %{#{@tab*1}</text>\n}
@@xml[:body] << "#{@tab*0}</object>" << "\n"
end
def table_structure(table='') #tables
@@xml[:body] << %{#{@tab*0}<object>}
@@xml[:body] << %{#{@tab*1}#{table}\n#{@tab*1}\n} # unless lv # main text, contents, body KEEP
@@xml[:body] << "#{@tab*0}</object>" << "\n" #if para[@regx]
@endnotes=[]
end
def tidywords(wordlist)
wordlist.each do |x|
x.gsub!(/&/,'&') unless x =~/&\S+;/
end
end
def xml_clean(para)
para.gsub!(/#{Mx[:gl_o]}[1-9]:\S*?#{Mx[:gl_c]}/,'') #Danger, watch
para
end
def markup
data=[]
xml_sc(@md)
@endnotes,@level,@cont,@copen,@xml_contents_close=[],[],[],[],[]
@rcdc=false
(0..6).each { |x| @cont[x]=@level[x]=false }
(4..6).each { |x| @xml_contents_close[x]='' }
@data.each do |para|
data << SiSU_AO_DocumentStructureExtract::Structure.new(@md,para).structure #takes on Mx marks
end
data.each do |para|
if para !~/^\s*(?:%+ |<:code>)/
if @md.sem_tag and para =~/[:;]\{|\}[:;]/
para=@trans.xml_semantic_tags(para)
end
if para =~/[:;]\{|\}[:;]/
para=SiSU_Sem::Tags.new(para,@md).rm.all
end
end
para=@trans.markup_light(para)
@trans.char_enc.utf8(para) if @sys.locale =~/utf-?8/i #% utf8
para.gsub!(/^@(\S+?):/,"#{Mx[:lv_o]}@\\1#{Mx[:lv_c]}")
if para =~/\A#{Mx[:lv_o]}@(\S+?)#{Mx[:lv_c]}\s*(.+?)\Z/m # for headers
d_meta=SiSU_TextUtils::HeaderScan.new(@md,para).meta
if d_meta; xml_head(d_meta)
end
end
para='' if para=~/#{Mx[:lv_o]}@\S+?#{Mx[:lv_c]}/
if @rcdc==false \
and (para =~/~metadata/ or para =~/^1~meta\s+Document Information/)
@rcdc=true
end
if para !~/(^@\S+?:|^\s*$|<ENDNOTES>|<EOF>)/
@sto=SiSU_text_parts::SplitTextObject.new(@md,para).lev_segname_para
unless @rcdc
SiSU_XML_Format::FormatScroll.new(@md,@sto.text) if @sto.format =~/i[1-9]|ordinary/
case @sto.format
when /^(1):(\S*)/
xml_clean(para)
xml_structure(para,$1,$2)
para=@sto.lev_para_ocn.heading_body1
when /^(2):(\S*)/
xml_clean(para)
xml_structure(para,$1,$2)
para=@sto.lev_para_ocn.heading_body2
when /^(3):(\S*)/
xml_clean(para)
xml_structure(para,$1,$2)
para=@sto.lev_para_ocn.heading_body3
when /^(4):(\S*)/ # work on see SplitTextObject
xml_clean(para)
xml_structure(para,$1,$2)
para=@sto.lev_para_ocn.heading_body4
when /^(5):(\S*)/
xml_clean(para)
xml_structure(para,$1,$2)
para=@sto.lev_para_ocn.heading_body5
when /^(6):(\S*)/
xml_clean(para)
xml_structure(para,$1,$2)
para=@sto.lev_para_ocn.heading_body6
else
if para =~ /<:verse>/
para=poem_structure(para)
elsif para =~ /<:group>/
para=group_structure(para)
elsif para =~ /<:code>/
para.gsub!(/</,'<')
para.gsub!(/>/,'>')
para=code_structure(para)
elsif para =~/<!Th?.+/ # tables come as single block #work area 2005w13
table=SiSU_Tables::TableXML.new(para)
para=table.table_split
para=table_structure(para)
else xml_structure(para,nil,nil)
end
end
if para =~/<a name="n\d+">/ \
and para =~/^(-\{{2}~\d+|<!e[:_]\d+!>)/ # -endnote
para=''
end
if para =~/.*<:#>.*$/
para=case para
when /<:i1>/
format_text=FormatTextObject.new(para,'')
format_text.scr_inden_ocn_e_no_paranum
when /<:i2>/
format_text=FormatTextObject.new(para,'')
format_text.scr_inden_ocn_e_no_paranum
end
end
if para =~/<:center>/
one,two=/(.*)<:center>(.*)/.match(para)[1,2]
format_text=FormatTextObject.new(one,two)
para=format_text.center
end
end
para.gsub!(/<:\S+?>/,'')
para.gsub!(/<!.+!>/,'') ## Clean Prepared Text #bugwatch reinstate
para
end
para
end
6.downto(4) do |x|
y=x - 1; v=x - 3
@@xml[:body] << "#{@tab*5}</content>\n#{@tab*y}</contents#{v}>\n" if @level[x]==true
end
3.downto(1) do |x|
y=x - 1
@@xml[:body] << "#{@tab*y}</heading#{x}>\n" if @level[x]==true
end
end
def pre
rdf=SiSU_XML_Tags::RDF.new(@md)
dir=SiSU_Env::InfoEnv.new
@@xml[:head],@@xml[:body]=[],[]
css=SiSU_Env::CSS_Select.new(@md).xml_sax
encoding=if @sys.locale =~/utf-?8/i then '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'
else '<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>'
end
@@xml[:open] =<<WOK
#{encoding}
<?xml-stylesheet type="text/css" href="../#{dir.path.style}/#{css}"?>
#{rdf.comment_xml_sax}
<document>
WOK
@@xml[:head] << "<head>\n"
@@xml[:body] << "<body>\n"
end
def post
@@xml[:head] << @@xml[:sc]
@@xml[:head] << "</head>\n"
@@xml[:body] << "</body>\n"
@@xml[:close] = "</document>\n"
end
def publish
content=[]
content << @@xml[:open] << @@xml[:head] << @@xml[:body] << @@xml[:metadata]
content << @@xml[:owner_details] if @md.stmp =~/\w\w/
content << @@xml[:tail] << @@xml[:close]
Output.new(content.join,@md).xml
@@xml={}
end
end
class Output
def initialize(data,md)
@data,@md=data,md
end
def xml
@sisu=[]
@data.each do |para|
para.gsub!(/<:\S+?>/,'')
para.gsub!(/<!.+?!>/,'')
para="#{para}\n" unless para.empty?
@sisu << para
end
new_file_data=@sisu.join
@sisu=new_file_data.scan(/.+/)
SiSU_Env::FileOp.new(@md).mkdir
filename_sxm=SiSU_Env::FileOp.new(@md,@md.fn[:sxs]).mkfile_pwd
if filename_sxm.is_a?(File)
@sisu.each {|para| filename_sxm.puts para}
filename_sxm.close
else puts 'file not created, is directory writable?'
end
end
end
class Tidy
def initialize(md,dir)
@md,@env=md,dir
@prog=SiSU_Env::InfoProgram.new
end
def xml
if @prog.tidy !=false #note values can be other than true
if (@md.opt.act[:verbose_plus][:set]==:on \
|| @md.opt.act[:maintenance][:set]==:on)
SiSU_Screen::Ansi.new(
@md.opt.act[:color_state][:set],
'invert',
'Using XML Tidy',
'check document structure'
).colorize unless @md.opt.act[:quiet][:set]==:on
SiSU_Screen::Ansi.new(
@md.opt.act[:color_state][:set],
'',
'',
'check document structure'
)
tell.grey_open unless @md.opt.act[:quiet][:set]==:on
tidyfile='/dev/null' #don't want one or screen output, check for alternative flags
tidy =SiSU_Env::SystemCall.new("#{Dir.pwd}/#{@md.fn[:sxs]}",tidyfile)
tidy.well_formed?
tell.p_off unless @md.opt.act[:quiet][:set]==:on
end
end
end
end
end
end
__END__
#+END_SRC
* sst_identify_markup.rb
#+BEGIN_SRC ruby :tangle "../lib/sisu/sst_identify_markup.rb"
<<sisu_document_header>>
module SiSU_Markup
class MarkupInform
attr_accessor :version
def initialize(version,message,declared_markup='',declared_type='')
@version,@message,@declared_markup,@declared_type=version,message,declared_markup,declared_type
end
def version
@version
end
def message
@message
end
def declared_version
@declared_markup
end
def declared_type
@declared_type
end
def history
MarkupHistory.new(@version).query
end
end
class MarkupIdentify
def initialize(opt)
@opt=opt
@description='This is a script attempts to identify the version of markup used in SiSU (and provides information on changes in markup)'
end
def help
print <<WOK
#{@description}
WOK
exit
end
def identify
f=@opt.fns
if f =~/(?:\.sst|\.ssm|\.ssi|\.s[123])$/ \
and File.exist?(f)
file=File.open(f,'r')
cont=file.readlines
file.close
links,oldlinks='',''
markup=nil
@declared_type,@declared_markup='[text?]',''
if cont[0] =~ /^(?:%\s+)?SiSU\s+(text|master|insert)\s+([0-9](?:\.[0-9]+){1,2})/ \
or cont[0] =~ /^(?:%\s+)?sisu-([0-9](?:\.[0-9]+){1,2})/
@declared_type,@declared_markup=$1,$2
elsif cont[0] =~ /^(?:%\s+)?SiSU\s+([0-9](?:\.[0-9]+){1,2})/ \
or cont[0] =~ /^(?:%\s+)?sisu-([0-9](?:\.[0-9]+){1,2})/
@declared_markup=$1
end
@flag_2_0,@flag_1_0,@flag_69,@flag_66,@flag_57,@flag_38=false,false,false,false,false,false
cont.each_with_index do |y,i|
if y =~/^(?:0\{?~links?|@links?:)\s/ \
and f =~/(?:\.sst|\.ssm|\.ssi|\.s[123])/
links=unless y =~/\{.+?\}\S+/; oldlinks=' (pre 0.20.4 header links)'
else ' (post 0.20.4 header links)'
end
end
if @flag_2_0 \
or y =~/^@make:|^@classify|^\s\s?:[a-z_-]+?:\s+\S/
version=2.0.to_f
markup=MarkupInform.new(version,'2.0' + oldlinks,@declared_markup,@declared_type)
@flag_2_0=true
break
end
unless @flag_38
if (y =~/^:?A~/ and f =~/(?:\.sst|\.ssm|\.ssi)/)
version='0.38'
markup=MarkupInform.new(version,'0.38' + oldlinks,@declared_markup,@declared_type)
@flag_38=true
end
end
if @flag_38
if @flag_1_0 \
or y =~/^=\{.+?\}\s*$/
version='0.69'
markup=MarkupInform.new(version,'0.69' + oldlinks,@declared_markup,@declared_type)
@flag_1_0=true
break
end
if @flag_66 \
or y =~/[a-z+][:;]\{.+?\}[:;][a-z+]/
version='0.66'
markup=MarkupInform.new(version,'0.66' + oldlinks,@declared_markup,@declared_type)
@flag_66=true
break
end
end
end
unless @flag_2_0 \
or @flag_1_0 \
or @flag_66
cont.each_with_index do |y,i|
if y =~/^(?:0\{?~links?|@links?:)\s/ \
and f =~/(?:\.sst|\.ssm|\.ssi|\.s[123])/
links=unless y =~/\{.+?\}\S+/; oldlinks=' (pre 0.20.4 header links)'
else ' (post 0.20.4 header links)'
end
end
if @flag_57 \
or (y =~/^:?A~\?? @title/ and f =~/(?:\.sst|\.ssm|\.ssi)/)
version='0.57'
markup=MarkupInform.new(version,'0.57' + oldlinks,@declared_markup,@declared_type)
@flag_57=true
break
end
if @flag_38 \
or (y =~/^:?A~/ and f =~/(?:\.sst|\.ssm|\.ssi)/)
version='0.38'
markup=MarkupInform.new(version,'0.38' + oldlinks,@declared_markup,@declared_type)
@flag_38=true
break if i >= 200
if y =~ /(?:~{\*+|~\[\*|~\[\+)\s/
version='0.42'
markup=MarkupInform.new(version,'0.42' + oldlinks,@declared_markup,@declared_type)
break
end
end
if (y =~/^1~/ and f =~/(?:\.sst|\.ssm|\.ssi)/) \
and not @flag_38
version='0.37'
markup=MarkupInform.new(version,'0.37 is substantially 0.16 - 0.36 markup with new file-extension' + oldlinks,@declared_markup,@declared_type)
break
end
if y =~/^1~/ \
and f =~/\.([rs])([123])/ \
and not @flag_38
t,n=$1,$2
version='0.16'
instruct=if t =~/r/
" (change file extension from .#{t}#{n} to .ssm)"
else " (change file extension from .#{t}#{n} to .sst)"
end
markup=MarkupInform.new(version,'0.16 - 0.36' + instruct + links,@declared_markup,@declared_type)
break
end
if y =~/^0\{~/ \
and not @flag_38
version='0.1'
markup=MarkupInform.new(version,'0.1 - 0.15',@declared_markup,@declared_type)
break
end
if y =~/^0\{{3}/ \
and not @flag_38
markup=MarkupInform.new('circa. 1997','old, check date',@declared_markup,@declared_type)
break
end
markup='Not a recognised file type '
end
end
markup
else MarkupHistory.new(@opt).help_query
end
end
def determine_markup_version
if @opt.fns.nil? \
or @opt.fns.empty?
MarkupHistory.new(@opt).help_identify
end
if File.exist?(@opt.fns)
if @opt.fns =~/\.(?:sst|ssm|ssi|s[123i]|r[123])/
markup=identify #(@opt.fns)
if defined? markup.version
unless @opt.act[:quiet][:set]==:on
message=unless markup.declared_version.empty?
"#{@opt.fns}\n markup Type Declared as SiSU #{markup.declared_version} #{markup.declared_type}\n appears to be SiSU #{markup.version}"
else
"Markup Type Appears to be SiSU #{markup.version}\n in file #{@opt.fns}"
end
puts message
puts %{"sisu --query-#{markup.version}" for a brief description of markup type}
end
end
else puts 'file-type not recognised: ' + @opt.fns
end
else puts 'file not found: ' + @opt.fns
end
(defined? markup.version) \
? markup.version
: 'markup type/version not determined'
end
def markup_version?
if @opt.fns.empty?
@opt.files.each do |fns|
@opt.fns=fns
determine_markup_version
end
else determine_markup_version
end
end
end
class MarkupHistory
def initialize(opt)
@opt=opt
end
def sisu_3_0
<<WOK
SiSU 3.0 same as 2.0, apart from change to headers
see document markup samples, and sisu --help headers
WOK
end
def sisu_2_0
<<WOK
SiSU 2.0 same as 1.0, apart from the changing of headers and the addition of a monospace tag
related headers now grouped, e.g.
@title:
:subtitle:
@creator:
:author:
:translator:
:illustrator:
see document markup samples, and sisu --help headers
the monospace tag takes the form of a has '#' \#{ this enclosed text would be monospaced }#
WOK
end
def sisu_1_0
<<WOK
SiSU 1.0 same as 0.69
WOK
end
def sisu_0_69
<<WOK
SiSU 0.69 (same as 1.0) as previous (0.57) with the addition of book index tags
/^=\{.+?\}$/
e.g. appended to a paragraph, on a new-line (without a blank line in between)
logical structure produced assuming this is the first text "object"
={GNU/Linux community distribution:Debian+2|Fedora|Gentoo;Free Software Foundation+5}
Free Software Foundation, 1-6
GNU/Linux community distribution, 1
Debian, 1-3
Fedora, 1
Gentoo,
WOK
end
def sisu_0_66
<<WOK
SiSU 0.66 same as previous, adds semantic tags
/[:;]\{.+?\}[:;][a-z+]/
e.g. :{ Ralph last;{Amissah};last }:author
WOK
end
def sisu_0_65
<<WOK
SiSU 0.65 same as previous, adds semantic tags
/[a-z+][:;]\{.+?\}[:;][a-z+]/
e.g. author:{ Ralph last;{Amissah};last }:author
WOK
end
def sisu_0_57
<<WOK
SiSU 0.57 (a subset of 1.0) is the same as 0.42 with the introduction of some
a shortcut to use the headers @title and @creator in the first heading
[expanded using the contents of the headers @title: and @author:]
:A~ @title by @author
WOK
end
def sisu_0_42
<<WOK
SiSU 0.42 (a subset of 1.0) is the same as 0.38 with the introduction of some additional endnote types,
Introduces some varations on endnotes, in particular the use of the asterisk
~{* for example for describing an author }~ and ~{** for describing a second author }~
* for example for describing an author
** for describing a second author
and ~[* my note ]~ or ~[+ another note ]~ which numerically increments an
asterisk and plus respectively
*1 my note
+1 another note
WOK
end
def sisu_0_38
<<WOK
SiSU 0.38 (a subset of 1.0) introduced alternative experimental header and heading/structure markers,
@headername: and headers :A~ :B~ :C~ 1~ 2~ 3~
as the equivalent of (the superceded)
0~headername and headers 1~ 2~ 3~ 4~ 5~ 6~
The internal document markup of SiSU 0.16 remains valid and standard
Though note that SiSU 0.37 introduced a new file naming convention
SiSU has in effect two sets of levels to be considered, using 0.38 notation
A-C headings/levels, pre-ordinary paragraphs /pre-substantive text, and
1-3 headings/levels, levels which are followed by ordinary text.
This may be conceptualised as levels A,B,C, 1,2,3, and using such letter
number notation, in effect:
A must exist, optional B and C may follow in sequence (not strict)
1 must exist, optional 2 and 3 may follow in sequence
i.e. there are two independent heading level sequences A,B,C and 1,2,3
(using the 0.16 standard notation 1,2,3 and 4,5,6)
on the positive side:
* the 0.38 A,B,C,1,2,3 alternative makes explicit an aspect of structuring
documents in SiSU that is not otherwise obvious to the newcomer (though
it appears more complicated, is more in your face and likely to be
understood fairly quickly)
* the substantive text follows levels 1,2,3 and it is 'nice' to do
most work in those levels
WOK
end
def sisu_0_37
<<WOK
SiSU 0.37 introduced the file naming convention, that remains in use in SiSU
v1 and v2, using the file extensions .sst .ssm and .ssi
to replace .s1 .s2 .s3 .r1 .r2 .r3 and .si
this is captured by the following file 'rename' instruction:
rename 's/\.s[123]$/\.sst/' *.s{1,2,3}
rename 's/\.r[123]$/\.ssm/' *.r{1,2,3}
rename 's/\.si$/\.ssi/' *.si
The internal document markup remains unchanged, from SiSU 0.16
WOK
end
def sisu_0_16
<<WOK
SiSU 0.16 (0.15 development branch) introduced the use of
the header 0~ and headings/structure 1~ 2~ 3~ 4~ 5~ 6~
in place of the 0.1 header, heading/structure notation
WOK
end
def sisu_0_1
<<WOK
SiSU 0.1 headers and headings structure represented by
header 0{~ and headings/structure 1{ 2{ 3{ 4{~ 5{ 6{
WOK
end
def help_query
<<WOK
sisu --query=[sisu version [0.38] or 'history]
provides a short history of changes to SiSU markup
WOK
end
def help_identify
<<WOK
sisu --identify [filename]
attempts to identify the SiSU markup used in a file
WOK
end
def query
tell=if @opt.selections.str =~/--query/
tell=case @opt.selections.str
when /history/
"#{sisu_3_0}#{sisu_2_0}#{sisu_1_0}#{sisu_0_69}#{sisu_0_66}#{sisu_0_57}#{sisu_0_42}#{sisu_0_38}\n#{sisu_0_37}\n#{sisu_0_16}\n#{sisu_0_1}"
when /3.0/
"#{sisu_3_0}#{sisu_2_0}#{sisu_1_0}#{sisu_0_69}#{sisu_0_66}#{sisu_0_57}#{sisu_0_42}#{sisu_0_38}#{sisu_0_16}"
when /2.0/
"#{sisu_2_0}#{sisu_1_0}#{sisu_0_69}#{sisu_0_66}#{sisu_0_57}#{sisu_0_42}#{sisu_0_38}#{sisu_0_16}"
when /1.0/
"#{sisu_1_0}#{sisu_0_69}#{sisu_0_66}#{sisu_0_57}#{sisu_0_42}#{sisu_0_38}#{sisu_0_16}"
when /0.69/
"#{sisu_0_69}#{sisu_0_66}#{sisu_0_57}#{sisu_0_42}#{sisu_0_38}#{sisu_0_16}"
when /0.66/
"#{sisu_0_66}#{sisu_0_57}#{sisu_0_42}#{sisu_0_38}#{sisu_0_16}"
when /0.65/
"#{sisu_0_65}#{sisu_0_57}#{sisu_0_42}#{sisu_0_38}#{sisu_0_16}"
when /0.57/
"#{sisu_0_57}#{sisu_0_42}#{sisu_0_38}#{sisu_0_16}"
when /0.42/
"#{sisu_0_42}#{sisu_0_38}#{sisu_0_16}"
when /0.38/
"#{sisu_0_38}#{sisu_0_16}"
when /0.37/
"#{sisu_0_37}\n#{sisu_0_16}"
when /0.1[6-9]|0.2[0-9]|0.3[0-6]/
"#{sisu_0_16}\n#{sisu_0_1}"
when /0.[1-9]|0.1[1-4]/
sisu_0_1
else puts "NOT RECOGNISED: #{@opt.selections.str}"
help_query
end
tell
else help_query
end
end
end
end
__END__
#%% to use as independent program ------------------------->
f=$*
cf=f[0].to_s
f.shift
match_and_replace=[]
unless f.length > 0; f=Dir.glob("[a-z]*.ss?") #restricted to sisu type files, it need not be
end
puts "SiSU files:"
puts f
f.each do |x|
SiSU_Markup::MarkupIdentify.new(x).markup_version?
end
#+END_SRC
* sst_do_inline_footnotes.rb
#+BEGIN_SRC ruby :tangle "../lib/sisu/sst_do_inline_footnotes.rb"
<<sisu_document_header>>
module SiSU_ConvertFootnotes
require_relative 'se' # se.rb
include SiSU_Env
require_relative 'dp' # dp.rb
include SiSU_Param
require_relative 'ao_syntax' # ao_syntax.rb
include SiSU_AO_Syntax
require_relative 'i18n' # i18n.rb
class Instantiate < SiSU_Param::Parameters::Instructions
@@flag={} #Beware!!
def initialize
@@flag['table_to']=false
@@counter=@@column=@@columns=@@flag_vocab=0
@@endnote={}
@@endnote_array=@@word_mode=[]
@@endnote_call_counter=1
@@line_mode=''
end
end
class Source <Instantiate
@@ao_array=[]
@@fns=nil
def initialize(opt)
@opt=opt
@@fns||@opt.fns
@my_make=SiSU_Env::CreateFile.new(@opt.fns)
@fnm=SiSU_Env::InfoFile.new(@opt.fns).marshal.ao_content
end
def read #creates ao
begin
@@ao_array=[]
@@fns=@opt.fns
create_ao
rescue
SiSU_Errors::Rescued.new($!,$@,@opt.cmd,@opt.fns).location do
__LINE__.to_s + ':' + __FILE__
end
ensure
Instantiate.new
end
end
def get #reads ao, unless does not exist then creates first
begin
ao=[]
unless @@fns==@opt.fns
@@fns=@opt.fns
@@ao_array=[]
end
ao=(@@ao_array.empty?) \
? read_fnm
: @@ao_array.dup #check
rescue
SiSU_Errors::Rescued.new($!,$@,@opt.cmd,@opt.fns).location do
__LINE__.to_s + ':' + __FILE__
end
ensure
Instantiate.new
end
end
protected
def create_ao
ao_array=[]
SiSU_Screen::Ansi.new(
@opt.act[:color_state][:set],
'convert footnotes'
).green_title_hi unless @opt.act[:quiet][:set]==:on
file_array=IO.readlines(@opt.fns,'')
file_array.each do |l|
if l =~/\r\n/ then l.gsub!(/\r\n/,"\n")
end
end
meta=file_array.dup
meta=meta.join.split("\n\n") #check whether can be eliminated, some of these are large objects to have twice
@md=SiSU_Param::Parameters::Instructions.new(meta,@opt).extract
if @md.en[:mismatch]==0 \
or @md.opt.selections.str =~/=footnotes-force/
meta=nil
ao=SiSU_ConvertFootnotes::Make.new(@md,file_array).song
SiSU_Screen::Ansi.new(
@opt.act[:color_state][:set],
@opt.fns,
"#{@md.fns}.fn"
).output if @md.opt.act[:verbose][:set]==:on
SiSU_Screen::Ansi.new(
@opt.act[:color_state][:set],
"#{@md.fns}.fn -> #{@md.fns}.fn"
).txt_red unless @md.opt.act[:quiet][:set]==:on
ao.each {|s| ao_array << "#{s.strip}\n\n" unless s.strip.empty?}
ao_array
else
SiSU_Screen::Ansi.new(
@md.opt.act[:color_state][:set],
'*WARN* no footnote conversion done, problem with source file',
'to override use --convert=footnote-force (this is not advised)'
).warn unless @md.opt.act[:quiet][:set]==:on
''
end
end
def read_fnm
ao=[]
ao=(FileTest.file?(@fnm)) \
? (File.open(@fnm){ |f| ao=Marshal.load(f)})
: (SiSU_ConvertFootnotes::Source.new(@opt).create_ao) #watch
end
end
class Output
def initialize(md,data)
@md,@data=md,data
@my_make=SiSU_Env::CreateFile.new(@md.fns)
SiSU_Env::InfoEnv.new(@md.fns)
@hard="#{Dir.pwd}/#{@md.fns}.fn"
end
def hard_output
filename_note=@my_make.file_note
@data.each {|s| filename_note.puts s.strip + "\n\n" unless s.strip.empty?}
end
end
class Make
@@endnote={}
@@endnote_array=@@word_mode=[]
@@endnote_call_counter=1
@@comment='%'
@@flag={ ['table_to']=>false }
def initialize(md,data)
@md,@data=md,data
@@word_mode=[]
@env=SiSU_Env::InfoEnv.new(@md.fns)
l=SiSU_Env::StandardiseLanguage.new(@md.opt.lng).language
@language=l[:n]
@translate=SiSU_Translate::Source.new(@md,@language)
end
def reset
@@counter=@@column=@@columns=@@flag_vocab=0
@@endnote={}
@@endnote_array=@@word_mode=[]
@@endnote_call_counter=1
@@line_mode=''
end
def song
reset
data=@data
@metafile="#{@env.processing_path.ao}/#{@md.fns}.meta"
SiSU_Env::CreateFile.new(@md.fns)
data=data.join.split("\n\n")
data_new=[]
data.each do |x|
data_new << (x =~ /\n\n/m) \
? (x.split(/\n\n+/))
: x
end
data=data_new.flatten
data=SiSU_ConvertFootnotes::Make.new(@md,data).character_check
data=SiSU_ConvertFootnotes::Make.new(@md,data).endnotes
SiSU_ConvertFootnotes::Output.new(@md,data).hard_output
reset
data
end
protected
def vocabulary
data=@data
tuned_file,vocab_insert=[],[]
data.each do |para|
if para =~/^1~/ \
and @@flag_vocab==0
vocab_insert << '@vocabulary: lex' << "\n\n" << para
tuned_file << vocab_insert unless para.nil?
@@flag_vocab=1
else tuned_file << para unless para.nil?
end
end
tuned_file
end
def character_check
reset
data=@data
@tuned_file=[]
endnote_no=1
data.each do |para|
para.strip!
para.gsub!(/^[{~}]\s*$/,'')
para.gsub!(/^#{@@comment}.*/,'') #remove comment and divider #%
para.gsub!(/<~#>|~#\s*/,'~#')
para.gsub!(/-#\s*/,'-#')
para.gsub!(/(~\{ )\s+/,'\1')
para.gsub!(/ \/\//,'<br />') #added 2004w29
para.gsub!(/<br>/,'<br />') #needed by xml, xhtml etc.
para.gsub!(/`/,"'")
para.gsub!(/\342\200\231/,"'") #if para =~/’/ #Avoid #‘ ’ #“ ”
para.gsub!(/\t/,' ')
para.gsub!(/�/,' ') #watch, replace with char code
para.gsub!(/[“”]/,'""')
para.gsub!(/[–—]/,'-') #— – chk
para.gsub!(/·/,'*')
para.gsub!(/\\copy(?:right)?\b/,'©')
para.gsub!(/\\trademark\b|\\tm\b/,'®')
para.gsub!(/\44/,'$') #$ watch
para=para + "\n"
case para
when /\^~/ # endnotes
#% Note must do this first (earlier loop) and then enter gathered data into ~^\d+
sub_para=para.dup
@@endnote_array << sub_para.gsub!(/\n/,'').gsub!(/\^~\s+(.+)\s*/,'~{ \1 }~').strip
endnote_no+=1
para=nil if para =~/\^~ .+/ #removes 'binary' endnote now in endnote array for later insertion
end
@tuned_file << para unless para.nil?
end
@tuned_file
end
def name_endnote_seg
data=@data
@tuned_file=[]
data.each do |para|
para.gsub!(/<:3>\s*<:ee>/,
"#{@@endnote['special_align']} <p /><br />\r " +
"#{@@endnote['seg_name_3']} <p /> " +
"#{@@endnote['special_align_close']}")
para.gsub!(/<:2>\s*<:ee>/,
"#{@@endnote['special_align']} <p /><br />\r " +
"#{@@endnote['seg_name_2']} <p />" +
"#{@@endnote['special_align_close']}")
para.gsub!(/<:1>\s*<:ee>/,
"#{@@endnote['special_align']} <p /><br />\r " +
"#{@@endnote['seg_name_1']} <p /> " +
"#{@@endnote['special_align_close']}")
@tuned_file << para
end
if @md.flag_auto_endnotes \
and @md.flag_separate_endnotes_make
@tuned_file << "\n1~endnotes Endnotes" #prob numbering, revisit
end
@tuned_file << "\n<ENDNOTES>"
@tuned_file
end
def owner_details_seg
data << '1~owner.details Owner Details'
end
def number_sub_heading(para,num,title_no)
case para
when /#{num}~- / then para.gsub!(/#{num}~- /,"#{title_no} ")
when /^#{num}~#\s*/ then para.gsub!(/^#{num}~#\s*/,"#{title_no} ")
when /^#{num}~[a-z_\.]+ /
para.gsub!(/^#{num}~([a-z_\.]+)\s+(.+)/i,%{#{num}~\\1 #{title_no} \\2 <:name##{title_no}>})
else para.gsub!(/^#{num}~ /,"#{num}~#{title_no} #{title_no} ") #main
end
if @md.toc_lev_limit \
and @md.toc_lev_limit < num
para.gsub!(/^[2-6]~(?:~\S+)?\s*/,'!_ ')
end
para
end
def set_heading_top #% make sure no false positives
unless @md.set_heading_top
if (@md.opt.act[:verbose_plus][:set]==:on \
|| @md.opt.act[:maintenance][:set]==:on)
puts "\tdocument contains no top level heading, (will have to manufacture one)"
end
data=@data
@tuned_file=[]
data.each do |para|
unless @md.set_heading_top
if para !~/^(?:@\S+:|0~\S+)\s/m \
and para !~/\A\s*\Z/m
@md.set_heading_top=true
head=(@md.title.full) \
? (":A~ #{@md.title.full}")
: (':A~ [no title provided]')
@tuned_file << head
end
end
@tuned_file << para
end
@tuned_file
end
end
def set_heading_seg #% make sure no false positives
unless @md.set_heading_seg
if (@md.opt.act[:verbose_plus][:set]==:on \
|| @md.opt.act[:maintenance][:set]==:on)
puts "\tdocument contains no segment level, (will have to manufacture one)"
end
data=@data
@tuned_file=[]
data.each do |para|
unless @md.set_heading_seg
if para !~/^(?:@\S+:|0~\S+|:[ABC]~)/m \
and para !~/\A\s*\Z/m \
and para !~/<:p[bn]>/
@md.set_heading_seg=true
head=(@md.title.full) \
? ("1~seg [#{@md.title.full}]")
: ('1~seg [segment]')
@tuned_file << head
end
end
@tuned_file << para
end
@tuned_file
end
end
def set_header_title #% make sure no false positives
unless @md.set_header_title
if (@md.opt.act[:verbose_plus][:set]==:on \
|| @md.opt.act[:maintenance][:set]==:on)
puts "\t no document title provided, (will have to manufacture one)"
end
data=@data
@tuned_file=[]
data.each do |para|
unless @md.set_header_title
if para !~/^%{1,2}\s/m \
and para !~/\A\s*\Z/m
@tuned_file << "0~title #{@md.heading_seg_first}"
@md.title.full=@md.heading_seg_first
@md.set_header_title=true
end
end
@tuned_file << para
end
@tuned_file
end
end
def endnotes #% endnote work zone
data=@data
@tuned_file=[]
endnote_ref=1
data.each do |para|
case para # manually numbered endnotes <!e(\d)!> <!e_(\d)!> -->
when /~\{\s+.+?\}~/ # auto-numbered endnotes <!e!> <!e_!> -->
para.gsub!(/\s*\}~/,' }~') # required 2003w31
@word_mode=para.scan(/\S+/)
word_mode=SiSU_ConvertFootnotes::Make.new(@md,@word_mode).endnote_call_number
para=word_mode.join(' ')
endnote_ref+=1
when /~\^(?:\s|$)|<:e>/ #%Note inserts endnotes previously gathered from /^(<!e[:_]!>|[-~]\{{3})/ (in earlier loop)
word_mode=para.scan(/\S+/)
word_mode=SiSU_ConvertFootnotes::Make.new(@md,word_mode).endnote_call_number
para=word_mode.join(' ')
endnote_ref+=1
end
@tuned_file << para
end
@tuned_file
end
def endnote_call_number
data=@data
data.each do |word|
case word
when /~\{/
unless word =~/~\{\*+/
@@endnote_call_counter+=1
end
when /~\^|<:e>/
word.gsub!(/~\^|<:e>/,"#{@@endnote_array[@@endnote_call_counter-1]}")
@@endnote_call_counter+=1
end
end
end
def strip_clean_extra_spaces(s) # ao output tuned
s=s.dup
s=s.gsub(/[ ]+([,.;:?](?:$|\s))/,'\1')
s=s.gsub(/ [ ]+/,' ')
s=s.gsub(/^ [ ]+/,'')
s=s.gsub(/ [ ]+$/,'')
s=s.gsub(/(<\/[bi]>')[ ]+(s )/,'\1\2')
end
def strip_clean_of_markup(s) # used for digest, define rules, make same as in db clean
s=s.dup
s=s.gsub(/(?:<\/?[ib]>|^:[A-C]~\S+|^[1-6]~\S+|~\{\d+\s.+?\}~)/,'') # markup and endnotes removed
#% same as db clean -->
s=s.gsub(/<del>(.+?)<\/del>/,'DELETED(\1)') # deletions
s=s.gsub(/<sup>(\d+)<\/sup>/,'[\1]')
s=s.gsub(/(?: \\;|#{Mx[:nbsp]})+/,' ') #checking source Mx not necessary
s=s.gsub(/\{.+?\.(?:png|jpg|gif).+?\}(?:https?|file|ftp)\\\:\S+ /,' [image] ') # else image names found in search
s=s.gsub(/#{Mx[:lnk_o]}.+?\.(?:png|jpg|gif).+?#{Mx[:lnk_c]}#{Mx[:url_o]}\S+?#{Mx[:url_c]}/,' [image] ') # else image names found in search, re-check
s=s.gsub(/\s\s+/,' ')
s=s.strip
end
end
end
__END__
@particulars=SiSU_Particulars::CombinedSingleton.instance.get_all(opt)
ao_array=@particulars.ao_array # ao file drawn here
#+END_SRC
* sst_convert_markup.rb
#+BEGIN_SRC ruby :tangle "../lib/sisu/sst_convert_markup.rb"
<<sisu_document_header>>
module SiSU_Modify
require_relative 'sst_identify_markup' # sst_identify_markup.rb
require_relative 'sst_from_xml' # sst_from_xml.rb
require_relative 'utils_response' # utils_response.rb
class ConvertMarkup
include SiSU_Response
def initialize(opt)
@opt=opt
@description='This is a script that contains canned text conversions for reuse'
@warn='WARNING, PROCEED AT YOUR OWN RISK, will make file changes.'
end
def current_match_and_replace
convert_37_to_38
end
def message(text)
response=''
unless @opt.cmd=~/QQ/ \
or @opt.act[:quiet][:set]==:on
response=response?(%{#{ text}\nProceed? })
end
end
def help
print <<WOK
#{@description}
sisu --convert --to38 [filename/wildcard]
converts pre 0.37 sisu markup to 0.38 experimental
[--37to38]
sisu --convert --to37 [filename/wildcard]
converts pre 0.37 sisu markup to 0.38 experimental
[--38to37]
sisu --convert --36to37 [filename/wildcard]
converts pre 0.36 file-name, to 0.37 file-name
[--36to37]
sisu --identify [filename]
attempts to identify markup version used in file
sisu --query [version number]
gives short summary of distinguishing characteristic
of that version of markup
WOK
exit
end
#%% substitutions to be made
def convert_37_to_38
message("#{@warn}\nConvert sisu markup from 0.37 to 0.38")
[
[/^0~(\S+?)([+-])\s+/, '@\1:\2 ', //],
[/^0~(\S+)\s+/, '@\1: ', //],
[/^@toc:\s+/, '@structure: ', //],
[/^1~/, ':A~', //],
[/^2~/, ':B~', //],
[/^3~/, ':C~', //],
[/^4~/, '1~', //],
[/^5~/, '2~', //],
[/^6~/, '3~', //],
[/^7~/, '4~', //],
[/^8~/, '5~', //],
[/^9~/, '6~', //],
[/1/, ':A', /^@(?:level|markup):\s/],
[/2/, ':B', /^@(?:level|markup):\s/],
[/3/, ':C', /^@(?:level|markup):\s/],
[/4/, '1', /^@(?:level|markup):\s/],
[/5/, '2', /^@(?:level|markup):\s/],
[/6/, '3', /^@(?:level|markup):\s/]
]
end
def convert_38_to_37
message("#{@warn}\nConvert sisu markup from 0.38 to 0.37")
[
[/^@(\S+?):([+-])\s+/, '0~\1\2 ', //],
[/^@(\S+?):\s+/, '0~\1 ', //],
[/^0~structure\s+/, '0~toc ', //],
[/^1~/, '4~', //],
[/^2~/, '5~', //],
[/^3~/, '6~', //],
[/^4~/, '7~', //],
[/^5~/, '8~', //],
[/^6~/, '9~', //],
[/^:?A~/, '1~', //],
[/^:?B~/, '2~', //],
[/^:?C~/, '3~', //],
[/1/, '4', /^0~(?:level|markup)\s/],
[/2/, '5', /^0~(?:level|markup)\s/],
[/3/, '6', /^0~(?:level|markup)\s/],
[/:?A/, '1', /^0~(?:level|markup)\s/],
[/:?B/, '2', /^0~(?:level|markup)\s/],
[/:?C/, '3', /^0~(?:level|markup)\s/]
]
end
def convert_filename_36_to_37
@opt.files.each do |f|
s=case f
when /(\.s[1-3])$/ then f.sub($1,'.sst')
when /(\.r[1-3])$/ then f.sub($1,'.ssm')
when /(\.ri)$/ then f.sub($1,'.ssi')
else f
end
pwd=Dir.pwd
unless f==s
unless File.exist?("#{pwd}/#{s}")
puts "./#{f} -> ./#{s}"
FileUtils::cp("#{pwd}/#{f}","#{pwd}/#{s}")
else "File already exists, < #{s} > will not overwrite"
end
end
end
end
def convert_to_simple_xml_model_sax
SiSU_SimpleXML_ModelSax::Convert.new(@opt).read
end
def convert_to_simple_xml_model_dom
SiSU_simple_xml_model_dom::Convert.new(@opt).read
end
def convert_to_simple_xml_model_node
SiSU_simple_xml_model_node::Convert.new(@opt).read
end
def convert_kdi_to_sst
SiSU_Kdissert::Convert.new(@opt).read
end
def convert_s_xml_to_sst
SiSU_sstFromXML::Convert.new(@opt).read
end
def convert_footnotes
require_relative 'sst_do_inline_footnotes'
SiSU_ConvertFootnotes::Source.new(@opt).read
end
def conversion
#%% do it -------------------------->
if @opt.files \
and @opt.files.length > 0
mr=nil
#%% changes to make m match, r replace -------------------------->
if @opt.selections.str =~/--help/ then help
elsif @opt.selections.str =~/(?:convert|to)[=-](?:xml |sxs|sax|sxd|dom|sxn|node)/
ext=case @opt.selections.str
when /(?:convert|to)[=-](?:xml|sxs|sax)/ then '.sxs.xml'
when /(?:convert|to)[=-](?:sxd|dom)/ then '.sxd.xml'
when /(?:convert|to)[=-](?:sxn|node)/ then '.sxn.xml'
end
message("#{@opt.files.inspect}\n\nWARNING, PROCEED AT YOUR OWN RISK,\noverwriting any equivalent file with the extension #{ext}")
mr=case @opt.selections.str
when /(?:convert|to)[=-](?:sxs|sax|xml )/ then convert_to_simple_xml_model_sax
when /(?:convert|to)[=-](?:sxd|dom)/ then convert_to_simple_xml_model_dom
when /(?:convert|to)[=-](?:sxn|node)/ then convert_to_simple_xml_model_node
else help
end
else
mr=case @opt.selections.str
when /(?:(?:37)?to-?38|--(?:convert|to)[=-](?:current|0.38))/ then convert_37_to_38
when /(?:(?:38)?to-?37|--(?:convert|to)[=-](?:0.37))/ then convert_38_to_37
when /(?:36to37)/ then convert_filename_36_to_37
when /(?:convert|from)[=-]kdi/ then convert_kdi_to_sst
when /(?:(?:convert|from)[=-])?(?:xml_to_sst|xml2sst|sxml|sxs|sxd|sxd)/ then convert_s_xml_to_sst
when /(?:convert|to)[=-]footnotes/ then convert_footnotes
when /convert|default/ then current_match_and_replace
else help
end
end
unless @opt.selections.str =~/kdi/
match_and_replace=mr
#start_processing =/not used in this example/i
end_processing =/END\s+OF\s+FILE/
i=@opt.fns
if i =~/(?:\.sst|\.ssm|\.ssi)$/
@new,@matched,@flag_start,@flag_end,@empty1,@empty2=true,false,false,false,false,false
o="#{i}.bk" #o is for old
markup_version=SiSU_Markup::MarkupIdentify.new(@opt).markup_version?
if (@opt.selections.str=~/37/ and markup_version=~/0.38/) \
or (@opt.selections.str=~/current|38/ and markup_version=~/0.37/)
puts "#{i} #{markup_version}"
file=File.open(i,'r')
cont=file.readlines
file.close
cont.each do |y|
match_and_replace.each do |m,r,w|
if y =~m \
and y =~w
if @new
@new=false
File.unlink(o) if File.exist?(o)
File.rename(i,o)
File.unlink(i) if File.exist?(i)
@file=File.new(i,'w')
@matched=true
break
end
end
end
end
if @matched
puts "conversion match in #{i}" unless @opt.act[:quiet][:set]==:on
@flag_start=true
cont.each do |y|
if y =~end_processing
@flag_end=true
end
if @flag_start \
and not @flag_end
match_and_replace.each do |m,r,w|
if y =~m \
and y =~w
puts m.inspect + ' -> ' + r unless @opt.act[:quiet][:set]==:on
if (@opt.act[:verbose][:set]==:on \
|| @opt.act[:verbose_plus][:set]==:on \
|| @opt.act[:maintenance][:set]==:on)
puts "in: #{y}"
end
y.gsub!(m,r) if m and r
if (@opt.act[:verbose][:set]==:on \
|| @opt.act[:verbose_plus][:set]==:on \
|| @opt.act[:maintenance][:set]==:on)
puts "out: #{y}"
end
end
end
end
@empty1=(y=~/^\s*$/) \
? true
: false
@file.puts y unless (@empty1==true and @empty2==true)
@empty2=(y=~/^\s*$/) \
? true
: false
end
@file.close
else puts "NO conversion match in #{i}" unless @opt.act[:quiet][:set]==:on
end
else
if (@opt.act[:verbose][:set]==:on \
|| @opt.act[:verbose_plus][:set]==:on \
|| @opt.act[:maintenance][:set]==:on)
puts "Requested conversion #{@opt.selections.str} markup #{markup_version} identified in #{i}"
end
end
end
end
else puts 'this routine makes permanent changes to the contents of the files matched, as instructed within [no matches]'
end
end
end
end
#%% files to match for this conversion set ------------------------->
require_relative 'hub_options' # hub_options.rb
argv=$*
base_path=Dir.pwd
@opt=SiSU_Commandline::Options.new(argv,base_path)
case @opt.selections.str
when /=kdi/
SiSU_Modify::ConvertMarkup.new(@opt).conversion
when /(?:36|37|38)?to-?(?:37|38)|--convert|--to|--from|default/
@opt.files.each do |fns|
@opt.fns=fns
SiSU_Modify::ConvertMarkup.new(@opt).conversion
end
else
@opt.selections.str='--help'
SiSU_Modify::ConvertMarkup.new(@opt).help
end
__END__
#+END_SRC
* document header
#+NAME: sisu_document_header
#+BEGIN_SRC text
#encoding: utf-8
=begin
- Name: SiSU
- Description: documents, structuring, processing, publishing, search
sst
- Author: Ralph Amissah
<ralph.amissah@gmail.com>
- Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019,
2020, 2021, Ralph Amissah,
All Rights Reserved.
- License: GPL 3 or later:
SiSU, a framework for document structuring, publishing and search
Copyright (C) Ralph Amissah
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
If you have Internet connection, the latest version of the GPL should be
available at these locations:
<http://www.fsf.org/licensing/licenses/gpl.html>
<http://www.gnu.org/licenses/gpl.html>
<http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>
- SiSU uses:
- Standard SiSU markup syntax,
- Standard SiSU meta-markup syntax, and the
- Standard SiSU object citation numbering and system
- Homepages:
<http://www.sisudoc.org>
- Git
<https://git.sisudoc.org/projects/>
<https://git.sisudoc.org/projects/?p=software/sisu.git;a=summary>
<https://git.sisudoc.org/projects/?p=markup/sisu-markup-samples.git;a=summary>
=end
#+END_SRC
|