Task

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



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]])



Extra extra credit

Use the   Media Wiki API   to test actual RC tasks.




--
-- demo\rosetta\Find_bare_lang_tags.exw
-- ====================================
--
--  (Uses '&' instead of/as well as 'a', for everyone's sanity..)
--  Finds/counts no of "<l&ng>" as opposed to eg "<l&ng Phix>" tags.
--  Since downloading all the pages can be very slow, this uses a cache.
--
without js -- (fairly obviously this will never ever run in a browser!)
constant include_drafts = true,
         sort_by_task = true,
         sort_by_lang = not sort_by_task    -- (one or t'other)

include rosettacode_cache.e -- see Rosetta_Code/Count_examples#Phix

constant {utf8,ansi} = columnize({{x"E28093","-"},
                                  {x"E28099","'"},
                                  {x"C3A8","e"},
                                  {x"C3A9","e"},
                                  {x"D09A","K"},
                                  {x"D09C","M"}})

function utf8_clean(string s)
    return substitute_all(s,utf8,ansi)
end function

function multi_lang(sequence s)
-- Convert eg {"Algol","Algol","C","C","C"} to "Algol[2],C[3]"
    integer i = 1, j = 2
    while i<length(s) do
        if s[i]=s[j] then
            while j<length(s) and s[i]=s[j+1] do j+=1 end while
            s[i..j] = {sprintf("%s[%d]",{s[i],j-i+1})}
        end if
        i += 1
        j = i+1
    end while
    return join(s,",")
end function

function multi_task(sequence s, tasks)
-- Similar to multi_lang() but with task[indexes]
    integer i = 1, j = 2
    while i<=length(s) do
        integer si = s[i]
        string tsi = html_clean(tasks[si])
        if j<=length(s) and si=s[j] then
            while j<length(s) and si=s[j+1] do j+=1 end while
            s[i..j] = {sprintf("%s[%d]",{tsi,j-i+1})}
        else
            s[i] = tsi
        end if
        i += 1
        j = i+1
    end while
    if length(s)>8 then s[4..-4] = {"..."} end if
    return join(s,",")
end function

bool first = true

function find_bare_lang_tags()
    if get_file_type("rc_cache")!=FILETYPE_DIRECTORY then
        if not create_directory("rc_cache") then
            crash("cannot create rc_cache directory")
        end if
    end if
    -- note this lot use web scraping (as cribbed from a similar task) ...
    sequence tasks = dewiki(open_category("Programming_Tasks"))
    if include_drafts then
        tasks &= dewiki(open_category("Draft_Programming_Tasks"))
        tasks = sort(tasks)
    end if
    integer blt = find("Rosetta_Code/Find_bare_lang_tags",tasks) -- not this one!
    tasks[blt..blt] = {}

    -- ... whereas the individual tasks use the web api instead (3x smaller/faster)
    integer total_count = 0,
            lt = length(tasks),
            kept = 0
    progress("%d tasks found\n",{lt})
    sequence task_langs = {},
             task_counts = iff(sort_by_task?repeat(0,lt):{}),
             task_things = iff(sort_by_task?repeat({},lt):{})
    for i=1 to length(tasks) do
        string ti = tasks[i],
               url = sprintf("http://rosettacode.org/mw/index.php?title=%s&action=raw",{ti}),
               contents = open_download(ti&".raw",url),
               curr
        integer count = 0, start = 1, header
        while true do
            start = match(`<l`&`ang>`,contents,start)
            if start=0 then exit end if
            -- look backward for the nearest header
            header = rmatch(`{`&`{he`&`ader|`,contents,start)
            if header=0 then
                curr = "no language"
            else
                header += length(`{`&`{he`&`ader|`)
                curr = utf8_clean(contents[header..match(`}}`,contents,header)-1])
            end if
            if sort_by_lang then
                integer k = find(curr,task_langs)
                if k=0 then
                    task_langs = append(task_langs,curr)
                    task_things = append(task_things,{i})
                    task_counts = append(task_counts,1)
                else
                    task_things[k] = append(task_things[k],i)
                    task_counts[k] += 1
                end if
            else
                task_things[i] = append(task_things[i],curr)
            end if
            count += 1
            start += length(`<l`&`ang>`)
        end while
        if count!=0 then
            if sort_by_task then
                task_counts[i] = count
            end if
            kept += 1
        end if
        progress("%d tasks kept, %d to go\r",{kept,lt-i})
        total_count += count
        if get_key()=#1B then progress("escape keyed\n") exit end if
    end for
    curl_cleanup()
    progress("%d tasks with bare lang tags\n",{kept})
    sequence tags = custom_sort(task_counts,tagset(length(task_counts)))
    for i=length(tags) to 1 by -1 do
        integer ti = tags[i],
                tc = task_counts[ti]
        if tc=0 then exit end if
        if sort_by_task then
            progress("%s %d (%s)\n",{html_clean(tasks[ti]),tc,multi_lang(task_things[ti])})
        else -- (sort_by_count)
            progress("%s %d (%s)\n",{task_langs[ti],tc,multi_task(task_things[ti],tasks)})
        end if
    end for
    return total_count
end function

progress("Total: %d\n",{find_bare_lang_tags()})

?"done"
{} = wait_key()
