-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_MD_Gen.ahk
849 lines (673 loc) · 32.1 KB
/
_MD_Gen.ahk
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
; ================================================
; Example Script - this just asks for a file and
; uses make_html() to convert the M-ArkDown into html.
; ================================================
; file_path := FileSelect("1",A_ScriptDir '\index.md',"Select markdown file:","Markdown (*.md)")
; If !file_path
; ExitApp
; SplitPath file_path,,&dir,,&file_title
; If FileExist(dir "\" file_title ".html")
; FileDelete dir "\" file_title ".html"
; md_txt := FileRead(file_path)
; css := FileRead("style.css")
; options := {css:css
; , font_name:"Segoe UI"
; , font_size:16
; , font_weight:400
; , line_height:"1.6"} ; 1.6em - put decimals in "" for easier accuracy/handling.
; html := make_html(md_txt, options, true) ; true/false = use some github elements
; FileAppend html, dir "\" file_title ".html", "UTF-8"
; Run dir "\" file_title ".html" ; open and test
; dbg(_in) { ; AHK v2
; Loop Parse _in, "`n", "`r"
; OutputDebug "AHK: " A_LoopField
; }
; ================================================
; make_html(_in_html, options_obj:="", github:=false)
;
; Ignore the last 2 params. Those are used internally.
;
; See above for constructing the options_obj.
;
; The "github" param is a work in progress, and tries to enforce some of the expected basics
; that are circumvented with my "flavor" of markdown.
;
; Current effects when [ github := true ]:
; * H1 and H2 always have underline (the [underline] tag still takes effect when specified)
; * the '=' is not usable for making an <hr>, but --- *** and ___ still make <hr>
;
; ================================================
make_html(_in_text, options:="", github:=false, final:=true, md_type:="") {
If !RegExMatch(_in_text,"[`r`n]+$") && (final) && md_type!="header" { ; add trailing CRLF if doesn't exist
_in_text .= "`r`n"
}
html1 := "<html><head><style>`r`n"
html2 := "`r`n</style></head>`r`n`r`n<body>"
toc_html1 := '<div id="toc-container">'
. '<div id="toc-icon" align="right">☰</div>' ; hamburger (3 horizontal lines) icon
. '<div id="toc-contents">'
toc_html2 := "</div></div>" ; end toc-container and toc-contents
html3 := '<div id="body-container"><div id="main">`r`n' ; <div id=" q "body-container" q ">
html4 := "</div></div></body></html>" ; </div>
body := ""
toc := [], do_toc := false
do_nav := false, nav_arr := []
ref := Map(), ref.CaseSense := false
foot := Map(), ref.CaseSense := false
link_ico := "•" ; 🔗 •
Static chk_id := 0 ; increments throughout entire document
If (final)
css := options.css
a := StrSplit(_in_text,"`n","`r")
i := 0
ref_link_rgx := '^\x5B(\^)?([\w ]+)\x5D:([^"]+)(?:"([^"]+)")?'
in_code := false, code_tag := ""
While (i < a.Length) { ; parse for ref-style links and footnotes first
i++, line := strip_comment(a[i])
If !in_code && RegExMatch(line,"^(``{3,4})$",&c)
in_code := true, code_tag := c[1] ; , msgbox("IN CODE`n`n" line "`n" a[i+1] "`n" a[i+2] "`n" a[i+3])
Else If in_code && RegExMatch(line,"^(``{3,4})",&c) && c[1]=code_tag
in_code := false, code_tag := ""
If !in_code && RegExMatch(line,ref_link_rgx,&m) {
If m[1]
foot[m[2]] := {link:m[3],title:m[4]} ; foot notes
Else
ref[m[2]] := {link:m[3],title:m[4]} ; reference-style links / images
}
}
i := 0
While (i < a.Length) { ; ( ) \x28 \x29 ; [ ] \x5B \x5D ; { } \x7B \x7D
i++, line := a[i]
If line="<!--debug-->"
options.debug := true
line := strip_comment(line)
ul := "", ul2 := ""
ol := "", ol2 := "", ol_type := ""
If RegExMatch(line,ref_link_rgx) ; skip ref-style links and footnotes
Continue
; If RegExMatch(line,"^<nav")
; Continue
If (final && line = "<toc>") {
do_toc := True
Continue
}
While RegExMatch(line,"\\$") { ; concat lines ending in '\'
If !line_inc(line := SubStr(line,1,-1) '<br>')
Break
}
While (i < a.Length && RegExMatch(line,"\\$")) ; concatenate lines ending in `\` with next line
line := SubStr(line,1,-1) '<br>' strip_comment(a[++i])
If final && RegExMatch(line, "^<nav\|") && a.Has(i+1) && (a[i+1] = "") {
do_nav := True
nav_arr := StrSplit(Trim(line,"<>"),"|")
nav_arr.RemoveAt(1)
Continue
}
box_block := "" ; <box> ... </box>
If line="<box>" {
While (line != '</box>') {
If !line_inc()
Break
box_block .= (box_block?'`r`n':'') line
}
If (box_block = "</box>") ; skip an empty box block
Continue
box_block := StrReplace(box_block,'`r`n','`r`n<br>')
body .= (body?'`r`n':'') '<div class="box">' inline_code(box_block) "</div>"
}
code_block := "" ; code block
If RegExMatch(line,"i)^(``````)([a-z]+)?$",&n) || RegExMatch(line,"i)^(````````)([a-z]+)?$",&n) {
match := n[1]
If !line_inc()
Break
While (line != match) {
code_block .= (code_block?"`r`n":"") line
If !line_inc()
Break
}
If n[2] = "pp"
code_block := prettyprint(code_block, options)
Else
code_block := conv(code_block)
body .= (body?"`r`n":"") "<pre><code>" code_block "</code></pre>"
Continue
}
; header h1 - h6
If RegExMatch(line, "^(#+) (.+?)(?:\x5B[ \t]*(\w+)[ \t]*\x5D)?$", &n) {
depth := StrLen(n[1])
, title := inline_code(Trim(n[2]," `t"))
, _class := (depth <= 2 || n[3]="underline") ? "underline" : ""
id := rem_txt_color(title) ; remove color text code for the id
id := undo(id)
id := RegExReplace(RegExReplace(StrLower(StrReplace(id," - ","-")),"[\[\]\{\}\(\)\@\!\.]",""),"[ \.]","-")
opener := "<h" depth (id?' id="' id '" ':'') (_class?' class="' _class '"':'') '>'
body .= (body?"`r`n":"") opener title
. '<a href="#' id '"><span class="link">' link_ico '</span></a>'
. '</h' depth '>'
toc.Push([depth, title, id])
Continue
}
; alt header h1 and h2
; ------ or ======= as underline in next_line
next_line := a.Has(i+1) ? strip_comment(a[i+1]) : ""
If next_line && line && RegExMatch(next_line,"^(\-+|=+)$") {
depth := (SubStr(next_line,1,1) = "=") ? 1 : 2
id := rem_txt_color(line) ; remove color text code for the id
id := undo(id)
id := RegExReplace(RegExReplace(StrLower(id),"[\[\]\{\}\(\)\@\!<>\|]",""),"[ \.]","-")
opener := "<h" depth ' id="' id '" class="underline">'
body .= (body?"`r`n":"") opener inline_code(line)
. '<a href="#' id '"><span class="link">' link_ico '</span></a>'
. '</h' depth '>'
toc.Push([depth, line, id]), i++ ; increase line count to skip the ---- or ==== form next_line
Continue
}
; check list
If RegExMatch(line,"^\- \x5B([ xX])\x5D (.+)",&n) {
body .= (body?"`r`n":"") '<ul class="checklist">'
While RegExMatch(line,"^\- \x5B([ xX])\x5D (.+)",&n) {
chk := (n[1]="x") ? 'checked=""' : ''
body .= '`r`n<li><input type="checkbox" id="check' chk_id '" disabled="" ' chk '>'
. '<label for="check' chk_id '"> ' inline_code(n[2]) '</label></li>'
chk_id++
If !line_inc()
Break
}
body .= '</ul>'
Continue
}
; spoiler
spoiler_text := ""
If RegExMatch(line, "^<spoiler=([^>]+)>$", &match) {
disp_text := match[1]
If !line_inc()
Break
While !RegExMatch(line, "^</spoiler>$") {
spoiler_text .= (spoiler_text?"`r`n":"") line
If !line_inc()
throw Error("No closing </spoiler> tag found.",-1)
}
body .= (body?"`r`n":"") '<p><details><summary class="spoiler">'
. disp_text "</summary>" make_html(spoiler_text,options,github,false,"spoiler") "</details></p>"
Continue
}
; hr
if RegExMatch(line, "^(\-{3,}|_{3,}|\*{3,}|={3,})(?:\x5B[ \t]*([^\x5D]+)*[ \t]*\x5D)?$", &match) {
hr_style := ""
If match[2] {
For i, style in StrSplit(match[2]," ","`t") {
If (SubStr(style, -2) = "px")
hr_style .= (hr_style?" ":"") "border-top-width: " style ";"
Else If RegExMatch(style, "(dotted|dashed|solid|double|groove|ridge|inset|outset|none|hidden)")
hr_style .= (hr_style?" ":"") "border-top-style: " style ";"
Else If InStr(style,"opacity")=1
hr_style .= (hr_style?" ":"") style ";"
Else
hr_style .= (hr_style?" ":"") "border-top-color: " style ";"
}
} Else {
hr_style := "opacity: 0.25;"
}
body .= (body?"`r`n":"") '<hr style="' hr_style '">'
Continue
}
; blockquote
If RegExMatch(line, "^\> *(.*)") {
blockquote := ""
While RegExMatch(line, "^\> *(.*)", &match) {
blockquote .= (blockquote?"`r`n":"") match[1]
If !line_inc()
Break
}
body .= (body?"`r`n":"") "<blockquote>" make_html(blockquote,options,github, false, "blockquote") "</blockquote>"
Continue
}
; table
If RegExMatch(line, "^\|.*?\|$") {
table := "", lines := 0
While RegExMatch(line, "^\|.*?\|$") {
table .= (table?"`r`n":"") line, lines++
If !line_inc()
Break
}
If lines < 3
Continue
If (table) {
b := [], h := [], body .= (body?"`r`n":"") '<table class="normal">'
Loop Parse table, "`n", "`r"
{
body .= "<tr>"
c := StrSplit(A_LoopField,"|"), c.RemoveAt(1), c.RemoveAt(c.Length)
If (A_Index = 1) {
For i, t in c { ; table headers
txt := inline_code(Trim(t," `t")) ; , align := "center"
If RegExMatch(txt,"^(:)?(.+?)(:)?$",&n) {
align := (n[1]&&n[3]) ? "center" : (n[3]) ? "right" : "left"
txt := n[2], h.Push([align,txt])
} Else
h.Push(["",txt])
}
} Else If (A_Index = 2) {
For i, t in c {
align := "left" ; column alignment
If RegExMatch(t,"^(:)?\-+(:)?$",&n)
align := (n[1]&&n[2]) ? "center" : (n[2]) ? "right" : "left"
b.Push(align)
body .= '<th align="' (h[i][1]?h[i][1]:'center') '">' h[i][2] '</th>'
}
} Else {
For i, t in c
body .= '<td align="' b[i] '">' Trim(inline_code(t)," `t") '</td>'
}
body .= "</tr>"
}
body .= "</table>"
Continue
}
}
; ordered and unordered lists
list_rgx := '^( *)' ; leading spaces (no tabs)
. '([\*\+\-]|\d+(?:\.|\x29))' ; */+/- or 1. or 1)
. '( +)' ; at least one more space
. '(.+)' ; list content
list := []
While RegExMatch(line,list_rgx,&n) {
itm := LT_spec(n[2]) ; bullet item ... [ -, *, +, 1., or 1) ]
tag := LT_tag(n[2]) ; ol or ul
pre := n.Len[1] ; spaces before bullet item
lead := pre + n.Len[2] + n.Len[3] ; # chars before actual list text
txt := n[4]
While RegExMatch(txt,"\\$") && (i < a.Length) ; append lines ending in '\'
txt := SubStr(txt,1,-1) '<br>' strip_comment(a[++i])
list.Push({itm:t, tag:tag, pre:pre, lead:lead, txt:txt, line:n[2] ' ' txt})
If !line_inc()
Break
}
d := 1 ; depth - for make_list()
err := false ; checking for poorly formatted ordered lists
While list.Length { ; add all lists, normally is one, but can be multiple
body .= '`r`n' make_list(list)
If err
Break
Continue
}
If list.Length { ; if no errs in list, then list array should be blank at this point
body .= '`r`n' AtoT(list) ; dump remaining plain text
Continue
}
; =======================================================================
; ...
; =======================================================================
If RegExMatch(md_type,"^(ol|ul)") { ; ordered/unordered lists
body .= (body?"`r`n":"") inline_code(line)
Continue
}
body .= (body?"`r`n":"") "<p>" inline_code(line) "</p>"
}
; processing toc ; try to process exact height
final_toc := "", toc_width := 0, toc_height := 0
If (Final && do_toc) {
temp := Gui()
temp.SetFont("s" options.font_size, options.font_name)
depth := toc[1][1]
diff := (depth > 1) ? depth - 1 : 0
indent := " "
For i, item in toc { ; 1=depth, 2=title, 3=id
depth := item[1] - diff - 1
ctl := temp.Add( "Text",, rpt(" ",depth) "• " undo(rem_txt_color(item[2])) )
ctl.GetPos(,,&w, &h)
toc_width := (w > toc_width) ? w : toc_width
toc_height += options.font_size * 2
final_toc .= (final_toc?"`r`n":"") '<a href="#' item[3] '">'
. '<div class="toc-item">' (depth?rpt(indent,depth):"")
. "• " item[2] "</div></a>"
}
temp.Destroy()
}
; processing navigation menu
nav_str := ""
If (final && do_nav) {
temp := Gui()
temp.SetFont("s" options.font_size, options.font_name)
Loop nav_arr.Length {
txt := nav_arr[A_Index]
title := SubStr(txt, 1, (sep := InStr(txt, "=")) - 1)
ctl := temp.Add("Text",,title)
ctl.GetPos(,,&w)
toc_width := (w > toc_width) ? w : toc_width
toc_height += options.font_size * 2
nav_str .= (final_toc?"`r`n":"") '<a href="' SubStr(txt, sep+1) '" target="_blank" rel="noopener noreferrer">'
. '<div class="toc-item">' title '</div></a>'
}
(do_toc) ? nav_str .= "<hr>" : ""
temp.Destroy()
}
; processing TOC
user_menu := ""
If Final && (do_nav || do_toc)
user_menu := toc_html1 nav_str final_toc toc_html2
If final {
If (do_nav && do_toc)
toc_height += Round(options.font_size * Float(options.line_height)) ; multiply by body line-height
css := StrReplace(css, "[_toc_width_]",toc_width + 25) ; account for scrollbar width
css := StrReplace(css, "[_toc_height_]",Round(toc_height))
css := StrReplace(css, "[_font_name_]", options.font_name)
css := StrReplace(css, "[_font_size_]", options.font_size)
css := StrReplace(css, "[_font_weight_]", options.font_weight)
css := StrReplace(css, "[_line_height_]", Round(options.line_height,1))
If (do_toc || do_nav)
result := html1 . css . html2 . user_menu . html3 . body . html4
Else
result := html1 . css . html2 . html3 . body . html4
} Else
result := body
return result
; =======================================================================
; Local Functions
; =======================================================================
; GitHub spec for lists:
; https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#lists
make_list(_L_) { ; o props = {itm, tag, pre, lead, txt}
_tag := _L_[1].tag, _itm := _L_[1].itm
_pre := _L_[1].pre, _lead := _L_[1].lead, t := ""
_txt := inline_code(_L_[1].txt) ; process escapes first line of list to determine optional list style
rgx := '\x5B *(?:type=([1AaIi]|disc|circle|square|none)) *\x5D$'
If (_typ := RegExMatch(_txt,rgx,&y) ? y[1] : '') ; if list style found ...
_L_[1].txt := RegExReplace(_L_[1].txt,rgx) ; ... remove it after recording it
If (_tag = 'ol')
t := (_typ) ? (' type="' _typ '"') : (' type="' ((d=1) ? '1' : (d=2) ? 'i' : 'a') '"')
Else
t := (_typ) ? (' style="list-style-type:' _typ ';"') : ''
list_html := '<' _L_[1].tag t '>' ; GitHub list levels ... 1. > i. > a.
end_tag := '</' _L_[1].tag '>'
_i := 0
While (_L_.Length) {
o := _L_[1]
If (o.pre >= _lead) { ; step in on proper indent (check GitHub spec) o.pre != _pre
d++ ; increase depth
If (r:=make_list(_L_)) && err ; if ordered list isn't properly numbered return only the 'good' part of the list
return list_html '`r`n' end_tag ; ... and quit parsing
list_html .= r
Continue
} Else If (o.pre < _pre) { ; stepping back
d-- ; decrease depth
return '`r`n' list_html '`r`n' end_tag
} Else If (o.tag != _tag) ; if changing unordered list types ...
|| (o.tag="ul" && o.itm != _itm) { ; ... or if next bullet type (*, +, -) doesn't match previous
d := 1 ; reset depth for new list
return '`r`n' list_html '`r`n' end_tag ; ... this starts a new list, according to GitHub spec
}
_i++
If ( o.tag = 'ol' && _i != Integer(o.itm) ) { ; if ordered list numbers are not in sequence ...
err := true ; ... flag err and return the 'good' part of the list
return '`r`n' list_html '`r`n' end_tag
}
list_html .= '`r`n<li>' inline_code(o.txt) '</li>'
_tag := o.tag, _itm := o.itm
_pre := o.pre, _lead := o.lead
_L_.RemoveAt(1)
}
list_html .= '`r`n' end_tag
return list_html
}
AtoT(_a_) { ; for dumping the remaining text of a poorly formated list
_txt_ := '<p>'
For i, o in _a_
_txt_ .= ((i>1)?'<br>':'') inline_code(o.line)
return _txt_ '</p>'
}
LT_tag(_in_) => IsInteger(t:=Trim(_in_,".)")) ? "ol" : IsAlpha(t) ? "" : "ul" ; list type
LT_spec(_in_) => IsInteger(t:=Trim(_in_,".)")) ? Integer(t) : t
rem_txt_color(_in_) => RegExReplace(RegExReplace(_in_,'<span style=".+?">'),"</span>")
inline_code(_in, is_code := false) {
output := _in, check := ""
While (check != output) { ; repeat until no changes are made
check := output
; inline code / inline code colors
While RegExMatch(output, "``(.+?)``", &x) {
is_code := true
If RegExMatch(x[1],"^\#[\da-fA-F]{3,3}$")
|| RegExMatch(x[1],"^\#[\da-fA-F]{6,6}$")
|| RegExMatch(x[1],"^rgb\(\d{1,3}, *\d{1,3}, *\d{1,3}\)$")
|| RegExMatch(x[1],"^hsl\(\d{1,3}, *\d{1,3}%, *\d{1,3}%\)$") {
repl := '<code>' x[1] ' <span class="circle" style="background-color: ' x[1] ';'
. ' width: ' (options.font_size//2) 'px;'
. ' height: ' (options.font_size//2) 'px;'
. ' display: inline-block;'
. ' border: 2px solid ' x[1] ';'
. ' border-radius: 50%;"></span></code>'
output := StrReplace(output, x[0], repl,,,1)
} Else {
repl := conv(x[1])
output := StrReplace(output, x[0], "<code>" repl "</code>",,,1)
}
}
; box <box>...</box>
While RegExMatch(output, "<box>(.+?)</box>", &x)
output := StrReplace(output,x[0],'<span class="box">' x[1] '</span>')
; pretty print <pp>...</pp>
While RegExMatch(output, "<pp>(.+?)</pp>", &x)
output := StrReplace(output,x[0],'<span style="font-family: Consolas">' prettyprint(x[1],options) '</span>')
; escape characters
While RegExMatch(output,"(\\)(.)",&x)
output := StrReplace(output,x[0],"&#" Ord(x[2]) ";")
; colored text
While RegExMatch(output, "<c *= *#([\da-fA-F]+)>(.+?)</c>", &x) && (StrLen(x[1])=3 || StrLen(x[1])=6) && !IsInCode() {
output := StrReplace(output, x[0], '<span style="color:#' x[1] '">' x[2] '</span>',,,1)
}
; image
While RegExMatch(output, "!\x5B *([^\x5D]*) *\x5D\x28 *([^\x29]+) *\x29(?:\x28 *([^\x29]+) *\x29)?", &x) && !IsInCode() {
dims := (dm:=Trim(x[3],"()")) ? " " dm : ""
output := StrReplace(output, x[0], '<img src="' x[2] '"' dims ' alt="' x[1] '" title="' x[1] '">',,,1)
}
; image reference-style
While RegExMatch(output, "!\x5B *([^\x5D]*) *\x5D\x5B *([^\x5D]+) *\x5D(?:\x28 *([^\x29]+) *\x29)?", &x)
&& ref.Has(x[2]) ; ref link stored
&& !IsInCode() {
dims := x[3] ? " " x[3] : ""
output := StrReplace(output, x[0], '<img src="' ref[x[2]].link '"' dims ' alt="' x[1] '" title="' ref[x[2]].title '">',,,1)
}
; link / url
While RegExMatch(output, "\x5B *([^\x5D]+) *\x5D\x28 *([^\x29]+) *\x29", &x) && !IsInCode() {
rel := RegExMatch(x[2],"^#[\w\-]+") ? "" : 'noopener noreferrer'
tgt := RegExMatch(x[2],"^#[\w\-]+") ? "" : '_blank'
output := StrReplace(output, x[0], '<a href="' x[2] '" target="" rel="noopener noreferrer">' x[1] "</a>",,,1)
}
; link / url reference-style 1
While RegExMatch(output, "\x5B *([^\x5D]+) *\x5D\x5B *([^\x5D]+) *\x5D", &x)
&& ref.Has(x[2])
&& !IsInCode() {
; rel :=
output := StrReplace(output, x[0]
, '<a href="' ref[x[2]].link '" title="' ref[x[2]].title '" target="_blank" rel="noopener noreferrer">' x[1] "</a>",,,1)
}
; link / url reference-style 2
While RegExMatch(output, "\x5B *([^\x5D]+) *\x5D", &x)
&& ref.Has(x[1])
&& !IsInCode()
output := StrReplace(output, x[0]
, '<a href="' ref[x[1]].link '" title="' ref[x[1]].title '" target="_blank" rel="noopener noreferrer">'
. (ref[x[1]].title?ref[x[1]].title:x[1]) "</a>",,,1)
; strong + emphasis (bold + italics)
While (RegExMatch(output, "(?<![\*\w])[\*]{3,3}([^\*]+)[\*]{3,3}", &x)
|| RegExMatch(output, "(?<!\w)[\_]{3,3}([^\_]+)[\_]{3,3}", &x)) && !IsInCode() {
output := StrReplace(output, x[0], "<em><strong>" x[1] "</strong></em>",,,1)
}
; strong (bold)
While (RegExMatch(output, "(?<![\*\w])[\*]{2,2}([^\*]+)[\*]{2,2}", &x)
|| RegExMatch(output, "(?<!\w)[\_]{2,2}([^\_]+)[\_]{2,2}", &x)) && !IsInCode() {
output := StrReplace(output, x[0], "<strong>" x[1] "</strong>",,,1)
}
; emphasis (italics)
While (RegExMatch(output, "(?<![\*\w])[\*]{1,1}([^\*]+)[\*]{1,1}", &x)
|| RegExMatch(output, "(?<![\w])[\_]{1,1}([^\_]+)[\_]{1,1}", &x)) && !IsInCode() {
output := StrReplace(output, x[0], "<em>" x[1] "</em>",,,1)
}
; strikethrough
While RegExMatch(output, "(?<!\w)~{2,2}([^~]+)~{2,2}", &x) && !IsInCode()
output := StrReplace(output, x[0], "<del>" x[1] "</del>",,,1)
}
return output
IsInCode() => ((st := x.Pos[0]-6) < 1) ? false : RegExMatch(output,"<code> *\Q" x[0] "\E",,st) ? true : false
}
line_inc(concat:="") {
(i < a.Length) ? (line := (concat?concat:"") strip_comment(a[++i]), result:=true) : (line := "", result:=false)
return result
}
strip_comment(_in_) => RTrim(RegExReplace(_in_,"^(.+)<\!\-\-[^>]+\-\->","$1")," `t")
; conv(_in_) => StrReplace(StrReplace(StrReplace(_in_,'&','&'),'<','<'),'>','>')
rpt(x,y) => StrReplace(Format("{:-" y "}","")," ",x) ; string repeat ... x=str, y=iterations
e(_) {
r := ""
For ch in StrSplit(_)
r .= ("&#x" Format("{:X}",Ord(ch)) ";")
return r
}
conv(_) {
static chr_list := ['&','<','>','"',"'",'\',"``",'=','+','-','/','*',',',':','^','|','!','?','.','~']
For ch in chr_list
_ := StrReplace(_,ch,e(ch))
return _
}
}
prettyprint(_in_, options) {
Static rgm := RegExMatch
cd := options.code
out := ""
in_line_comment := false
in_comment := false
in_string := false
c_str := ""
line := ""
i := 1, c := ""
arr := StrSplit(_in_)
Loop arr.Length {
L := c ; last character
, c := arr[i]
; , L := SubStr(line,-1)
; , L2 := SubStr(line,-2)
; , L3 := SubStr(line,-3)
, N := arr.Has(i+1) ? arr[i+1] : ""
If (in_string && c!=c_str)
|| (in_line_comment && c!='`n' && c!='`r')
|| (in_comment && c!='/' && L!='*') {
line .= c, i++
Continue
}
Switch c {
Case '`r', '`n':
If in_line_comment {
out .= span(cd.comment, conv(line) )
line := ""
in_line_comment := false
}
out .= conv(line) c
line := ""
Case '"', "'":
If (c_str="") {
out .= conv(line)
c_str := line := c
in_string := true
} Else If (c = c_str) && (L != "``") {
out .= span(cd.str, conv(line c) )
c_str := line := ""
in_string := false
}
Case ';':
out .= conv(line)
line := c
in_line_comment := true
Case '*':
If (L='/') {
out .= conv(SubStr(line,1,-1)), line := '/*'
in_comment := true
} Else
line .= c
Case '/':
If (L='*') && in_comment {
out .= span(cd.comment, conv(line c) )
line := ""
in_comment := false
} Else
line .= c
Case '+','-','*','/','=','!','~','&','^','|','>','<','?':
Static punct := '+-*/=!~&^|><?:'
, rgx_math := "(>>>=|<<=|>>=|>>>|<<|>>|\^=|\&=|\x7C=|\.=|//=|/=|\*=|\-=|\+=|:=|//|\*\*|\+\+|\-\-|\*|/|\^|\!|\x7C|~)"
, rgx_comp := "(!==|!=|~=|==|<=|>=|&&|\x7C\x7C|\?\?|:|\?|>|<|=)"
If (!InStr(N,punct) && (N!="")) || (N="") {
If RegExMatch(line c,rgx_math "$",&y) {
out .= conv(SubStr(line c,1,y.Pos-1))
out .= span(cd.math, conv(y[1]) )
line := ""
} Else If RegExMatch(line c,rgx_comp "$",&y) {
out .= conv(SubStr(line c,1,y.Pos-1))
out .= span(cd.compare, conv(y[1]) )
line := ""
} Else If (c='-' && !IsInteger(N)) || (c='+') {
out .= conv(line) span(cd.math, c )
line := ""
} Else
line .= c
} Else
line .= c
Case '(',')','[',']','{','}':
out .= conv(line) span(cd.bc, conv(c) )
line := ""
Case ',':
out .= conv(line) span(cd.comma, c )
line := ""
Case '.':
If (IsAlnum(L) || L="_" || L=")" || L="]") && (IsAlpha(N) || N="_") {
out .= conv(line) span(cd.objdot, conv(c) )
line := ""
} Else
line .= c
Default:
line .= c
}
i++
}
If in_line_comment {
out .= span(cd.comment, conv(line) ), line := ""
}
out .= conv(line)
return out
; conv(_in_) => StrReplace(StrReplace(StrReplace(_in_,'&','&'),'<','<'),'>','>')
span(_clr, _) => '<span style="color:' _clr '">' _ '</span>'
e(_) {
r := ""
For ch in StrSplit(_)
r .= ("&#x" Format("{:X}",Ord(ch)) ";")
return r
}
conv(_) {
static chr_list := ['&','<','>','"',"'",'\',"``",'=','+','-','/','*',',',':','^','|','!','?','.','~']
For ch in chr_list
_ := StrReplace(_,ch,e(ch))
return _
}
}
convert(_) {
static chr_list := ['&','<','>','"',"'",'\',"``",'=','+','-','/','*',',',':','^','|','!','?','.','~']
For ch in chr_list
_ := StrReplace(_,ch,e(ch))
return _
e(_) {
r := ""
For ch in StrSplit(_)
r .= ("&#x" Format("{:X}",Ord(ch)) ";")
return r
}
}
undo(_) {
static chr_list := ['<','>','"',"'",'\',"``",'=','+','-','/','*',',',':','^','|','!','?','&','.','~']
For ch in chr_list
_ := StrReplace(_,e(ch),ch)
return _
e(_) {
r := ""
For ch in StrSplit(_)
r .= ("&#x" Format("{:X}",Ord(ch)) ";")
return r
}
}