-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMarkdown_main.tcl
49 lines (43 loc) · 1.27 KB
/
Markdown_main.tcl
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
#!/usr/bin/env tclsh
# this file should be placed in a folder ~/.config/tclmain
# the file should be used together with the script tclmain(.tcl)
package require Markdown
proc usage {} {
puts "Usage: tclmain -m Markdown main mdfile|- ?htmlfile|-?"
puts " instead of filenames as well - can be used to indicate stdin and stdout"
puts "\nExample:\n echo '**Hello World!**' | tclmain -m Markdown main - out.html"
}
if {[llength $argv] < 1} {
usage
} else {
set mdfile [lindex $argv 0]
set htmlfile -
if {[llength $argv] > 1} {
set htmlfile [lindex $argv 1]
}
if {![file exists $mdfile] && $mdfile ne "-"} {
usage
puts "Error: File '$mdfile' does not exists!"
} else {
if {$mdfile eq "-"} {
set infh stdin
} else {
if [catch {open $mdfile r} infh] {
puts stderr "Cannot open $mdfile: $infh"
exit
}
}
if {$htmlfile eq "-"} {
set out stdout
} else {
set out [open $htmlfile w 0600]
}
set txt ""
while {[gets $infh line] >= 0} {
append txt "$line\n"
}
puts $out [Markdown::convert $txt]
close $out
close $infh
}
}