-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl7parsers.PRG
More file actions
3016 lines (2421 loc) · 118 KB
/
l7parsers.PRG
File metadata and controls
3016 lines (2421 loc) · 118 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
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
******************************************************
* Program...........: L7Parsers.prg
* Author............: Lauren Clarke
* Project...........: L7
* Created...........: 05/30/2002
* Copyright.........: see LICENSE BLOCK
* Description.......: Various text to html parsers for general <textarea to memo use
* Unit Test.........: L7Parsers_ut.prg
* Dependencies......: VFP7, RELATION, L7Utils, VBScript.RegExp (or equiv)
* Assumptions.......: SET EXACT OFF
* Last Change.......: 12/30/2002
* Change Log........: See end of file
******************************************************
#IF .F.
***** BEGIN LICENSE BLOCK *****
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is "Level 7 Framework for Web Connection" and
"Level 7 Toolkit" (collectively referred to as "L7").
The Initial Developer of the Original Code is Randy Pearson of
Cycla Corporation.
Portions created by the Initial Developer are Copyright (C) 2004 by
the Initial Developer. All Rights Reserved.
Contributor(s):
1. Lauren Clarke, Cornerstone Systems NW, provided the design for the
L7 parser technology (see L7Parsers.PRG).
***** END LICENSE BLOCK *****
#ENDIF
* COMMENTS
*
*
* USAGE
* See unit test for usage
*
#DEFINE L7_USE_PARAGRAPH_PARSER .T.
#INCLUDE L7.H
#UNDEFINE L7BR
#DEFINE L7BR [<br />]
#DEFINE L7_PARSER_PLACEHOLDER_DELIM CHR(222)
#DEFINE L7_PARSER_DEBUG_OBJECTS .f.
#DEFINE L7_PARSER_DEBUG .f. && important to turn this off for the #N# parsers etc to work
#DEFINE L7_PARSER_PERFLOG .f.
*==============================================================================
*==============================================================================
* PRODUCTION PARSING CLASS IMPLEMENTATION
*==============================================================================
*==============================================================================
* The following classes are the final implementation level of the parsing society
* they are the ones most likely to be changed as we adjust the parsing features
* we want to implement. They in turn rest on a more generic society of classes
* defined in the next section
*==============================================================================
* PRODUCTION PARSING CHAIN MANAGER
*==============================================================================
*** ========================================================== ***
DEFINE CLASS L7Parsers as L7ParseChainManager
* This is the big boss, manages and serves up parsing chains as requested
FUNCTION init()
LOCAL loChain
loChain = this.addChain( "L7ParseOnSaveChain" ) && hit this on save
*-- set this here so we don't have to pass it to parseByName() all the time
loChain.setall("cDefaultContext",[SAVE])
loChain = this.addChain( "L7ParseOnSaveChainADMIN" ) && hit this on admin save (does not strip active content)
loChain.setall("cDefaultContext",[SAVE])
loChain = this.addChain( "L7ParseOnViewChain" ) && hit this on view
loChain.setall("cDefaultContext",[VIEW])
ENDFUNC
ENDDEFINE
*==============================================================================
* PRODUCTION PARSING CHAINS
*==============================================================================
*** ========================================================== ***
DEFINE CLASS L7ParseOnSaveChain as L7ParseChainAnchor
* This chain is set up for "on save" parsing. IOW, it does things
* that would be performed on raw submissions prior to save
* NOTE: Keep this in sync with ADMIN version below...
FUNCTION forgeChainImplementation()
LOCAL loChain, loTemp
this.setSuccessor( "L7StripActiveContent" )
this.setSuccessor( "L7FixPastedValues" )
this.setSuccessor( "L7DynamicMarkers" )
this.setContext("SAVE") && set the default context for this chain
ENDFUNC && forgeChainImplementation
ENDDEFINE
*-- EOC L7ParseOnSaveChain
*** ========================================================== ***
DEFINE CLASS L7ParseOnSaveChainADMIN as L7ParseChainAnchor
* This chain is set up for "on save" parsing. IOW, it does things
* that would be performed on raw submissions prior to save
* NOTE: Keep this in sync with non-ADMIN version above
FUNCTION forgeChainImplementation()
LOCAL loChain, loTemp
this.setSuccessor( "L7DynamicMarkers" )
this.setContext("SAVE") && set the default context for this chain
ENDFUNC && forgeChainImplementation
ENDDEFINE
*-- EOC L7ParseOnSaveChainADMIN
*** ========================================================== ***
DEFINE CLASS L7ParseOnViewChain as L7ParseChainAnchor
* A first stab at a generalized plain text parsing chain
* this one is set up for "viewing", iow, content would be parsed
* by this chain on each hit.
FUNCTION forgeChainImplementation()
this.setSuccessor( "L7IgnoreParser" )
This.setSuccessor( "L7FlexTableParser" ) && must preceed paragraph/BR parsing, and old table parser (if retained)
this.setSuccessor( "L7IgnoreHTMLParser" )
this.setSuccessor( "L7DynamicMarkers" )
this.setSuccessor( "L7TranslateLinks" )
this.setSuccessor( "L7InternetParser" )
This.setSuccessor( "L7ListParser" )
This.setSuccessor( "L7TableParser" ) && must preceed TextDecorations to preserve the |* => <th> metaphor
This.setSuccessor( "L7TextDecorationParser" )
#IF L7_USE_PARAGRAPH_PARSER
This.setSuccessor( "L7ParagraphParser" )
#ELSE
This.setSuccessor( "L7CRParser" )
#ENDIF
RETURN
ENDFUNC && forgeChainImplementation
ENDDEFINE
*-- EOC L7ParseOnViewChain
*==============================================================================
* PRODUCTION PARSERS
*==============================================================================
*** ========================================================== ***
DEFINE CLASS L7BreakLines as L7BaseParser
* breaks lines at spaces (where possible) to be no longer that nMaxWidth
*-- a rough first pass this...there is probably a way to do this with Regexp alone, but it escapes me.
* cPattern = [(.{75,75})(\s)]
* cReplacement = [$1$2] + CHR(13) + CHR(10)
nMaxWidth = 70 && lines will be broken if longer than this
cBreakChar = CHR(13) + CHR(10)
*!* FUNCTION setMemoWidth( tnWidth )
*!*
*!* IF VARTYPE( tnWidth ) # "N"
*!* tnWidth = 0
*!* ENDIF
*!*
*!* *-- defensive
*!* IF BETWEEN( tnWidth, 8, 8192 )
*!* SET MEMOWIDTH TO tnWidth
*!* RETURN .t.
*!* ELSE
*!* RETURN .f.
*!* ENDIF
*!*
*!* ENDFUNC
*!*
* this is nice, but won't work as it will break things like ALT="This is the alt text" which will
* cause javascript errors, could be fixed, but also don't like the problems _Mline usage might cause
* with nesting
*!* FUNCTION parse( tcText )
*!* LOCAL lnOldMemoWidth
*!* lnOldMemoWidth = SET("memowidth")
*!*
*!* IF this.setMemoWidth( this.nMaxWidth )
*!* lcTemp = []
*!* _MLINE = 0
*!* FOR lnK = 1 TO MEMLINES( tcText ) - 1
*!* lcTemp = lcTemp + MLINE( tcText, 1, _MLINE ) + this.cBreakChar
*!* ENDFOR
*!* lcTemp = lcTemp + MLINE( tcText, 1, _MLINE )
*!* ENDIF
*!*
*!* tcText = lcTemp
*!*
*!* this.setMemoWidth( lnOldMemoWidth )
*!* ENDFUNC
* --------------------------------------------------------- *
FUNCTION carefulBreak( tcText )
* breaks string ( delimited attribute ) assignments into separate lines
LOCAL lnLine, lnK, lnJ, lnOccurs, lnLineLen, lcLine, laLines(1), lcDelim, lnPos, ;
lnBreakLocation
lnLines = ALINES( lalines, m.tcText)
tcText = []
FOR lnK = 1 TO m.lnLines
lcLine = laLines( m.lnK )
lnLineLen = LEN( m.lcLine )
IF NOT m.lnLineLen <= this.nMaxWidth AND ;
( (["] $ m.lcLine ) OR (['] $ m.lcLine) ) AND ;
( ([=] $ m.lcLine ) )
*-- we worry about string assignments as they will cause client-side errors if broken
*-- break at the beginning of each string assignment
lnOccurs = OCCURS([=], m.lcLine)
FOR lnJ = 1 TO m.lnOccurs
*-- break at the word prior to the =
* eg: "<img src="test" alt="this is a test">
* ^ ^
lnBreakLocation = RAT([ ],LEFT( m.lcLine, AT( [=], m.lcLine, m.lnJ ))) + 1
lcLine = STUFF( m.lcLine, m.lnBreakLocation , 0 , this.cBreakChar )
ENDFOR
*-- break at the end of each string assignment
* eg: "<img src="test" alt="this is a test">
* ^ ^
FOR lnJ = 1 TO m.lnOccurs
lnPos = AT( [=] , m.lcLine, m.lnJ )
*-- not at end of line?
IF m.lnPos < LEN( m.lcLine ) - 1
lcDelim = LEFT( ALLTRIM( SUBSTR( m.lcLine, m.lnPos + 1) ), 1) && the first non-space char after the =
*-- delimiter is valid?
IF m.lcDelim $ ['"]
lnEndPos = AT( m.lcDelim, SUBSTR( m.lcLine, m.lnPos ), 2)
*-- found end of delimited region?
IF lnEndPos > 0
lnEndPos = m.lnEndPos + m.lnPos
*-- break after second (closing) delimiter
lcLine = STUFF( m.lcLine, m.lnEndPos, 0, this.cBreakChar )
ENDIF
ENDIF
ENDIF
ENDFOR
ENDIF
*-- note, dependence on strip size below
tcText = m.tcText + m.lcLine + CHR(13) + CHR(10)
ENDFOR
*-- strip the last crlf
tcText = LEFT( m.tcText, LEN( m.tcText) - 2 )
ENDFUNC
* --------------------------------------------------------- *
FUNCTION beforeParse( tcText )
*-- reset the pattern in case properties changed
lcMaxLen = ALLTRIM(STR(this.nMaxWidth))
this.cPattern = [(.{] + m.lcMaxLen + [,] + m.lcMaxLen + [})(\s)]
this.cReplacement = [$1$2] + this.cBreakChar
*-- check that we might have assignments
IF (["] $ m.tcText OR ['] $ m.tcText) AND ([=] $ m.tcText)
this.carefulBreak( @m.tcText )
ENDIF
*-- note, this does not mean we'll escape breaking any strings up, but
* it gives us a better chance, since all attribute assignments in lines
* that are over the limit will be moved into their own lines
* if the attribute assignments themselves are over this.nMaxWidth, we
* are out of luck.
ENDFUNC
ENDDEFINE
*** ========================================================== ***
DEFINE CLASS L7FixPastedValues as L7MultiParser
* finds and replaces common problem characters that arise when content from word processors (like word)
* is pasted into a textarea. This parser can help keep open content W3C valid.
* Normally, you'll probably use this parser in a "ParseOnSave" chain.
function parse( tcText )
*-- look for & NOT part of a standard encoding
* read: & followed by 2 or more non-whitespace chars, followed by ; ... we push to stack to get them out of harm's way
this.replaceex( @tcText, "((&)(\S{2,};))", [=this.pushtoStack($1)] )
tcText = strtran( m.tcText, [&], [&] ) && naked apersands
tcText = strtran( m.tcText, chr(133), [...] ) && elipsis
tcText = strtran( m.tcText, chr(145), [‘] ) && single quote open
tcText = strtran( m.tcText, chr(146), [’] ) && single quote close
tcText = strtran( m.tcText, chr(149), [*] ) && this is the "bullet" from word, use in conjunction with the ListParser, maybe 183 too?
tcText = strtran( m.tcText, chr(167), [§] ) && this is the section symbol
endfunc
ENDDEFINE
*** ========================================================== ***
DEFINE CLASS L7StripActiveContent as L7MultiParser
* obviously, there are different levels of scrictness we could go to here
* http://www.cert.org/tech_tips/malicious_code_mitigation.html
FUNCTION init()
* note, this one may cause problems when people are "allowed" to post code, say with <code lang=> delimiters with the csCodeParser
* this stripper will cause their <script code to be pre'd and not colored (maybe?)
this.addParse( "(\<script)((.|\n)*?)((<\/script.?\>)|$)", [="<pre>"+encodeforHTML($1+$2+$4)+"</pre>"], .t., .t., .t. )
this.addParse( "(^|\n|\W)(\<\%)", [ <%], .t., .t., .f. )
ENDFUNC
FUNCTION afterParse( tcText )
IF this.nHits > 0
* log an admin warning here?
ENDIF
RETURN DODEFAULT( @m.tcText )
ENDFUNC
ENDDEFINE
*** ========================================================== ***
DEFINE CLASS L7TranslateLinks as L7BaseParser
* dependent on Page object for relative URL info
**[[to do: regEx approach -- worth it??
FUNCTION parse( tcText )
IF ATC("<link" + L7_TRANSLATE_LINKS_DELIMITER, m.tcText) = 0
RETURN
ENDIF
IF( VARTYPE( m.PAGE ) # "O" )
RETURN
ENDIF
** tcText = m.PAGE.TranslateLinks( m.tcText )
* Convert text with encoded links into actual
* hyperlinks at runtime using StuffURL as needed. Links
* are encoded using a format like:
*
* <LINK:home>Text...</LINK>
* or <LINK:otherPage>
* or <LINK:custForm:cus=12>
* or <LINK:custForm:cus=(this.oCus.cus_pk)> <<*** NOT AVAILABLE -- SECURITY RISK ***
*
* This allows portability and incorporation URL-management context.
LOCAL lcOut, lcTemp, lcURL, lcBaseUrl, lcLinked, lcParms, lnParms, ii, ;
lcNewPage, lnAt11, lnAt12, lnAt21, lnAtColon, lnAtColon2, ;
lnParmLength, lnAtEqual, lcParm, lcVar, lcValue
LOCAL ARRAY laParms[1]
* Determine starting point for URL.
lcBaseURL = Page.cUrlA
* Process <LINK:...>..</LINK> tags:
lcTemp = m.tcText
lcOut = ""
DO WHILE .T.
lcUrl = m.lcBaseUrl && start over for each link
* Keep going until no more links found.
lnAt11 = ATC( "<LINK" + L7_TRANSLATE_LINKS_DELIMITER, m.lcTemp)
IF m.lnAt11 = 0 && No tag found -- append balance of text and exit.
lcOut = m.lcOut + m.lcTemp
EXIT
ENDIF
lnAt12 = ATC( ">", SUBSTR( m.lcTemp, m.lnAt11) )
IF m.lnAt12 < 7 && Could not find valid closing ">" - abort.
lcOut = m.lcOut + m.lcTemp
EXIT
ENDIF
* Extract "parameters" between "<LINK:" and ">":
lcParms = SUBSTR( m.lcTemp, m.lnAt11 + 6, m.lnAt12 - 7)
lnParms = GETWORDCOUNT(m.lcParms, L7_TRANSLATE_LINKS_DELIMITER)
IF m.lnParms < 1 && Invalid format--less than 1 parameter--ignore.
lcOut = m.lcOut + m.lcTemp
EXIT
ENDIF
* Extract hyper-linked text.
lcNewPage = GETWORDNUM(m.lcParms, 1, L7_TRANSLATE_LINKS_DELIMITER)
IF EMPTY( m.lcNewPage)
lcOut = m.lcOut + m.lcTemp
EXIT
ENDIF
lnAt21 = ATC( "</LINK>", m.lcTemp )
IF m.lnAt21 < m.lnAt11 + m.lnAt12 && Invalid/missing closing tag - IGNORE.
lcOut = m.lcOut + m.lcTemp
EXIT
ENDIF
lcLinked = SUBSTR(m.lcTemp, m.lnAt11 + m.lnAt12, m.lnAt21 - m.lnAt11 - m.lnAt12)
IF m.lnAt11 > 1 && Insert all text before opening <LINK> begins:
lcOut = m.lcOut + LEFT( m.lcTemp, m.lnAt11 - 1)
ENDIF
* Switch to new page:
lcUrl = StuffURL( m.lcURL, 2, m.lcNewPage)
* Now, add any extra "parameters" to the URL:
FOR ii = 2 TO m.lnParms
lcParm = GETWORDNUM(m.lcParms, m.ii, L7_TRANSLATE_LINKS_DELIMITER)
lnAtEqual = AT( "=", m.lcParm )
IF m.lnAtEqual >= 2 && it's var=value format
lnParmLength = LEN( m.lcParm )
lcVar = LEFT( m.lcParm, m.lnAtEqual - 1)
IF m.lnAtEqual = m.lnParmLength
* no value means "strip the parameter"
lcURL = StuffURL( m.lcURL, m.lcVar, .F. )
ELSE
lcValue = SUBSTR(m.lcParm, m.lnAtEqual + 1)
*!* DON'T USE FOLLOWING--SECURITY HOLE!
*!* IF m.lcValue = "(" && support for embedded equations
*!* lcValue = EVALUATE(m.lcValue)
*!* ENDIF
lcURL = StuffURL( m.lcURL, m.lcVar, m.lcValue)
ENDIF
ENDIF
ENDFOR
lcOut = m.lcOut + [<a href="] + m.lcUrl + [">] + m.lcLinked + [</a>]
IF LEN( m.lcTemp) <= m.lnAt21 + LEN("</LINK>") && No more text after closing tag.
EXIT
ENDIF
* Iterate using remainder of string:
lcTemp = SUBSTR( m.lcTemp, m.lnAt21 + LEN("</LINK>") )
LOOP
ENDDO && WHILE .T.
tcText = m.lcOut && supports call-by-ref
ENDFUNC
ENDDEFINE
*-- EOC TranslateL7Links
*** ========================================================== ***
DEFINE CLASS L7InternetParser as L7MultiParser
cProtocols = "http|https|ftp|mailto" && possibly add: news|telnet|gopher
lpushtostack = .t. && we'll get these links out of harm's way
lencodeEmail = .t. && an anti-spam measure see: http://www.cdt.org/speech/spam/030319spamreport.shtml
lWordWrap = .t. && try to help make long addresses wrappable
* --------------------------------------------------------- *
FUNCTION INIT()
*-- remove all existing tags likely to be clobbered by the following link parsers
*? would this be better as part of the ignore parser? or its own parser?
this.addparse("((<a|<img)((.|\n)*?)>)", "$1" )
* note that the above only removes the opening tag, the contents and closing tag will be left behind for subsequent parsing
* 7/18/2007: Experimental! Wikipedia-like external link in brackets with optional text.
this.addparse("\[((?:" + this.cProtocols + '):[^\]\s]+)(?:\s*([^\]]*))\]', [=this.buildBracketedLink($1,$2)])
*-- links with protocols
*? perhaps .png, .jpg, .gif etc should be parsed to <img ?
this.addparse("((?:(?:" + this.cProtocols + '):[^\]\}(\s\|\)|\"<>]+[^\]\}(\s\|\)|\"<>\.]))', [<a href="$1">$1</a>])
* 7/18/07, above had been this:
* this.addparse("((?:(?:" + this.cProtocols + '):[^\]\}(\s\|\)|\"<>]+))', [<a href="$1">$1</a>])
* but change made to avoind end-of-sentence period being captured.
*-- mail addresses
this.addparse("([\w\.!#\$%\-+.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)", [=this.buildMailLink($1)])
*? alternative that might work better: ^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
*-- www. (with no protocol)
this.addparse("(www[\.!#\$%\-+.]+[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)+)", [<a href="http://$1">$1</a>])
* this.addparse("(\W)((www.(.|\n)*?)(\s|\|))", [$1<a href="http:\\$3">$3</a>])
RETURN DODEFAULT()
ENDFUNC && init
* --------------------------------------------------------- *
function convertToHTMLCodes( tcString )
local lcHTMLEncoded , lnChar
lcHTMLEncoded = ""
for lnChar = 1 to len( tcString )
lcHTMLEncoded = m.lcHTMLEncoded + "&#" + TRANSFORM( ASC( SUBSTR( m.tcString, m.lnChar, 1 ) ) ) + ";"
endfor
return m.lcHTMLEncoded
endfunc
* --------------------------------------------------------- *
function buildBracketedLink( tcAddress, tcText )
LOCAL lcText
lcText = EVL(m.tcText, m.tcAddress)
return [<a href="]+m.tcAddress+[" title="]+m.tcAddress+[">]+m.lcText+[</a>]
endfunc
* --------------------------------------------------------- *
function buildMailLink( tcAddress )
local lcHref, lcAddress
lcAddress = alltrim( m.tcAddress)
lcHref = "mailto:" + m.lcAddress
IF this.lWordWrap
lcAddress = STRTRAN(m.lcAddress, '@', ' @ ')
ENDIF
*-- shall we encode the href to throw off the harvesters?
if this.lencodeEmail
lcHref = this.convertToHtmlCodes( m.lcHref )
lcAddress = this.convertToHTMLCodes( m.lcAddress )
ENDIF
return [<a href="]+m.lcHref+[">]+m.lcAddress+[</a>]
endfunc
ENDDEFINE
*-- EOC InternetParser
*** ========================================================== ***
DEFINE CLASS L7TextDecorationParser as L7MultiParser
* provides basic plain-text to html wysiwyg pattern parsing
* --------------------------------------------------------- *
FUNCTION init()
* Note: Parsing will be done in the order in which the parse patterns are added
* in some implementations, we may wish to drive this with meta data
* arguments are addParse( tcPattern, tcReplacement, [tlGlobal] , [tlIgnoreCase], [tlPushToStack] )
*-- "Heading" parsers. H2, H3, and H4. Note: H4 must be checked first, etc.
this.addParse( "((^|\n)(\s)*)\[\[\[\[([^\s\*].*?)\]\]\]\]",[$1<h4>$4</h4>], .t., .t., .f. )
this.addParse( "((^|\n)(\s)*)\[\[\[([^\s\*].*?)\]\]\]", [$1<h3>$4</h3>], .t., .t., .f. )
this.addParse( "((^|\n)(\s)*)\[\[([^\s\*].*?)\]\]", [$1<h2>$4</h2>], .t., .t., .f. )
* Usage:
* If any new line starts with (optional white space and then) [[, [[[, or [[[[, followed by any
* content on the same line that doesn't start with a space or asterisk, followed by an equal number of
* closing "]" symbols, treat as a metaphor for a level 2, 3, or 4 heading (<h?> tag).
* [NOTE: We don't do <h1>, because [] is likely intended as something else, and there should be only one <h1>
* tag ever on a page, which is likely coming from a different source anyway. And we don't do <h5> and <h6>
* because of frequency-vs-performance trade-off.]
*-- Emphasis and strikeout inline parsers:
this.addParse( "(^|\n|\W)-{2,}([^\s|-].*?)-{2,}(\W|$)", [$1<strike>$2</strike>$3], .t., .t., .f. )
this.addParse( "(^|\n|\W|_)\*([^\s|\*].*?)\*(_|\W|$)", [$1<strong>$2</strong>$3], .t., .t., .f. )
*-- Same as above, but with 2 or more **
this.addParse( "(^|\n|\W)\*{2,}([^\s].*?)\*{2,}(\W|$)", [$1<strong><em>$2</em></strong>$3], .t., .t., .f. )
this.addParse( "(^|\n|\W)_([^\s|_].*?)_(\W|$)", [$1<em>$2</em>$3], .t., .t., .f. )
*-- Same as above, but with 2 or more _
this.addParse( "(^|\n|\W)_{2,}([^\s].*?)_{2,}(\W|$)", [$1<strong><em>$2</em></strong>$3], .t., .t., .f. )
* Usage (varies somewhat for minor reasons):
* Words wrapped with matcing symbols have the symbols replaced with HTML tags as follows:
* --text-- == <s> (strikethrough)
* *text* == <strong>
* **text** == <strong><em>
* _text_ == <em>
* __text__ == <strong><em>
*-- Superscript and subscript parsers:
this.addParse( "\^\{(.*?)\}", [<sup>$1</sup>], .t., .t., .f. )
this.addParse( "v\{(.*?)\}", [<sub>$1</sub>], .t., .t., .f. )
* Usage:
* Wrapping text like ^{text} or v{text} is interpreted
* as superscript and subscript.
*-- "Horizontal Rule" parsers:
this.addparse( "(^|\n)----(\s?\r|$)", [$1<hr size=1>$2], .t., .t., .f. )
this.addparse( "(^|\n)====(\s?\r|$)", [$1<hr size=4>$2], .t., .t., .f. )
* Usage:
* At the beginning of any line, if ---- or ==== is followed by (optional white space and then) nothing but an end-of-line,
* then substitute an a thin or thick <hr> tag.
RETURN DODEFAULT()
ENDFUNC && init
ENDDEFINE
*-- EOC TextDecorationParser
* CO-OPT parser is under construction
*!* *** ========================================================== ***
*!* DEFINE CLASS L7SiteCoOptParser as L7MultiParser
*!* * use this parser to change \ to / for urls (so Mozilla-based browsers, others?)
*!* * Typicaly, the next to properties would be set from
*!* * Request.getCurrentUrl() and Page.cBaseQueryString prior to parsing these properties establish
*!* * the "context" in which the page at hand should be parsed.
*!* cCurrentURL = [] && the complete url for the current hit (configured from page)
*!* cBaseQueryString = [] && the Current base url that L7 is proposing for this hit (configured from page)
*!* oPathMaps = NULL && collection of mappings from current url, located url to "co-opted url"
*!*
*!*
*!* #IF .F.
*!* So, we'll look for URLs that are relative-only and where, say, the path and extension match the current page,
*!* and assume that those "belong" to the site we're coopting.
*!* I suppose you could just parse any such string and find extras, but I had thought of limiting to ;
*!* <a href=""> and <form action="">
*!* First, the Rule: If "same path" and "same extension" and a valid-looking relative URL, then
*!* attach the string from Page.cBaseQueryString.
*!* You need 3 columns: current logical path, string in page, and result.
*!* Randy Pearson says:
*!* Current LogicalPath: String found in page: Convert to:
*!* /virtualA/mypage.htm?stuff /virtualA/anotherPage.htm /virtualA/anotherPage.htm?stuff
*!* /more/virtualA/apage.htm *LEAVE* not same path
*!* /virtualA/mypage.aspx *LEAVE* wrong extension (however tempting)
*!* Anything *not* in delimiters, I imagine, should not be converted.
*!* Randy Pearson says:
*!* Finally, you should probably plan for the possibility of some other way of specifying which paths apply.
*!* Not sure where thsi would be specified. But imagine a situation where the whole web site is fair game,
*!* or maybe a handful of paths, or maybe all subdirs of the current, etc. Possibly a collection of path patterns?
*!* #ENDIF
*!* function init()
*!* *-- <a followed by a set of chars (non-greedy), followed by href=, followed by some chars,
*!* * followed by " or ' or > or space
*!* * we send the 5th sub-match (the url) for possible co-opting
*!* *[[ this scheme sends the delimiters along with the URL, coOptURL() will clean them.
*!* * we have <a followed by word boundary followed by some sort of href = followed by either " stuff " or ' stuff ' or stuff
*!* * followed by a space or >.
*!*
*!* this.addParse( strtran("(<a\b)([^\>]+)(href\s*\=\s*)(``([^``]*)``|'([^']*)'|[^\'\``\>\s]+)[\s|>]",[``],["]), ;
*!* [=$1+$2+$3+this.coOptUrl( $4 )+$5],.t., .t., .f. )
*!*
*!* this.oPathMaps = createobject("collection")
*!* this.setPathMaps()
*!*
*!* endfunc
*!* * --------------------------------------------------------- *
*!* FUNCTION garbagecollect()
*!* dodefault()
*!* this.oPathMaps = NULL
*!* endfunc
*!*
*!* function setPathMaps()
*!* * hook for subclassing
*!* endfunc
*!* * --------------------------------------------------------- *
*!* function addPathMap( tcCurrent, tcURLPattern, tcReplacement )
*!* local loMap
*!* loMap = createobject("empty")
*!* addproperty( m.loMap, [cCurrentPattern], m.tcCurrent )
*!* addproperty( m.loMap, [cUrlPattern], m.tcURLPattern )
*!* addproperty( m.loMap, [cReplacement], m.tcReplacement )
*!* this.oPathMap.add( m.loMap )
*!* endfunc
*!* * --------------------------------------------------------- *
*!* function config( toPage )
*!*
*!* this.cCurrentURL = m.toPage.cBaseURL && NOTE!! THIS NEEDS CHANGING
*!* this.cBaseURL = m.toPage.cBaseURL
*!* endfunc
*!* * --------------------------------------------------------- *
*!* function coOptUrl( tcURL )
*!* local lcURL, loMap, loParse, lcDelim
*!* if empty( m.tcURL )
*!* return m.tcURL
*!* endif
*!*
*!* lcURL = m.tcURL
*!* *-- after this, lcURL will be clean (trimmed, no '" delimiters on outside) and lcDelim will contain the delimiter used (if any)
*!* m.lcDelim = this.cleanURL( @lcURL )
*!*
*!* *[[ opt: could hold this as a property
*!* loParse = createobject("L7BaseParser") && for .m() and .s() usage
*!* with loParse
*!* .setRegExp()
*!* .lGlobal = .T. && find all occurances by default
*!* .lIgnoreCase = .T. &&
*!* .lMultiLine = .F.
*!* .lPushToStack = .F. && if .t., matches will be placed on the stack and placeholders left behind in text
*!* endwith
*!* *-- now, we loop through all the mappings to see if the current URL, combined with the current baseQS and current URL
*!* * are a hit
*!* local lcURLPattern, lcCurrentPattern, lcReplacement
*!* for each loMap in this.oPathMaps
*!* lcURLPattern = loMap.cURLPattern
*!* lcCurrentPattern = loMpa.cCurrentPattern
*!* lcReplacement = strtran( loMap.cReplacement, [$BASEQS], lcBaseQS,1,5,1)
*!* lcCurrentURL = this.cCurrentURL
*!*
*!* *-- does the current url match the map's pattern?
*!* if loParse.m( m.lcCurrentURL, m.lcCurrentPattern )
*!*
*!* *-- does the URL we've found match the map's pattern?
*!* *[[ is this redundant with the s() call below?
*!* if ! loParse.m( m.lcURL, m.lcUrlPattern )
*!*
*!* *-- at this point, we know we have found a mapping for this
*!* * url given the current querystring
*!*
*!* * /virtualA/mypage.htm?stuff /virtualA/anotherPage.htm /virtualA/anotherPage.htm?stuff
*!*
*!* * recall cReplacement may look something like: "P1?C5$BASEQS"
*!* lcReplacement = strtran( loMap.cReplacement, [$BASEQS], lcBaseQS,1,5,1)
*!* lcReplacement = strtran( m.lcReplacement, [P],[$]) && this is a kluge!
*!* loParse.s( @lcURL, loMap.cUrlPattern, m.lcReplacement )
*!* exit
*!* endif
*!* endif
*!*
*!* endfor
*!*
*!* return m.lcDelim + m.lcURL + m.lcDelim
*!*
*!* endfunc
*!*
*!* * --------------------------------------------------------- *
*!* function cleanURL( tcURL )
*!* * strip " or ' or leading spaces etc from passed URL
*!* * returns the delimiter (if any)
*!* * tcURL is passed by ref
*!* *[[ this might run us into delimter nesting problems on things like onMouseClick actions etc.
*!* local lcRet
*!* tcURL = alltrim( m.tcURL )
*!* lcRet = substr( m.tcURL,1,1 )
*!* lcRet = iif(m.lcRet $ ['"],m.lcRet, [])
*!* if m.lcRet $ ['"]
*!* tcURL = substr( m.tcURL,2,len( m.tcURL)-2)
*!* endif
*!* return m.lcRet
*!* endfunc
*!*
*!* enddefine
*!* *** ========================================================== ***
*!* define class L7CoOptSample as L7SiteCoOptParser
*!* * /virtualA/mypage.htm?stuff /virtualA/anotherPage.htm /virtualA/anotherPage.htm?stuff
*!* function setPathPatterns()
*!* * addPathPattern( tcCurrentPattern, tcPageURLPattern, tcReplacement)
*!* * think: If the currentURL looks like: tcCurrentPattern,
*!* * and we fine a url that looks like tcURLPattern
*!* * replace tcURLPattern with tcReplacement
*!* * and, you can use Cn, Pn as submatch substitutions for the Current and Page
*!* * patterns respectively, also, you can use ($BASEQS) as a holder for the current
*!* * BaseURL proffered by the framework (stored in this.cBaseQueryString
*!* this.addPathPattern("\/virtualA\/([A-Za-z0-9\-]+)(.htm)(\?)((.|\n)*)", ;
*!* "(\/virtualA\/([A-Za-z0-9\-]+)(.htm))",;
*!* "P1?C5$BASEQS")
*!* endfunc
*!*
*!* enddefine
*** ========================================================== ***
DEFINE CLASS L7FixSlashParser as L7MultiParser
* use this parser to change \ to / for urls (so Mozilla-based browsers, others?)
* will work.
* NOTE: May want to log hits on this parser so content can be fixed properly
* but, on open content sites, this is a nice parser to have as people
* may not be aware MSIE is cutting them slack with \ chars in URI's
function init()
*-- <a followed by a set of chars (non-greedy), followed by href=, followed by some chars, followed by >
* we clean the 5th submatch.
this.addParse( "(<a )((.|\n)*?)(href=)((.|\n)*?)(>)", [=$1+$2+$4+strtran($5,"\","/")+$7],.t., .t., .f. )
this.addParse( "(<img)((.|\n)*?)(src=)((.|\n)*?)(>)", [=$1+$2+$4+strtran($5,"\","/")+$7],.t., .t., .f. )
*[[ do we need <form here ?, probably a bad habit-forming thing.)
endfunc
enddefine
*** ========================================================== ***
DEFINE CLASS L7IgnoreHTMLParser as L7MultiParser
* use this class when parsing "in traffic" that is, parsing text that
* may already include some html eg: <!--Don't strike me--> etc.
*
* This parser should be placed prior to L7IgnoreParser in the chain.
*
* Note there is some dependency betwen this parser and the CR parser, the CR parser selectively
* places <br>'s at ends of lines, but it does NOT do so for <tr, td etc... this parser needs to be
* careful to leave enough of the tag behind so the CR parser can know if it needs to place a <br> or not.
FUNCTION init()
* arguements are addParse( tcPattern, tcReplacement, [tlGlobal] , [tlIgnoreCase], [tlPushToStack] )
*-- script snippets
this.addParse( "(<script)((.|\n)*?)(</script)", [=$1+THIS.pushToStack($2)+$4],.t., .t., .f. )
*-- PRE
this.addParse( "(<pre)((.|\n)*?)(</pre)", [=$1+THIS.pushToStack($2)+$4],.t., .t., .f. )
*-- comments
this.addParse( "(<!-)((.|\n)*?)(->)", [=$1+THIS.pushToStack($2)+$4], .t., .t., .f. )
*-- anchors
this.addParse( "(<a )((.|\n)*?)(</a>)", [=$1+THIS.pushToStack($2)+$4], .t., .t., .f. )
*-- table tags
this.addParse( "(<table )((.|\n)*?)(>)", [=$1+THIS.pushToStack($2)+$4], .t., .t., .f. )
this.addParse( "(<tr )((.|\n)*?)(>)", [=$1+THIS.pushToStack($2)+$4], .t., .t., .f. )
this.addParse( "(<td )((.|\n)*?)(>)", [=$1+THIS.pushToStack($2)+$4], .t., .t., .f. )
this.addParse( "(<th )((.|\n)*?)(>)", [=$1+THIS.pushToStack($2)+$4], .t., .t., .f. )
*-- img etc
this.addParse( "(<img)((.|\n)*?)(>)", [=$1+THIS.pushToStack($2)+$4], .t., .t., .f. )
this.addParse( "(<ol )((.|\n)*?)(>)", [=$1+THIS.pushToStack($2)+$4], .t., .t., .f. )
this.addParse( "(<ul )((.|\n)*?)(>)", [=$1+THIS.pushToStack($2)+$4], .t., .t., .f. )
this.addParse( "(<li )((.|\n)*?)(>)", [=$1+THIS.pushToStack($2)+$4], .t., .t., .f. )
*-- span div etc
this.addParse( "(<span )((.|\n)*?)(>)", [=$1+THIS.pushToStack($2)+$4], .t., .t., .f. )
this.addParse( "(<div )((.|\n)*?)(>)", [=$1+THIS.pushToStack($2)+$4], .t., .t., .f. )
*-- style
this.addParse( "(<style)((.|\n)*?)(</style)", [=$1+THIS.pushToStack($2)+$4], .t., .t., .f. )
*-- form stuff
this.addParse( "(<form)((.|\n)*?)(>)", [=$1+THIS.pushToStack($2)+$4],.t., .t., .f. )
this.addParse( "(<textarea)((.|\n)*?)(</textarea)", [=$1+THIS.pushToStack($2)+$4],.t., .t., .f. )
this.addParse( "(<input)((.|\n)*?)(>)", [=$1+THIS.pushToStack($2)+$4],.t., .t., .f. )
this.addParse( "(<select)((.|\n)*?)(</select)", [=$1+THIS.pushToStack($2)+$4],.t., .t., .f. )
this.addParse( "(<button)((.|\n)*?)(</button)", [=$1+THIS.pushToStack($2)+$4],.t., .t., .f. )
RETURN DODEFAULT()
ENDFUNC
ENDDEFINE
*** ========================================================== ***
DEFINE CLASS L7CRParser as L7BaseParser
* provides <br's> for carriage returns
* due to lack of good lookbehind assertion grouping in vbscript, we'll
* take the ALINES() route here.
* tags we don't want to put <br> behind
* use lowercase, both opening and closing tags will be blocked
* ie, <table and </table will be blocked if |table| appears in the list
cDisallowedEndingTags = [|table|tr|td|th|caption|ol|li|ul|hr|br|ig|p|div|!|h1|h2|h3|h4|-|script|style|pre|col|colgroup|thead|tbody|]
*-- we won't add any <br> in blank lines following these tags, note you must
*-- include both open and close tags here as needed
cDisallowedFollowTags = [table,tr,/tr,td,/td,th,/th,p,/p]
*-- note that we do allow <br>s after tables
cBRTag = L7BR
* --------------------------------------------------------- *
FUNCTION isPatternPresent( tcText )
RETURN !EMPTY( m.tcText )
* --------------------------------------------------------- *
FUNCTION tagOKForBR( tcTag )
LOCAL llRetVal
llRetval = ! ( [|] + ALLTRIM( LOWER( m.tcTag ) ) + [|] $ this.cDisallowedEndingTags )
RETURN m.llRetVal
ENDFUNC && tagOKForBR
* --------------------------------------------------------- *
FUNCTION parse( tcText )
*-- note: tcText is passed by reference
LOCAL lnTotLines, laLines(1), lcLine, lcParsedText, llValidLine, lcTag, lnLastTag, lnLine, lcWholeTag, lcBRTag
lcBRTag = this.cBRTag
lnTotLines = ALINES( m.laLines, m.tcText )
lcParsedText = []
lcTag = [~] && placeholder for first pass.
lcWholeTag = [~]
FOR m.lnLine= 1 TO m.lnTotLines
lcLine = laLines( m.lnLine )
llValidLine = .t.
llValidLine = NOT( EMPTY( m.lcLine ) AND ;
m.lcWholeTag $ this.cDisallowedFollowTags )
*---since the HTMLIgnore parser may be yanking things we should not
* be placing a <br> behind, we need to avoid the placeholder (left by HTMLIgnore)
* this can be a problem if *other* parsers yank stuff we *do* want to <br>
* Testing changes.. HTMLIgnore now leaves the tag tailings so CR parser can decide.
* llValidLine = llValidLine and NOT( RIGHT( ALLTRIM( m.lcLine ), 1) = PLACEHOLDER_DELIM )
IF llValidLine AND RIGHT( ALLTRIM( m.lcLine ), 1) = [>]
*-- does line end with a tag?
lnLasttag = OCCURS([<], m.lcLine)
IF m.lnLastTag>0
lcTag = STREXTRACT( ALLTRIM(m.lcLine) + [*], [<], [>*], m.lnLastTag)
lcWholeTag = GETWORDNUM( m.lcTag, 1) && includes possible leading / char
lcTag = IIF( m.lcTag = "/", SUBSTR( m.lcTag, 2 ), m.lcTag)
lcTag = IIF( m.lcTag = "!", LEFT( m.lcTag, 1 ), m.lcTag)
lcTag = GETWORDNUM( m.lcTag, 1)
llValidLine = this.tagOKforBR( m.lctag )
ENDIF
ENDIF
*-- llValidLine will be .t. if this doesn't end with a bad tag
lcParsedText = m.lcParsedText + ;
m.lcLine + ;
IIF( m.llValidLine and m.lnLine < m.lntotLines, m.lcBRTag, [] ) + CRLF
ENDFOR
tcText = m.lcParsedText
ENDFUNC && parse
ENDDEFINE
*-- EOC CRParser
*** ========================================================== ***
DEFINE CLASS L7ParagraphParser as L7BaseParser
* provides <p's> and <br's> for carriage returns
* due to lack of good lookbehind assertion grouping in vbscript, we'll
* take the ALINES() route here.
* tags we don't want to put <br> behind
* use lowercase, both opening and closing tags will be blocked
* ie, <table and </table will be blocked if |table| appears in the list
cDisallowedEndingTags = [|table|tr|td|th|caption|ol|li|ul|hr|br|ig|p|div|!|h1|h2|h3|h4|-|script|style|pre|col|colgroup|thead|tbody|]
*-- we won't add any <br> in blank lines following these tags, note you must
*-- include both open and close tags here as needed
cDisallowedFollowTags = [table,tr,/tr,td,/td,th,/th,p,/p]
*-- note that we do allow <br>s after tables
cBRTag = L7BR
lSingleCrDenotesParagraph = .F. && .F. means 2 CR's are required for a <p>, single means <br>
lLastLineIsParagraph = .T. && treat last line as <p> even though not an extra CR at end
* --------------------------------------------------------- *
FUNCTION isPatternPresent( tcText )
RETURN !EMPTY( m.tcText )
* --------------------------------------------------------- *
FUNCTION tagOKForBR( tcTag )
LOCAL llRetVal
llRetval = ! ( [|] + ALLTRIM( LOWER( m.tcTag ) ) + [|] $ this.cDisallowedEndingTags )
RETURN m.llRetVal
ENDFUNC && tagOKForBR
* --------------------------------------------------------- *
FUNCTION parse( tcText )
*-- note: tcText is passed by reference
LOCAL lnTotLines, laLines(1), lcLine, lcNextLine, lcParsedText, llValidLine, lcTag, ;
lnLastTag, lnLine, lcWholeTag, lcBRTag, llIsBr, llWasBr
lcBRTag = this.cBRTag
lnTotLines = ALINES( m.laLines, m.tcText )
lcParsedText = []
lcTag = [~] && placeholder for first pass.
lcWholeTag = [~]
FOR m.lnLine= 1 TO m.lnTotLines
lcLine = laLines( m.lnLine )
llValidLine = .t.
llValidLine = NOT( EMPTY( m.lcLine ) AND ;
m.lcWholeTag $ this.cDisallowedFollowTags )
*---since the HTMLIgnore parser may be yanking things we should not
* be placing a <br> behind, we need to avoid the placeholder (left by HTMLIgnore)