/usr/share/tcltk/tcllib1.18/markdown/markdown.tcl is in tcllib 1.18-dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #
# The MIT License (MIT)
#
# Copyright (c) 2014 Caius Project
#
# 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.
#
package require textutil
## \file
# \brief Functions for converting markdown to HTML.
##
# \brief Functions for converting markdown to HTML.
#
namespace eval Markdown {
namespace export convert
##
#
# Converts text written in markdown to HTML.
#
# @param markdown currently takes as a single argument the text in markdown
#
# The output of this function is only a fragment, not a complete HTML
# document. The format of the output is generic XHTML.
#
proc convert {markdown} {
set markdown [regsub {\r\n?} $markdown {\n}]
set markdown [::textutil::untabify2 $markdown 4]
set markdown [string trimright $markdown]
# COLLECT REFERENCES
array unset ::Markdown::_references
array set ::Markdown::_references [collect_references markdown]
# PROCESS
return [apply_templates markdown]
}
## \private
proc collect_references {markdown_var} {
upvar $markdown_var markdown
set lines [split $markdown \n]
set no_lines [llength $lines]
set index 0
array set references {}
while {$index < $no_lines} {
set line [lindex $lines $index]
if {[regexp \
{^[ ]{0,3}\[((?:[^\]]|\[[^\]]*?\])+)\]:\s*(\S+)(?:\s+(([\"\']).*\4|\(.*\))\s*$)?} \
$line match ref link title]} \
{
set title [string trim [string range $title 1 end-1]]
if {$title eq {}} {
set next_line [lindex $lines [expr $index + 1]]
if {[regexp \
{^(?:\s+(?:([\"\']).*\1|\(.*\))\s*$)} \
$next_line]} \
{
set title [string range [string trim $next_line] 1 end-1]
incr index
}
}
set ref [string tolower $ref]
set link [string trim $link {<>}]
set references($ref) [list $link $title]
}
incr index
}
return [array get references]
}
## \private
proc apply_templates {markdown_var {parent {}}} {
upvar $markdown_var markdown
set lines [split $markdown \n]
set no_lines [llength $lines]
set index 0
set result {}
set ul_match {^[ ]{0,3}(?:\*(?!\s*\*\s*\*\s*$)|-(?!\s*-\s*-\s*$)|\+) }
set ol_match {^[ ]{0,3}\d+\. }
# PROCESS MARKDOWN
while {$index < $no_lines} {
set line [lindex $lines $index]
switch -regexp $line {
{^\s*$} {
# EMPTY LINES
if {![regexp {^\s*$} [lindex $lines [expr $index - 1]]]} {
append result "\n\n"
}
incr index
}
{^[ ]{0,3}\[(?:[^\]]|\[[^\]]*?\])+\]:\s*\S+(?:\s+(?:([\"\']).*\1|\(.*\))\s*$)?} {
# SKIP REFERENCES
set next_line [lindex $lines [expr $index + 1]]
if {[regexp \
{^(?:\s+(?:([\"\']).*\1|\(.*\))\s*$)} \
$next_line]} \
{
incr index
}
incr index
}
{^[ ]{0,3}-[ ]*-[ ]*-[- ]*$} -
{^[ ]{0,3}_[ ]*_[ ]*_[_ ]*$} -
{^[ ]{0,3}\*[ ]*\*[ ]*\*[\* ]*$} {
# HORIZONTAL RULES
append result "<hr/>"
incr index
}
{^[ ]{0,3}#{1,6}} {
# ATX STYLE HEADINGS
set h_level 0
set h_result {}
while {$index < $no_lines && ![is_empty_line $line]} {
incr index
if {!$h_level} {
regexp {^\s*#+} $line m
set h_level [string length [string trim $m]]
}
lappend h_result $line
set line [lindex $lines $index]
}
set h_result [\
parse_inline [\
regsub -all {^\s*#+\s*|\s*#+\s*$} [join $h_result \n] {} \
]\
]
append result "<h$h_level>$h_result</h$h_level>"
}
{^[ ]{0,3}\>} {
# BLOCK QUOTES
set bq_result {}
while {$index < $no_lines} {
incr index
lappend bq_result [regsub {^[ ]{0,3}\>[ ]?} $line {}]
if {[is_empty_line [lindex $lines $index]]} {
set eoq 0
for {set peek $index} {$peek < $no_lines} {incr peek} {
set line [lindex $lines $peek]
if {![is_empty_line $line]} {
if {![regexp {^[ ]{0,3}\>} $line]} {
set eoq 1
}
break
}
}
if {$eoq} { break }
}
set line [lindex $lines $index]
}
set bq_result [string trim [join $bq_result \n]]
append result <blockquote>\n \
[apply_templates bq_result] \
\n</blockquote>
}
{^\s{4,}\S+} {
# CODE BLOCKS
set code_result {}
while {$index < $no_lines} {
incr index
lappend code_result [html_escape [\
regsub {^ } $line {}]\
]
set eoc 0
for {set peek $index} {$peek < $no_lines} {incr peek} {
set line [lindex $lines $peek]
if {![is_empty_line $line]} {
if {![regexp {^\s{4,}} $line]} {
set eoc 1
}
break
}
}
if {$eoc} { break }
set line [lindex $lines $index]
}
set code_result [join $code_result \n]
append result <pre><code> $code_result \n </code></pre>
}
{^(?:(?:`{3,})|(?:~{3,}))(?:\{?\S+\}?)?\s*$} {
# FENCED CODE BLOCKS
set code_result {}
if {[string index $line 0] eq {`}} {
set end_match {^`{3,}\s*$}
} else {
set end_match {^~{3,}\s*$}
}
while {$index < $no_lines} {
incr index
set line [lindex $lines $index]
if {[regexp $end_match $line]} {
incr index
break
}
lappend code_result [html_escape $line]
}
set code_result [join $code_result \n]
append result <pre><code> $code_result </code></pre>
}
{^[ ]{0,3}(?:\*|-|\+) |^[ ]{0,3}\d+\. } {
# LISTS
set list_result {}
# continue matching same list type
if {[regexp $ol_match $line]} {
set list_type ol
set list_match $ol_match
} else {
set list_type ul
set list_match $ul_match
}
set last_line AAA
while {$index < $no_lines} \
{
if {![regexp $list_match [lindex $lines $index]]} {
break
}
set item_result {}
set in_p 1
set p_count 1
if {[is_empty_line $last_line]} {
incr p_count
}
set last_line $line
set line [regsub "$list_match\\s*" $line {}]
# prevent recursion on same line
set line [regsub {\A(\d+)\.(\s+)} $line {\1\\.\2}]
set line [regsub {\A(\*|\+|-)(\s+)} $line {\\\1\2}]
lappend item_result $line
for {set peek [expr $index + 1]} {$peek < $no_lines} {incr peek} {
set line [lindex $lines $peek]
if {[is_empty_line $line]} {
set in_p 0
}\
elseif {[regexp {^ } $line]} {
if {!$in_p} {
incr p_count
}
set in_p 1
}\
elseif {[regexp $list_match $line]} {
if {!$in_p} {
incr p_count
}
break
}\
elseif {!$in_p} {
break
}
set last_line $line
lappend item_result [regsub {^ } $line {}]
}
set item_result [join $item_result \n]
if {$p_count > 1} {
set item_result [apply_templates item_result li]
} else {
if {[regexp -lineanchor \
{(\A.*?)((?:^[ ]{0,3}(?:\*|-|\+) |^[ ]{0,3}\d+\. ).*\Z)} \
$item_result \
match para rest]} \
{
set item_result [parse_inline $para]
append item_result [apply_templates rest]
} else {
set item_result [parse_inline $item_result]
}
}
lappend list_result "<li>$item_result</li>"
set index $peek
}
append result <$list_type>\n \
[join $list_result \n] \
</$list_type>\n\n
}
{^<(?:p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del)} {
# HTML BLOCKS
set re_htmltag {<(/?)(\w+)(?:\s+\w+=(?:\"[^\"]+\"|'[^']+'))*\s*>}
set buffer {}
while {$index < $no_lines} \
{
while {$index < $no_lines} \
{
incr index
append buffer $line \n
if {[is_empty_line $line]} {
break
}
set line [lindex $lines $index]
}
set tags [regexp -inline -all $re_htmltag $buffer]
set stack_count 0
foreach {match type name} $tags {
if {$type eq {}} {
incr stack_count +1
} else {
incr stack_count -1
}
}
if {$stack_count == 0} { break }
}
append result $buffer
}
{(?:^\s{0,3}|[^\\]+)\|} {
# SIMPLE TABLES
set cell_align {}
set row_count 0
while {$index < $no_lines} \
{
# insert a space between || to handle empty cells
set row_cols [regexp -inline -all {(?:[^|]|\\\|)+} \
[regsub -all {\|(?=\|)} [string trim $line] {| }] \
]
if {$row_count == 0} \
{
set sep_cols [lindex $lines [expr $index + 1]]
# check if we have a separator row
if {[regexp {^\s{0,3}\|?(?:\s*:?-+:?(?:\s*$|\s*\|))+} $sep_cols]} \
{
set sep_cols [regexp -inline -all {(?:[^|]|\\\|)+} \
[string trim $sep_cols]]
foreach {cell_data} $sep_cols \
{
switch -regexp $cell_data {
{:-*:} {
lappend cell_align center
}
{:-+} {
lappend cell_align left
}
{-+:} {
lappend cell_align right
}
default {
lappend cell_align {}
}
}
}
incr index
}
append result "<table class=\"table\">\n"
append result "<thead>\n"
append result " <tr>\n"
if {$cell_align ne {}} {
set num_cols [llength $cell_align]
} else {
set num_cols [llength $row_cols]
}
for {set i 0} {$i < $num_cols} {incr i} \
{
if {[set align [lindex $cell_align $i]] ne {}} {
append result " <th style=\"text-align: $align\">"
} else {
append result " <th>"
}
append result [parse_inline [string trim \
[lindex $row_cols $i]]] </th> "\n"
}
append result " </tr>\n"
append result "</thead>\n"
} else {
if {$row_count == 1} {
append result "<tbody>\n"
}
append result " <tr>\n"
if {$cell_align ne {}} {
set num_cols [llength $cell_align]
} else {
set num_cols [llength $row_cols]
}
for {set i 0} {$i < $num_cols} {incr i} \
{
if {[set align [lindex $cell_align $i]] ne {}} {
append result " <td style=\"text-align: $align\">"
} else {
append result " <td>"
}
append result [parse_inline [string trim \
[lindex $row_cols $i]]] </td> "\n"
}
append result " </tr>\n"
}
incr row_count
set line [lindex $lines [incr index]]
if {![regexp {(?:^\s{0,3}|[^\\]+)\|} $line]} {
switch $row_count {
1 {
append result "</table>\n"
}
default {
append result "</tbody>\n"
append result "</table>\n"
}
}
break
}
}
}
default {
# PARAGRAPHS AND SETTEXT STYLE HEADERS
set p_type p
set p_result {}
while {($index < $no_lines) && ![is_empty_line $line]} \
{
incr index
switch -regexp $line {
{^[ ]{0,3}=+$} {
set p_type h1
break
}
{^[ ]{0,3}-+$} {
set p_type h2
break
}
{^[ ]{0,3}(?:\*|-|\+) |^[ ]{0,3}\d+\. } {
if {$parent eq {li}} {
incr index -1
break
} else {
lappend p_result $line
}
}
{^[ ]{0,3}-[ ]*-[ ]*-[- ]*$} -
{^[ ]{0,3}_[ ]*_[ ]*_[_ ]*$} -
{^[ ]{0,3}\*[ ]*\*[ ]*\*[\* ]*$} -
{^[ ]{0,3}#{1,6}} \
{
incr index -1
break
}
default {
lappend p_result $line
}
}
set line [lindex $lines $index]
}
set p_result [\
parse_inline [\
string trim [join $p_result \n]\
]\
]
if {[is_empty_line [regsub -all {<!--.*?-->} $p_result {}]]} {
# Do not make a new paragraph for just comments.
append result $p_result
} else {
append result "<$p_type>$p_result</$p_type>"
}
}
}
}
return $result
}
## \private
proc parse_inline {text} {
set text [regsub -all -lineanchor {[ ]{2,}$} $text <br/>]
set index 0
set result {}
set re_backticks {\A`+}
set re_whitespace {\s}
set re_inlinelink {\A\!?\[((?:[^\]]|\[[^\]]*?\])+)\]\s*\(\s*((?:[^\s\)]+|\([^\s\)]+\))+)?(\s+([\"'])(.*)?\4)?\s*\)}
set re_reflink {\A\!?\[((?:[^\]]|\[[^\]]*?\])+)\](?:\s*\[((?:[^\]]|\[[^\]]*?\])*)\])?}
set re_htmltag {\A</?\w+\s*>|\A<\w+(?:\s+\w+=(?:\"[^\"]+\"|\'[^\']+\'))*\s*/?>}
set re_autolink {\A<(?:(\S+@\S+)|(\S+://\S+))>}
set re_comment {\A<!--.*?-->}
set re_entity {\A\&\S+;}
while {[set chr [string index $text $index]] ne {}} {
switch $chr {
"\\" {
# ESCAPES
set next_chr [string index $text [expr $index + 1]]
if {[string first $next_chr {\`*_\{\}[]()#+-.!>|}] != -1} {
set chr $next_chr
incr index
}
}
{_} -
{*} {
# EMPHASIS
if {[regexp $re_whitespace [string index $result end]] &&
[regexp $re_whitespace [string index $text [expr $index + 1]]]} \
{
#do nothing
} \
elseif {[regexp -start $index \
"\\A(\\$chr{1,3})((?:\[^\\$chr\\\\]|\\\\\\$chr)*)\\1" \
$text m del sub]} \
{
switch [string length $del] {
1 {
append result "<em>[parse_inline $sub]</em>"
}
2 {
append result "<strong>[parse_inline $sub]</strong>"
}
3 {
append result "<strong><em>[parse_inline $sub]</em></strong>"
}
}
incr index [string length $m]
continue
}
}
{`} {
# CODE
regexp -start $index $re_backticks $text m
set start [expr $index + [string length $m]]
if {[regexp -start $start -indices $m $text m]} {
set stop [expr [lindex $m 0] - 1]
set sub [string trim [string range $text $start $stop]]
append result "<code>[html_escape $sub]</code>"
set index [expr [lindex $m 1] + 1]
continue
}
}
{!} -
{[} {
# LINKS AND IMAGES
if {$chr eq {!}} {
set ref_type img
} else {
set ref_type link
}
set match_found 0
if {[regexp -start $index $re_inlinelink $text m txt url ign del title]} {
# INLINE
incr index [string length $m]
set url [html_escape [string trim $url {<> }]]
set txt [parse_inline $txt]
set title [parse_inline $title]
set match_found 1
} elseif {[regexp -start $index $re_reflink $text m txt lbl]} {
if {$lbl eq {}} {
set lbl [regsub -all {\s+} $txt { }]
}
set lbl [string tolower $lbl]
if {[info exists ::Markdown::_references($lbl)]} {
lassign $::Markdown::_references($lbl) url title
set url [html_escape [string trim $url {<> }]]
set txt [parse_inline $txt]
set title [parse_inline $title]
# REFERENCED
incr index [string length $m]
set match_found 1
}
}
# PRINT IMG, A TAG
if {$match_found} {
if {$ref_type eq {link}} {
if {$title ne {}} {
append result "<a href=\"$url\" title=\"$title\">$txt</a>"
} else {
append result "<a href=\"$url\">$txt</a>"
}
} else {
if {$title ne {}} {
append result "<img src=\"$url\" alt=\"$txt\" title=\"$title\"/>"
} else {
append result "<img src=\"$url\" alt=\"$txt\"/>"
}
}
continue
}
}
{<} {
# HTML TAGS, COMMENTS AND AUTOLINKS
if {[regexp -start $index $re_comment $text m]} {
append result $m
incr index [string length $m]
continue
} elseif {[regexp -start $index $re_autolink $text m email link]} {
if {$link ne {}} {
set link [html_escape $link]
append result "<a href=\"$link\">$link</a>"
} else {
set mailto_prefix "mailto:"
if {![regexp "^${mailto_prefix}(.*)" $email mailto email]} {
# $email does not contain the prefix "mailto:".
set mailto "mailto:$email"
}
append result "<a href=\"$mailto\">$email</a>"
}
incr index [string length $m]
continue
} elseif {[regexp -start $index $re_htmltag $text m]} {
append result $m
incr index [string length $m]
continue
}
set chr [html_escape $chr]
}
{&} {
# ENTITIES
if {[regexp -start $index $re_entity $text m]} {
append result $m
incr index [string length $m]
continue
}
set chr [html_escape $chr]
}
{>} -
{'} -
"\"" {
# OTHER SPECIAL CHARACTERS
set chr [html_escape $chr]
}
default {}
}
append result $chr
incr index
}
return $result
}
## \private
proc is_empty_line {line} {
return [regexp {^\s*$} $line]
}
## \private
proc html_escape {text} {
return [string map {& & < < > > \" "} $text]
}
}
package provide Markdown 1.0
|