Rosetta Code/Find bare lang tags: Difference between revisions
Restore AutoHotkey code by Crazyfirex, but disable Rosetta Code's syntax highlighter. |
→Tcl: Added implementation |
||
Line 82: | Line 82: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Tcl}}== |
|||
Doing one of the extra credit options… |
|||
{{tcllib|textutil::split}} |
|||
<lang tcl>package require textutil::split |
|||
proc init {} { |
|||
global total count found |
|||
set total 0 |
|||
array set count {} |
|||
array set found {} |
|||
} |
|||
proc findBareTags {pageName pageContent} { |
|||
global total count found |
|||
set t {{no language}} |
|||
lappend t {*}[textutil::split::splitx $pageContent \ |
|||
{==\s*\{\{\s*header\s*\|\s*([^{}]+?)\s*\}\}\s*==}] |
|||
foreach {sectionName sectionText} $t { |
|||
set n [regexp -all {<lang>} $sectionText] |
|||
if {!$n} continue |
|||
incr count($sectionName) $n |
|||
lappend found($sectionName) $pageName |
|||
incr total $n |
|||
} |
|||
} |
|||
proc printResults {} { |
|||
global total count found |
|||
puts "$total bare language tags." |
|||
if {$total} { |
|||
puts "" |
|||
foreach sectionName [array names found] { |
|||
puts "$count($sectionName) in $sectionName\ |
|||
(\[\[[join $found($sectionName) {]], [[}]\]\])" |
|||
} |
|||
} |
|||
} |
|||
# 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 |
|||
foreach {filename title} $argv { |
|||
set f [open $filename] |
|||
findBareTags $title [read $f] |
|||
close $f |
|||
} |
|||
printResults</lang> |
|||
{{omit from|GUISS}} |
{{omit from|GUISS}} |
Revision as of 08:36, 23 September 2011
Find all <lang> tags without a language specified in the text of a page. Display counts by language section:
Description <lang>Pseudocode</lang> =={{header|C}}== <lang C>printf("Hello world!\n");</lang> =={{header|Perl}}== <lang>print "Hello world!\n"</lang>
should display something like
2 bare language tags. 1 in perl 1 in no language
For extra credit, allow multiple files to be read. Summarize all results by language:
5 bare language tags. 2 in c ([[Foo]], [[Bar]]) 1 in perl ([[Foo]]) 2 in no language ([[Baz]])
For more extra credit, use the Media Wiki API to test actual RC tasks.
AutoHotkey
This code has no syntax highlighting, because Rosetta Code's highlighter fails with code that contains literal </lang> tags.
Stole RegEx Needle from Perl
task = ( Description <lang>Pseudocode</lang> =={{header|C}}== <lang C>printf("Hello world!\n");</lang> =={{header|Perl}}== <lang>print "Hello world!\n"</lang> ) lang := "no lanugage", out := Object(lang, 0), total := 0 Loop Parse, task, `r`n If RegExMatch(A_LoopField, "==\s*{{\s*header\s*\|\s*([^\s\}]+)\s*}}\s*==", $) lang := $1, out[lang] := 0 else if InStr(A_LoopField, "<lang>") out[lang]++ For lang, num in Out If num total++, str .= "`n" num " in " lang MsgBox % clipboard := total " bare lang tags.`n" . str
Output:
2 bare lang tags. 1 in no lanugage 1 in Perl
Perl
This is a simple implementation that does not attempt either extra credit. <lang perl>my $lang = 'no language'; my $total = 0; my %blanks = (); while (<>) {
if (m/<lang>/) { if (exists $blanks{lc $lang}) { $blanks{lc $lang}++ } else { $blanks{lc $lang} = 1 } $total++ } elsif (m/==\s*Template:\s*header\s*\\s*==/) { $lang = lc $1 }
}
if ($total) { print "$total bare language tag" . ($total > 1 ? 's' : ) . ".\n\n"; while ( my ($k, $v) = each(%blanks) ) { print "$k in $v\n" } }</lang>
Tcl
Doing one of the extra credit options…
<lang tcl>package require textutil::split
proc init {} {
global total count found set total 0 array set count {} array set found {}
} proc findBareTags {pageName pageContent} {
global total count found set t Template:No language lappend t {*}[textutil::split::splitx $pageContent \
{==\s*\{\{\s*header\s*\|\s*([^{}]+?)\s*\}\}\s*==}]
foreach {sectionName sectionText} $t {
set n [regexp -all {<lang>} $sectionText] if {!$n} continue incr count($sectionName) $n lappend found($sectionName) $pageName incr total $n
}
} proc printResults {} {
global total count found puts "$total bare language tags." if {$total} {
puts "" foreach sectionName [array names found] { puts "$count($sectionName) in $sectionName\ (\[\[[join $found($sectionName) {]], [[}]\]\])" }
}
}
- 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 foreach {filename title} $argv {
set f [open $filename] findBareTags $title [read $f] close $f
} printResults</lang>