Rosetta Code/Find bare lang tags: Difference between revisions

m
(fix up the rest of the actual <lang tags to <syntaxhighlight tags and fix API url)
m (→‎{{header|Wren}}: Minor tidy)
 
(4 intermediate revisions by 3 users not shown)
Line 563:
}
}</syntaxhighlight>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq.'''
 
'''Adapted from [[#Wren|Wren]]'''
<syntaxhighlight lang=jq>
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def trim: sub("^[ \t]+";"") | sub("[ \t]+$";"");
 
# Insert into a sorted list using bsearch
def binsert($x):
(-bsearch($x) - 1) as $ix
| if $ix < 0 then .
else .[:$ix] + [$x] + .[$ix:]
end;
 
def report:
def header:
"==\\s*{{\\s*header\\s*\\|\\s*(?<title>[^\\s\\}]+)\\s*}}\\s*==";
 
reduce inputs as $line ( { bareCount:0, bareLang: {} };
if .fileName != input_filename
then .lastHeader = "No language"
| .fileName = input_filename
else .
end
| .line = ($line|trim)
| if .line | length == 0 then .
else .header = ((.line | capture(header)) // null)
| if .header
then .lastHeader = .header.title
elif .line|startswith("<lang>")
then .bareCount += 1
| .bareLang[.lastHeader][0] += 1
| .fileName as $fileName
| .bareLang[.lastHeader][1] |= binsert($fileName)
else .
end
end )
| "\(.bareCount) bare language tags:",
(.bareLang
| to_entries[] as {"key": $lang, "value": $value}
| $value[0] as $count
| $value[1] as $names
| ("\($count|lpad(3)) in \($lang|lpad(15))" + ": " + ($names | join(", ")) )) ;
 
report
</syntaxhighlight>
'''Invocation'''
 
Using the examples in the [[#Go|Go]] entry:
<pre>
jq -Rnr -f find-bare-lang-tags.jq rc-example1.txt rc-example2.txt
rc-example3.txt
</langpre>
{{output}}
<pre>
5 bare language tags:
2 in No language: rc-example1.txt, rc-example3.txt
1 in Perl: rc-example1.txt
2 in C: rc-example2.txt, rc-example3.txt
</pre>
 
=={{header|Julia}}==
Line 2,033 ⟶ 2,097:
===More-extra credit===
Add the following code at the bottom, run, watch results.
<langsyntaxhighlight lang=racket>
(define (get-category cat)
(let loop ([c #f])
Line 2,050 ⟶ 2,114:
(printf "Page: ~a " page)
(find-bare-tags page))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 2,056 ⟶ 2,120:
{{trans|Perl}}
The only tricky thing here is the use of the <tt>ms</tt> form of match, short for <tt>m:sigspace</tt>. This causes whitespace in the regex to be considered "significant", that is, it matches optional whitespace at those positions, as if you'd put <tt>\s*</tt> there. Of course, the regexes themselves are in Raku syntax, which is quite different from Perl 5 regex syntax (and arguably much cleaner). Regex syntax is perhaps the area in which Raku diverges most from Perl 5.
<syntaxhighlight lang=perl6"raku" line>my $lang = '(no language)';
my $total = 0;
my %blanks;
Line 2,426 ⟶ 2,490:
{{libheader|Wren-fmt}}
Uses Go's example files.
<syntaxhighlight lang=ecmascriptwren>import "./ioutil" for FileUtil
import "./pattern" for Pattern
import "./set" for Set
import "./sort" for Sort
import "./fmt" for Fmt
 
var p = Pattern.new("/=/={{header/|[+0/y]}}/=/=", Pattern.start)
9,482

edits