-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreole.e
4280 lines (3709 loc) · 111 KB
/
creole.e
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
--
-- Creole Markup
--
namespace creole
ifdef UNITTEST then
include std/unittest.e
end ifdef
include std/get.e
include std/sequence.e as seq
include std/search.e as search
include std/text.e
include std/math.e
include std/map.e
include std/types.e
include std/sort.e
include std/map.e
include std/filesys.e
include euphoria/syncolor.e
object gDebug gDebug = 0
global enum -- Action Codes for the Generator
HostID, -- (1) id of application hosting this parser
OptReparseHeadings, -- "" don't reparse, anything then reparse.
InternalLink, -- internal link
QualifiedLink, -- A link with a file name and an anchor point within that file
InterWikiLink, -- Inter wiki link
InterWikiLinkError, -- Inter wiki link w/o a valid definition
NormalLink, -- normal link
InternalImage, -- internal image
InterWikiImage, -- interwiki image
NormalImage, -- (10) normal image
Heading, -- headings
OrderedList, -- An ordered (numbered) list
UnorderedList, -- An unordered (bullet) list
ListItem, -- An item in a list
ItalicText, -- italized text
BoldText, -- bolded text
MonoText, -- monospace font text
UnderlineText, -- underlined text
Superscript, -- superscripted text
Subscript, -- (20) subscripted text
StrikeText, -- striked out text
InsertText, -- Inserted text
ColorText, -- colored text
CodeExample, -- program code example
TableDef, -- entire table
HeaderRow, -- table header row
HeaderCell, -- table header cell
NormalRow, -- table row
NormalCell, -- table cell
NonBreakSpace, -- (30) non-breaking space
ForcedNewLine, -- break the line now
HorizontalLine, -- a line across the display
NoWikiBlock, -- block style no wiki parsed text
NoWikiInline, -- inline style no wiki parsed text
DefinitionList, -- A set of definitions
BeginIndent, -- Start a new indentation level
EndIndent, -- End the current indentation level
Paragraph, -- defines a paragraph
Division, -- defines a division
Document, -- (40) defines a document
Bookmark, -- define a bookmark
Sanitize, -- Ensure input has no illegal characters
SanitizeURL, -- Ensure URL has no illegal characters
PassThru, -- Raw text is being asked for.
CamelCase, -- Convert a CamelCase word to normal text
Plugin, -- A plugin has been called for
ContextChange, -- A new !!CONTEXT: record found.
Comment, -- A comment
Quoted, -- A quoted section
LastActionCode -- (50)
global enum -- Action Codes for the Creole
Get_Headings, -- returns all the headings and their bookmarks
Get_CurrentHeading, -- returns the heading last processed and its bookmark
Get_CurrentLevels, -- returns a sequence of the current heading level
Get_Macro, -- returns the requested macro's definition
Get_Bookmarks, -- returns all the known bookmarks
Get_Elements, -- returns all the known elements
Get_Context, -- returns the current context value.
Get_Styles, -- returns the style stack
Set_Macro, -- stores the supplied macro
Set_Option, -- sets the supplied option
Disallow, -- Turn off specific markup parsing
Allow, -- Turn on specific markup parsing
CO_MaxNumLevel, -- Depth of heading levels to number
CO_UpperCase, -- Set of characters that are the uppercase ones.
CO_LowerCase, -- Set of characters that are the lowercase ones.
CO_SpecialWordChars,-- Set of characters that are also allowed in 'words'
CO_Digits, -- Set of characters that are the digits.
CO_CodeColors, -- Set of ten strings that are the colors for source code.
CO_Protocols, -- Set of protocols that are recognized as linkable.
CO_SplitLevel, -- Set the heading level at which to split into separate files.
CO_SplitName, -- Set the file prefix for the split files.
CO_AllowMacros, -- Set the flag to allow/disallow macros.
CO_Verbose, -- Set the verbose flag
MU_Bold, -- Allow/Disallow bold markup
MU_Italic, -- Allow/Disallow italic markup
MU_Monospace, -- Allow/Disallow monospace markup
MU_Underline, -- Allow/Disallow underline markup
MU_Superscript, -- Allow/Disallow superscript markup
MU_Subscript, -- Allow/Disallow subscript markup
MU_Strikethru, -- Allow/Disallow strikethru markup
MU_Insert, -- Allow/Disallow insert markup
MU_CamelCase, -- Allow/Disallow camel case detection
LastCreoleActionCode
enum -- Temporary embedded tags
TAG_ENDFILE = -100,
TAG_PLUGIN,
TAG_STARTPARA,
TAG_ENDPARA,
TAG_UNRESOLVED,
TAG_TABLE_BAR,
$
constant kLeadIn = "=*/<{#[\n\\-|~!_:^,+`;>%@&"
constant kDecorTag = "*/#_^,-+"
constant kDecorAction = {
BoldText, -- *
ItalicText, -- /
MonoText, -- #
UnderlineText, -- _
Superscript, -- ^
Subscript, -- ,
StrikeText, -- -
InsertText, -- +
0
}
constant vLineBeginings = {"#", "*","=","|", "{{{","<eucode>", ":", ">", "----\n", "%(", "%%"}
integer vParser_rid
integer vFinal_Generator_rid
integer vMacro_Processor_rid
integer vMaxNumLevel = 0
integer vSplitLevel = 0
integer vAllowMacros = 0
integer vAllowCamelCase = 1
sequence vAllowedDecorations = "*/#_^,-+"
sequence vSplitName = "file"
sequence vOutputFile = {}
integer vExplicitOutput = 0
sequence vReparseHeading
sequence vHostID
sequence vProtocols
sequence vUpperCase
sequence vLowerCase
sequence vAllLetters
sequence vWordChars
sequence vNameChars
sequence vSpecialWordChars
sequence vDigits
sequence vWhiteSpace
sequence vUnresolved = {}
sequence vHeadingNums = {}
sequence vCurrentContext = ""
sequence vCurrentNamespace = ""
sequence vRawContext = ""
sequence vStyle = {"default"}
integer vVerbose = 0
constant vHonorifics = {"mr", "mrs", "miss", "ms", "dr", "sir"}
constant vTLD = {
"aero","asia","biz","cat","com","coop","edu","gov","info","int",
"jobs","mil","mobi","museum","name","net","org","pro","tel","travel"
}
constant vCountry_TLD = -- These must be kept in ascending order.
{
"ac","ad","ae","af","ag","ai","al","am","an","ao","aq","ar","as",
"at","au","aw","ax","az","ba","bb","bd","be","bf","bg","bh","bi",
"bj","bm","bn","bo","br","bs","bt","bv","bw","by","bz","ca","cc",
"cd","cf","cg","ch","ci","ck","cl","cm","cn","co","cr","cu","cv",
"cx","cy","cz","de","dj","dk","dm","do","dz","ec","ee","eg","er",
"es","et","eu","fi","fj","fk","fm","fo","fr","ga","gb","gd","ge",
"gf","gg","gh","gi","gl","gm","gn","gp","gq","gr","gs","gt","gu",
"gw","gy","hk","hm","hn","hr","ht","hu","id","ie","il","im","in",
"io","iq","ir","is","it","je","jm","jo","jp","ke","kg","kh","ki",
"km","kn","kp","kr","kw","ky","kz","la","lb","lc","li","lk","lr",
"ls","lt","lu","lv","ly","ma","mc","md","me","mg","mh","mk","ml",
"mm","mn","mo","mp","mq","mr","ms","mt","mu","mv","mw","mx","my",
"mz","na","nc","ne","nf","ng","ni","nl","no","np","nr","nu","nz",
"om","pa","pe","pf","pg","ph","pk","pl","pm","pn","pr","ps","pt",
"pw","py","qa","re","ro","rs","ru","rw","sa","sb","sc","sd","se",
"sg","sh","si","sj","sk","sl","sm","sn","so","sr","st","su","sv",
"sy","sz","tc","td","tf","tg","th","tj","tk","tl","tm","tn","to",
"tp","tr","tt","tv","tw","tz","ua","ug","uk","us","uy","uz","va",
"vc","ve","vg","vi","vn","vu","wf","ws","ye","yt","yu","za","zm",
"zw"}
-- Elements: This contains a list of interesting elements discovered during
-- the parsing. They are in the order of being found, thus we can
-- use this to do relative positioning processing.
-- format: [1] = Type.
-- 'b' --> A bookmark
-- 'p' --> A plugin
-- [2] = Pointer to Type record. This is an index into the relevant
-- sequence containing records of 'Type'.
sequence vElements = {}
-- Plugins: This contains a list of plugins.
-- format: [1] = Parameters from the <<>> tag.
-- The first parameter is the Plugin name, then remainder
-- are key-value pairs.
-- [2] = The subcontext id. Contains the value of the !!CONTEXT:
-- tag that occured prior to this bookmark.
-- [3] = The name of the generated output file. If blank, there is
-- no file spliting being done.
-- [4] = Pointer to the Element sequence for this plugin.
sequence vPluginList = {}
-- Bookmarks: This contains a list of bookmarks.
-- format: [1] = Type
-- 'h' --> A heading tag auto-generated bookmark
-- [2] = Pointer to Type record. This is an index into the relevant
-- sequence containing records of 'Type'.
-- [3] = The bookmark name.
-- [4] = The subcontext id. Contains the value of the !!CONTEXT:
-- tag that occured prior to this bookmark.
-- [5] = The name of the generated output file. If blank, there is
-- no file spliting being done.
-- [6] = Pointer to the Element sequence for this bookmark.
-- [7] = The name cleaned up for finding purposes
sequence vBookMarks = {}
export enum
BM_TYPE,
BM_POINTER,
BM_NAME,
BM_SUBCONTEXT_ID,
BM_FILENAME,
BM_ELEMENT_PTR,
BM_CLEAN_NAME, -- processed by find_bookmark
BM_GETHEADINGS, -- BM_NAME..BM_ELEMENT_PTR
BM_ELEMENTS, -- type='h' BM_NAME..BM_FILENAME
-- type='p' BM_POINTER..BM_ELEMENT_PTR
$
-- Headings: This contains a list of headings found.
-- format: [1] = Depth (level)
-- [2] = Text (including numbering if applicable)
sequence vHeadings = {}
sequence vHeadingBMIndex = {}
export enum
H_DEPTH,
H_TEXT
sequence vCodeColors
object vMacros
------------------------------------------------------------------------------
procedure init()
------------------------------------------------------------------------------
vReparseHeading = ""
vHostID = ""
vProtocols = {
"HTTP:",
"FTP:",
"FILE:",
"HTTPS:",
"SVN:",
"GOPHER:",
"IRC:",
"NEWS:",
"NTTP:",
"MAILTO:"
}
vCodeColors = {
"#330033", --normal",
"#FF0055", --comment",
"#0000FF", --keyword",
"#00A033", --builtin",
"#330033", --string",
"#880033", --bracket1",
"#993333", --bracket2",
"#0000FF", --bracket3",
"#5500FF", --bracket4",
"#00FF00" --bracket5"
}
set_colors({
{"NORMAL", 1},
{"COMMENT", 2},
{"KEYWORD", 3},
{"BUILTIN", 4},
{"STRING", 5},
{"BRACKET", {6, 7, 8, 9, 10}}
})
vUpperCase = "ESTRADIOBCFGHKLMNPUVWJYQZX"
vLowerCase = lower(vUpperCase)
vAllLetters = vUpperCase & vLowerCase
vDigits = "0123456789"
vSpecialWordChars = "_"
vWordChars = vAllLetters & vSpecialWordChars
vNameChars = vWordChars & vDigits
vWhiteSpace = " \t"
vPluginList = {}
vBookMarks = {}
vHeadings = {}
vHeadingNums = {}
vMacros = map:new()
object base_options = map:load_map( locate_file("creole.opts") )
if map(base_options) then
vProtocols = map:get( base_options, "protocols", vProtocols)
vCodeColors = map:get( base_options, "codecolors", vCodeColors)
end if
end procedure
init()
-- looks for a string of alphanumeric chars enclosed in parenthesis
-- If found, returns a sequence {START, END}
-- If not found, returns 0
------------------------------------------------------------------------------
function find_paren_end(sequence pText)
------------------------------------------------------------------------------
integer lStart
integer lEnd
lStart = find('(', pText)
if lStart = 0 then
return 0
end if
lEnd = find(')', pText, lStart + 1)
if lEnd = 0 then
return 0
end if
if not t_alnum( pText[lStart+1 .. lEnd - 1]) then
return 0
end if
return {lStart, lEnd}
end function
-- looks for the end of the current word.
-- If found, returns an integer with the index of the last character in the word.
------------------------------------------------------------------------------
function find_word(sequence pText, integer pStart = 1)
------------------------------------------------------------------------------
for i = pStart to length(pText) do
if not t_alnum(pText[i]) then
return i - 1
end if
end for
return length(pText)
end function
-- looks for common url formats: X.X.X and X.X.X.X
-- If found, returns a sequence {{START, END}, {word1_start, word1_end}, ..., {wordn_start, wordn_end}}
-- If not found, returns 0
------------------------------------------------------------------------------
function find_url(sequence pText, integer pStart)
------------------------------------------------------------------------------
sequence lRes
integer lStart
integer lEnd
integer lPos
if length(pText) - pStart < 7 then
-- The minimum length of any text to contain a valid URL is 7 characters.
return 0
end if
lPos = pStart - 1
while lPos < length(pText) do
lRes = {{0,0}}
lStart = lPos
-- Find first white space, then skip over it.
for i = lPos + 1 to length(pText) do
if t_space(pText[i]) then
lPos = i
exit
end if
end for
if lPos = lStart then
lPos += 1
end if
for i = lPos to length(pText) do
if not t_space(pText[i]) then
lPos = i
exit
end if
end for
if lPos = lStart then
return 0
end if
while length(lRes) < 5 do
lStart = lPos
lEnd = find_word(pText, lStart)
if lEnd < lStart then
lEnd += 1
exit -- Not at the start of a word
end if
lRes = append(lRes, {lStart, lEnd})
lPos = lEnd + 2
if lEnd = length(pText) then
exit -- Must be the last word 'cos we ran out of text.
end if
if not equal(pText[lEnd + 1], '.') then
exit -- Found a terminating non-dot, so no more words.
end if
end while
if length(lRes) < 4 or length(lRes) > 5 then
lPos = lEnd
continue
end if
return lRes
end while
return 0
end function
-- returns a string containing only characters in vNameChars and in pExtra.
------------------------------------------------------------------------------
function cleanup( sequence pText, sequence pExtra = "")
------------------------------------------------------------------------------
integer lPos
sequence lChars
lChars = vNameChars & pExtra
lPos = 0
for i = 1 to length(pText) do
if eu:find( pText[i], lChars) > 0 then
lPos +=1
if lPos < i then
pText[lPos] = pText[i]
end if
end if
end for
if lPos != length(pText) then
return lower(pText[1..lPos])
else
return lower(pText)
end if
end function
ifdef UNITTEST then
test_equal("cleanup 1", "abcdefghi123", cleanup(" a(b)--c ** D !EFg `\thi 1.23$"))
test_equal("cleanup 2", "a(b)cdefghi123", cleanup(" a(b)--c ** D !EFg `\thi 1.23$", "()"))
end ifdef
-- Returns the index to the next character to be processed.
------------------------------------------------------------------------------
function find_eol(sequence pString, integer pFrom=1, integer pIgnoreComment = 0)
------------------------------------------------------------------------------
integer lStart
integer lEOL
integer lLogEOL
integer lEOS
integer lFirstComment
if pFrom > length(pString) then
return pFrom
end if
if pFrom < 1 then
lEOL = 1
else
lEOL = pFrom
end if
while lEOL <= length(pString) and pString[lEOL] != '\n' do
lEOL += 1
end while
lLogEOL = lEOL
if lEOL > length(pString) then
lEOL -= 1
end if
if not pIgnoreComment then
-- Backtrack to see if there is any comments on this line.
lFirstComment = match("!!", pString[pFrom .. lEOL])
if lFirstComment != 0 then
lLogEOL = pFrom
if lFirstComment != pFrom then
while lLogEOL < lEOL do
if lLogEOL > 1 and pString[lLogEOL-1] = '~' then
lLogEOL += 1
continue
end if
if pString[lLogEOL] = '!' then
if pString[lLogEOL + 1] = '!' then
exit
end if
end if
lLogEOL += 1
end while
end if
end if
end if
return lLogEOL
end function
ifdef UNITTEST then
test_equal("find_eol 1", 1, find_eol(""))
test_equal("find_eol 2", 2, find_eol("a\n"))
test_equal("find_eol 3", 4, find_eol("a \n"))
test_equal("find_eol 4", 2, find_eol("a\n\n"))
test_equal("find_eol 5", 1, find_eol("\na\n"))
test_equal("find_eol 6", 3, find_eol(" \na\n"))
test_equal("find_eol 7", 1, find_eol("\n a \n"))
test_equal("find_eol 8", 6, find_eol("n a n"))
test_equal("find_eol 1a", 2, find_eol("", 2))
test_equal("find_eol 2a", 2, find_eol("a\n", 2))
test_equal("find_eol 3a", 4, find_eol("a \n", 2))
test_equal("find_eol 4a", 2, find_eol("a\n\n", 2))
test_equal("find_eol 5a", 3, find_eol("\na\n", 2))
test_equal("find_eol 6a", 3, find_eol(" \na\n", 2))
test_equal("find_eol 7a", 5, find_eol("\n a \n", 2))
test_equal("find_eol 8a", 6, find_eol("n a n", 2))
test_equal("find_eol 1b", 1, find_eol("!!"))
test_equal("find_eol 2b", 2, find_eol("a!!\n"))
test_equal("find_eol 3b", 2, find_eol("a!! \n"))
test_equal("find_eol 4b", 2, find_eol("a!!\n\n"))
test_equal("find_eol 5b", 1, find_eol("\n!!a\n"))
test_equal("find_eol 6b", 3, find_eol(" !!\na\n"))
test_equal("find_eol 7b", 1, find_eol("\n!! a \n"))
test_equal("find_eol 8b", 2, find_eol("n!! a n"))
end ifdef
------------------------------------------------------------------------------
function find_nonspace(sequence pString, integer pFrom = 1)
------------------------------------------------------------------------------
if pFrom < 1 then
pFrom = 1
end if
while pFrom <= length(pString) and eu:find(pString[pFrom]," \t") != 0 do
pFrom += 1
end while
return pFrom
end function
ifdef UNITTEST then
test_equal("find_nonspace 1", 1, find_nonspace(""))
test_equal("find_nonspace 2", 4, find_nonspace(" "))
test_equal("find_nonspace 3", 4, find_nonspace("\t "))
test_equal("find_nonspace 4", 1, find_nonspace("a\t "))
test_equal("find_nonspace 5", 1, find_nonspace("a "))
test_equal("find_nonspace 6", 1, find_nonspace("abc"))
test_equal("find_nonspace 7", 2, find_nonspace(" abc"))
test_equal("find_nonspace 8", 9, find_nonspace(" \t \t \t\t abc"))
test_equal("find_nonspace 1a", 3, find_nonspace("", 3))
test_equal("find_nonspace 2a", 4, find_nonspace(" ", 3))
test_equal("find_nonspace 3a", 4, find_nonspace("\t ", 3))
test_equal("find_nonspace 4a", 5, find_nonspace("a\t ", 3))
test_equal("find_nonspace 5a", 4, find_nonspace("a ", 3))
test_equal("find_nonspace 6a", 3, find_nonspace("abc", 3))
test_equal("find_nonspace 7a", 3, find_nonspace(" abc", 3))
test_equal("find_nonspace 8a", 9, find_nonspace(" \t \t \t\t abc", 3))
end ifdef
------------------------------------------------------------------------------
function find_bookmark(sequence pBMText, sequence pDisplayText, sequence pContext, integer pHere,
object pNameSpace)
------------------------------------------------------------------------------
sequence lCleanBMText
sequence lCleanDisplayText
sequence lCleanStored
sequence lCleanStoredAlt
sequence lPossibles = {}
lCleanBMText = cleanup(pBMText)
lCleanDisplayText = cleanup(pDisplayText)
-- First, look through the bookmarks in the supplied context.
-- Next, look through the bookmarks out of the supplied context.
for x = 1 to 2 do
for i = 1 to length(vBookMarks) do
if sequence(vBookMarks[i][BM_POINTER]) then
lCleanStoredAlt = cleanup(vBookMarks[i][BM_POINTER])
else
lCleanStoredAlt = ""
end if
if x = 1 then
if not equal(vBookMarks[i][BM_SUBCONTEXT_ID], pContext) then
continue
end if
else -- x = 2
if equal(vBookMarks[i][BM_SUBCONTEXT_ID], pContext) then
continue
end if
end if
if equal(pBMText, vBookMarks[i][BM_NAME]) then
return i
end if
if (equal(vBookMarks[BM_FILENAME],pNameSpace) or atom(pNameSpace)) and
equal(pDisplayText, vBookMarks[i][BM_NAME]) then
return i
end if
if sequence( vBookMarks[i][BM_CLEAN_NAME] ) then
lCleanStored = vBookMarks[i][BM_CLEAN_NAME]
else
lCleanStored = cleanup(vBookMarks[i][3])
if lCleanStored[1] = '_' then
integer pos
pos = find_from('_', lCleanStored, 2)
if pos != 0 then
lCleanStored = lCleanStored[pos+1 .. $]
end if
end if
vBookMarks[i][BM_CLEAN_NAME] = lCleanStored
end if
if equal(lCleanBMText, lCleanStored) then
return i
end if
if equal(lCleanBMText, lCleanStoredAlt) then
return i
end if
if length(lCleanDisplayText) > 0 then
if equal(lCleanDisplayText, lCleanStored) then
return i
end if
if equal(lCleanDisplayText, lCleanStoredAlt) then
return i
end if
-- if length(lCleanStored) - length(lCleanDisplayText) <= 4 then
-- if match(lCleanDisplayText, lCleanStored) then
-- lPossibles = append(lPossibles, {abs(i - pHere), length(lCleanStored) - length(lCleanDisplayText),lCleanStored, i})
-- end if
-- end if
-- if not equal(lCleanStoredAlt, lCleanStored) then
-- if length(lCleanStoredAlt) - length(lCleanDisplayText) <= 4 then
-- if match(lCleanDisplayText, lCleanStoredAlt) then
-- lPossibles = append(lPossibles, {abs(i - pHere),length(lCleanStoredAlt) - length(lCleanDisplayText), lCleanStoredAlt, i})
-- end if
-- end if
-- end if
if match(lCleanDisplayText, lCleanStored) then
lPossibles = append(lPossibles, {sim_index(lCleanDisplayText, lCleanStored), length(lCleanStored) - length(lCleanDisplayText),lCleanStored, i})
end if
if match(lCleanDisplayText, lCleanStoredAlt) then
lPossibles = append(lPossibles, {sim_index(lCleanDisplayText, lCleanStoredAlt), length(lCleanStoredAlt) - length(lCleanDisplayText),lCleanStoredAlt, i})
end if
end if
if not equal(lCleanBMText, lCleanDisplayText) then
if equal(lCleanBMText, lCleanStored) then
return i
end if
if equal(lCleanBMText, lCleanStoredAlt) then
return i
end if
-- if length(lCleanStored) - length(lCleanBMText) <= 4 then
-- if match(lCleanBMText, lCleanStored) then
-- lPossibles = append(lPossibles, {abs(i - pHere), length(lCleanStored) - length(lCleanBMText), lCleanStored, i})
-- end if
-- end if
-- if not equal(lCleanStoredAlt, lCleanStored) then
-- if length(lCleanStoredAlt) - length(lCleanBMText) <= 4 then
-- if match(lCleanBMText, lCleanStoredAlt) then
-- lPossibles = append(lPossibles, {abs(i - pHere), length(lCleanStoredAlt) - length(lCleanBMText), lCleanStoredAlt, i})
-- end if
-- end if
-- end if
if match(lCleanBMText, lCleanStored) then
lPossibles = append(lPossibles, {sim_index(lCleanBMText, lCleanStored), length(lCleanStored) - length(lCleanBMText),lCleanStored, i})
end if
if match(lCleanBMText, lCleanStoredAlt) then
lPossibles = append(lPossibles, {sim_index(lCleanBMText, lCleanStoredAlt), length(lCleanStoredAlt) - length(lCleanBMText),lCleanStoredAlt, i})
end if
end if
end for
end for
if length(lPossibles) = 0 then
return 0
end if
if length(lPossibles) > 1 then
lPossibles = sort_columns(lPossibles, {1, 2, -3})
end if
if vVerbose then
printf(1, "assumed match '%s' with '%s'\n", {pBMText, lPossibles[1][3]})
end if
return lPossibles[1][4]
end function
------------------------------------------------------------------------------
function has_leading_chars(sequence pString, integer pFrom = 1, sequence pAllowed = " \t")
------------------------------------------------------------------------------
pFrom -= 1
while pFrom > 0 and eu:find(pString[pFrom], pAllowed) > 0 do
pFrom -= 1
end while
if pFrom < 1 or pString[pFrom] = '\n' then
return 1
end if
return 0
end function
ifdef UNITTEST then
test_equal("has_leading_chars 1", 1, has_leading_chars(" \tabc\t ", 1))
test_equal("has_leading_chars 2", 1, has_leading_chars(" \tabc\t ", 2))
test_equal("has_leading_chars 3", 1, has_leading_chars(" \tabc\t ", 3))
test_equal("has_leading_chars 4", 0, has_leading_chars(" \tabc\t ", 4))
test_equal("has_leading_chars 5", 0, has_leading_chars(" \tabc\t ", 5))
test_equal("has_leading_chars 6", 0, has_leading_chars(" \tabc\t ", 6))
test_equal("has_leading_chars 7", 0, has_leading_chars(" \tabc\t ", 7))
test_equal("has_leading_chars 8", 1, has_leading_chars(" \ta\nc\t ", 5))
test_equal("has_leading_chars 9", 0, has_leading_chars(" \ta\nc\t ", 6))
test_equal("has_leading_chars A", 1, has_leading_chars(" \ta\n\ta ", 6))
end ifdef
------------------------------------------------------------------------------
function has_trailing_chars(sequence pString, integer pFrom, sequence pAllowed = " \t")
------------------------------------------------------------------------------
pFrom += 1
while pFrom <= length(pString) and eu:find(pString[pFrom], pAllowed) > 0 do
pFrom += 1
end while
if pFrom > length(pString) then
return 1
end if
if pString[pFrom] = '\n' then
return 1
end if
return 0
end function
ifdef UNITTEST then
test_equal("has_trailing_chars 1", 0, has_trailing_chars(" \tabc\t ", 1))
test_equal("has_trailing_chars 2", 0, has_trailing_chars(" \tabc\t ", 2))
test_equal("has_trailing_chars 3", 0, has_trailing_chars(" \tabc\t ", 3))
test_equal("has_trailing_chars 4", 0, has_trailing_chars(" \tabc\t ", 4))
test_equal("has_trailing_chars 5", 1, has_trailing_chars(" \tabc\t ", 5))
test_equal("has_trailing_chars 6", 1, has_trailing_chars(" \tabc\t ", 6))
test_equal("has_trailing_chars 7", 1, has_trailing_chars(" \tabc\t ", 7))
test_equal("has_trailing_chars 8", 0, has_trailing_chars(" \ta \n c\t ", 5))
test_equal("has_trailing_chars 9", 0, has_trailing_chars(" \ta \n c\t ", 6))
test_equal("has_trailing_chars A", 1, has_trailing_chars(" \ta \na \n", 6))
test_equal("has_trailing_chars B", 1, has_trailing_chars("\n", 1))
end ifdef
------------------------------------------------------------------------------
function compare_prev(sequence pSubString, sequence pString, integer pFrom)
------------------------------------------------------------------------------
integer lLen
if length(pSubString) = 0 then
return 0
end if
if atom(pSubString[1]) then
pSubString = {pSubString}
end if
for i = 1 to length(pSubString) do
lLen = length(pSubString[i])
if pFrom > lLen then
if equal(upper(pString[pFrom - lLen .. pFrom - 1]), upper(pSubString[i])) then
return i
end if
end if
end for
return 0
end function
------------------------------------------------------------------------------
function compare_next(sequence pSubString, sequence pString, integer pFrom)
------------------------------------------------------------------------------
integer lLen
if length(pSubString) = 0 then
return 0
end if
if atom(pSubString[1]) then
pSubString = {pSubString}
end if
for i = 1 to length(pSubString) do
lLen = length(pSubString[i])
if pFrom + lLen <= length(pString) then
if equal(upper(pString[pFrom + 1 .. pFrom + lLen]), upper(pSubString[i])) then
return i
end if
end if
end for
return 0
end function
------------------------------------------------------------------------------
function compare_begin(sequence pSubString, sequence pString)
------------------------------------------------------------------------------
if length(pSubString) = 0 then
return 0
end if
return compare_next(pSubString, pString, 0)
end function
------------------------------------------------------------------------------
function compare_end(sequence pSubString, sequence pString)
------------------------------------------------------------------------------
integer lLen
if length(pSubString) = 0 then
return 0
end if
if atom(pSubString[1]) then
pSubString = {pSubString}
end if
for i = 1 to length(pSubString) do
lLen = length(pSubString[i]) - 1
if length(pString) > lLen then
if equal(upper(pString[$ - lLen .. $ ]), upper(pSubString[i])) then
return i
end if
end if
end for
return 0
end function
------------------------------------------------------------------------------
function get_to_eol(sequence pString, integer pFrom, integer pTrimmed = 1)
------------------------------------------------------------------------------
integer lPos
integer lEndPos
sequence lText
if pFrom > length(pString) then
return {pFrom, ""}
end if
lEndPos = find_eol(pString, pFrom)
lPos = lEndPos - 1
lText = pString[pFrom .. lPos]
if pTrimmed then
lText = trim(lText)
end if
return {lEndPos, lText}
end function
ifdef UNITTEST then
test_equal("get_to_eol 1", {9, "ab cd ef"}, get_to_eol("ab cd ef\ngh hi\n\n", 1))
test_equal("get_to_eol 2", {9, ""}, get_to_eol("ab cd ef\ngh hi\n\n", 9))
test_equal("get_to_eol 3", {15, "gh hi"}, get_to_eol("ab cd ef\ngh hi\n\n", 10))
test_equal("get_to_eol 4", {15, ""}, get_to_eol("ab cd ef\ngh hi\n\n", 15))
test_equal("get_to_eol 5", {25, ""}, get_to_eol("ab cd ef\ngh hi\n\n", 25))
test_equal("get_to_eol 6", {13, " ab cd ef\t\t"}, get_to_eol(" ab cd ef\t\t\ngh hi\n\n", 1, 0))
end ifdef
-- This returns a Logical line. This can span multiple physical lines.
-- A logical line ends when it finds a physical line that begins with one
-- of the FrontDelim entries or a physical line that ends with one of the
-- BackDelim entries, or a blank line.
-- This also returns an index to the next character after the logical line.
------------------------------------------------------------------------------
function get_logical_line(sequence pString, integer pFrom, sequence pFrontDelim, sequence pBackDelim, integer pTrimmed = 1)
------------------------------------------------------------------------------
sequence lNewLine
integer lPos
integer lNewPos
sequence lLine
lLine = ""
lPos = pFrom
while 1 do
lNewLine = get_to_eol(pString, lPos, pTrimmed)
if length(lNewLine[2]) > 0 then
lNewPos = lNewLine[1]
lNewLine = lNewLine[2]
if length(pFrontDelim) > 0 and compare_begin(pFrontDelim, lNewLine) > 0 then
-- This line begins with a delimiter so is the start of the next logical line.
if length(lLine) > 0 then
-- We don't update the next position so this line is re-read
-- in the next iteration.
exit
else
lLine = lNewLine
lPos = lNewPos+1
end if
else
-- The line is a continuaton of the previous cell.
lPos = lNewPos + 1
if length(lLine) > 0 then
lLine &= '\n'
end if
lLine &= lNewLine
end if
if length(pBackDelim) > 0 and compare_end(pBackDelim, lLine) > 0 then
-- We got a completed logical line.
exit
end if
else
-- A blank line signals the end of a logical line.
exit
end if
end while
return {lPos, lLine}
end function
------------------------------------------------------------------------------
procedure add_bookmark(integer pType, object pTypeLink, sequence pText)
------------------------------------------------------------------------------
sequence lNewBM
integer lElement_No
integer lBM_No
lElement_No = length(vElements) + 1
lBM_No = length(vBookMarks) + 1
if length(vOutputFile) > 0 then
lNewBM = {pType, pTypeLink, pText, vCurrentContext, vOutputFile[$], lElement_No, 0, 0, 0}
else
lNewBM = {pType, pTypeLink, pText, vCurrentContext, "", lElement_No, 0, 0, 0}
end if
lNewBM[BM_GETHEADINGS] = lNewBM[BM_NAME..BM_ELEMENT_PTR]
if pType = 'h' then
lNewBM[BM_ELEMENTS] = lNewBM[BM_NAME..BM_FILENAME]
else
lNewBM[BM_ELEMENTS] = lNewBM[BM_POINTER..BM_ELEMENT_PTR]
end if
vBookMarks = append(vBookMarks, lNewBM)
vElements = append(vElements, {'b', lBM_No})
end procedure
------------------------------------------------------------------------------
function Generate_Final( integer pAction, sequence pParms = {})
------------------------------------------------------------------------------
sequence lText
lText = call_func(vFinal_Generator_rid, {pAction, pParms, vCurrentContext})
return lText
end function
------------------------------------------------------------------------------
function get_camel_case(sequence pRawText, atom pFrom)
------------------------------------------------------------------------------
sequence lText
integer lPos
integer lChar
integer lUC
integer lLC
integer lDigits
integer lLast
-- CamelCase words are ...
-- Begin with Upper case
-- Must more than one upper case
-- Second upper case must be after first lower case
-- Must have at least one lower case