public
Description: Markdown processor in Ruby; foked from official SVN repo to fix bugs
Homepage: http://www.deveiate.org/projects/BlueCloth
Clone URL: git://github.com/mislav/bluecloth.git
split BlueCloth implementation across transform/blocks,inline,links,util 
files
mislav (author)
Tue Apr 22 16:47:16 -0700 2008
commit  a2f123d592dda5539fd317f9530a9936e2b6e3f3
tree    1e5ed6d236208287d74f6d00b7945665d5062092
parent  7667deac7fdc6cdb738247682b6a9f16ebdfaa0d
...
1
2
3
4
...
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
...
137
138
139
140
 
141
142
143
144
145
146
147
148
149
150
151
152
...
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
 
 
...
 
1
2
3
...
48
49
50
 
 
 
 
 
51
 
52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
54
55
56
57
58
59
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
62
63
64
65
...
73
74
75
 
76
77
78
79
80
 
 
 
 
 
81
82
83
...
87
88
89
90
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
93
0
@@ -1,4 +1,3 @@
0
-#!/usr/bin/ruby
0
 #
0
 # Bluecloth is a Ruby implementation of Markdown, a text-to-HTML conversion
0
 # tool.
0
@@ -49,81 +48,18 @@
0
 # regexps.
0
 #
0
 # * Put the StringScanner in the render state for thread-safety.
0
-#
0
-# == Version
0
-#
0
-# $Id$
0
-#
0
 
0
-require 'digest/md5'
0
 require 'logger'
0
-require 'strscan'
0
-
0
-
0
-### BlueCloth is a Ruby implementation of Markdown, a text-to-HTML conversion
0
-### tool.
0
-class BlueCloth < String
0
-
0
- ### Exception class for formatting errors.
0
- class FormatError < RuntimeError
0
-
0
- ### Create a new FormatError with the given source +str+ and an optional
0
- ### message about the +specific+ error.
0
- def initialize( str, specific=nil )
0
- if specific
0
- msg = "Bad markdown format near %p: %s" % [ str, specific ]
0
- else
0
- msg = "Bad markdown format near %p" % str
0
- end
0
-
0
- super( msg )
0
- end
0
- end
0
-
0
 
0
+# BlueCloth is a Ruby implementation of Markdown, a text-to-HTML conversion
0
+# tool.
0
+class BlueCloth
0
+
0
   # Release Version
0
   Version = '0.0.3'
0
 
0
- # SVN Revision
0
- SvnRev = %q$Rev$
0
-
0
- # SVN Id tag
0
- SvnId = %q$Id$
0
-
0
- # SVN URL
0
- SvnUrl = %q$URL$
0
-
0
-
0
- # Rendering state struct. Keeps track of URLs, titles, and HTML blocks
0
- # midway through a render. I prefer this to the globals of the Perl version
0
- # because globals make me break out in hives. Or something.
0
- RenderState = Struct::new( "RenderState", :urls, :titles, :html_blocks, :log )
0
-
0
- # Tab width for #detab! if none is specified
0
- TabWidth = 4
0
-
0
- # The tag-closing string -- set to '>' for HTML
0
- EmptyElementSuffix = "/>";
0
-
0
- # Table of MD5 sums for escaped characters
0
- EscapeTable = {}
0
- '\\`*_{}[]()#.!'.split(//).each {|char|
0
- hash = Digest::MD5::hexdigest( char )
0
-
0
- EscapeTable[ char ] = {
0
- :md5 => hash,
0
- :md5re => Regexp::new( hash ),
0
- :re => Regexp::new( '\\\\' + Regexp::escape(char) ),
0
- }
0
- }
0
-
0
-
0
- #################################################################
0
- ### I N S T A N C E M E T H O D S
0
- #################################################################
0
-
0
- ### Create a new BlueCloth string.
0
- def initialize( content="", *restrictions )
0
+ # Create a new BlueCloth string.
0
+ def initialize(content = "", *restrictions)
0
     @log = Logger::new( $deferr )
0
     @log.level = $DEBUG ?
0
       Logger::DEBUG :
0
@@ -137,16 +73,11 @@ class BlueCloth < String
0
     restrictions.flatten.each {|r| __send__("#{r}=", true) }
0
     @fold_lines = true
0
 
0
- super( content )
0
+ @content = content
0
 
0
     @log.debug "String is: %p" % self
0
   end
0
 
0
-
0
- ######
0
- public
0
- ######
0
-
0
   # Filters for controlling what gets output for untrusted input. (But really,
0
   # you're filtering bad stuff out of untrusted input at submission-time via
0
   # untainting, aren't you?)
0
@@ -156,989 +87,7 @@ class BlueCloth < String
0
   # so this isn't used by anything.
0
   attr_accessor :fold_lines
0
 
0
+end
0
 
0
- ### Render Markdown-formatted text in this string object as HTML and return
0
- ### it. The parameter is for compatibility with RedCloth, and is currently
0
- ### unused, though that may change in the future.
0
- def to_html( lite=false )
0
-
0
- # Create a StringScanner we can reuse for various lexing tasks
0
- @scanner = StringScanner::new( '' )
0
-
0
- # Make a structure to carry around stuff that gets placeholdered out of
0
- # the source.
0
- rs = RenderState::new( {}, {}, {} )
0
-
0
- # Make a copy of the string with normalized line endings, tabs turned to
0
- # spaces, and a couple of guaranteed newlines at the end
0
- text = self.gsub( /\r\n?/, "\n" ).detab
0
- text += "\n\n"
0
- @log.debug "Normalized line-endings: %p" % text
0
-
0
- # Filter HTML if we're asked to do so
0
- if self.filter_html
0
- text.gsub!( "<", "&lt;" )
0
- text.gsub!( ">", "&gt;" )
0
- @log.debug "Filtered HTML: %p" % text
0
- end
0
-
0
- # Simplify blank lines
0
- text.gsub!( /^ +$/, '' )
0
- @log.debug "Tabs -> spaces/blank lines stripped: %p" % text
0
-
0
- # Replace HTML blocks with placeholders
0
- text = hide_html_blocks( text, rs )
0
- @log.debug "Hid HTML blocks: %p" % text
0
- @log.debug "Render state: %p" % rs
0
-
0
- # Strip link definitions, store in render state
0
- text = strip_link_definitions( text, rs )
0
- @log.debug "Stripped link definitions: %p" % text
0
- @log.debug "Render state: %p" % rs
0
-
0
- # Escape meta-characters
0
- text = escape_special_chars( text )
0
- @log.debug "Escaped special characters: %p" % text
0
-
0
- # Transform block-level constructs
0
- text = apply_block_transforms( text, rs )
0
- @log.debug "After block-level transforms: %p" % text
0
-
0
- # Now swap back in all the escaped characters
0
- text = unescape_special_chars( text )
0
- @log.debug "After unescaping special characters: %p" % text
0
-
0
- return text
0
- end
0
-
0
-
0
- ### Convert tabs in +str+ to spaces.
0
- def detab( tabwidth=TabWidth )
0
- copy = self.dup
0
- copy.detab!( tabwidth )
0
- return copy
0
- end
0
-
0
-
0
- ### Convert tabs to spaces in place and return self if any were converted.
0
- def detab!( tabwidth=TabWidth )
0
- newstr = self.split( /\n/ ).collect {|line|
0
- line.gsub( /(.*?)\t/ ) do
0
- $1 + ' ' * (tabwidth - $1.length % tabwidth)
0
- end
0
- }.join("\n")
0
- self.replace( newstr )
0
- end
0
-
0
-
0
- #######
0
- #private
0
- #######
0
-
0
- ### Do block-level transforms on a copy of +str+ using the specified render
0
- ### state +rs+ and return the results.
0
- def apply_block_transforms( str, rs )
0
- # Port: This was called '_runBlockGamut' in the original
0
-
0
- @log.debug "Applying block transforms to:\n %p" % str
0
- text = transform_headers( str, rs )
0
- text = transform_hrules( text, rs )
0
- text = transform_lists( text, rs )
0
- text = transform_code_blocks( text, rs )
0
- text = transform_block_quotes( text, rs )
0
- text = transform_auto_links( text, rs )
0
- text = hide_html_blocks( text, rs )
0
-
0
- text = form_paragraphs( text, rs )
0
-
0
- @log.debug "Done with block transforms:\n %p" % text
0
- return text
0
- end
0
-
0
-
0
- ### Apply Markdown span transforms to a copy of the specified +str+ with the
0
- ### given render state +rs+ and return it.
0
- def apply_span_transforms( str, rs )
0
- @log.debug "Applying span transforms to:\n %p" % str
0
-
0
- str = transform_code_spans( str, rs )
0
- str = encode_html( str )
0
- str = transform_images( str, rs )
0
- str = transform_anchors( str, rs )
0
- str = transform_italic_and_bold( str, rs )
0
-
0
- # Hard breaks
0
- str.gsub!( / {2,}\n/, "<br#{EmptyElementSuffix}\n" )
0
-
0
- @log.debug "Done with span transforms:\n %p" % str
0
- return str
0
- end
0
-
0
-
0
- # The list of tags which are considered block-level constructs and an
0
- # alternation pattern suitable for use in regexps made from the list
0
- StrictBlockTags = %w[ p div h[1-6] blockquote pre table dl ol ul script noscript
0
- form fieldset iframe math ins del ]
0
- StrictTagPattern = StrictBlockTags.join('|')
0
-
0
- LooseBlockTags = StrictBlockTags - %w[ins del]
0
- LooseTagPattern = LooseBlockTags.join('|')
0
-
0
- # Nested blocks:
0
- # <div>
0
- # <div>
0
- # tags for inner block must be indented.
0
- # </div>
0
- # </div>
0
- StrictBlockRegex = %r{
0
- ^ # Start of line
0
- <(#{StrictTagPattern}) # Start tag: \2
0
- \b # word break
0
- (.*\n)*? # Any number of lines, minimal match
0
- </\1> # Matching end tag
0
- [ ]* # trailing spaces
0
- $ # End of line or document
0
- }ix
0
-
0
- # More-liberal block-matching
0
- LooseBlockRegex = %r{
0
- ^ # Start of line
0
- <(#{LooseTagPattern}) # start tag: \2
0
- \b # word break
0
- (.*\n)*? # Any number of lines, minimal match
0
- .*</\1> # Anything + Matching end tag
0
- [ ]* # trailing spaces
0
- $ # End of line or document
0
- }ix
0
-
0
- # Special case for <hr />.
0
- HruleBlockRegex = %r{
0
- ( # $1
0
- \A\n? # Start of doc + optional \n
0
- | # or
0
- .*\n\n # anything + blank line
0
- )
0
- ( # save in $2
0
- [ ]* # Any spaces
0
- <hr # Tag open
0
- \b # Word break
0
- ([^<>])*? # Attributes
0
- /?> # Tag close
0
- $ # followed by a blank line or end of document
0
- )
0
- }ix
0
-
0
- ### Replace all blocks of HTML in +str+ that start in the left margin with
0
- ### tokens.
0
- def hide_html_blocks( str, rs )
0
- @log.debug "Hiding HTML blocks in %p" % str
0
-
0
- # Tokenizer proc to pass to gsub
0
- tokenize = lambda {|match|
0
- key = Digest::MD5::hexdigest( match )
0
- rs.html_blocks[ key ] = match
0
- @log.debug "Replacing %p with %p" % [ match, key ]
0
- "\n\n#{key}\n\n"
0
- }
0
-
0
- rval = str.dup
0
-
0
- @log.debug "Finding blocks with the strict regex..."
0
- rval.gsub!( StrictBlockRegex, &tokenize )
0
-
0
- @log.debug "Finding blocks with the loose regex..."
0
- rval.gsub!( LooseBlockRegex, &tokenize )
0
-
0
- @log.debug "Finding hrules..."
0
- rval.gsub!( HruleBlockRegex ) {|match| $1 + tokenize[$2] }
0
-
0
- return rval
0
- end
0
-
0
-
0
- # Link defs are in the form: ^[id]: url "optional title"
0
- LinkRegex = %r{
0
- ^[ ]*\[(.+)\]: # id = $1
0
- [ ]*
0
- \n? # maybe *one* newline
0
- [ ]*
0
- <?(\S+?)>? # url = $2
0
- [ ]*
0
- \n? # maybe one newline
0
- [ ]*
0
- (?:
0
- # Titles are delimited by "quotes" or (parens).
0
- ["(]
0
- (.+?) # title = $3
0
- [")] # Matching ) or "
0
- [ ]*
0
- )? # title is optional
0
- (?:\n+|\Z)
0
- }x
0
-
0
- ### Strip link definitions from +str+, storing them in the given RenderState
0
- ### +rs+.
0
- def strip_link_definitions( str, rs )
0
- str.gsub( LinkRegex ) {|match|
0
- id, url, title = $1, $2, $3
0
-
0
- rs.urls[ id.downcase ] = encode_html( url )
0
- unless title.nil?
0
- rs.titles[ id.downcase ] = title.gsub( /"/, "&quot;" )
0
- end
0
- ""
0
- }
0
- end
0
-
0
-
0
- ### Escape special characters in the given +str+
0
- def escape_special_chars( str )
0
- @log.debug " Escaping special characters"
0
- text = ''
0
-
0
- # The original Markdown source has something called '$tags_to_skip'
0
- # declared here, but it's never used, so I don't define it.
0
-
0
- tokenize_html( str ) {|token, str|
0
- @log.debug " Adding %p token %p" % [ token, str ]
0
- case token
0
-
0
- # Within tags, encode * and _
0
- when :tag
0
- text += str.
0
- gsub( /\*/, EscapeTable['*'][:md5] ).
0
- gsub( /_/, EscapeTable['_'][:md5] )
0
-
0
- # Encode backslashed stuff in regular text
0
- when :text
0
- text += encode_backslash_escapes( str )
0
- else
0
- raise TypeError, "Unknown token type %p" % token
0
- end
0
- }
0
-
0
- @log.debug " Text with escapes is now: %p" % text
0
- return text
0
- end
0
-
0
-
0
- ### Swap escaped special characters in a copy of the given +str+ and return
0
- ### it.
0
- def unescape_special_chars( str )
0
- EscapeTable.each {|char, hash|
0
- @log.debug "Unescaping escaped %p with %p" % [ char, hash[:md5re] ]
0
- str.gsub!( hash[:md5re], char )
0
- }
0
-
0
- return str
0
- end
0
-
0
-
0
- ### Return a copy of the given +str+ with any backslashed special character
0
- ### in it replaced with MD5 placeholders.
0
- def encode_backslash_escapes( str )
0
- # Make a copy with any double-escaped backslashes encoded
0
- text = str.gsub( /\\\\/, EscapeTable['\\'][:md5] )
0
-
0
- EscapeTable.each_pair {|char, esc|
0
- next if char == '\\'
0
- text.gsub!( esc[:re], esc[:md5] )
0
- }
0
-
0
- return text
0
- end
0
-
0
-
0
- ### Transform any Markdown-style horizontal rules in a copy of the specified
0
- ### +str+ and return it.
0
- def transform_hrules( str, rs )
0
- @log.debug " Transforming horizontal rules"
0
- str.gsub( /^( ?[\-\*_] ?){3,}$/, "\n<hr#{EmptyElementSuffix}\n" )
0
- end
0
-
0
-
0
-
0
- # Patterns to match and transform lists
0
- ListMarkerOl = %r{\d+\.}
0
- ListMarkerUl = %r{[*+-]}
0
- ListMarkerAny = Regexp::union( ListMarkerOl, ListMarkerUl )
0
-
0
- ListRegexp = %r{
0
- (?:
0
- ^[ ]{0,#{TabWidth - 1}} # Indent < tab width
0
- (#{ListMarkerAny}) # unordered or ordered ($1)
0
- [ ]+ # At least one space
0
- )
0
- (?m:.+?) # item content (include newlines)
0
- (?:
0
- \z # Either EOF
0
- | # or
0
- \n{2,} # Blank line...
0
- (?=\S) # ...followed by non-space
0
- (?![ ]* # ...but not another item
0
- (#{ListMarkerAny})
0
- [ ]+)
0
- )
0
- }x
0
-
0
- ### Transform Markdown-style lists in a copy of the specified +str+ and
0
- ### return it.
0
- def transform_lists( str, rs )
0
- @log.debug " Transforming lists at %p" % (str[0,100] + '...')
0
-
0
- str.gsub( ListRegexp ) {|list|
0
- @log.debug " Found list %p" % list
0
- bullet = $1
0
- list_type = (ListMarkerUl.match(bullet) ? "ul" : "ol")
0
- list.gsub!( /\n{2,}/, "\n\n\n" )
0
-
0
- %{<%s>\n%s</%s>\n} % [
0
- list_type,
0
- transform_list_items( list, rs ),
0
- list_type,
0
- ]
0
- }
0
- end
0
-
0
-
0
- # Pattern for transforming list items
0
- ListItemRegexp = %r{
0
- (\n)? # leading line = $1
0
- (^[ ]*) # leading whitespace = $2
0
- (#{ListMarkerAny}) [ ]+ # list marker = $3
0
- ((?m:.+?) # list item text = $4
0
- (\n{1,2}))
0
- (?= \n* (\z | \2 (#{ListMarkerAny}) [ ]+))
0
- }x
0
-
0
- ### Transform list items in a copy of the given +str+ and return it.
0
- def transform_list_items( str, rs )
0
- @log.debug " Transforming list items"
0
-
0
- # Trim trailing blank lines
0
- str = str.sub( /\n{2,}\z/, "\n" )
0
-
0
- str.gsub( ListItemRegexp ) {|line|
0
- @log.debug " Found item line %p" % line
0
- leading_line, item = $1, $4
0
-
0
- if leading_line or /\n{2,}/.match( item )
0
- @log.debug " Found leading line or item has a blank"
0
- item = apply_block_transforms( outdent(item), rs )
0
- else
0
- # Recursion for sub-lists
0
- @log.debug " Recursing for sublist"
0
- item = transform_lists( outdent(item), rs ).chomp
0
- item = apply_span_transforms( item, rs )
0
- end
0
-
0
- %{<li>%s</li>\n} % item
0
- }
0
- end
0
-
0
-
0
- # Pattern for matching codeblocks
0
- CodeBlockRegexp = %r{
0
- (?:\n\n|\A)
0
- ( # $1 = the code block
0
- (?:
0
- (?:[ ]{#{TabWidth}} | \t) # a tab or tab-width of spaces
0
- .*\n+
0
- )+
0
- )
0
- (^[ ]{0,#{TabWidth - 1}}\S|\Z) # Lookahead for non-space at
0
- # line-start, or end of doc
0
- }x
0
-
0
- ### Transform Markdown-style codeblocks in a copy of the specified +str+ and
0
- ### return it.
0
- def transform_code_blocks( str, rs )
0
- @log.debug " Transforming code blocks"
0
-
0
- str.gsub( CodeBlockRegexp ) {|block|
0
- codeblock = $1
0
- remainder = $2
0
-
0
- # Generate the codeblock
0
- %{\n\n<pre><code>%s\n</code></pre>\n\n%s} %
0
- [ encode_code( outdent(codeblock), rs ).rstrip, remainder ]
0
- }
0
- end
0
-
0
-
0
- # Pattern for matching Markdown blockquote blocks
0
- BlockQuoteRegexp = %r{
0
- (?:
0
- ^[ ]*>[ ]? # '>' at the start of a line
0
- .+\n # rest of the first line
0
- (?:.+\n)* # subsequent consecutive lines
0
- \n* # blanks
0
- )+
0
- }x
0
- PreChunk = %r{ ( ^ \s* <pre> .+? </pre> ) }xm
0
-
0
- ### Transform Markdown-style blockquotes in a copy of the specified +str+
0
- ### and return it.
0
- def transform_block_quotes( str, rs )
0
- @log.debug " Transforming block quotes"
0
-
0
- str.gsub( BlockQuoteRegexp ) {|quote|
0
- @log.debug "Making blockquote from %p" % quote
0
-
0
- quote.gsub!( /^ *> ?/, '' ) # Trim one level of quoting
0
- quote.gsub!( /^ +$/, '' ) # Trim whitespace-only lines
0
-
0
- indent = " " * TabWidth
0
- quoted = %{<blockquote>\n%s\n</blockquote>\n\n} %
0
- apply_block_transforms( quote, rs ).
0
- gsub( /^/, indent ).
0
- gsub( PreChunk ) {|m| m.gsub(/^#{indent}/o, '') }
0
- @log.debug "Blockquoted chunk is: %p" % quoted
0
- quoted
0
- }
0
- end
0
-
0
-
0
- AutoAnchorURLRegexp = /<((https?|ftp):[^'">\s]+)>/
0
- AutoAnchorEmailRegexp = %r{
0
- <
0
- (
0
- [-.\w]+
0
- \@
0
- [-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+
0
- )
0
- >
0
- }xi
0
-
0
- ### Transform URLs in a copy of the specified +str+ into links and return
0
- ### it.
0
- def transform_auto_links( str, rs )
0
- @log.debug " Transforming auto-links"
0
- str.gsub( AutoAnchorURLRegexp, %{<a href="\\1">\\1</a>}).
0
- gsub( AutoAnchorEmailRegexp ) {|add