Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Nov 20, 2020. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmarkdown.lua
More file actions
Latest commit
1359 lines (1205 loc) · 40.1 KB
/
Copy pathmarkdown.lua
File metadata and controls
1359 lines (1205 loc) · 40.1 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
#!/usr/bin/env lua
--[[
# markdown.lua -- version 0.32
<http://www.frykholm.se/files/markdown.lua>
**Author:** Niklas Frykholm, <niklas@frykholm.se>
**Date:** 31 May 2008
This is an implementation of the popular text markup language Markdown in pure Lua.
Markdown can convert documents written in a simple and easy to read text format
to well-formatted HTML. For a more thourough description of Markdown and the Markdown
syntax, see <http://daringfireball.net/projects/markdown>.
The original Markdown source is written in Perl and makes heavy use of advanced
regular expression techniques (such as negative look-ahead, etc) which are not available
in Lua's simple regex engine. Therefore this Lua port has been rewritten from the ground
up. It is probably not completely bug free. If you notice any bugs, please report them to
me. A unit test that exposes the error is helpful.
## Usage
require "markdown"
markdown(source)
``markdown.lua`` exposes a single global function named ``markdown(s)`` which applies the
Markdown transformation to the specified string.
``markdown.lua`` can also be used directly from the command line:
lua markdown.lua test.md
Creates a file ``test.html`` with the converted content of ``test.md``. Run:
lua markdown.lua -h
For a description of the command-line options.
``markdown.lua`` uses the same license as Lua, the MIT license.
## License
Copyright © 2008 Niklas Frykholm.
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
## Version history
- **0.32** -- 31 May 2008
- Fix for links containing brackets
- **0.31** -- 1 Mar 2008
- Fix for link definitions followed by spaces
- **0.30** -- 25 Feb 2008
- Consistent behavior with Markdown when the same link reference is reused
- **0.29** -- 24 Feb 2008
- Fix for <pre> blocks with spaces in them
- **0.28** -- 18 Feb 2008
- Fix for link encoding
- **0.27** -- 14 Feb 2008
- Fix for link database links with ()
- **0.26** -- 06 Feb 2008
- Fix for nested italic and bold markers
- **0.25** -- 24 Jan 2008
- Fix for encoding of naked <
- **0.24** -- 21 Jan 2008
- Fix for link behavior.
- **0.23** -- 10 Jan 2008
- Fix for a regression bug in longer expressions in italic or bold.
- **0.22** -- 27 Dec 2007
- Fix for crash when processing blocks with a percent sign in them.
- **0.21** -- 27 Dec 2007
- Fix for combined strong and emphasis tags
- **0.20** -- 13 Oct 2007
- Fix for < as well in image titles, now matches Dingus behavior
- **0.19** -- 28 Sep 2007
- Fix for quotation marks " and ampersands & in link and image titles.
- **0.18** -- 28 Jul 2007
- Does not crash on unmatched tags (behaves like standard markdown)
- **0.17** -- 12 Apr 2007
- Fix for links with %20 in them.
- **0.16** -- 12 Apr 2007
- Do not require arg global to exist.
- **0.15** -- 28 Aug 2006
- Better handling of links with underscores in them.
- **0.14** -- 22 Aug 2006
- Bug for *`foo()`*
- **0.13** -- 12 Aug 2006
- Added -l option for including stylesheet inline in document.
- Fixed bug in -s flag.
- Fixed emphasis bug.
- **0.12** -- 15 May 2006
- Fixed several bugs to comply with MarkdownTest 1.0 <http://six.pairlist.net/pipermail/markdown-discuss/2004-December/000909.html>
- **0.11** -- 12 May 2006
- Fixed bug for escaping `*` and `_` inside code spans.
- Added license terms.
- Changed join() to table.concat().
- **0.10** -- 3 May 2006
- Initial public release.
// Niklas
]]
-- Set up a table for holding local functions to avoid polluting the global namespace
localM= {}
localMT= {__index=_G}
setmetatable(M, MT)
setfenv(1, M)
----------------------------------------------------------------------
-- Utility functions
----------------------------------------------------------------------
-- Locks table t from changes, writes an error if someone attempts to change the table.
-- This is useful for detecting variables that have "accidently" been made global. Something
-- I tend to do all too much.
functionlock(t)
functionlock_new_index(t, k, v)
error("module has been locked -- " ..k.." must be declared local", 2)
end
localmt= {__newindex=lock_new_index}
ifgetmetatable(t) thenmt.__index=getmetatable(t).__indexend
setmetatable(t, mt)
end
-- Returns the result of mapping the values in table t through the function f
functionmap(t, f)
localout= {}
fork,vinpairs(t) doout[k] =f(v,k) end
returnout
end
-- The identity function, useful as a placeholder.
functionidentity(text) returntextend
-- Functional style if statement. (NOTE: no short circuit evaluation)
functioniff(t, a, b) iftthenreturnaelsereturnbendend
-- Splits the text into an array of separate lines.
functionsplit(text, sep)
sep=sepor"\n"
locallines= {}
localpos=1
whiletruedo
localb,e=text:find(sep, pos)
ifnotbthentable.insert(lines, text:sub(pos)) breakend
table.insert(lines, text:sub(pos, b-1))
pos=e+1
end
returnlines
end
-- Converts tabs to spaces
functiondetab(text)
localtab_width=4
localfunctionrep(match)
localspaces=-match:len()
whilespaces<1dospaces=spaces+tab_widthend
returnmatch..string.rep("", spaces)
end
text=text:gsub("([^\n]-)\t", rep)
returntext
end
-- Applies string.find for every pattern in the list and returns the first match
functionfind_first(s, patterns, index)
localres= {}
for_,pinipairs(patterns) do
localmatch= {s:find(p, index)}
if#match>0and (#res==0ormatch[1] <res[1]) thenres=matchend
end
returnunpack(res)
end
-- If a replacement array is specified, the range [start, stop] in the array is replaced
-- with the replacement array and the resulting array is returned. Without a replacement
-- array the section of the array between start and stop is returned.
functionsplice(array, start, stop, replacement)
ifreplacementthen
localn=stop-start+1
whilen>0do
table.remove(array, start)
n=n-1
end
fori,vinipairs(replacement) do
table.insert(array, start, v)
end
returnarray
else
localres= {}
fori=start,stopdo
table.insert(res, array[i])
end
returnres
end
end
-- Outdents the text one step.
functionoutdent(text)
text="\n" ..text
text=text:gsub("\n ? ? ?", "\n")
text=text:sub(2)
returntext
end
-- Indents the text one step.
functionindent(text)
text=text:gsub("\n", "\n")
returntext
end
-- Does a simple tokenization of html data. Returns the data as a list of tokens.
-- Each token is a table with a type field (which is either "tag" or "text") and
-- a text field (which contains the original token data).
functiontokenize_html(html)
localtokens= {}
localpos=1
whiletruedo
localstart=find_first(html, {"<!%-%-", "<[a-z/!$]", "<%?"}, pos)
ifnotstartthen
table.insert(tokens, {type="text", text=html:sub(pos)})
break
end
ifstart~=posthentable.insert(tokens, {type="text", text=html:sub(pos, start-1)}) end
local_, stop
ifhtml:match("^<!%-%-", start) then
_,stop=html:find("%-%->", start)
elseifhtml:match("^<%?", start) then
_,stop=html:find("?>", start)
else
_,stop=html:find("%b<>", start)
end
ifnotstopthen
-- error("Could not match html tag " .. html:sub(start,start+30))
table.insert(tokens, {type="text", text=html:sub(start, start)})
pos=start+1
else
table.insert(tokens, {type="tag", text=html:sub(start, stop)})
pos=stop+1
end
end
returntokens
end
----------------------------------------------------------------------
-- Hash
----------------------------------------------------------------------
-- This is used to "hash" data into alphanumeric strings that are unique
-- in the document. (Note that this is not cryptographic hash, the hash
-- function is not one-way.) The hash procedure is used to protect parts
-- of the document from further processing.
localHASH= {
-- Has the hash been inited.
inited=false,
-- The unique string prepended to all hash values. This is to ensure
-- that hash values do not accidently coincide with an actual existing
-- string in the document.
identifier="",
-- Counter that counts up for each new hash instance.
counter=0,
-- Hash table.
table= {}
}
-- Inits hashing. Creates a hash_identifier that doesn't occur anywhere
-- in the text.
functioninit_hash(text)
HASH.inited=true
HASH.identifier=""
HASH.counter=0
HASH.table= {}
locals="HASH"
localcounter=0
localid
whiletruedo
id=s..counter
ifnottext:find(id, 1, true) thenbreakend
counter=counter+1
end
HASH.identifier=id
end
-- Returns the hashed value for s.
functionhash(s)
assert(HASH.inited)
ifnotHASH.table[s] then
HASH.counter=HASH.counter+1
localid=HASH.identifier..HASH.counter.."X"
HASH.table[s] =id
end
returnHASH.table[s]
end
----------------------------------------------------------------------
-- Protection
----------------------------------------------------------------------
-- The protection module is used to "protect" parts of a document
-- so that they are not modified by subsequent processing steps.
-- Protected parts are saved in a table for later unprotection
-- Protection data
localPD= {
-- Saved blocks that have been converted
blocks= {},
-- Block level tags that will be protected
tags= {"p", "div", "h1", "h2", "h3", "h4", "h5", "h6", "blockquote",
"pre", "table", "dl", "ol", "ul", "script", "noscript", "form", "fieldset",
"iframe", "math", "ins", "del"}
}
-- Pattern for matching a block tag that begins and ends in the leftmost
-- column and may contain indented subtags, i.e.
-- <div>
-- A nested block.
-- <div>
-- Nested data.
-- </div>
-- </div>
functionblock_pattern(tag)
return"\n<" ..tag..".-\n</" ..tag..">[ \t]*\n"
end
-- Pattern for matching a block tag that begins and ends with a newline
functionline_pattern(tag)
return"\n<" ..tag..".-</" ..tag..">[ \t]*\n"
end
-- Protects the range of characters from start to stop in the text and
-- returns the protected string.
functionprotect_range(text, start, stop)
locals=text:sub(start, stop)
localh=hash(s)
PD.blocks[h] =s
text=text:sub(1,start) ..h..text:sub(stop)
returntext
end
-- Protect every part of the text that matches any of the patterns. The first
-- matching pattern is protected first, etc.
functionprotect_matches(text, patterns)
whiletruedo
localstart, stop=find_first(text, patterns)
ifnotstartthenbreakend
text=protect_range(text, start, stop)
end
returntext
end
-- Protects blocklevel tags in the specified text
functionprotect(text)
-- First protect potentially nested block tags
text=protect_matches(text, map(PD.tags, block_pattern))
-- Then protect block tags at the line level.
text=protect_matches(text, map(PD.tags, line_pattern))
-- Protect <hr> and comment tags
text=protect_matches(text, {"\n<hr[^>]->[ \t]*\n"})
text=protect_matches(text, {"\n<!%-%-.-%-%->[ \t]*\n"})
returntext
end
-- Returns true if the string s is a hash resulting from protection
functionis_protected(s)
returnPD.blocks[s]
end
-- Unprotects the specified text by expanding all the nonces
functionunprotect(text)
fork,vinpairs(PD.blocks) do
v=v:gsub("%%", "%%%%")
text=text:gsub(k, v)
end
returntext
end
----------------------------------------------------------------------
-- Block transform
----------------------------------------------------------------------
-- The block transform functions transform the text on the block level.
-- They work with the text as an array of lines rather than as individual
-- characters.
-- Returns true if the line is a ruler of (char) characters.
-- The line must contain at least three char characters and contain only spaces and
-- char characters.
functionis_ruler_of(line, char)
ifnotline:match("^[ %" ..char.."]*$") thenreturnfalseend
ifnotline:match("%" ..char..".*%" ..char..".*%" ..char) thenreturnfalseend
returntrue
end
-- Identifies the block level formatting present in the line
functionclassify(line)
localinfo= {line=line, text=line}
ifline:match("^ ") then
info.type="indented"
info.outdented=line:sub(5)
returninfo
end
for_,cinipairs({'*', '-', '_', '='}) do
ifis_ruler_of(line, c) then
info.type="ruler"
info.ruler_char=c
returninfo
end
end
ifline=="" then
info.type="blank"
returninfo
end
ifline:match("^(#+)[ \t]*(.-)[ \t]*#*[ \t]*$") then
localm1, m2=line:match("^(#+)[ \t]*(.-)[ \t]*#*[ \t]*$")
info.type="header"
info.level=m1:len()
info.text=m2
returninfo
end
ifline:match("^ ? ? ?(%d+)%.[ \t]+(.+)") then
localnumber, text=line:match("^ ? ? ?(%d+)%.[ \t]+(.+)")
info.type="list_item"
info.list_type="numeric"
info.number=0+number
info.text=text
returninfo
end
ifline:match("^ ? ? ?([%*%+%-])[ \t]+(.+)") then
localbullet, text=line:match("^ ? ? ?([%*%+%-])[ \t]+(.+)")
info.type="list_item"
info.list_type="bullet"
info.bullet=bullet
info.text=text
returninfo
end
ifline:match("^>[ \t]?(.*)") then
info.type="blockquote"
info.text=line:match("^>[ \t]?(.*)")
returninfo
end
ifis_protected(line) then
info.type="raw"
info.html=unprotect(line)
returninfo
end
info.type="normal"
returninfo
end
-- Find headers constisting of a normal line followed by a ruler and converts them to
-- header entries.
functionheaders(array)
locali=1
whilei<=#array-1do
ifarray[i].type=="normal" andarray[i+1].type=="ruler" and
(array[i+1].ruler_char=="-" orarray[i+1].ruler_char=="=") then
localinfo= {line=array[i].line}
info.text=info.line
info.type="header"
info.level=iff(array[i+1].ruler_char=="=", 1, 2)
table.remove(array, i+1)
array[i] =info
end
i=i+1
end
returnarray
end
-- Find list blocks and convert them to protected data blocks
functionlists(array, sublist)
localfunctionprocess_list(arr)
localfunctionany_blanks(arr)
fori=1, #arrdo
ifarr[i].type=="blank" thenreturntrueend
end
returnfalse
end
localfunctionsplit_list_items(arr)
localacc= {arr[1]}
localres= {}
fori=2,#arrdo
ifarr[i].type=="list_item" then
table.insert(res, acc)
acc= {arr[i]}
else
table.insert(acc, arr[i])
end
end
table.insert(res, acc)
returnres
end
localfunctionprocess_list_item(lines, block)
whilelines[#lines].type=="blank" do
table.remove(lines)
end
localitemtext=lines[1].text
fori=2,#linesdo
itemtext=itemtext.."\n" ..outdent(lines[i].line)
end
ifblockthen
itemtext=block_transform(itemtext, true)
ifnotitemtext:find("<pre>") thenitemtext=indent(itemtext) end
return" <li>" ..itemtext.."</li>"
else
locallines=split(itemtext)
lines=map(lines, classify)
lines=lists(lines, true)
lines=blocks_to_html(lines, true)
itemtext=table.concat(lines, "\n")
ifnotitemtext:find("<pre>") thenitemtext=indent(itemtext) end
return" <li>" ..itemtext.."</li>"
end
end
localblock_list=any_blanks(arr)
localitems=split_list_items(arr)
localout=""
for_, iteminipairs(items) do
out=out..process_list_item(item, block_list) .."\n"
end
ifarr[1].list_type=="numeric" then
return"<ol>\n" ..out.."</ol>"
else
return"<ul>\n" ..out.."</ul>"
end
end
-- Finds the range of lines composing the first list in the array. A list
-- starts with (^ list_item) or (blank list_item) and ends with
-- (blank* $) or (blank normal).
--
-- A sublist can start with just (list_item) does not need a blank...
localfunctionfind_list(array, sublist)
localfunctionfind_list_start(array, sublist)
ifarray[1].type=="list_item" thenreturn1end
ifsublistthen
fori=1,#arraydo
ifarray[i].type=="list_item" thenreturniend
end
else
fori=1, #array-1do
ifarray[i].type=="blank" andarray[i+1].type=="list_item" then
returni+1
end
end
end
returnnil
end
localfunctionfind_list_end(array, start)
localpos=#array
fori=start, #array-1do
ifarray[i].type=="blank" andarray[i+1].type~="list_item"
andarray[i+1].type~="indented" andarray[i+1].type~="blank" then
pos=i-1
break
end
end
whilepos>startandarray[pos].type=="blank" do
pos=pos-1
end
returnpos
end
localstart=find_list_start(array, sublist)
ifnotstartthenreturnnilend
returnstart, find_list_end(array, start)
end
whiletruedo
localstart, stop=find_list(array, sublist)
ifnotstartthenbreakend
localtext=process_list(splice(array, start, stop))
localinfo= {
line=text,
type="raw",
html=text
}
array=splice(array, start, stop, {info})
end
-- Convert any remaining list items to normal
for_,lineinipairs(array) do
ifline.type=="list_item" thenline.type="normal" end
end
returnarray
end
-- Find and convert blockquote markers.
functionblockquotes(lines)
localfunctionfind_blockquote(lines)
localstart
fori,lineinipairs(lines) do
ifline.type=="blockquote" then
start=i
break
end
end
ifnotstartthenreturnnilend
localstop=#lines
fori=start+1, #linesdo
iflines[i].type=="blank" orlines[i].type=="blockquote" then
elseiflines[i].type=="normal" then
iflines[i-1].type=="blank" thenstop=i-1breakend
else
stop=i-1break
end
end
whilelines[stop].type=="blank" dostop=stop-1end
returnstart, stop
end
localfunctionprocess_blockquote(lines)
localraw=lines[1].text
fori=2,#linesdo
raw=raw.."\n" ..lines[i].text
end
localbt=block_transform(raw)
ifnotbt:find("<pre>") thenbt=indent(bt) end
return"<blockquote>\n" ..bt..
"\n</blockquote>"
end
whiletruedo
localstart, stop=find_blockquote(lines)
ifnotstartthenbreakend
localtext=process_blockquote(splice(lines, start, stop))
localinfo= {
line=text,
type="raw",
html=text
}
lines=splice(lines, start, stop, {info})
end
returnlines
end
-- Find and convert codeblocks.
functioncodeblocks(lines)
localfunctionfind_codeblock(lines)
localstart
fori,lineinipairs(lines) do
ifline.type=="indented" thenstart=ibreakend
end
ifnotstartthenreturnnilend
localstop=#lines
fori=start+1, #linesdo
iflines[i].type~="indented" andlines[i].type~="blank" then
stop=i-1
break
end
end
whilelines[stop].type=="blank" dostop=stop-1end
returnstart, stop
end
localfunctionprocess_codeblock(lines)
localraw=detab(encode_code(outdent(lines[1].line)))
fori=2,#linesdo
raw=raw.."\n" ..detab(encode_code(outdent(lines[i].line)))
end
return"<pre><code>" ..raw.."\n</code></pre>"
end
whiletruedo
localstart, stop=find_codeblock(lines)
ifnotstartthenbreakend
localtext=process_codeblock(splice(lines, start, stop))
localinfo= {
line=text,
type="raw",
html=text
}
lines=splice(lines, start, stop, {info})
end
returnlines
end
-- Convert lines to html code
functionblocks_to_html(lines, no_paragraphs)
localout= {}
locali=1
whilei<=#linesdo
localline=lines[i]
ifline.type=="ruler" then
table.insert(out, "<hr/>")
elseifline.type=="raw" then
table.insert(out, line.html)
elseifline.type=="normal" then
locals=line.line
whilei+1<=#linesandlines[i+1].type=="normal" do
i=i+1
s=s.."\n" ..lines[i].line
end
ifno_paragraphsthen
table.insert(out, span_transform(s))
else
table.insert(out, "<p>" ..span_transform(s) .."</p>")
end
elseifline.type=="header" then
locals="<h" ..line.level..">" ..span_transform(line.text) .."</h" ..line.level..">"
table.insert(out, s)
else
table.insert(out, line.line)
end
i=i+1
end
returnout
end
-- Perform all the block level transforms
functionblock_transform(text, sublist)
locallines=split(text)
lines=map(lines, classify)
lines=headers(lines)
lines=lists(lines, sublist)
lines=codeblocks(lines)
lines=blockquotes(lines)
lines=blocks_to_html(lines)
localtext=table.concat(lines, "\n")
returntext
end
-- Debug function for printing a line array to see the result
-- of partial transforms.
functionprint_lines(lines)
fori, lineinipairs(lines) do
print(i, line.type, line.textorline.line)
end
end
----------------------------------------------------------------------
-- Span transform
----------------------------------------------------------------------
-- Functions for transforming the text at the span level.
-- These characters may need to be escaped because they have a special
-- meaning in markdown.
escape_chars="'\\`*_{}[]()>#+-.!'"
escape_table= {}
functioninit_escape_table()
escape_table= {}
fori=1,#escape_charsdo
localc=escape_chars:sub(i,i)
escape_table[c] =hash(c)
end
end
-- Adds a new escape to the escape table.
functionadd_escape(text)
ifnotescape_table[text] then
escape_table[text] =hash(text)
end
returnescape_table[text]
end
-- Escape characters that should not be disturbed by markdown.
functionescape_special_chars(text)
localtokens=tokenize_html(text)
localout=""
for_, tokeninipairs(tokens) do
localt=token.text
iftoken.type=="tag" then
-- In tags, encode * and _ so they don't conflict with their use in markdown.
t=t:gsub("%*", escape_table["*"])
t=t:gsub("%_", escape_table["_"])
else
t=encode_backslash_escapes(t)
end
out=out..t
end
returnout
end
-- Encode backspace-escaped characters in the markdown source.
functionencode_backslash_escapes(t)
fori=1,escape_chars:len() do
localc=escape_chars:sub(i,i)
t=t:gsub("\\%" ..c, escape_table[c])
end
returnt
end
-- Unescape characters that have been encoded.
functionunescape_special_chars(t)
localtin=t
fork,vinpairs(escape_table) do
k=k:gsub("%%", "%%%%")
t=t:gsub(v,k)
end
ift~=tinthent=unescape_special_chars(t) end
returnt
end
-- Encode/escape certain characters inside Markdown code runs.
-- The point is that in code, these characters are literals,
-- and lose their special Markdown meanings.
functionencode_code(s)
s=s:gsub("%&", "&")
s=s:gsub("<", "<")
s=s:gsub(">", ">")
fork,vinpairs(escape_table) do
s=s:gsub("%"..k, v)
end
returns
end
-- Handle backtick blocks.
functioncode_spans(s)
s=s:gsub("\\\\", escape_table["\\"])
s=s:gsub("\\`", escape_table["`"])
localpos=1
whiletruedo
localstart, stop=s:find("`+", pos)
ifnotstartthenreturnsend
localcount=stop-start+1
-- Find a matching numbert of backticks
localestart, estop=s:find(string.rep("`", count), stop+1)
localbrstart=s:find("\n", stop+1)
ifestartand (notbrstartorestart<brstart) then
localcode=s:sub(stop+1, estart-1)
code=code:gsub("^[ \t]+", "")
code=code:gsub("[ \t]+$", "")
code=code:gsub(escape_table["\\"], escape_table["\\"] ..escape_table["\\"])
code=code:gsub(escape_table["`"], escape_table["\\"] ..escape_table["`"])
code="<code>" ..encode_code(code) .."</code>"
code=add_escape(code)
s=s:sub(1, start-1) ..code..s:sub(estop+1)
pos=start+code:len()
else
pos=stop+1
end
end
returns
end
-- Encode alt text... enodes &, and ".
functionencode_alt(s)
ifnotsthenreturnsend
s=s:gsub('&', '&')
s=s:gsub('"', '"')
s=s:gsub('<', '<')
returns
end
-- Handle image references
functionimages(text)
localfunctionreference_link(alt, id)
alt=encode_alt(alt:match("%b[]"):sub(2,-2))
id=id:match("%[(.*)%]"):lower()
ifid=="" thenid=text:lower() end
link_database[id] =link_database[id] or {}
ifnotlink_database[id].urlthenreturnnilend
localurl=link_database[id].urlorid
url=encode_alt(url)
localtitle=encode_alt(link_database[id].title)
iftitlethentitle=" title=\"" ..title.."\"" elsetitle="" end
returnadd_escape ('<img src="' ..url..'" alt="' ..alt..'"' ..title.."/>")
end
localfunctioninline_link(alt, link)
alt=encode_alt(alt:match("%b[]"):sub(2,-2))
localurl, title=link:match("%(<?(.-)>?[ \t]*['\"](.+)['\"]")
url=urlorlink:match("%(<?(.-)>?%)")
url=encode_alt(url)
title=encode_alt(title)
iftitlethen
returnadd_escape('<img src="' ..url..'" alt="' ..alt..'" title="' ..title..'"/>')
else
returnadd_escape('<img src="' ..url..'" alt="' ..alt..'"/>')
end
end
text=text:gsub("!(%b[])[ \t]*\n?[ \t]*(%b[])", reference_link)
text=text:gsub("!(%b[])(%b())", inline_link)
returntext
end
-- Handle anchor references
functionanchors(text)
localfunctionreference_link(text, id)
text=text:match("%b[]"):sub(2,-2)
id=id:match("%b[]"):sub(2,-2):lower()
ifid=="" thenid=text:lower() end
link_database[id] =link_database[id] or {}
ifnotlink_database[id].urlthenreturnnilend
localurl=link_database[id].urlorid
url=encode_alt(url)
localtitle=encode_alt(link_database[id].title)
iftitlethentitle=" title=\"" ..title.."\"" elsetitle="" end
returnadd_escape("<a href=\"" ..url.."\"" ..title..">") ..text..add_escape("</a>")
end
localfunctioninline_link(text, link)
text=text:match("%b[]"):sub(2,-2)
localurl, title=link:match("%(<?(.-)>?[ \t]*['\"](.+)['\"]")
title=encode_alt(title)
url=urlorlink:match("%(<?(.-)>?%)") or""
url=encode_alt(url)
iftitlethen
returnadd_escape("<a href=\"" ..url.."\" title=\"" ..title.."\">") ..text.."</a>"
else
returnadd_escape("<a href=\"" ..url.."\">") ..text..add_escape("</a>")
end
end
text=text:gsub("(%b[])[ \t]*\n?[ \t]*(%b[])", reference_link)
text=text:gsub("(%b[])(%b())", inline_link)
returntext
end
-- Handle auto links, i.e. <http://www.google.com/>.
functionauto_links(text)
localfunctionlink(s)
returnadd_escape("<a href=\"" ..s.."\">") ..s.."</a>"
end
-- Encode chars as a mix of dec and hex entitites to (perhaps) fool
-- spambots.
localfunctionencode_email_address(s)
-- Use a deterministic encoding to make unit testing possible.
-- Code 45% hex, 45% dec, 10% plain.
localhex= {code=function(c) return"&#x" ..string.format("%x", c:byte()) ..";" end, count=1, rate=0.45}
localdec= {code=function(c) return"&#" ..c:byte() ..";" end, count=0, rate=0.45}
localplain= {code=function(c) returncend, count=0, rate=0.1}
localcodes= {hex, dec, plain}
localfunctionswap(t,k1,k2) localtemp=t[k2] t[k2] =t[k1] t[k1] =tempend
localout=""
fori=1,s:len() do
for_,codeinipairs(codes) docode.count=code.count+code.rateend
ifcodes[1].count<codes[2].countthenswap(codes,1,2) end
ifcodes[2].count<codes[3].countthenswap(codes,2,3) end
ifcodes[1].count<codes[2].countthenswap(codes,1,2) end
localcode=codes[1]
localc=s:sub(i,i)
-- Force encoding of "@" to make email address more invisible.
ifc=="@" andcode==plainthencode=codes[2] end
out=out..code.code(c)
code.count=code.count-1
end
returnout
end
localfunctionmail(s)
s=unescape_special_chars(s)
localaddress=encode_email_address("mailto:" ..s)
localtext=encode_email_address(s)
returnadd_escape("<a href=\"" ..address.."\">") ..text.."</a>"
end
-- links
text=text:gsub("<(https?:[^'\">%s]+)>", link)
text=text:gsub("<(ftp:[^'\">%s]+)>", link)
-- mail
text=text:gsub("<mailto:([^'\">%s]+)>", mail)