# Syntax-highlight special tags.
proc highlight {text} {
set text [regsub -all {(\[\[)(.*?)(\]\])} $text {\1\2\3}]
set text [regsub -all {^\m(.*?)(:)} $text {\1\2}]
set text [regsub -all {(<<)(.*?)(>>)} $text {\1\2\3}]
set text [regsub -all {(\{\{)(.*?)(\}\})} $text {\1\2\3}]
}
# Get the search query.
set q [lindex [lsearch -inline -index 0 $query q] 1]
# Process the query if given.
if {$q ne ""} {
# Load the database.
lappend ::auto_path [file join / home andy package host andy.junkdrome.org\
foreign sqlite library]
package require sqlite3
sqlite3 db /[file_join $::docroot $path data comics.db]
# Search the database. Treat SQL failures as empty results.
if {[catch {
set results {}
foreach {title id body} [db eval {
select title, id, body from comics
where comics match $q order by id + 0
}] {
lappend results [list $title $id $body]
}
}]} {
set results {}
}
# Close the database; it is no longer needed.
db close
# Get the requested page number, clamped to the number of result pages.
set num_results [llength $results]
set num_pages [expr {-(-$num_results / 10)}]
set page [lindex [lsearch -inline -index 0 $query p] 1]
if {![string is integer -strict $page] || $page < 1 || $page > $num_pages} {
set page 1
}
# Determine the beginning and ending indices, and filter the results list
# down to the selected page.
set begin [expr {($page - 1) * 10}]
set end [expr {$begin + 9}]
if {$end >= $num_results} {
set end [expr {$num_results - 1}]
}
set results [lrange $results $begin $end]
}
# Read and process the template.
emit_header {Content-type: text/html}
set chan [open /[file_join $::docroot $path .template]]
emit_template [read $chan]
close $chan
# vim: set ts=4 sts=4 sw=4 tw=80 et ft=tcl: