Rosetta Code/Find bare lang tags: Difference between revisions

Content added Content deleted
(→‎Tcl: Added implementation)
(→‎{{header|Tcl}}: Full solution)
Line 84: Line 84:


=={{header|Tcl}}==
=={{header|Tcl}}==
For all the extra credit (note, takes a substantial amount of time due to number of HTTP requests):
Doing one of the extra credit options…
{{tcllib|json}}
{{tcllib|textutil::split}}
{{tcllib|textutil::split}}
{{tcllib|uri}}
<lang tcl>package require textutil::split
<lang tcl>package require Tcl 8.5
package require http
package require json
package require textutil::split
package require uri

proc getUrlWithRedirect {base args} {
set url $base?[http::formatQuery {*}$args]
while 1 {
set t [http::geturl $url]
if {[http::status $t] ne "ok"} {
error "Oops: url=$url\nstatus=$s\nhttp code=[http::code $token]"
}
if {[string match 2?? [http::ncode $t]]} {
return $t
}
# OK, but not 200? Must be a redirect...
set url [uri::resolve $url [dict get [http::meta $t] Location]]
http::cleanup $t
}
}

proc get_tasks {category} {
global cache
if {[info exists cache($category)]} {
return $cache($category)
}
set query [dict create cmtitle Category:$category]
set tasks [list]
while {1} {
set response [getUrlWithRedirect http://rosettacode.org/mw/api.php \
action query list categorymembers format json cmlimit 500 {*}$query]
# Get the data out of the message
set data [json::json2dict [http::data $response]]
http::cleanup $response
# add tasks to list
foreach task [dict get $data query categorymembers] {
lappend tasks [dict get [dict create {*}$task] title]
}
if {[catch {
dict get $data query-continue categorymembers cmcontinue
} continue_task]} then {
# no more continuations, we're done
break
}
dict set query cmcontinue $continue_task
}
return [set cache($category) $tasks]
}
proc getTaskContent task {
set token [getUrlWithRedirect http://rosettacode.org/mw/api.php \
title $task action raw]
set content [http::data $token]
http::cleanup $token
return $content
}


proc init {} {
proc init {} {
Line 96: Line 157:
proc findBareTags {pageName pageContent} {
proc findBareTags {pageName pageContent} {
global total count found
global total count found
set t {{no language}}
set t {{}}
lappend t {*}[textutil::split::splitx $pageContent \
lappend t {*}[textutil::split::splitx $pageContent \
{==\s*\{\{\s*header\s*\|\s*([^{}]+?)\s*\}\}\s*==}]
{==\s*\{\{\s*header\s*\|\s*([^{}]+?)\s*\}\}\s*==}]
Line 112: Line 173:
if {$total} {
if {$total} {
puts ""
puts ""
foreach sectionName [array names found] {
if {[info exists found()]} {
puts "$count() in task descriptions\
(\[\[[join $found() {]], [[}]\]\])"
unset found()
}
foreach sectionName [lsort -dictionary [array names found]] {
puts "$count($sectionName) in $sectionName\
puts "$count($sectionName) in $sectionName\
(\[\[[join $found($sectionName) {]], [[}]\]\])"
(\[\[[join $found($sectionName) {]], [[}]\]\])"
Line 119: Line 185:
}
}


# Do the first level of extra credit. Note that filenames are interleaved with
# page titles as the page content doesn't (necessarily) contain the title.
init
init
set tasks [get_tasks Programming_Tasks]
foreach {filename title} $argv {
#puts stderr "querying over [llength $tasks] tasks..."
set f [open $filename]
foreach task [get_tasks Programming_Tasks] {
findBareTags $title [read $f]
close $f
#puts stderr "$task..."
findBareTags $task [getTaskContent $task]
}
}
printResults</lang>
printResults</lang>