~rbn/tlog

ed85dd3f3cc9a42fdbddea0205d24eaef57c35ee — Ruben Schuller 9 months ago 361a752
add main script detection, wrap procs in namespace
1 files changed, 71 insertions(+), 66 deletions(-)

M tlog.tcl
M tlog.tcl => tlog.tcl +71 -66
@@ 2,90 2,95 @@
package require cmdline
package require Markdown

proc verbatim { in } {
	return $in
}
namespace eval tlog {
	proc verbatim { in } {
		return $in
	}

proc markdown { in } {
	##nagelfar ignore Unknown command
	return [ ::Markdown::convert $in ]
}
	proc markdown { in } {
		##nagelfar ignore Unknown command
		return [ ::Markdown::convert $in ]
	}

proc tcl { in } {
	upvar 1 context context
	return [ eval $in ]
}
	proc tcl { in } {
		upvar 1 context context
		return [ eval $in ]
	}

proc extensions { converters } {
	return [ dict keys $converters ]
}
	proc extensions { converters } {
		return [ dict keys $converters ]
	}

proc page { template contents } {
	##nagelfar ignore Found constant
	return [ dict create template $template contents $contents ]
}
	proc page { template contents } {
		##nagelfar ignore Found constant
		return [ dict create template $template contents $contents ]
	}

proc content_files { dir converters } {
	# find all files with registered processors
	set globs "*{[join [ extensions $converters ] , ]}"
	foreach name [ glob -directory $dir $globs ] {
		lappend files $name
	proc content_files { dir converters } {
		# find all files with registered processors
		set globs "*{[join [ extensions $converters ] , ]}"
		foreach name [ glob -directory $dir $globs ] {
			lappend files $name
		}
		return $files
	}
	return $files
}

proc contents { files converters } {
	set context [ dict create ]
	foreach { path } $files {
		set f [ open $path r ]
		set converter [ dict get $converters [ file extension $path ] ]
		set name [ file rootname [ lindex [ file split $path ] end ] ]
		if { [ dict exists $context $name ] } {
			error "duplicate content with name: $name"
	proc contents { files converters } {
		set context [ dict create ]
		foreach { path } $files {
			set f [ open $path r ]
			set converter [ dict get $converters [ file extension $path ] ]
			set name [ file rootname [ lindex [ file split $path ] end ] ]
			if { [ dict exists $context $name ] } {
				error "duplicate content with name: $name"
			}
			dict set context $name [ $converter [ read $f ] ]
			close $f
		}
		dict set context $name [ $converter [ read $f ] ]
		close $f

		return $context
	}

	return $context
}
	proc insert { name } {
		upvar 1 $name _name
		return $_name 
	}

proc insert { name } {
	upvar 1 $name _name
	return $_name 
	proc execute { template { contextvar context } } {
		upvar 1 $contextvar context
		dict with context {}
		return [ subst $template ]
	}
}

proc execute { template { contextvar context } } {
	upvar 1 $contextvar context
	dict with context {}
	return [ subst $template ]
}
# check if we are sourced or running as main script
if {[info exists argv0] && [file dirname [file normalize [file join [info script] ...]]] eq [file dirname [file normalize [file join $argv0 ...]]] } {
	set options {
		    { t.arg "templates/main.html"	"template file" }
	}

set options {
            { t.arg "templates/main.html"	"template file" }
}
	set usage {[options] [content_dir content_dir ...]}

set usage {[options] [content_dir content_dir ...]}
	try {
		set opts [ ::cmdline::getoptions argv $options $usage ]
	} trap { CMDLINE USAGE } { msg o } {
		puts $msg
		exit 0
	}

try {
	set opts [ ::cmdline::getoptions argv $options $usage ]
} trap { CMDLINE USAGE } { msg o } {
	puts $msg
	exit 0
}
	set template_file [ dict get $opts t ]

set template_file [ dict get $opts t ]
	set converters [ dict create .html verbatim .md markdown .tcl tcl ]

set converters [ dict create .html verbatim .md markdown .tcl tcl ]
	set f [ open $template_file r ]
	set template [ read $f ]
	close $f

set f [ open $template_file r ]
set template [ read $f ]
close $f
	set contents {}
	foreach {dir} $argv {
		lappend contents {*}[ ::tlog::content_files $dir $converters ]
	}

set contents {}
foreach {dir} $argv {
	lappend contents {*}[ content_files $dir $converters ]
	set context [ ::tlog::contents $contents $converters ]
	puts [ ::tlog::execute $template ]
}

set context [ contents $contents $converters ]
puts [ execute $template ]