Rosetta Code/Find bare lang tags: Difference between revisions
Content added Content deleted
(Added a Python implementation) |
|||
Line 973: | Line 973: | ||
4 in XSLT 3.0 ([[Knight's tour]]) |
4 in XSLT 3.0 ([[Knight's tour]]) |
||
5 in zkl ([[Carmichael 3 strong pseudoprimes]], [[Hamming numbers]], [[Introspection]], [[Lucas-Lehmer test]], [[Non-continuous subsequences]])</pre> |
5 in zkl ([[Carmichael 3 strong pseudoprimes]], [[Hamming numbers]], [[Introspection]], [[Lucas-Lehmer test]], [[Non-continuous subsequences]])</pre> |
||
=={{header|Nim}}== |
|||
{{trans|Julia}} |
|||
<lang Nim>import algorithm, htmlparser, httpclient, json |
|||
import sequtils, strformat, strscans, tables, times, xmltree |
|||
import strutils except escape |
|||
const |
|||
Rosorg = "http://rosettacode.org" |
|||
QUri = "/mw/api.php?action=query&list=categorymembers&cmtitle=Category:Programming_Tasks&cmlimit=500&format=json" |
|||
QdUri = "/mw/api.php?action=query&list=categorymembers&cmtitle=Category:Draft_Programming_Tasks&cmlimit=500&format=json" |
|||
SqUri = "http://www.rosettacode.org/mw/index.php?title=" |
|||
proc addPages(pages: var seq[string], fromJson: JsonNode) = |
|||
for d in fromJson{"query", "categorymembers"}: |
|||
pages.add SqUri & d["title"].getStr().replace(" ", "_").escape() & "&action=raw" |
|||
proc getPages(client: var HttpClient; uri: string): seq[string] = |
|||
let response = client.get(Rosorg & uri) |
|||
if response.status == $Http200: |
|||
var fromJson = response.body.parseJson() |
|||
result.addPages(fromJson) |
|||
while fromJson.hasKey("continue"): |
|||
let cmcont = fromJson{"continue", "cmcontinue"} |
|||
let cont = fromJson{"continue", "continue"} |
|||
let response = client.get(Rosorg & uri & fmt"&cmcontinue={cmcont}&continue={cont}") |
|||
fromJson = response.body.parseJson() |
|||
result.addPages(fromJson) |
|||
proc processTaskPages(client: var HttpClient; pages: seq[string]; verbose = false) = |
|||
var totalCount = 0 |
|||
var langCount: CountTable[string] |
|||
for page in pages: |
|||
var count, checked = 0 |
|||
try: |
|||
let response = client.get(page) |
|||
if response.status == $Http200: |
|||
let doc = response.body.parseHtml() |
|||
if doc.kind != xnElement: continue |
|||
var lastText = "" |
|||
for elem in doc: |
|||
if elem.kind == xnElement and elem.tag == "lang": |
|||
if elem.attrs.isNil: |
|||
inc count |
|||
if lastText.len != 0: |
|||
if verbose: |
|||
echo "Missing lang attribute for lang ", lastText |
|||
langCount.inc lastText |
|||
else: |
|||
inc checked |
|||
elif elem.kind == xnText: |
|||
discard elem.text.scanf("=={{header|$+}}", lastText): |
|||
except CatchableError: |
|||
if verbose: |
|||
echo &"Page {page} is not loaded or found: {getCurrentExceptionMsg()}" |
|||
continue |
|||
if count > 0 and verbose: |
|||
echo &"Page {page} had {count} bare lang tags." |
|||
inc totalCount, count |
|||
echo &"Total bare tags: {totalCount}." |
|||
for k in sorted(toSeq(langCount.keys)): |
|||
echo &"Total bare <lang> for language {k}: ({langcount[k]})" |
|||
echo "Programming examples at ", now() |
|||
var client = newHttpClient() |
|||
client.processTaskPages(client.getPages(QUri)) |
|||
echo "\nDraft programming tasks:" |
|||
client.processTaskPages(client.getPages(QdUri))</lang> |
|||
{{out}} |
|||
<pre>Programming examples at 2021-04-01T00:27:04+02:00 |
|||
Total bare tags: 792. |
|||
Total bare <lang> for language 0815: (14) |
|||
Total bare <lang> for language 11l: (392) |
|||
Total bare <lang> for language 1C: (1) |
|||
Total bare <lang> for language 360 Assembly: (199) |
|||
Total bare <lang> for language 4D: (6) |
|||
Total bare <lang> for language 4DOS Batch: (9) |
|||
Total bare <lang> for language 6502 Assembly: (19) |
|||
Total bare <lang> for language 6800 Assembly: (1) |
|||
Total bare <lang> for language 68000 Assembly: (3) |
|||
Total bare <lang> for language 8080 Assembly: (58) |
|||
Total bare <lang> for language 8th: (7) |
|||
Total bare <lang> for language AArch64 Assembly: (1) |
|||
Total bare <lang> for language ALGOL 60: (1) |
|||
Total bare <lang> for language ALGOL 68: (1) |
|||
Total bare <lang> for language APL: (1) |
|||
Total bare <lang> for language ARM Assembly: (6) |
|||
Total bare <lang> for language ATS: (2) |
|||
Total bare <lang> for language AWK: (1) |
|||
Total bare <lang> for language Ada: (1) |
|||
Total bare <lang> for language AutoHotkey: (4) |
|||
Total bare <lang> for language Axe: (1) |
|||
Total bare <lang> for language BASIC256: (1) |
|||
Total bare <lang> for language BaCon: (1) |
|||
Total bare <lang> for language Befunge: (1) |
|||
Total bare <lang> for language Bracmat: (1) |
|||
Total bare <lang> for language C sharp: (1) |
|||
Total bare <lang> for language ChucK: (2) |
|||
Total bare <lang> for language Dart: (1) |
|||
Total bare <lang> for language E: (3) |
|||
Total bare <lang> for language EasyLang: (1) |
|||
Total bare <lang> for language Icon: (1) |
|||
Total bare <lang> for language JavaScript: (1) |
|||
Total bare <lang> for language Julia: (2) |
|||
Total bare <lang> for language REXX: (1) |
|||
Total bare <lang> for language Wart: (1) |
|||
Draft programming tasks: |
|||
Total bare tags: 45. |
|||
Total bare <lang> for language 11l: (24) |
|||
Total bare <lang> for language 1C: (1) |
|||
Total bare <lang> for language 360 Assembly: (1) |
|||
Total bare <lang> for language 8080 Assembly: (3) |
|||
Total bare <lang> for language APL: (1) |
|||
Total bare <lang> for language AppleScript: (1) |
|||
Total bare <lang> for language C: (1) |
|||
Total bare <lang> for language Fermat: (1) |
|||
Total bare <lang> for language J: (3) |
|||
Total bare <lang> for language Mathematica: (1)</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |