Rosetta Code/Fix code tags: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(41 intermediate revisions by 22 users not shown)
Line 1: Line 1:
{{task|Text processing}}[[Category:Rosetta Code related]]
{{task|Text processing}}
[[Category:Rosetta Code related]]


;; This task has been <em>revised</em> for the hosting change Rosetta Code has had in August 2022.
;; See the Python example for a new example to fit the revised task.

;Task:
Fix Rosetta Code deprecated code tags, with these rules:
Fix Rosetta Code deprecated code tags, with these rules:
<pre>
Change <lang %s> to <syntaxhighlight lang=%s>
Change </lang> to </syntaxhighlight>
Change <lang> to <syntaxhighlight lang=text>
</pre>


;Demonstrate the task on these examples:
<pre>Change <%s> to <lang %s>
<pre>
Change </%s> to </lang>
<lang AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
Change <code %s> to <lang %s>
FileRead, text, %1%
Change </code> to </lang></pre>
langs = ada,awk,autohotkey,etc
slang = /lang
slang := "<" . slang . "/>"
Loop, Parse, langs, `,
{
tag1 = <%A_LoopField%>
tag2 = </%A_LoopField%>
text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
text := RegExReplace(text, tag2, slang)
text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
, "<lang $1>$2" . slang)
}
FileAppend, % text, %2%
</lang>
</pre>
<pre>
<lang perl>my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext);


my $text = join "", <STDIN>;
Usage:
my $slang="/lang";
for (@langs) {
$text =~ s|<$_>|<lang $_>|g;
$text =~ s|</$_>|<$slang>|g;
}


$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;
<pre>./convert.py < wikisource.txt > converted.txt</pre>

print $text;</lang>
</pre>
<pre>
<lang>HAI 1.3

I HAS A bottles ITZ 99 I HAS A plural ITZ "Z" I HAS A lyric ITZ "99 BOTTLZ OV BEER"

IM IN YR song

VISIBLE lyric " ON TEH WALL"
VISIBLE lyric
VISIBLE "TAEK 1 DOWN, PAZ IT AROUN"
bottles R DIFF OF bottles AN 1
NOT bottles, O RLY?
YA RLY, VISIBLE "NO MOAR BOTTLZ OV BEER ON TEH WALL", GTFO
OIC
BOTH SAEM bottles AN 1, O RLY?
YA RLY, plural R ""
OIC
lyric R SMOOSH bottles " BOTTL" plural " OV BEER" MKAY
VISIBLE lyric " ON TEH WALL:)"
IM OUTTA YR song

KTHXBYE</lang>
</pre>
<br><br>

=={{header|11l}}==
{{trans|Java}}

<syntaxhighlight lang="11l">V languages = [‘abap’, ‘actionscript’, ‘actionscript3’,
‘ada’, ‘apache’, ‘applescript’, ‘apt_sources’, ‘asm’, ‘asp’,
‘autoit’, ‘avisynth’, ‘bar’, ‘bash’, ‘basic4gl’, ‘bf’,
‘blitzbasic’, ‘bnf’, ‘boo’, ‘c’, ‘caddcl’, ‘cadlisp’, ‘cfdg’,
‘cfm’, ‘cil’, ‘c_mac’, ‘cobol’, ‘cpp’, ‘cpp-qt’, ‘csharp’, ‘css’,
‘d’, ‘delphi’, ‘diff’, ‘_div’, ‘dos’, ‘dot’, ‘eiffel’, ‘email’,
‘foo’, ‘fortran’, ‘freebasic’, ‘genero’, ‘gettext’, ‘glsl’, ‘gml’,
‘gnuplot’, ‘go’, ‘groovy’, ‘haskell’, ‘hq9plus’, ‘html4strict’,
‘idl’, ‘ini’, ‘inno’, ‘intercal’, ‘io’, ‘java’, ‘java5’,
‘javascript’, ‘kixtart’, ‘klonec’, ‘klonecpp’, ‘latex’, ‘lisp’,
‘lolcode’, ‘lotusformulas’, ‘lotusscript’, ‘lscript’, ‘lua’,
‘m68k’, ‘make’, ‘matlab’, ‘mirc’, ‘modula3’, ‘mpasm’, ‘mxml’,
‘mysql’, ‘nsis’, ‘objc’, ‘ocaml’, ‘ocaml-brief’, ‘oobas’,
‘oracle11’, ‘oracle8’, ‘pascal’, ‘per’, ‘perl’, ‘php’, ‘php-brief’,
‘pic16’, ‘pixelbender’, ‘plsql’, ‘povray’, ‘powershell’,
‘progress’, ‘prolog’, ‘providex’, ‘python’, ‘qbasic’, ‘rails’,
‘reg’, ‘robots’, ‘ruby’, ‘sas’, ‘scala’, ‘scheme’, ‘scilab’,
‘sdlbasic’, ‘smalltalk’, ‘smarty’, ‘sql’, ‘tcl’, ‘teraterm’,
‘text’, ‘thinbasic’, ‘tsql’, ‘typoscript’, ‘vb’, ‘vbnet’,
‘verilog’, ‘vhdl’, ‘vim’, ‘visualfoxpro’, ‘visualprolog’,
‘whitespace’, ‘winbatch’, ‘xml’, ‘xorg_conf’, ‘xpp’, ‘z80’]

V text =
‘Lorem ipsum <code foo>saepe audire</code> elaboraret ne quo, id equidem
atomorum inciderint usu. <foo>In sit inermis deleniti percipit</foo>,
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea, <code bar>soluta appetere ut mel</bar>.
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</code>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.’

L(lang) languages
text = text.replace(‘<’lang‘>’, ‘<lang ’lang‘>’)
text = text.replace(‘</’lang‘>’, ‘</’""‘lang>’)
text = text.replace(‘<code ’lang‘>’, ‘<lang ’lang‘>’)
text = text.replace(‘</code>’, ‘</’""‘lang>’)

print(text)</syntaxhighlight>

{{out}}
<pre>
Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
atomorum inciderint usu. <lang foo>In sit inermis deleniti percipit</lang>,
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</lang>.
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.
</pre>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
<syntaxhighlight lang="autohotkey">; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
langs = ada,awk,autohotkey,etc
Line 28: Line 152:
}
}
FileAppend, % text, %2%
FileAppend, % text, %2%
</syntaxhighlight>
</lang>


=={{header|D}}==
=={{header|D}}==
{{trans|Python}}
{{trans|Python}}
<lang d>import std.stdio, std.regex;
<syntaxhighlight lang="d">import std.stdio, std.regex, std.string, std.array;
import std.string: replace, split, format;


immutable langs = "_div abap actionscript actionscript3 ada apache
immutable langs = "_div abap actionscript actionscript3 ada apache
Line 72: Line 195:
.fixTags
.fixTags
.writeln;
.writeln;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>lorem ipsum <lang c>some c code</lang>dolor sit amet, <lang csharp>some csharp code</lang> consectetur adipisicing elit, <lang r> some r code </lang>sed do eiusmod tempor incididunt</pre>
<pre>lorem ipsum <lang c>some c code</lang>dolor sit amet, <lang csharp>some csharp code</lang> consectetur adipisicing elit, <lang r> some r code </lang>sed do eiusmod tempor incididunt</pre>
=={{header|Delphi}}==
{{libheader| Winapi.Windows}}
{{libheader| System.SysUtils}}
{{libheader| System.Classes}}
{{libheader| System.RegularExpressions}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Fix_code_tags;

{$APPTYPE CONSOLE}

uses
Winapi.Windows,
System.SysUtils,
System.Classes,
System.RegularExpressions;

const
LANGS: array of string = ['abap', 'actionscript', 'actionscript3', 'ada',
'apache', 'applescript', 'apt_sources', 'asm', 'asp', 'autoit', 'avisynth',
'bash', 'basic4gl', 'bf', 'blitzbasic', 'bnf', 'boo', 'c', 'caddcl',
'cadlisp', 'cfdg', 'cfm', 'cil', 'c_mac', 'cobol', 'cpp', 'cpp-qt', 'csharp',
'css', 'd', 'delphi', 'diff', '_div', 'dos', 'dot', 'eiffel', 'email',
'fortran', 'freebasic', 'genero', 'gettext', 'glsl', 'gml', 'gnuplot', 'go',
'groovy', 'haskell', 'hq9plus', 'html4strict', 'idl', 'ini', 'inno',
'intercal', 'io', 'java', 'java5', 'javascript', 'kixtart', 'klonec',
'klonecpp', 'latex', 'lisp', 'lolcode', 'lotusformulas', 'lotusscript',
'lscript', 'lua', 'm68k', 'make', 'matlab', 'mirc', 'modula3', 'mpasm',
'mxml', 'mysql', 'nsis', 'objc', 'ocaml', 'ocaml-brief', 'oobas', 'oracle11',
'oracle8', 'pascal', 'per', 'perl', 'php', 'php-brief', 'pic16',
'pixelbender', 'plsql', 'povray', 'powershell', 'progress', 'prolog',
'providex', 'python', 'qbasic', 'rails', 'reg', 'robots', 'ruby', 'sas',
'scala', 'scheme', 'scilab', 'sdlbasic', 'smalltalk', 'smarty', 'sql', 'tcl',
'teraterm', 'text', 'thinbasic', 'tsql', 'typoscript', 'vb', 'vbnet',
'verilog', 'vhdl', 'vim', 'visualfoxpro', 'visualprolog', 'whitespace',
'winbatch', 'xml', 'xorg_conf', 'xpp', 'z80'];

procedure repl(const Match: TMatch; var code: ansistring);

procedure replace(_new: ansistring);
begin
code := StringReplace(code, Match.Value, '<' + _new + '>', [rfReplaceAll]);
end;

begin
if Match.Value = '/code' then
begin
replace('/lang');
exit;
end;

if Match.Value = '</code>' then
begin
replace('/lang');
exit;
end;

var mid := Lowercase(Trim(copy(Match.Value, 2, length(Match.Value) - 2)));

for var lg in LANGS do
begin
if mid = lg then
begin
replace(format('lang %s', [lg]));
exit;
end;

if mid[1] = '/' then
begin
replace('/lang');
exit;
end;

if (pos('code', mid) = 1) and (copy(mid, 6, length(mid) - 2) = lg) then
begin
replace(format('lang %s', [lg]));
exit;
end;
end;
end;

function Lang(input: ansistring): ansistring;
var
reg: Tregex;
match: TMatch;
begin
reg := TRegex.Create('<[^>]+>');
Result := input;
for match in reg.Matches(input) do
Repl(match, Result);
end;

function ReadAll: Ansistring;
var
Buffer: array[0..1000] of byte;
StdIn: TStream;
Count: Integer;
buf: Ansistring;
begin
StdIn := THandleStream.Create(GetStdHandle(STD_INPUT_HANDLE));
Result := '';
while True do
begin
Count := StdIn.Read(Buffer, 1000);
if Count = 0 then
Break;
SetLength(buf, Count);
CopyMemory(@buf[1], @Buffer[0], Count);
Result := Result + buf;
end;
StdIn.Free;
end;

procedure Fix();
var
input, output: ansistring;
begin
input := ReadAll;
output := Lang(input);
writeln(output);
end;

begin
Fix;
end.</syntaxhighlight>


=={{header|Erlang}}==
=={{header|Erlang}}==
Commented away are 3 lines that would create a dict of the existing languages on Rosetta Code. Since the examples have 3 imaginary code tags, I replaced that code this way.
Commented away are 3 lines that would create a dict of the existing languages on Rosetta Code. Since the examples have 3 imaginary code tags, I replaced that code this way.
<syntaxhighlight lang="erlang">
<lang Erlang>
#! /usr/bin/env escript
#! /usr/bin/env escript
-module( fix_code_tags ).
-module( fix_code_tags ).
Line 117: Line 365:
loop( eof, Acc ) -> lists:reverse( Acc );
loop( eof, Acc ) -> lists:reverse( Acc );
loop( Line, Acc ) -> loop( io:get_line(""), [Line | Acc] ).
loop( Line, Acc ) -> loop( io:get_line(""), [Line | Acc] ).
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 138: Line 386:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
While the ubiquitous loop over languages approach can be used, here we capture all tag variations to fix in one dotNet regex.
While the ubiquitous loop over languages approach can be used, here we capture all tag variations to fix in one dotNet regex.
<lang fsharp>open System
<syntaxhighlight lang="fsharp">open System
open System.Text.RegularExpressions
open System.Text.RegularExpressions


Line 167: Line 415:


printfn "%s" (regexForOldLangSyntax.Replace(Console.In.ReadToEnd(), replaceEvaluator))
printfn "%s" (regexForOldLangSyntax.Replace(Console.In.ReadToEnd(), replaceEvaluator))
0</lang>
0</syntaxhighlight>
Output
Output
<pre>&gt;Rosetta.exe
<pre>&gt;Rosetta.exe
Line 177: Line 425:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 264: Line 512:


return in
return in
}</lang>
}</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
'''Solution:'''
'''Solution:'''
<lang j>require 'printf strings files'
<syntaxhighlight lang="j">require 'printf strings files'


langs=. <;._1 LF -.~ noun define NB. replace with real lang strings
langs=. <;._1 LF -.~ noun define NB. replace with real lang strings
foo bar
foo bar
baz
baz
)</lang>
)</syntaxhighlight>
<pre>patterns=. noun define
<pre>patterns=. noun define
<%s>|<lang %s>|
<%s>|<lang %s>|
Line 280: Line 528:
</code>|</lang>|
</code>|</lang>|
)</pre>
)</pre>
<lang j>fixCodeTags=: rplc&(, <;._2;._2 &> patterns vbsprintf _5]\ 5#langs)</lang>
<syntaxhighlight lang="j">fixCodeTags=: rplc&(, <;._2;._2 &> patterns vbsprintf _5]\ 5#langs)</syntaxhighlight>


'''Example Usage:'''
'''Example Usage:'''
Line 300: Line 548:
constituto id, mea an omittam fierent vituperatoribus.</pre>
constituto id, mea an omittam fierent vituperatoribus.</pre>
Reading/writing file:
Reading/writing file:
<lang j> 'converted.txt' fwrite~ fixCodeTags fread 'wikisource.txt'</lang>
<syntaxhighlight lang="j"> 'converted.txt' fwrite~ fixCodeTags fread 'wikisource.txt'</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
<syntaxhighlight lang="java">import java.io.BufferedReader;
{{lines too long|lang}}
<lang java>import java.io.BufferedReader;
import java.io.File;
import java.io.File;
import java.io.FileReader;
import java.io.FileReader;
Line 317: Line 564:
convert(sourcefile,convertedfile);
convert(sourcefile,convertedfile);
}
}
static String[] languages = {"abap", "actionscript", "actionscript3",
static String[] languages={"abap", "actionscript", "actionscript3", "ada", "apache", "applescript","apt_sources", "asm", "asp", "autoit", "avisynth","bar","bash", "basic4gl","bf", "blitzbasic", "bnf", "boo", "c", "caddcl", "cadlisp", "cfdg", "cfm","cil", "c_mac", "cobol", "cpp", "cpp-qt", "csharp", "css", "d", "delphi","diff", "_div", "dos", "dot", "eiffel", "email", "foo", "fortran", "freebasic","genero", "gettext", "glsl", "gml", "gnuplot", "go", "groovy", "haskell","hq9plus", "html4strict", "idl", "ini", "inno", "intercal", "io", "java","java5", "javascript", "kixtart", "klonec", "klonecpp", "latex", "lisp","lolcode", "lotusformulas", "lotusscript", "lscript", "lua", "m68k","make", "matlab", "mirc", "modula3", "mpasm", "mxml", "mysql", "nsis","objc", "ocaml", "ocaml-brief", "oobas", "oracle11", "oracle8", "pascal","per", "perl", "php", "php-brief", "pic16", "pixelbender", "plsql","povray", "powershell", "progress", "prolog", "providex", "python","qbasic", "rails", "reg", "robots", "ruby", "sas", "scala", "scheme","scilab", "sdlbasic", "smalltalk", "smarty", "sql", "tcl", "teraterm","text", "thinbasic", "tsql", "typoscript", "vb", "vbnet", "verilog","vhdl", "vim", "visualfoxpro", "visualprolog", "whitespace", "winbatch","xml", "xorg_conf","xpp","z80"};
"ada", "apache", "applescript", "apt_sources", "asm", "asp",
"autoit", "avisynth", "bar", "bash", "basic4gl", "bf",
"blitzbasic", "bnf", "boo", "c", "caddcl", "cadlisp", "cfdg",
"cfm", "cil", "c_mac", "cobol", "cpp", "cpp-qt", "csharp", "css",
"d", "delphi", "diff", "_div", "dos", "dot", "eiffel", "email",
"foo", "fortran", "freebasic", "genero", "gettext", "glsl", "gml",
"gnuplot", "go", "groovy", "haskell", "hq9plus", "html4strict",
"idl", "ini", "inno", "intercal", "io", "java", "java5",
"javascript", "kixtart", "klonec", "klonecpp", "latex", "lisp",
"lolcode", "lotusformulas", "lotusscript", "lscript", "lua",
"m68k", "make", "matlab", "mirc", "modula3", "mpasm", "mxml",
"mysql", "nsis", "objc", "ocaml", "ocaml-brief", "oobas",
"oracle11", "oracle8", "pascal", "per", "perl", "php", "php-brief",
"pic16", "pixelbender", "plsql", "povray", "powershell",
"progress", "prolog", "providex", "python", "qbasic", "rails",
"reg", "robots", "ruby", "sas", "scala", "scheme", "scilab",
"sdlbasic", "smalltalk", "smarty", "sql", "tcl", "teraterm",
"text", "thinbasic", "tsql", "typoscript", "vb", "vbnet",
"verilog", "vhdl", "vim", "visualfoxpro", "visualprolog",
"whitespace", "winbatch", "xml", "xorg_conf", "xpp", "z80"};
static void convert(String sourcefile,String convertedfile)
static void convert(String sourcefile,String convertedfile)
{
{
Line 332: Line 599:
String lang=languages[i];
String lang=languages[i];
line=line.replaceAll("<"+lang+">", "<lang "+lang+">");
line=line.replaceAll("<"+lang+">", "<lang "+lang+">");
line=line.replaceAll("</"+lang+">", "</"+lang+">");
line=line.replaceAll("</"+lang+">", "</"+"lang>");
line=line.replaceAll("<code "+lang+">", "<lang "+lang+">");
line=line.replaceAll("<code "+lang+">", "<lang "+lang+">");
line=line.replaceAll("</code>", "</"+"lang>");
line=line.replaceAll("</code>", "</"+"lang>");
Line 351: Line 618:
}
}
}
}
</syntaxhighlight>
</lang>


Example:
Example:
Line 366: Line 633:
user@ubuntu:~$ cat fix.out
user@ubuntu:~$ cat fix.out
Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
atomorum inciderint usu. <lang foo>In sit inermis deleniti percipit</foo>,
atomorum inciderint usu. <lang foo>In sit inermis deleniti percipit</lang>,
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</bar>.
altera electram. Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</lang>.
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.
constituto id, mea an omittam fierent vituperatoribus.
Line 376: Line 643:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
{{works with|SpiderMonkey}}
{{works with|SpiderMonkey}}
<lang javascript>var langs = ['foo', 'bar', 'baz']; // real list of langs goes here
<syntaxhighlight lang="javascript">var langs = ['foo', 'bar', 'baz']; // real list of langs goes here
var end_tag = '</'+'lang>';
var end_tag = '</'+'lang>';


Line 386: Line 653:
.replace(new RegExp('</' + langs[i] + '>', 'gi'), end_tag);
.replace(new RegExp('</' + langs[i] + '>', 'gi'), end_tag);
print(line);
print(line);
}</lang>
}</syntaxhighlight>

=={{header|Julia}}==
{{works with|Julia|0.6}}
{{trans|Python}}

<syntaxhighlight lang="julia">function fixtags(text::AbstractString)
langs = ["ada", "cpp-qt", "pascal", "lscript", "z80", "visualprolog",
"html4strict", "cil", "objc", "asm", "progress", "teraterm", "hq9plus",
"genero", "tsql", "email", "pic16", "tcl", "apt_sources", "io", "apache",
"vhdl", "avisynth", "winbatch", "vbnet", "ini", "scilab", "ocaml-brief",
"sas", "actionscript3", "qbasic", "perl", "bnf", "cobol", "powershell",
"php", "kixtart", "visualfoxpro", "mirc", "make", "javascript", "cpp",
"sdlbasic", "cadlisp", "php-brief", "rails", "verilog", "xml", "csharp",
"actionscript", "nsis", "bash", "typoscript", "freebasic", "dot",
"applescript", "haskell", "dos", "oracle8", "cfdg", "glsl", "lotusscript",
"mpasm", "latex", "sql", "klonec", "ruby", "ocaml", "smarty", "python",
"oracle11", "caddcl", "robots", "groovy", "smalltalk", "diff", "fortran",
"cfm", "lua", "modula3", "vb", "autoit", "java", "text", "scala",
"lotusformulas", "pixelbender", "reg", "_div", "whitespace", "providex",
"asp", "css", "lolcode", "lisp", "inno", "mysql", "plsql", "matlab",
"oobas", "vim", "delphi", "xorg_conf", "gml", "prolog", "bf", "per",
"scheme", "mxml", "d", "basic4gl", "m68k", "gnuplot", "idl", "abap",
"intercal", "c_mac", "thinbasic", "java5", "xpp", "boo", "klonecpp",
"blitzbasic", "eiffel", "povray", "c", "gettext"]

slang = "/lang"
code = "code"

for l in langs
text = replace(text, "<$l>","<lang $l>")
text = replace(text, "</$l>", "<$slang>")
end

text = replace(text, Regex("(?s)<$code (.+?)>(.*?)</$code>"), "<lang \\1>\\2<$slang>")
end

const txt = readstring(ARGS[1])
println(fixtags(txt))</syntaxhighlight>


=={{header|Lua}}==
=={{header|Lua}}==
<lang lua>
<syntaxhighlight lang="lua">
--thanks, random python guy
--thanks, random python guy
langs = {'ada', 'cpp-qt', 'pascal', 'lscript', 'z80', 'visualprolog',
langs = {'ada', 'cpp-qt', 'pascal', 'lscript', 'z80', 'visualprolog',
Line 419: Line 724:
print(line)
print(line)
end
end
</syntaxhighlight>
</lang>


=={{header|Mathematica}}==
=={{header|Maple}}==
<syntaxhighlight lang="maple">#Used <#/lang> to desensitize wiki
<pre>StringReplace[Import["wikisource.txt"],
txt := FileTools[Text][ReadFile]("C:/Users/username/Desktop/text.txt"):
#langs should be a real list of programming languages
langs := ['foo', 'bar', 'baz', 'barf'];
for lan in langs do
txt := StringTools:-SubstituteAll(txt, cat("<", lan, ">"), cat ("<lang ", lan, ">")):
txt := StringTools:-SubstituteAll(txt, cat("</", lan, ">"), "<#/lang>"):
txt := StringTools:-SubstituteAll(txt, cat("<code ", lan, ">"), cat ("<lang ", lan, ">")):
txt := StringTools:-SubstituteAll(txt, "</code>", "<#/lang>"):
end do;
print(txt);</syntaxhighlight>
{{Out|Output}}
<pre>
"Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
atomorum inciderint usu. <lang foo>In sit inermis deleniti percipit</lang>,
ius ex tale civibus omittam. <lang barf>Vix ut doctus cetero invenire</lang>, his eu
altera electram. Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</lang>.
Quo quis graecis vivendo te, <lang baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus. "
</pre>

=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">StringReplace[Import["wikisource.txt"],
{"</"~~Shortest[__]~~">"->"</lang>",
{"</"~~Shortest[__]~~">"->"</lang>",
("<code "|"<")~~Shortest[x__]~~">"->"<lang "~~ x~~">"}
("<code "|"<")~~Shortest[x__]~~">"->"<lang "~~ x~~">"}
]>>"converted.txt"
]>>"converted.txt"</syntaxhighlight>
{{out}}

<pre>Lorem ipsum<lang foo>saepe audire</lang>elaboraret ne quo,id equidem
->conversion of sample text
Lorem ipsum<lang foo>saepe audire</lang>elaboraret ne quo,id equidem
atomorum inciderint usu. <lang foo>In sit inermis deleniti percipit</lang>,
atomorum inciderint usu. <lang foo>In sit inermis deleniti percipit</lang>,
ius ex tale civibus omittam. <lang barf>Vix ut doctus cetero invenire</lang>,
ius ex tale civibus omittam. <lang barf>Vix ut doctus cetero invenire</lang>,
his eualtera electram.Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</lang>.
his eualtera electram.Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</lang>.
Quo quis graecis vivendo te, <lang baz>posse nullam lobortis ex usu</lang>.
Quo quis graecis vivendo te, <lang baz>posse nullam lobortis ex usu</lang>.
Eam volumus perpetua constituto id,mea an omittam fierent vituperatoribus
Eam volumus perpetua constituto id,mea an omittam fierent vituperatoribus</pre>

</pre>
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="nim">import re, strutils

const
Langs = ["ada", "cpp-qt", "pascal", "lscript", "z80", "visualprolog",
"html4strict", "cil", "objc", "asm", "progress", "teraterm", "hq9plus",
"genero", "tsql", "email", "pic16", "tcl", "apt_sources", "io", "apache",
"vhdl", "avisynth", "winbatch", "vbnet", "ini", "scilab", "ocaml-brief",
"sas", "actionscript3", "qbasic", "perl", "bnf", "cobol", "powershell",
"php", "kixtart", "visualfoxpro", "mirc", "make", "javascript", "cpp",
"sdlbasic", "cadlisp", "php-brief", "rails", "verilog", "xml", "csharp",
"actionscript", "nsis", "bash", "typoscript", "freebasic", "dot",
"applescript", "haskell", "dos", "oracle8", "cfdg", "glsl", "lotusscript",
"mpasm", "latex", "sql", "klonec", "ruby", "ocaml", "smarty", "python",
"oracle11", "caddcl", "robots", "groovy", "smalltalk", "diff", "fortran",
"cfm", "lua", "modula3", "vb", "autoit", "java", "text", "scala",
"lotusformulas", "pixelbender", "reg", "_div", "whitespace", "providex",
"asp", "css", "lolcode", "lisp", "inno", "mysql", "plsql", "matlab",
"oobas", "vim", "delphi", "xorg_conf", "gml", "prolog", "bf", "per",
"scheme", "mxml", "d", "basic4gl", "m68k", "gnuplot", "idl", "abap",
"intercal", "c_mac", "thinbasic", "java5", "xpp", "boo", "klonecpp",
"blitzbasic", "eiffel", "povray", "c", "gettext"]

var text = stdin.readAll()

for lang in Langs:
text = text.replace("<$#>" % lang, "<lang $#>" % lang)
text = text.replace("</$#>" % lang, "</lang>")

text = text.replacef(re"(?s)<code (.+?)>(.*?)</code>", r"<lang $1>$2</lang>")

stdout.write text</syntaxhighlight>


=={{header|OCaml}}==
=={{header|OCaml}}==
<lang ocaml>#load "str.cma"
<syntaxhighlight lang="ocaml">#load "str.cma"


let langs =
let langs =
Line 445: Line 804:
fortran fsharp haskell haxe j java javascript lisaac lisp logo lua m4 \
fortran fsharp haskell haxe j java javascript lisaac lisp logo lua m4 \
mathematica maxscript modula3 moo objc ocaml octave oz pascal perl \
mathematica maxscript modula3 moo objc ocaml octave oz pascal perl \
perl6 php pike pop11 powershell prolog python qbasic r rebol ruby \
raku php pike pop11 powershell prolog python qbasic r rebol ruby \
scala scheme slate smalltalk tcl ti89b vbnet vedit"
scala scheme slate smalltalk tcl ti89b vbnet vedit"


Line 495: Line 854:
List.fold_left (fun str lang ->
List.fold_left (fun str lang ->
(repl4 lang (repl3 lang (repl2 lang (repl1 lang str))))
(repl4 lang (repl3 lang (repl2 lang (repl1 lang str))))
) (read_in stdin) langs)</lang>
) (read_in stdin) langs)</syntaxhighlight>


(in the code the strings <nowiki></lang></nowiki> have been split in order to not confuse the wiki)
(in the code the strings <nowiki></lang></nowiki> have been split in order to not confuse the wiki)


this line of code:
this line of code:
<lang ocaml> (repl4 lang (repl3 lang (repl2 lang (repl1 lang str))))</lang>
<syntaxhighlight lang="ocaml"> (repl4 lang (repl3 lang (repl2 lang (repl1 lang str))))</syntaxhighlight>


could also be written like this:
could also be written like this:
<lang ocaml> List.fold_right (fun repl -> repl lang) [repl1; repl2; repl3; repl4] str</lang>
<syntaxhighlight lang="ocaml"> List.fold_right (fun repl -> repl lang) [repl1; repl2; repl3; repl4] str</syntaxhighlight>




Line 509: Line 868:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
<syntaxhighlight lang="perl">my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
Line 534: Line 893:
$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;
$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;


print $text;</lang>
print $text;</syntaxhighlight>
=={{header|Perl 6}}==
<lang perl6>my @langs = <
abap actionscript actionscript3 ada apache applescript apt_sources
asm asp autoit avisynth bash basic4gl bf blitzbasic bnf boo c caddcl
cadlisp cfdg cfm cil c_mac cobol cpp cpp-qt csharp css d delphi
diff _div dos dot eiffel email fortran freebasic genero gettext
glsl gml gnuplot groovy haskell hq9plus html4strict idl ini inno
intercal io java java5 javascript kixtart klonec klonecpp latex lisp
lolcode lotusformulas lotusscript lscript lua m68k make matlab mirc
modula3 mpasm mxml mysql nsis objc ocaml ocaml-brief oobas oracle11
oracle8 pascal per perl php php-brief pic16 pixelbender plsql povray
powershell progress prolog providex python qbasic rails reg robots
ruby sas scala scheme scilab sdlbasic smalltalk smarty sql tcl teraterm
text thinbasic tsql typoscript vb vbnet verilog vhdl vim visualfoxpro
visualprolog whitespace winbatch xml xorg_conf xpp z80
>;


=={{header|Phix}}==
$_ = slurp;
{{trans|D}}

<!--<syntaxhighlight lang="phix">(phixonline)-->
for @langs -> $l {
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
s:g [ '<' $l '>' ] = "<lang $l>";
<span style="color: #008080;">constant</span> <span style="color: #000000;">ltext</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">`_div abap actionscript actionscript3 ada apache
s:g [ '</' $l '>' ] = '</' ~ 'lang>';
applescript apt_sources asm asp autoit avisynth bash basic4gl bf
}
blitzbasic bnf boo c c_mac caddcl cadlisp cfdg cfm cil cobol cpp

cpp-qt csharp css d delphi diff dos dot eiffel email fortran
s:g [ '<code '(.+?) '>' (.*?) '</code>' ] = "<lang $0>{$1}</"~"lang>";
freebasic genero gettext glsl gml gnuplot groovy haskell hq9plus

html4strict idl ini inno intercal io java java5 javascript kixtart
.say;</lang>
klonec klonecpp latex lisp lolcode lotusformulas lotusscript
lscript lua m68k make matlab mirc modula3 mpasm mxml mysql nsis
objc ocaml ocaml-brief oobas oracle11 oracle8 pascal per perl php
php-brief pic16 pixelbender plsql povray powershell progress
prolog providex python qbasic r rails reg robots ruby sas scala
scheme scilab sdlbasic smalltalk smarty sql tcl teraterm text
thinbasic tsql typoscript vb vbnet verilog vhdl vim visualfoxpro
visualprolog whitespace winbatch xml xorg_conf xpp z80`</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">langs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ltext</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">fix_tags</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">text</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">langs</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">lang</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">langs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">openl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"&lt;%s&gt;"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">lang</span><span style="color: #0000FF;">}),</span>
<span style="color: #000000;">openc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"&lt;code %s&gt;"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">lang</span><span style="color: #0000FF;">}),</span>
<span style="color: #000000;">lopen</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"&lt;lang %s&gt;"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">lang</span><span style="color: #0000FF;">}),</span>
<span style="color: #000000;">closl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"&lt;/%s&gt;"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">lang</span><span style="color: #0000FF;">}),</span>
<span style="color: #000000;">closc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"&lt;/%s&gt;"</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"code"</span><span style="color: #0000FF;">}),</span>
<span style="color: #000000;">lclos</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"&lt;/%s&gt;"</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"lang"</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">text</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute_all</span><span style="color: #0000FF;">(</span><span style="color: #000000;">text</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">openl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">openc</span><span style="color: #0000FF;">,</span><span style="color: #000000;">closl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">closc</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">lopen</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lopen</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lclos</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lclos</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">text</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">test</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
lorem ipsum &lt;c&gt;some c code&lt;/c&gt;dolor sit amet, &lt;csharp&gt;some
csharp code&lt;/csharp&gt; consectetur adipisicing elit, &lt;code r&gt;
some r code &lt;/code&gt;sed do eiusmod tempor incididunt
"""</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fix_tags</span><span style="color: #0000FF;">(</span><span style="color: #000000;">test</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
lorem ipsum <lang c>some c code</lang>dolor sit amet, <lang csharp>some
csharp code</lang> consectetur adipisicing elit, <lang r>
some r code </lang>sed do eiusmod tempor incididunt
</pre>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
<syntaxhighlight lang="lisp">#!/usr/bin/picolisp /usr/lib/picolisp/lib.l


(let Lang '("ada" "awk" "c" "forth" "prolog" "python" "z80")
(let Lang '("ada" "awk" "c" "forth" "prolog" "python" "z80")
Line 577: Line 958:
(prin "</lang") )
(prin "</lang") )
(T (prin "<" S)) ) ) ) ) )
(T (prin "<" S)) ) ) ) ) )
(bye)</lang>
(bye)</syntaxhighlight>

=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang PureBasic>If Not OpenConsole()
<syntaxhighlight lang="basic">If Not OpenConsole()
End
End
ElseIf CountProgramParameters() <> 2
ElseIf CountProgramParameters() <> 2
Line 618: Line 1,000:
CloseFile(1)
CloseFile(1)
EndIf
EndIf
EndIf</lang>
EndIf</syntaxhighlight>


=={{header|Python}}==
=={{header|Python}}==


<lang python># coding: utf-8
<syntaxhighlight lang=python>
""" Rosetta Code task rosettacode.org/wiki/Rosetta_Code/Fix_code_tags """


import sys
from re import sub
import re


testtexts = [
langs = ['ada', 'cpp-qt', 'pascal', 'lscript', 'z80', 'visualprolog',
"""<lang AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
'html4strict', 'cil', 'objc', 'asm', 'progress', 'teraterm', 'hq9plus',
FileRead, text, %1%
'genero', 'tsql', 'email', 'pic16', 'tcl', 'apt_sources', 'io', 'apache',
langs = ada,awk,autohotkey,etc
'vhdl', 'avisynth', 'winbatch', 'vbnet', 'ini', 'scilab', 'ocaml-brief',
slang = /lang
'sas', 'actionscript3', 'qbasic', 'perl', 'bnf', 'cobol', 'powershell',
slang := "<" . slang . "/>"
'php', 'kixtart', 'visualfoxpro', 'mirc', 'make', 'javascript', 'cpp',
Loop, Parse, langs, `,
'sdlbasic', 'cadlisp', 'php-brief', 'rails', 'verilog', 'xml', 'csharp',
{
'actionscript', 'nsis', 'bash', 'typoscript', 'freebasic', 'dot',
tag1 = <%A_LoopField%>
'applescript', 'haskell', 'dos', 'oracle8', 'cfdg', 'glsl', 'lotusscript',
tag2 = </%A_LoopField%>
'mpasm', 'latex', 'sql', 'klonec', 'ruby', 'ocaml', 'smarty', 'python',
text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
'oracle11', 'caddcl', 'robots', 'groovy', 'smalltalk', 'diff', 'fortran',
text := RegExReplace(text, tag2, slang)
'cfm', 'lua', 'modula3', 'vb', 'autoit', 'java', 'text', 'scala',
text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
'lotusformulas', 'pixelbender', 'reg', '_div', 'whitespace', 'providex',
, "<lang $1>$2" . slang)
'asp', 'css', 'lolcode', 'lisp', 'inno', 'mysql', 'plsql', 'matlab',
}
'oobas', 'vim', 'delphi', 'xorg_conf', 'gml', 'prolog', 'bf', 'per',
FileAppend, % text, %2%
'scheme', 'mxml', 'd', 'basic4gl', 'm68k', 'gnuplot', 'idl', 'abap',
</lang>""",
'intercal', 'c_mac', 'thinbasic', 'java5', 'xpp', 'boo', 'klonecpp',
"""<lang perl>my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
'blitzbasic', 'eiffel', 'povray', 'c', 'gettext']
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext);


my $text = join "", <STDIN>;
slang = '/lang'
my $slang="/lang";
code='code'
for (@langs) {
$text =~ s|<$_>|<lang $_>|g;
$text =~ s|</$_>|<$slang>|g;
}


$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;
text = sys.stdin.read()


print $text;</lang>""",
for i in langs:
text = text.replace("<%s>" % i,"<lang %s>" % i)
"""<lang>HAI 1.3
text = text.replace("</%s>" % i, "<%s>" % slang)


I HAS A bottles ITZ 99 I HAS A plural ITZ "Z" I HAS A lyric ITZ "99 BOTTLZ OV BEER"
text = re.sub("(?s)<%s (.+?)>(.*?)</%s>"%(code,code), r"<lang \1>\2<%s>" % slang, text)


IM IN YR song
sys.stdout.write(text)

</lang>
VISIBLE lyric " ON TEH WALL"
VISIBLE lyric
VISIBLE "TAEK 1 DOWN, PAZ IT AROUN"
bottles R DIFF OF bottles AN 1
NOT bottles, O RLY?
YA RLY, VISIBLE "NO MOAR BOTTLZ OV BEER ON TEH WALL", GTFO
OIC
BOTH SAEM bottles AN 1, O RLY?
YA RLY, plural R ""
OIC
lyric R SMOOSH bottles " BOTTL" plural " OV BEER" MKAY
VISIBLE lyric " ON TEH WALL:)"
IM OUTTA YR song

KTHXBYE</lang>
"""]

for txt in testtexts:
text2 = sub(r'<lang\s+\"?([\w\d\s]+)\"?\s?>', r'<syntaxhighlight lang=\1>', txt)
text2 = sub(r'<lang\s*>', '<syntaxhighlight lang=text>', text2)
text2 = sub(r'</lang\s*>', '</syntax'+'highlight>', text2)
print(txt, '\n=>\n', text2)

</syntaxhighlight>{{out}}
<pre>
<lang AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
slang = /lang
slang := "<" . slang . "/>"
Loop, Parse, langs, `,
{
tag1 = <%A_LoopField%>
tag2 = </%A_LoopField%>
text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
text := RegExReplace(text, tag2, slang)
text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
, "<lang $1>$2" . slang)
}
FileAppend, % text, %2%
</lang>
=>
<syntaxhighlight lang=AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
slang = /lang
slang := "<" . slang . "/>"
Loop, Parse, langs, `,
{
tag1 = <%A_LoopField%>
tag2 = </%A_LoopField%>
text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
text := RegExReplace(text, tag2, slang)
text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
, "<lang $1>$2" . slang)
}
FileAppend, % text, %2%
</syntaxhighlight>
<lang perl>my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext);

my $text = join "", <STDIN>;
my $slang="/lang";
for (@langs) {
$text =~ s|<$_>|<lang $_>|g;
$text =~ s|</$_>|<$slang>|g;
}

$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;

print $text;</lang>
=>
<syntaxhighlight lang="perl">my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext);

my $text = join "", <STDIN>;
my $slang="/lang";
for (@langs) {
$text =~ s|<$_>|<lang $_>|g;
$text =~ s|</$_>|<$slang>|g;
}

$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;

print $text;</syntaxhighlight>
<lang>HAI 1.3

I HAS A bottles ITZ 99 I HAS A plural ITZ "Z" I HAS A lyric ITZ "99 BOTTLZ OV BEER"

IM IN YR song

VISIBLE lyric " ON TEH WALL"
VISIBLE lyric
VISIBLE "TAEK 1 DOWN, PAZ IT AROUN"
bottles R DIFF OF bottles AN 1
NOT bottles, O RLY?
YA RLY, VISIBLE "NO MOAR BOTTLZ OV BEER ON TEH WALL", GTFO
OIC
BOTH SAEM bottles AN 1, O RLY?
YA RLY, plural R ""
OIC
lyric R SMOOSH bottles " BOTTL" plural " OV BEER" MKAY
VISIBLE lyric " ON TEH WALL:)"
IM OUTTA YR song

KTHXBYE</lang>
=>
<syntaxhighlight lang="text">HAI 1.3

I HAS A bottles ITZ 99 I HAS A plural ITZ "Z" I HAS A lyric ITZ "99 BOTTLZ OV BEER"

IM IN YR song

VISIBLE lyric " ON TEH WALL"
VISIBLE lyric
VISIBLE "TAEK 1 DOWN, PAZ IT AROUN"
bottles R DIFF OF bottles AN 1
NOT bottles, O RLY?
YA RLY, VISIBLE "NO MOAR BOTTLZ OV BEER ON TEH WALL", GTFO
OIC
BOTH SAEM bottles AN 1, O RLY?
YA RLY, plural R ""
OIC
lyric R SMOOSH bottles " BOTTL" plural " OV BEER" MKAY
VISIBLE lyric " ON TEH WALL:)"
IM OUTTA YR song

KTHXBYE</syntaxhighlight>
</pre>


=={{header|R}}==
=={{header|R}}==
Note that the instances of ##### are to stop the wiki getting confused. Please remove them before running the code.
Note that the instances of ##### are to stop the wiki getting confused. Please remove them before running the code.
<syntaxhighlight lang="r">
<lang R>
fixtags <- function(page)
fixtags <- function(page)
{
{
Line 677: Line 1,232:
consectetur adipisicing elit,<code r>some r code</code>sed do eiusmod tempor incididunt"
consectetur adipisicing elit,<code r>some r code</code>sed do eiusmod tempor incididunt"
fixtags(page)
fixtags(page)
</syntaxhighlight>
</lang>


=={{header|Racket}}==
=={{header|Racket}}==


<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket


Line 702: Line 1,257:
[else all]))
[else all]))
(loop)))
(loop)))
</syntaxhighlight>
</lang>


=={{header|REXX}}==
=={{header|Raku}}==
(formerly Perl 6)
<lang rexx>/*REXX program fixes (changes) depreciated code tags with newer tags.*/
<syntaxhighlight lang="raku" line>my @langs = <
@ = '<' /*ensure RC doesn't get confused.*/
abap actionscript actionscript3 ada apache applescript apt_sources
old. = /*define a default value for OLD.*/
asm asp autoit avisynth bash basic4gl bf blitzbasic bnf boo c caddcl
old.1 = @'%s>' ; new.1 = @'lang %s>'
cadlisp cfdg cfm cil c_mac cobol cpp cpp-qt csharp css d delphi
old.2 = @'/%s>' ; new.2 = @'/lang>'
diff _div dos dot eiffel email fortran freebasic genero gettext
old.3 = @'code %s>' ; new.3 = @'lang %s>'
glsl gml gnuplot groovy haskell hq9plus html4strict idl ini inno
old.4 = @'/code>' ; new.4 = @'/lang>'
intercal io java java5 javascript kixtart klonec klonecpp latex lisp
lolcode lotusformulas lotusscript lscript lua m68k make matlab mirc
modula3 mpasm mxml mysql nsis objc ocaml ocaml-brief oobas oracle11
oracle8 pascal per perl php php-brief pic16 pixelbender plsql povray
powershell progress prolog providex python qbasic rails reg robots
ruby sas scala scheme scilab sdlbasic smalltalk smarty sql tcl teraterm
text thinbasic tsql typoscript vb vbnet verilog vhdl vim visualfoxpro
visualprolog whitespace winbatch xml xorg_conf xpp z80
>;


$_ = slurp;
ifid = 'Wikisource.txt' /* Input File IDentifier. */
ofid = 'converted.txt' /*Output " " */


for @langs -> $l {
do while lines(ifid) \== 0 /*keep trunkin' until it's done. */
s:g:i [ '<' 'lang '? $l '>' ] = "<lang $l>";
_=linein(ifid) /*read a record from input file. */
s:g [ '</' $l '>' ] = '</' ~ 'lang>';
}

s:g [ '<code '(.+?) '>' (.*?) '</code>' ] = "<lang $0>{$1}</"~"lang>";

.say;</syntaxhighlight>
<syntaxhighlight lang="raku" line>use v6;

constant @langs = < abap actionscript actionscript3 ada … >;

slurp().subst(
rx:r{
| '<' <( $<need-add-space>=<?> )> @langs '>'
| '</' <( @langs )> '>'
| '<' '/'? <( code )> [<.ws> @langs]? '>'
},
'lang' ~ " " x *<need-add-space>.so,
:g,
).print</syntaxhighlight>

=={{header|REXX}}==
<syntaxhighlight lang="rexx">/*REXX program fixes (changes) depreciated HTML code tags with newer tags. */
@="<"; old.=; old.1 = @'%s>' ; new.1 = @"lang %s>"
old.2 = @'/%s>' ; new.2 = @"/lang>"
old.3 = @'code %s>' ; new.3 = @"lang %s>"
old.4 = @'/code>' ; new.4 = @"/lang>"


do j=1 while old.j \== '' /*change old ──► new until done. */
iFID = 'Wikisource.txt' /*the Input File IDentifier. */
oFID = 'converted.txt' /*the Output " " */
_=changestr(new.j,_,old.j) /*let REXX do the heavy lifting. */
end /*j*/


call lineout ofid,_ /*write the re-formatted record. */
do while lines(iFID)\==0 /*keep reading the file until finished.*/
$= linein(iFID) /*read a record from the input file. */
end /*while lines(ifid...*/
/*stick a fork in it, we're done.*/</lang>
do j=1 while old.j \== '' /*change old ──► new until finished. */
$= changestr(old.j,$,new.j) /*let REXX do the heavy lifting. */
Some older REXXes don't have a '''changestr''' bif, so one is included here.
end /*j*/
<lang rexx>/*╔══════════════════════════════╗ CHANGESTR ╔═════════════════════════╗
call lineout oFID,$ /*write re-formatted record to output. */
╔═╩══════════════════════════════╝ function ╚═════════════════════════╩═╗
end /*while*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
║ new string to be used──────────┐ ┌─────limit of # changes (times)║
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, so one is included here &nbsp; ───► &nbsp; [[CHANGESTR.REX]].
║ original string (haystack)────────┐ │ │ [default: ≈ one billian]║
<br><br>
║ old string to be changed───┐ │ │ │ ┌───begin at this occurance #.║
║ {O, H, and N can be null.} │ │ │ │ │ [default: 1st occurrance]║
╚═╦════════════════════════════╗ │ │ │ │ │ ╔═══════════════════════╦═╝
╚════════════════════════════╝ ↓ ↓ ↓ ↓ ↓ ╚═══════════════════════╝*/
changestr: procedure; parse arg o,h,n,t,b /* T and B are optional.*/
$='' /*$: the returned string.*/
t=word(t 999999999 , 1) /*maybe use the default? */
b=word(b 1 , 1) /* " " " " */
w=length(o) /*length of OLD string.*/
if w==0 & t\=0 then return n || h /*changing a null char ? */
#=0 /*# of changed occurances*/
do j=1 until # >= t /*keep changing, T times.*/
parse var h y (o) _ +(w) h /*parse the string ... */
if _=='' then return $ || y /*no more left, return. */
if j<b then $=$ || y || o /*didn't meet begin at ? */
else do
$=$ || y || n /*build new STR from S. */
#=#+1 /*bump occurance number. */
end
end /*j*/
/*Most REXX BIFs only ···*/
return $ || h /* support three options.*/</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby># get all stdin in one string
<syntaxhighlight lang="ruby"># get all stdin in one string
#text = $stdin.read
#text = $stdin.read
# for testing, use
# for testing, use
Line 775: Line 1,342:
altera electram. Tota adhuc altera te sea, <code bar>soluta appetere ut mel</bar>.
altera electram. Tota adhuc altera te sea, <code bar>soluta appetere ut mel</bar>.
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</code>. Eam volumus perpetua
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</code>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus. </lang>
constituto id, mea an omittam fierent vituperatoribus. </syntaxhighlight>


<pre>Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
<pre>Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
Line 783: Line 1,350:
Quo quis graecis vivendo te, <lang baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
Quo quis graecis vivendo te, <lang baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus. </pre>
constituto id, mea an omittam fierent vituperatoribus. </pre>

=={{header|Rust}}==
<syntaxhighlight lang="rust">
extern crate regex;

use std::io;
use std::io::prelude::*;

use regex::Regex;

const LANGUAGES: &str =
"_div abap actionscript actionscript3 ada apache applescript apt_sources asm asp autoit \
avisynth bash basic4gl bf blitzbasic bnf boo c c_mac caddcl cadlisp cfdg cfm cil cobol cpp \
cpp-qt csharp css d delphi diff dos dot eiffel email fortran freebasic genero gettext glsl \
gml gnuplot groovy haskell hq9plus html4strict idl ini inno intercal io java java5 \
javascript kixtart klonec klonecpp latex lisp lolcode lotusformulas lotusscript lscript lua \
m68k make matlab mirc modula3 mpasm mxml mysql nsis objc ocaml ocaml-brief oobas oracle11 \
oracle8 pascal per perl php php-brief pic16 pixelbender plsql povray powershell progress \
prolog providex python qbasic rails reg robots ruby rust sas scala scheme scilab sdlbasic \
smalltalk smarty sql tcl teraterm text thinbasic tsql typoscript vb vbnet verilog vhdl vim \
visualfoxpro visualprolog whitespace winbatch xml xorg_conf xpp z80";

fn fix_tags(languages: &[&str], text: &str) -> String {
let mut replaced_text = text.to_owned();

for lang in languages.iter() {
let bad_open = Regex::new(&format!("<{lang}>|<code {lang}>", lang = lang)).unwrap();
let bad_close = Regex::new(&format!("</{lang}>|</code>", lang = lang)).unwrap();
let open = format!("<lang {}>", lang);
let close = "&lt;/lang&gt;";

replaced_text = bad_open.replace_all(&replaced_text, &open[..]).into_owned();
replaced_text = bad_close
.replace_all(&replaced_text, &close[..])
.into_owned();
}

replaced_text
}

fn main() {
let stdin = io::stdin();
let mut buf = String::new();
stdin.lock().read_to_string(&mut buf).unwrap();
println!(
"{}",
fix_tags(&LANGUAGES.split_whitespace().collect::<Vec<_>>(), &buf)
);
}

</syntaxhighlight>

=={{header|Scala}}==
===Single pass converter===
Tackles e.g. also multiple whitespaces.
{{Out}}
Experience it running in your browser at [https://scastie.scala-lang.org/5U6vqsOaTi6AU5FcqfA2lA Scastie (remote JVM)].
<syntaxhighlight lang="scala">object FixCodeTags extends App {
val rx = // See for regex explanation: https://regex101.com/r/N8X4x7/3/
// Flags ignore case, dot matching line breaks, unicode support
s"(?is)<(?:(?:code\\s+)?(${langs.mkString("|")}))>(.+?|)<\\/(?:code|\\1)>".r

def langs = // Real list of langs goes here
Seq("bar", "baz", "foo", "Scala", "உயிர்/Uyir", "Müller")

def markDown =
"""Lorem ipsum <Code foo>saepe audire</code> elaboraret ne quo, id equidem
|atomorum inciderint usu. <foo>In sit inermis deleniti percipit</foo>, ius
|ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
|altera electram. Tota adhuc altera te sea, <code bar>soluta appetere ut mel
|</bar>. Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</code>.
|Eam volumus perpetua constituto id, mea an omittam fierent vituperatoribus.
|Empty element: <Müller></Müller><scaLa></Scala><உயிர்/Uyir></உயிர்/Uyir>""".stripMargin

println(rx.replaceAllIn(markDown, _ match {
case rx(langName, langCode) => s"<lang ${langName.capitalize}>${langCode}<${"/lan"}g>"
})) // ${"/lan"}g is the <noWiki> escape.

}</syntaxhighlight>

=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var langs = %w(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext).join('|');

var text = ARGF.slurp;

text.gsub!(Regex.new('<(' + langs + ')>'), {|s1| "<lang #{s1}>" });
text.gsub!(Regex.new('</(' + langs + ')>'), "</" + "lang>");
text.gsub!(
Regex.new('<code\h+(' + langs + ')>(.*?)</code>', 's'),
{|s1,s2| "<lang #{s1}>#{s2}</" + "lang>"}
);

print text;</syntaxhighlight>


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>set langs {
<syntaxhighlight lang="tcl">set langs {
ada cpp-qt pascal lscript z80 visualprolog html4strict cil objc asm progress teraterm
ada cpp-qt pascal lscript z80 visualprolog html4strict cil objc asm progress teraterm
hq9plus genero tsql email pic16 tcl apt_sources io apache vhdl avisynth winbatch vbnet
hq9plus genero tsql email pic16 tcl apt_sources io apache vhdl avisynth winbatch vbnet
Line 816: Line 1,490:
lappend replacements "<code $lang>" "<lang $lang>"
lappend replacements "<code $lang>" "<lang $lang>"
}
}
set text [string map $replacements $text]</lang>
set text [string map $replacements $text]</syntaxhighlight>

=={{header|Wren}}==
{{libheader|Wren-pattern}}
Although it shouldn't make any difference for these examples, I've put the language name in quotes as that's what Pygments recommends.
<syntaxhighlight lang="wren">import "./pattern" for Pattern

var source1 = """
<lang AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
slang = /lang
slang := "<" . slang . "/>"
Loop, Parse, langs, `,
{
tag1 = <%A_LoopField%>
tag2 = </%A_LoopField%>
text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
text := RegExReplace(text, tag2, slang)
text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
, "<lang $1>$2" . slang)
}
FileAppend, % text, %2%
</lang>
"""

var source2 = """
<lang perl>my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext);

my $text = join "", <STDIN>;
my $slang="/lang";
for (@langs) {
$text =~ s|<$_>|<lang $_>|g;
$text =~ s|</$_>|<$slang>|g;
}

$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;

print $text;</lang>
"""

var source3 = """
<lang>HAI 1.3

I HAS A bottles ITZ 99 I HAS A plural ITZ "Z" I HAS A lyric ITZ "99 BOTTLZ OV BEER"

IM IN YR song

VISIBLE lyric " ON TEH WALL"
VISIBLE lyric
VISIBLE "TAEK 1 DOWN, PAZ IT AROUN"
bottles R DIFF OF bottles AN 1
NOT bottles, O RLY?
YA RLY, VISIBLE "NO MOAR BOTTLZ OV BEER ON TEH WALL", GTFO
OIC
BOTH SAEM bottles AN 1, O RLY?
YA RLY, plural R ""
OIC
lyric R SMOOSH bottles " BOTTL" plural " OV BEER" MKAY
VISIBLE lyric " ON TEH WALL:)"
IM OUTTA YR song

KTHXBYE</lang>
"""

var p = Pattern.new("<lang [+1^>]>")
var sh = "syntaxhighlight"
var repl = "<%(sh) lang=\"$1\">"
for (source in [source1, source2, source3]) {
source = p.replace(source, repl, 1, 0).
replace("<lang>", "<%(sh) lang=\"text\">").
replace("</lang>", "</%(sh)>")
System.print(source)
System.print()
}</syntaxhighlight>

{{out}}
Just showing the examples after updating.
<pre>
<syntaxhighlight lang="AutoHotkey">; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
slang = /lang
slang := "<" . slang . "/>"
Loop, Parse, langs, `,
{
tag1 = <%A_LoopField%>
tag2 = </%A_LoopField%>
text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
text := RegExReplace(text, tag2, slang)
text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
, "<lang $1>$2" . slang)
}
FileAppend, % text, %2%
</syntaxhighlight>

<syntaxhighlight lang="perl">my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext);

my $text = join "", <STDIN>;
my $slang="/lang";
for (@langs) {
$text =~ s|<$_>|<lang $_>|g;
$text =~ s|</$_>|<$slang>|g;
}

$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;

print $text;</syntaxhighlight>

<syntaxhighlight lang="text">HAI 1.3

I HAS A bottles ITZ 99 I HAS A plural ITZ "Z" I HAS A lyric ITZ "99 BOTTLZ OV BEER"

IM IN YR song

VISIBLE lyric " ON TEH WALL"
VISIBLE lyric
VISIBLE "TAEK 1 DOWN, PAZ IT AROUN"
bottles R DIFF OF bottles AN 1
NOT bottles, O RLY?
YA RLY, VISIBLE "NO MOAR BOTTLZ OV BEER ON TEH WALL", GTFO
OIC
BOTH SAEM bottles AN 1, O RLY?
YA RLY, plural R ""
OIC
lyric R SMOOSH bottles " BOTTL" plural " OV BEER" MKAY
VISIBLE lyric " ON TEH WALL:)"
IM OUTTA YR song

KTHXBYE</syntaxhighlight>
</pre>

=={{header|zkl}}==
<syntaxhighlight lang="zkl">fcn replace(data,src,dstpat){
re,n,buf:=RegExp(src),0,Data();
while(re.search(data,True,n)){
matched:=re.matched; // L(L(12,3),"c")
data[matched[0].xplode()]=re.sub(data,dstpat,buf); // "\1" --> "c"
n=matched[0].sum(0); // move past change
}
}
data:=File.stdin.read();
foreach src,dst in (T(
T(0'|<(\w+)>|, 0'|<lang \1>|), T(0'|</(\w+)>|,"</" "lang>"),
T(0'|<code (\w+)>|,0'|<lang \1>|) )){
replace(data,src,dst)
}
print(data.text);</syntaxhighlight>
Note: the "</" "lang>" to keep /lang the wiki from getting confused (it is string concatenation).
{{out}}
<pre>
$ cat wikisource.txt
lorem ipsum <c>some c code</c>dolor sit amet, <csharp>some
csharp code</csharp> consectetur adipisicing elit, <code r>
some r code </code>sed do eiusmod tempor incididunt

$ zkl bbb < wikisource.txt
lorem ipsum <lang c>some c code</lang>dolor sit amet, <lang csharp>some
csharp code</lang> consectetur adipisicing elit, <lang r>
some r code </lang>sed do eiusmod tempor incididunt
</pre>


{{omit from|Brlcad}}
{{omit from|Brlcad}}

Latest revision as of 17:00, 3 February 2024

Task
Rosetta Code/Fix code tags
You are encouraged to solve this task according to the task description, using any language you may know.
This task has been revised for the hosting change Rosetta Code has had in August 2022.
See the Python example for a new example to fit the revised task.
Task

Fix Rosetta Code deprecated code tags, with these rules:

Change <lang %s> to <syntaxhighlight lang=%s>
Change </lang> to </syntaxhighlight>
Change <lang> to <syntaxhighlight lang=text>
Demonstrate the task on these examples
<lang AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
slang = /lang
slang := "<" . slang . "/>"
Loop, Parse, langs, `,
{
  tag1 = <%A_LoopField%>
  tag2 = </%A_LoopField%>
  text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
  text := RegExReplace(text, tag2, slang)
  text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
          , "<lang $1>$2" . slang)
}
FileAppend, % text, %2%
</lang>
<lang perl>my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext);

my $text = join "", <STDIN>;
my $slang="/lang";
for (@langs) {
    $text =~ s|<$_>|<lang $_>|g;
    $text =~ s|</$_>|<$slang>|g;
}

$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;

print $text;</lang>
<lang>HAI 1.3

I HAS A bottles ITZ 99 I HAS A plural ITZ "Z" I HAS A lyric ITZ "99 BOTTLZ OV BEER"

IM IN YR song

   VISIBLE lyric " ON TEH WALL"
   VISIBLE lyric
   VISIBLE "TAEK 1 DOWN, PAZ IT AROUN"
   bottles R DIFF OF bottles AN 1
   NOT bottles, O RLY?
       YA RLY, VISIBLE "NO MOAR BOTTLZ OV BEER ON TEH WALL", GTFO
   OIC
   BOTH SAEM bottles AN 1, O RLY?
       YA RLY, plural R ""
   OIC
   lyric R SMOOSH bottles " BOTTL" plural " OV BEER" MKAY
   VISIBLE lyric " ON TEH WALL:)"
IM OUTTA YR song

KTHXBYE</lang>



11l

Translation of: Java
V languages = [‘abap’, ‘actionscript’, ‘actionscript3’,
               ‘ada’, ‘apache’, ‘applescript’, ‘apt_sources’, ‘asm’, ‘asp’,
               ‘autoit’, ‘avisynth’, ‘bar’, ‘bash’, ‘basic4gl’, ‘bf’,
               ‘blitzbasic’, ‘bnf’, ‘boo’, ‘c’, ‘caddcl’, ‘cadlisp’, ‘cfdg’,
               ‘cfm’, ‘cil’, ‘c_mac’, ‘cobol’, ‘cpp’, ‘cpp-qt’, ‘csharp’, ‘css’,
               ‘d’, ‘delphi’, ‘diff’, ‘_div’, ‘dos’, ‘dot’, ‘eiffel’, ‘email’,
               ‘foo’, ‘fortran’, ‘freebasic’, ‘genero’, ‘gettext’, ‘glsl’, ‘gml’,
               ‘gnuplot’, ‘go’, ‘groovy’, ‘haskell’, ‘hq9plus’, ‘html4strict’,
               ‘idl’, ‘ini’, ‘inno’, ‘intercal’, ‘io’, ‘java’, ‘java5’,
               ‘javascript’, ‘kixtart’, ‘klonec’, ‘klonecpp’, ‘latex’, ‘lisp’,
               ‘lolcode’, ‘lotusformulas’, ‘lotusscript’, ‘lscript’, ‘lua’,
               ‘m68k’, ‘make’, ‘matlab’, ‘mirc’, ‘modula3’, ‘mpasm’, ‘mxml’,
               ‘mysql’, ‘nsis’, ‘objc’, ‘ocaml’, ‘ocaml-brief’, ‘oobas’,
               ‘oracle11’, ‘oracle8’, ‘pascal’, ‘per’, ‘perl’, ‘php’, ‘php-brief’,
               ‘pic16’, ‘pixelbender’, ‘plsql’, ‘povray’, ‘powershell’,
               ‘progress’, ‘prolog’, ‘providex’, ‘python’, ‘qbasic’, ‘rails’,
               ‘reg’, ‘robots’, ‘ruby’, ‘sas’, ‘scala’, ‘scheme’, ‘scilab’,
               ‘sdlbasic’, ‘smalltalk’, ‘smarty’, ‘sql’, ‘tcl’, ‘teraterm’,
               ‘text’, ‘thinbasic’, ‘tsql’, ‘typoscript’, ‘vb’, ‘vbnet’,
               ‘verilog’, ‘vhdl’, ‘vim’, ‘visualfoxpro’, ‘visualprolog’,
               ‘whitespace’, ‘winbatch’, ‘xml’, ‘xorg_conf’, ‘xpp’, ‘z80’]

V text =
‘Lorem ipsum <code foo>saepe audire</code> elaboraret ne quo, id equidem
atomorum inciderint usu. <foo>In sit inermis deleniti percipit</foo>,
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea, <code bar>soluta appetere ut mel</bar>.
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</code>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.’

L(lang) languages
   text = text.replace(‘<’lang‘>’, ‘<lang ’lang‘>’)
   text = text.replace(‘</’lang‘>’, ‘</’""‘lang>’)
   text = text.replace(‘<code ’lang‘>’, ‘<lang ’lang‘>’)
text = text.replace(‘</code>’, ‘</’""‘lang>’)

print(text)
Output:
Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
atomorum inciderint usu. <lang foo>In sit inermis deleniti percipit</lang>,
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</lang>.
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.

AutoHotkey

; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
slang = /lang
slang := "<" . slang . "/>"
Loop, Parse, langs, `,
{
  tag1 = <%A_LoopField%>
  tag2 = </%A_LoopField%>
  text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
  text := RegExReplace(text, tag2, slang)
  text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
          , "<lang $1>$2" . slang)
}
FileAppend, % text, %2%

D

Translation of: Python
import std.stdio, std.regex, std.string, std.array;

immutable langs = "_div abap actionscript actionscript3 ada apache
applescript apt_sources asm asp autoit avisynth bash basic4gl bf
blitzbasic bnf boo c c_mac caddcl cadlisp cfdg cfm cil cobol cpp
cpp-qt csharp css d delphi diff dos dot eiffel email fortran
freebasic genero gettext glsl gml gnuplot groovy haskell hq9plus
html4strict idl ini inno intercal io java java5 javascript kixtart
klonec klonecpp latex lisp lolcode lotusformulas lotusscript
lscript lua m68k make matlab mirc modula3 mpasm mxml mysql nsis
objc ocaml ocaml-brief oobas oracle11 oracle8 pascal per perl php
php-brief pic16 pixelbender plsql povray powershell progress
prolog providex python qbasic rails reg robots ruby sas scala
scheme scilab sdlbasic smalltalk smarty sql tcl teraterm text
thinbasic tsql typoscript vb vbnet verilog vhdl vim visualfoxpro
visualprolog whitespace winbatch xml xorg_conf xpp z80".split;

string fixTags(string text) {
    static immutable slang = "/lang";
    static immutable code = "code";

    foreach (immutable lang; langs) {
        text = text.replace("<%s>".format(lang),
                            "<lang %s>".format(lang));
        text = text.replace("</%s>".format(lang),
                            "<%s>".format(slang));
    }

    return text.replace("<%s (.+?)>(.*?)</%s>"
                        .format(code, code).regex("g"),
                        "<lang $1>$2<%s>".format(slang));
}

void main() {
    ("lorem ipsum <c>some c code</c>dolor sit amet, <csharp>some " ~
    "csharp code</csharp> consectetur adipisicing elit, <code r>" ~
    " some r code </code>sed do eiusmod tempor incididunt")
    .fixTags
    .writeln;
}
Output:
lorem ipsum <lang c>some c code</lang>dolor sit amet, <lang csharp>some csharp code</lang> consectetur adipisicing elit, <lang r> some r code </lang>sed do eiusmod tempor incididunt

Delphi

Translation of: Go
program Fix_code_tags;

{$APPTYPE CONSOLE}

uses
  Winapi.Windows,
  System.SysUtils,
  System.Classes,
  System.RegularExpressions;

const
  LANGS: array of string = ['abap', 'actionscript', 'actionscript3', 'ada',
    'apache', 'applescript', 'apt_sources', 'asm', 'asp', 'autoit', 'avisynth',
    'bash', 'basic4gl', 'bf', 'blitzbasic', 'bnf', 'boo', 'c', 'caddcl',
    'cadlisp', 'cfdg', 'cfm', 'cil', 'c_mac', 'cobol', 'cpp', 'cpp-qt', 'csharp',
    'css', 'd', 'delphi', 'diff', '_div', 'dos', 'dot', 'eiffel', 'email',
    'fortran', 'freebasic', 'genero', 'gettext', 'glsl', 'gml', 'gnuplot', 'go',
    'groovy', 'haskell', 'hq9plus', 'html4strict', 'idl', 'ini', 'inno',
    'intercal', 'io', 'java', 'java5', 'javascript', 'kixtart', 'klonec',
    'klonecpp', 'latex', 'lisp', 'lolcode', 'lotusformulas', 'lotusscript',
    'lscript', 'lua', 'm68k', 'make', 'matlab', 'mirc', 'modula3', 'mpasm',
    'mxml', 'mysql', 'nsis', 'objc', 'ocaml', 'ocaml-brief', 'oobas', 'oracle11',
    'oracle8', 'pascal', 'per', 'perl', 'php', 'php-brief', 'pic16',
    'pixelbender', 'plsql', 'povray', 'powershell', 'progress', 'prolog',
    'providex', 'python', 'qbasic', 'rails', 'reg', 'robots', 'ruby', 'sas',
    'scala', 'scheme', 'scilab', 'sdlbasic', 'smalltalk', 'smarty', 'sql', 'tcl',
    'teraterm', 'text', 'thinbasic', 'tsql', 'typoscript', 'vb', 'vbnet',
    'verilog', 'vhdl', 'vim', 'visualfoxpro', 'visualprolog', 'whitespace',
    'winbatch', 'xml', 'xorg_conf', 'xpp', 'z80'];

procedure repl(const Match: TMatch; var code: ansistring);

  procedure replace(_new: ansistring);
  begin
    code := StringReplace(code, Match.Value, '<' + _new + '>', [rfReplaceAll]);
  end;

begin
  if Match.Value = '/code' then
  begin
    replace('/lang');
    exit;
  end;

  if Match.Value = '</code>' then
  begin
    replace('/lang');
    exit;
  end;

  var mid := Lowercase(Trim(copy(Match.Value, 2, length(Match.Value) - 2)));

  for var lg in LANGS do
  begin
    if mid = lg then
    begin
      replace(format('lang %s', [lg]));
      exit;
    end;

    if mid[1] = '/' then
    begin
      replace('/lang');
      exit;
    end;

    if (pos('code', mid) = 1) and (copy(mid, 6, length(mid) - 2) = lg) then
    begin
      replace(format('lang %s', [lg]));
      exit;
    end;
  end;
end;

function Lang(input: ansistring): ansistring;
var
  reg: Tregex;
  match: TMatch;
begin
  reg := TRegex.Create('<[^>]+>');
  Result := input;
  for match in reg.Matches(input) do
    Repl(match, Result);
end;

function ReadAll: Ansistring;
var
  Buffer: array[0..1000] of byte;
  StdIn: TStream;
  Count: Integer;
  buf: Ansistring;
begin
  StdIn := THandleStream.Create(GetStdHandle(STD_INPUT_HANDLE));
  Result := '';
  while True do
  begin
    Count := StdIn.Read(Buffer, 1000);
    if Count = 0 then
      Break;
    SetLength(buf, Count);
    CopyMemory(@buf[1], @Buffer[0], Count);
    Result := Result + buf;
  end;
  StdIn.Free;
end;

procedure Fix();
var
  input, output: ansistring;
begin
  input := ReadAll;
  output := Lang(input);
  writeln(output);
end;

begin
  Fix;
end.

Erlang

Commented away are 3 lines that would create a dict of the existing languages on Rosetta Code. Since the examples have 3 imaginary code tags, I replaced that code this way.

#! /usr/bin/env escript
-module( fix_code_tags ).
-mode( compile ).

main( _ ) ->
	File_lines = loop( io:get_line(""), [] ),
	Code_fixed_lines = fix_code_tag( binary:list_to_bin(File_lines) ),
%	true = code:add_pathz( "ebin" ),
%	ok = find_unimplemented_tasks:init(),
%	Dict = dict:from_list( [dict_tuple(X) || X <- rank_languages_by_popularity:rosettacode_languages()],
	Dict = dict:from_list( [dict_tuple(X) || X <- ["foo", "bar", "baz"]] ),
	{_Dict, All_fixed_lines} = lists:foldl( fun fix_language_tag/2, {Dict, Code_fixed_lines}, dict:fetch_keys(Dict) ),
	io:fwrite( "~s", [binary:bin_to_list(All_fixed_lines)] ).



dict_tuple( Language ) -> {binary:list_to_bin(string:to_lower(Language)), binary:list_to_bin(Language)}.

fix_code_tag( Binary ) ->
	Avoid_wiki = binary:list_to_bin( [<<"<">>, <<"lang ">>] ),
	Code_fixed_lines = binary:replace( Binary, <<"<code ">>, Avoid_wiki, [global] ),
	Avoid_wiki_again = binary:list_to_bin( [<<"</">>, <<"lang ">>] ),
	binary:replace( Code_fixed_lines, <<"</code>">>, Avoid_wiki_again, [global] ).

fix_language_tag( Language_key, {Dict, Binary} ) ->
	Language = fix_language_tag_rosettacode_language( Language_key, dict:find(Language_key, Dict) ),
	Language_start_old = binary:list_to_bin( [<<"<">>, Language, <<">">>] ),
	Language_start_new = binary:list_to_bin( [<<"<">>, <<"lang ">>, Language, <<">">>] ),
	Fixed_lines = binary:replace( Binary, Language_start_old, Language_start_new, [global] ),
	Language_stop_old = binary:list_to_bin( [<<"</">>, Language, <<">">>] ),
	Language_stop_new = binary:list_to_bin( [<<"</">>, <<"lang>">>] ),
	{Dict, binary:replace( Fixed_lines, Language_stop_old, Language_stop_new, [global] )}.

fix_language_tag_rosettacode_language( _Language_key, {ok, Language} ) -> Language;
fix_language_tag_rosettacode_language( Language_key, error ) -> Language_key.

loop( eof, Acc ) -> lists:reverse( Acc );
loop( Line, Acc ) -> loop( io:get_line(""), [Line | Acc] ).
Output:
% cat fix_code_tags
Lorem ipsum <code foo>saepe audire</code> elaboraret ne quo, id equidem
atomorum inciderint usu. <foo>In sit inermis deleniti percipit</foo>,
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea, <code bar>soluta appetere ut mel</bar>.
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</code>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.
% fix_code_tags.escript < fix_code_tags
Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
atomorum inciderint usu. <lang foo>In sit inermis deleniti percipit</lang>,
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</lang>.
Quo quis graecis vivendo te, <lang baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.

F#

While the ubiquitous loop over languages approach can be used, here we capture all tag variations to fix in one dotNet regex.

open System
open System.Text.RegularExpressions

[<EntryPoint>]
let main argv =
    let langs = [| "foo"; "foo 2"; "bar"; "baz" |];    // An array of (pseudo) languages we handle
    let regexStringAlternationOfLanguageNames = String.Join("|", (Array.map Regex.Escape langs))
    let regexForOldLangSyntax =
        new Regex(String.Format("""
            <                   # Opening of a tag.
            (                   # Group alternation of 2 cases
                (                   # Group for alternation of 2 cases with a language name
                    (?<CloseMarker>/)   # Might be a closing tag,
                    |                   # Or
                    code\s              # an old <code ...> tag 
                )?                  # End of alternation; optional
                \b(?<Lang>{0})\b    # Followed by the captured Language alternation
            |                   # Or
                (?<CloseMarker>/code)# An old </code> end tag
            )                   # End of group
            >                   # The final tag closing
            """, regexStringAlternationOfLanguageNames),
            RegexOptions.IgnorePatternWhitespace ||| RegexOptions.ExplicitCapture)

    let replaceEvaluator (m : Match) =
        if m.Groups.Item("CloseMarker").Length > 0 then "</" + "lang>"
        else "<lang " + m.Groups.Item("Lang").Value + ">"

    printfn "%s" (regexForOldLangSyntax.Replace(Console.In.ReadToEnd(), replaceEvaluator))
    0

Output

>Rosetta.exe
<foo 2>...</foo 2>
<code bar>...</code>
^Z
<lang foo 2>...</lang>
<lang bar>...</lang>

Go

package main

import "fmt"
import "io/ioutil"
import "log"
import "os"
import "regexp"
import "strings"

func main() {
	err := fix()
	if err != nil {
		log.Fatalln(err)
	}
}

func fix() (err error) {
	buf, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return err
	}
	out, err := Lang(string(buf))
	if err != nil {
		return err
	}
	fmt.Println(out)
	return nil
}

func Lang(in string) (out string, err error) {
	reg := regexp.MustCompile("<[^>]+>")
	out = reg.ReplaceAllStringFunc(in, repl)
	return out, nil
}

func repl(in string) (out string) {
	if in == "</code>" {
		// Change </code> to </ lang>
		return "</"+"lang>"
	}

	// mid is the content in between '<' and '>'.
	mid := in[1 : len(in)-1]

	// thanks, random lua guy
	var langs = []string{
		"abap", "actionscript", "actionscript3", "ada", "apache", "applescript",
		"apt_sources", "asm", "asp", "autoit", "avisynth", "bash", "basic4gl",
		"bf", "blitzbasic", "bnf", "boo", "c", "caddcl", "cadlisp", "cfdg", "cfm",
		"cil", "c_mac", "cobol", "cpp", "cpp-qt", "csharp", "css", "d", "delphi",
		"diff", "_div", "dos", "dot", "eiffel", "email", "fortran", "freebasic",
		"genero", "gettext", "glsl", "gml", "gnuplot", "go", "groovy", "haskell",
		"hq9plus", "html4strict", "idl", "ini", "inno", "intercal", "io", "java",
		"java5", "javascript", "kixtart", "klonec", "klonecpp", "latex", "lisp",
		"lolcode", "lotusformulas", "lotusscript", "lscript", "lua", "m68k",
		"make", "matlab", "mirc", "modula3", "mpasm", "mxml", "mysql", "nsis",
		"objc", "ocaml", "ocaml-brief", "oobas", "oracle11", "oracle8", "pascal",
		"per", "perl", "php", "php-brief", "pic16", "pixelbender", "plsql",
		"povray", "powershell", "progress", "prolog", "providex", "python",
		"qbasic", "rails", "reg", "robots", "ruby", "sas", "scala", "scheme",
		"scilab", "sdlbasic", "smalltalk", "smarty", "sql", "tcl", "teraterm",
		"text", "thinbasic", "tsql", "typoscript", "vb", "vbnet", "verilog",
		"vhdl", "vim", "visualfoxpro", "visualprolog", "whitespace", "winbatch",
		"xml", "xorg_conf", "xpp", "z80",
	}
	for _, lang := range langs {
		if mid == lang {
			// Change <%s> to <lang %s>
			return fmt.Sprintf("<lang %s>", lang)
		}

		if strings.HasPrefix(mid, "/") {
			if mid[len("/"):] == lang {
				// Change </%s> to </ lang>
				return "</"+"lang>"
			}
		}

		if strings.HasPrefix(mid, "code ") {
			if mid[len("code "):] == lang {
				// Change <code %s> to <lang %s>
				return fmt.Sprintf("<lang %s>", lang)
			}
		}
	}

	return in
}

J

Solution:

require 'printf strings files'

langs=. <;._1 LF -.~ noun define   NB. replace with real lang strings
 foo bar
 baz
)
patterns=. noun define
<%s>|<lang %s>|
</%s>|</lang>|
<code %s>|<lang %s>|
</code>|</lang>|
)
fixCodeTags=: rplc&(, <;._2;._2 &> patterns vbsprintf _5]\ 5#langs)

Example Usage:

   SampleText=: noun define
Lorem ipsum <code foo>saepe audire</code> elaboraret ne quo, id equidem
atomorum inciderint usu. <foo>In sit inermis deleniti percipit</foo>,
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea, <code bar>soluta appetere ut mel</bar>.
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</code>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.
)

   fixCodeTags SampleText
Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
atomorum inciderint usu. <lang foo>In sit inermis deleniti percipit</lang>,
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</lang>.
Quo quis graecis vivendo te, <lang baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.

Reading/writing file:

   'converted.txt' fwrite~ fixCodeTags fread 'wikisource.txt'

Java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class FixCodeTags 
{
	public static void main(String[] args)
	{
		String sourcefile=args[0];
		String convertedfile=args[1];
		convert(sourcefile,convertedfile);
	}
		static String[] languages = {"abap", "actionscript", "actionscript3",
			"ada", "apache", "applescript", "apt_sources", "asm", "asp",
			"autoit", "avisynth", "bar", "bash", "basic4gl", "bf",
			"blitzbasic", "bnf", "boo", "c", "caddcl", "cadlisp", "cfdg",
			"cfm", "cil", "c_mac", "cobol", "cpp", "cpp-qt", "csharp", "css",
			"d", "delphi", "diff", "_div", "dos", "dot", "eiffel", "email",
			"foo", "fortran", "freebasic", "genero", "gettext", "glsl", "gml",
			"gnuplot", "go", "groovy", "haskell", "hq9plus", "html4strict",
			"idl", "ini", "inno", "intercal", "io", "java", "java5",
			"javascript", "kixtart", "klonec", "klonecpp", "latex", "lisp",
			"lolcode", "lotusformulas", "lotusscript", "lscript", "lua",
			"m68k", "make", "matlab", "mirc", "modula3", "mpasm", "mxml",
			"mysql", "nsis", "objc", "ocaml", "ocaml-brief", "oobas",
			"oracle11", "oracle8", "pascal", "per", "perl", "php", "php-brief",
			"pic16", "pixelbender", "plsql", "povray", "powershell",
			"progress", "prolog", "providex", "python", "qbasic", "rails",
			"reg", "robots", "ruby", "sas", "scala", "scheme", "scilab",
			"sdlbasic", "smalltalk", "smarty", "sql", "tcl", "teraterm",
			"text", "thinbasic", "tsql", "typoscript", "vb", "vbnet",
			"verilog", "vhdl", "vim", "visualfoxpro", "visualprolog",
			"whitespace", "winbatch", "xml", "xorg_conf", "xpp", "z80"};
	static void convert(String sourcefile,String convertedfile)
	{
		try
		{
			BufferedReader br=new BufferedReader(new FileReader(sourcefile));
			//String buffer to store contents of the file
			StringBuffer sb=new StringBuffer("");
			String line;
			while((line=br.readLine())!=null)
			{
				for(int i=0;i<languages.length;i++)
				{
					String lang=languages[i];
					line=line.replaceAll("<"+lang+">", "<lang "+lang+">");
					line=line.replaceAll("</"+lang+">", "</"+"lang>");
					line=line.replaceAll("<code "+lang+">", "<lang "+lang+">");
					line=line.replaceAll("</code>", "</"+"lang>");
				}
				sb.append(line);
			}
			br.close();
			
			FileWriter fw=new FileWriter(new File(convertedfile));
			//Write entire string buffer into the file
			fw.write(sb.toString());
			fw.close();
		}
		catch (Exception e)
		{
			System.out.println("Something went horribly wrong: "+e.getMessage());
		}
	}
}

Example:

user@ubuntu:~$ cat fix.in
Lorem ipsum <code foo>saepe audire</code> elaboraret ne quo, id equidem
atomorum inciderint usu. <foo>In sit inermis deleniti percipit</foo>,
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea, <code bar>soluta appetere ut mel</bar>.
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</code>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.

user@ubuntu:~$ java FixCodeTags fix.in fix.out
user@ubuntu:~$ cat fix.out
Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
atomorum inciderint usu. <lang foo>In sit inermis deleniti percipit</lang>,
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</lang>.
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.

JavaScript

Works with: SpiderMonkey
var langs = ['foo', 'bar', 'baz']; // real list of langs goes here
var end_tag = '</'+'lang>';

var line;
while (line = readline()) {
    line = line.replace(new RegExp('</code>', 'gi'), end_tag);
    for (var i = 0; i < langs.length; i++)
        line = line.replace(new RegExp('<(?:code )?(' + langs[i] + ')>', 'gi'), '<lang $1>')
                   .replace(new RegExp('</' + langs[i] + '>', 'gi'), end_tag);
    print(line);
}

Julia

Works with: Julia version 0.6
Translation of: Python
function fixtags(text::AbstractString)
    langs = ["ada", "cpp-qt", "pascal", "lscript", "z80", "visualprolog",
            "html4strict", "cil", "objc", "asm", "progress", "teraterm", "hq9plus",
            "genero", "tsql", "email", "pic16", "tcl", "apt_sources", "io", "apache",
            "vhdl", "avisynth", "winbatch", "vbnet", "ini", "scilab", "ocaml-brief",
            "sas", "actionscript3", "qbasic", "perl", "bnf", "cobol", "powershell",
            "php", "kixtart", "visualfoxpro", "mirc", "make", "javascript", "cpp",
            "sdlbasic", "cadlisp", "php-brief", "rails", "verilog", "xml", "csharp",
            "actionscript", "nsis", "bash", "typoscript", "freebasic", "dot",
            "applescript", "haskell", "dos", "oracle8", "cfdg", "glsl", "lotusscript",
            "mpasm", "latex", "sql", "klonec", "ruby", "ocaml", "smarty", "python",
            "oracle11", "caddcl", "robots", "groovy", "smalltalk", "diff", "fortran",
            "cfm", "lua", "modula3", "vb", "autoit", "java", "text", "scala",
            "lotusformulas", "pixelbender", "reg", "_div", "whitespace", "providex",
            "asp", "css", "lolcode", "lisp", "inno", "mysql", "plsql", "matlab",
            "oobas", "vim", "delphi", "xorg_conf", "gml", "prolog", "bf", "per",
            "scheme", "mxml", "d", "basic4gl", "m68k", "gnuplot", "idl", "abap",
            "intercal", "c_mac", "thinbasic", "java5", "xpp", "boo", "klonecpp",
            "blitzbasic", "eiffel", "povray", "c", "gettext"]

    slang = "/lang"
    code  = "code"

    for l in langs
            text = replace(text, "<$l>","<lang $l>")
            text = replace(text, "</$l>", "<$slang>")
    end

    text = replace(text, Regex("(?s)<$code (.+?)>(.*?)</$code>"), "<lang \\1>\\2<$slang>")
end

const txt = readstring(ARGS[1])
println(fixtags(txt))

Lua

--thanks, random python guy
langs = {'ada', 'cpp-qt', 'pascal', 'lscript', 'z80', 'visualprolog',
'html4strict', 'cil', 'objc', 'asm', 'progress', 'teraterm', 'hq9plus',
'genero', 'tsql', 'email', 'pic16', 'tcl', 'apt_sources', 'io', 'apache',
'vhdl', 'avisynth', 'winbatch', 'vbnet', 'ini', 'scilab', 'ocaml-brief',
'sas', 'actionscript3', 'qbasic', 'perl', 'bnf', 'cobol', 'powershell',
'php', 'kixtart', 'visualfoxpro', 'mirc', 'make', 'javascript', 'cpp',
'sdlbasic', 'cadlisp', 'php-brief', 'rails', 'verilog', 'xml', 'csharp',
'actionscript', 'nsis', 'bash', 'typoscript', 'freebasic', 'dot',
'applescript', 'haskell', 'dos', 'oracle8', 'cfdg', 'glsl', 'lotusscript',
'mpasm', 'latex', 'sql', 'klonec', 'ruby', 'ocaml', 'smarty', 'python',
'oracle11', 'caddcl', 'robots', 'groovy', 'smalltalk', 'diff', 'fortran',
'cfm', 'lua', 'modula3', 'vb', 'autoit', 'java', 'text', 'scala',
'lotusformulas', 'pixelbender', 'reg', '_div', 'whitespace', 'providex',
'asp', 'css', 'lolcode', 'lisp', 'inno', 'mysql', 'plsql', 'matlab',
'oobas', 'vim', 'delphi', 'xorg_conf', 'gml', 'prolog', 'bf', 'per',
'scheme', 'mxml', 'd', 'basic4gl', 'm68k', 'gnuplot', 'idl', 'abap',
'intercal', 'c_mac', 'thinbasic', 'java5', 'xpp', 'boo', 'klonecpp',
'blitzbasic', 'eiffel', 'povray', 'c', 'gettext'}

for line in io.lines() do
  for i, v in ipairs(langs) do
	line = line:gsub("<" .. v .. ">", "<lang " .. v .. ">")
    line = line:gsub("<code " .. v .. ">", "<lang " .. v .. ">")
    line = line:gsub("</" .. v .. ">", "</" .. "lang>")  --the weird concatenation is to prevent the markup from breaking
    line = line:gsub("</" .. "code>", "</" .. "lang>")
  end
  print(line)
end

Maple

#Used <#/lang> to desensitize wiki
txt := FileTools[Text][ReadFile]("C:/Users/username/Desktop/text.txt"):
#langs should be a real list of programming languages 
langs := ['foo', 'bar', 'baz', 'barf'];
for lan in langs do
	txt := StringTools:-SubstituteAll(txt, cat("<", lan, ">"), cat ("<lang ", lan, ">")):
	txt := StringTools:-SubstituteAll(txt, cat("</", lan, ">"), "<#/lang>"):
	txt := StringTools:-SubstituteAll(txt, cat("<code ", lan, ">"), cat ("<lang ", lan, ">")):
	txt := StringTools:-SubstituteAll(txt, "</code>", "<#/lang>"):
end do;
print(txt);
Output:
"Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
atomorum inciderint usu. <lang foo>In sit inermis deleniti percipit</lang>,
ius ex tale civibus omittam. <lang barf>Vix ut doctus cetero invenire</lang>, his eu
altera electram. Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</lang>.
Quo quis graecis vivendo te, <lang baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus. "

Mathematica/Wolfram Language

StringReplace[Import["wikisource.txt"],
{"</"~~Shortest[__]~~">"->"</lang>",
("<code "|"<")~~Shortest[x__]~~">"->"<lang "~~ x~~">"}
]>>"converted.txt"
Output:
Lorem ipsum<lang foo>saepe audire</lang>elaboraret ne quo,id equidem
atomorum inciderint usu. <lang foo>In sit inermis deleniti percipit</lang>,
ius ex tale civibus omittam. <lang barf>Vix ut doctus cetero invenire</lang>,
his eualtera electram.Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</lang>.
Quo quis graecis vivendo te, <lang baz>posse nullam lobortis ex usu</lang>.
Eam volumus perpetua constituto id,mea an omittam fierent vituperatoribus

Nim

Translation of: Python
import re, strutils

const
  Langs = ["ada", "cpp-qt", "pascal", "lscript", "z80", "visualprolog",
           "html4strict", "cil", "objc", "asm", "progress", "teraterm", "hq9plus",
           "genero", "tsql", "email", "pic16", "tcl", "apt_sources", "io", "apache",
           "vhdl", "avisynth", "winbatch", "vbnet", "ini", "scilab", "ocaml-brief",
           "sas", "actionscript3", "qbasic", "perl", "bnf", "cobol", "powershell",
           "php", "kixtart", "visualfoxpro", "mirc", "make", "javascript", "cpp",
           "sdlbasic", "cadlisp", "php-brief", "rails", "verilog", "xml", "csharp",
           "actionscript", "nsis", "bash", "typoscript", "freebasic", "dot",
           "applescript", "haskell", "dos", "oracle8", "cfdg", "glsl", "lotusscript",
           "mpasm", "latex", "sql", "klonec", "ruby", "ocaml", "smarty", "python",
           "oracle11", "caddcl", "robots", "groovy", "smalltalk", "diff", "fortran",
           "cfm", "lua", "modula3", "vb", "autoit", "java", "text", "scala",
           "lotusformulas", "pixelbender", "reg", "_div", "whitespace", "providex",
           "asp", "css", "lolcode", "lisp", "inno", "mysql", "plsql", "matlab",
           "oobas", "vim", "delphi", "xorg_conf", "gml", "prolog", "bf", "per",
           "scheme", "mxml", "d", "basic4gl", "m68k", "gnuplot", "idl", "abap",
           "intercal", "c_mac", "thinbasic", "java5", "xpp", "boo", "klonecpp",
           "blitzbasic", "eiffel", "povray", "c", "gettext"]

var text = stdin.readAll()

for lang in Langs:
  text = text.replace("<$#>" % lang, "<lang $#>" % lang)
  text = text.replace("</$#>" % lang, "</lang>")

text = text.replacef(re"(?s)<code (.+?)>(.*?)</code>", r"<lang $1>$2</lang>")

stdout.write text

OCaml

#load "str.cma"

let langs = 
  Str.split (Str.regexp " ")
    "actionscript ada algol68 amigae applescript autohotkey awk bash basic \
     befunge bf c cfm cobol cpp csharp d delphi e eiffel factor false forth \
     fortran fsharp haskell haxe j java javascript lisaac lisp logo lua m4 \
     mathematica maxscript modula3 moo objc ocaml octave oz pascal perl \
     raku php pike pop11 powershell prolog python qbasic r rebol ruby \
     scala scheme slate smalltalk tcl ti89b vbnet vedit"

let read_in ic =
  let buf = Buffer.create 16384
  and tmp = String.create 4096 in
  let rec aux() =
    let bytes = input ic tmp 0 4096 in
    if bytes > 0 then begin
      Buffer.add_substring buf tmp 0 bytes;
      aux()
    end
  in
  (try aux() with End_of_file -> ());
  (Buffer.contents buf)

let repl pat tpl str =
  let reg = Str.regexp_string_case_fold pat in
  let str = Str.global_replace reg tpl str in
  (str)

(* change <%s> to <lang %s> *)
let repl1 lang str =
  let pat = "<" ^ lang ^ ">"
  and tpl = "<lang " ^ lang ^ ">" in
  (repl pat tpl str)

(* change </%s> to </la\ng> *)
let repl2 lang str =
  let pat = "</" ^ lang ^ ">"
  and tpl = "</lang"^">" in
  (repl pat tpl str)

(* change <code %s> to <lang %s> *)
let repl3 lang str =
  let pat = "<code " ^ lang ^ ">"
  and tpl = "<lang " ^ lang ^ ">" in
  (repl pat tpl str)

(* change </code> to </la\ng> *)
let repl4 lang str =
  let pat = "</code>"
  and tpl = "</lang"^">" in
  (repl pat tpl str)


let () =
  print_string (
    List.fold_left (fun str lang ->
        (repl4 lang (repl3 lang (repl2 lang (repl1 lang str))))
      ) (read_in stdin) langs)

(in the code the strings </lang> have been split in order to not confuse the wiki)

this line of code:

        (repl4 lang (repl3 lang (repl2 lang (repl1 lang str))))

could also be written like this:

        List.fold_right (fun repl -> repl lang) [repl1; repl2; repl3; repl4] str


Here we implement the read_in function to read all the content from an input channel because there is no such function in the standard library, but we can use the extLib which provides the function Std.input_all (in its module Std).

Perl

my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext);

my $text = join "", <STDIN>;
my $slang="/lang";
for (@langs) {
    $text =~ s|<$_>|<lang $_>|g;
    $text =~ s|</$_>|<$slang>|g;
}

$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;

print $text;

Phix

Translation of: D
with javascript_semantics
constant ltext = `_div abap actionscript actionscript3 ada apache
applescript apt_sources asm asp autoit avisynth bash basic4gl bf
blitzbasic bnf boo c c_mac caddcl cadlisp cfdg cfm cil cobol cpp
cpp-qt csharp css d delphi diff dos dot eiffel email fortran
freebasic genero gettext glsl gml gnuplot groovy haskell hq9plus
html4strict idl ini inno intercal io java java5 javascript kixtart
klonec klonecpp latex lisp lolcode lotusformulas lotusscript
lscript lua m68k make matlab mirc modula3 mpasm mxml mysql nsis
objc ocaml ocaml-brief oobas oracle11 oracle8 pascal per perl php
php-brief pic16 pixelbender plsql povray powershell progress
prolog providex python qbasic r rails reg robots ruby sas scala
scheme scilab sdlbasic smalltalk smarty sql tcl teraterm text
thinbasic tsql typoscript vb vbnet verilog vhdl vim visualfoxpro
visualprolog whitespace winbatch xml xorg_conf xpp z80`,
         langs = split(substitute(ltext,"\n"," "))
 
function fix_tags(string text)
    for i=1 to length(langs) do
        string lang = langs[i],
               openl = sprintf("<%s>",{lang}),
               openc = sprintf("<code %s>",{lang}),
               lopen = sprintf("<lang %s>",{lang}),
               closl = sprintf("</%s>",{lang}),
               closc = sprintf("</%s>",{"code"}),
               lclos = sprintf("</%s>",{"lang"})
        text = substitute_all(text,{openl,openc,closl,closc},
                                   {lopen,lopen,lclos,lclos})
    end for
    return text
end function
 
constant test = """
lorem ipsum <c>some c code</c>dolor sit amet, <csharp>some 
csharp code</csharp> consectetur adipisicing elit, <code r> 
some r code </code>sed do eiusmod tempor incididunt
"""
puts(1,fix_tags(test))
Output:
lorem ipsum <lang c>some c code</lang>dolor sit amet, <lang csharp>some
csharp code</lang> consectetur adipisicing elit, <lang r>
some r code </lang>sed do eiusmod tempor incididunt

PicoLisp

#!/usr/bin/picolisp /usr/lib/picolisp/lib.l

(let Lang '("ada" "awk" "c" "forth" "prolog" "python" "z80")
   (in NIL
      (while (echo "<")
         (let S (till ">" T)
            (cond
               ((pre? "code " S) (prin "<lang" (cddddr (chop S))))
               ((member S Lang) (prin "<lang " S))
               ((= S "/code") (prin "</lang"))
               ((and (pre? "/" S) (member (pack (cdr (chop S))) Lang))
                  (prin "</lang") )
               (T (prin "<" S)) ) ) ) ) )
(bye)

PureBasic

If Not OpenConsole()
  End 
ElseIf CountProgramParameters() <> 2
  PrintN("Usage: "+GetFilePart(ProgramFilename())+" InFile OutFile")
  End 
EndIf

Define Infile$ =ProgramParameter(), Outfile$=ProgramParameter()
If ReadFile(0,Infile$)
  NewList Out$()
  Define line$, part$, new$, pos1, pos2
  While Not Eof(0)
    line$=ReadString(0): pos2=0
    Repeat
      pos1=FindString(line$,"<",pos2)
      pos2=FindString(line$,">",pos1)
      If pos1 And pos2
        part$=Mid(line$,pos1+1,pos2-pos1-1)
        If Mid(part$,1,1)="/"
          new$="<"+"/lang>" ; Line split to avoid problem forum coding
        ElseIf Mid(part$,1,5)="code "
          new$="<lang "+Mid(part$,6)+">"
        Else
          new$="<lang "+part$+">"
        EndIf
        line$=ReplaceString(line$,"<"+part$+">",new$)
      Else
        Break   
      EndIf
    ForEver
    AddElement(Out$()): Out$()=line$
  Wend
  CloseFile(0)
  If CreateFile(1, Outfile$)
    ForEach Out$()
      WriteStringN(1,Out$())
    Next
    CloseFile(1)
  EndIf
EndIf

Python

""" Rosetta Code task rosettacode.org/wiki/Rosetta_Code/Fix_code_tags """

from re import sub

testtexts = [
"""<lang AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
slang = /lang
slang := "<" . slang . "/>"
Loop, Parse, langs, `,
{
  tag1 = <%A_LoopField%>
  tag2 = </%A_LoopField%>
  text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
  text := RegExReplace(text, tag2, slang)
  text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
          , "<lang $1>$2" . slang)
}
FileAppend, % text, %2%
</lang>""",
    """<lang perl>my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext);

my $text = join "", <STDIN>;
my $slang="/lang";
for (@langs) {
    $text =~ s|<$_>|<lang $_>|g;
    $text =~ s|</$_>|<$slang>|g;
}

$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;

print $text;</lang>""",
    """<lang>HAI 1.3

I HAS A bottles ITZ 99 I HAS A plural ITZ "Z" I HAS A lyric ITZ "99 BOTTLZ OV BEER"

IM IN YR song

   VISIBLE lyric " ON TEH WALL"
   VISIBLE lyric
   VISIBLE "TAEK 1 DOWN, PAZ IT AROUN"
   bottles R DIFF OF bottles AN 1
   NOT bottles, O RLY?
       YA RLY, VISIBLE "NO MOAR BOTTLZ OV BEER ON TEH WALL", GTFO
   OIC
   BOTH SAEM bottles AN 1, O RLY?
       YA RLY, plural R ""
   OIC
   lyric R SMOOSH bottles " BOTTL" plural " OV BEER" MKAY
   VISIBLE lyric " ON TEH WALL:)"
IM OUTTA YR song

KTHXBYE</lang>
"""]

for txt in testtexts:
    text2 = sub(r'<lang\s+\"?([\w\d\s]+)\"?\s?>', r'<syntaxhighlight lang=\1>', txt)
    text2 = sub(r'<lang\s*>', '<syntaxhighlight lang=text>', text2)
    text2 = sub(r'</lang\s*>', '</syntax'+'highlight>', text2)
    print(txt, '\n=>\n', text2)
Output:
<lang AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
slang = /lang
slang := "<" . slang . "/>"
Loop, Parse, langs, `,
{
  tag1 = <%A_LoopField%>
  tag2 = </%A_LoopField%>
  text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
  text := RegExReplace(text, tag2, slang)
  text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
          , "<lang $1>$2" . slang)
}
FileAppend, % text, %2%
</lang> 
=>
 <syntaxhighlight lang=AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
slang = /lang
slang := "<" . slang . "/>"
Loop, Parse, langs, `,
{
  tag1 = <%A_LoopField%>
  tag2 = </%A_LoopField%>
  text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
  text := RegExReplace(text, tag2, slang)
  text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
          , "<lang $1>$2" . slang)
}
FileAppend, % text, %2%
</syntaxhighlight>
<lang perl>my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext);

my $text = join "", <STDIN>;
my $slang="/lang";
for (@langs) {
    $text =~ s|<$_>|<lang $_>|g;
    $text =~ s|</$_>|<$slang>|g;
}

$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;

print $text;</lang> 
=>
 <syntaxhighlight lang="perl">my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext);

my $text = join "", <STDIN>;
my $slang="/lang";
for (@langs) {
    $text =~ s|<$_>|<lang $_>|g;
    $text =~ s|</$_>|<$slang>|g;
}

$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;

print $text;</syntaxhighlight>
<lang>HAI 1.3

I HAS A bottles ITZ 99 I HAS A plural ITZ "Z" I HAS A lyric ITZ "99 BOTTLZ OV BEER"

IM IN YR song

   VISIBLE lyric " ON TEH WALL"
   VISIBLE lyric
   VISIBLE "TAEK 1 DOWN, PAZ IT AROUN"
   bottles R DIFF OF bottles AN 1
   NOT bottles, O RLY?
       YA RLY, VISIBLE "NO MOAR BOTTLZ OV BEER ON TEH WALL", GTFO
   OIC
   BOTH SAEM bottles AN 1, O RLY?
       YA RLY, plural R ""
   OIC
   lyric R SMOOSH bottles " BOTTL" plural " OV BEER" MKAY
   VISIBLE lyric " ON TEH WALL:)"
IM OUTTA YR song

KTHXBYE</lang>
 
=>
 <syntaxhighlight lang="text">HAI 1.3

I HAS A bottles ITZ 99 I HAS A plural ITZ "Z" I HAS A lyric ITZ "99 BOTTLZ OV BEER"

IM IN YR song

   VISIBLE lyric " ON TEH WALL"
   VISIBLE lyric
   VISIBLE "TAEK 1 DOWN, PAZ IT AROUN"
   bottles R DIFF OF bottles AN 1
   NOT bottles, O RLY?
       YA RLY, VISIBLE "NO MOAR BOTTLZ OV BEER ON TEH WALL", GTFO
   OIC
   BOTH SAEM bottles AN 1, O RLY?
       YA RLY, plural R ""
   OIC
   lyric R SMOOSH bottles " BOTTL" plural " OV BEER" MKAY
   VISIBLE lyric " ON TEH WALL:)"
IM OUTTA YR song

KTHXBYE</syntaxhighlight>

R

Note that the instances of ##### are to stop the wiki getting confused. Please remove them before running the code.

fixtags <- function(page)
{
   langs <- c("c", "c-sharp", "r")   # a complete list is required, obviously
   langs <- paste(langs, collapse="|")
   page <- gsub(paste("<(", langs, ")>", sep=""), "<lang \\1>", page)
   page <- gsub(paste("</(", langs, ")>", sep=""), "</#####lang>", page) 
   page <- gsub(paste("<code(", langs, ")>", sep=""), "<lang \\1>", page)
   page <- gsub(paste("</code>", sep=""), "</#####lang>", page)
   page
}

page <- "lorem ipsum <c>some c code</c>dolor sit amet,<c-sharp>some c-sharp code</c-sharp>
consectetur adipisicing elit,<code r>some r code</code>sed do eiusmod tempor incididunt"
fixtags(page)

Racket

#lang racket

(define lang-names '("X" "Y" "Z"))

(define rx
  (regexp (string-join lang-names "|"
                       #:before-first "<((/?(?:code)?)(?:( )?("
                       #:after-last "))?)>")))

(let loop () ; does all in a single scan
  (define m (regexp-match rx (current-input-port) 0 #f (current-output-port)))
  (when m
    (define-values [all pfx space lang] (apply values (cdr m)))
    (printf "<~a>"
      (cond [(not lang) (if (equal? pfx #"/code") #"/lang" all)]
            [space (if (equal? pfx #"code") (bytes-append #"lang " lang) all)]
            [(equal? pfx #"") (bytes-append #"lang " lang)]
            [(equal? pfx #"/") #"/lang"]
            [else all]))
    (loop)))

Raku

(formerly Perl 6)

my @langs = <
    abap actionscript actionscript3 ada apache applescript apt_sources
    asm asp autoit avisynth bash basic4gl bf blitzbasic bnf boo c caddcl
    cadlisp cfdg cfm cil c_mac cobol cpp cpp-qt csharp css d delphi
    diff _div dos dot eiffel email fortran freebasic genero gettext
    glsl gml gnuplot groovy haskell hq9plus html4strict idl ini inno
    intercal io java java5 javascript kixtart klonec klonecpp latex lisp
    lolcode lotusformulas lotusscript lscript lua m68k make matlab mirc
    modula3 mpasm mxml mysql nsis objc ocaml ocaml-brief oobas oracle11
    oracle8 pascal per perl php php-brief pic16 pixelbender plsql povray
    powershell progress prolog providex python qbasic rails reg robots
    ruby sas scala scheme scilab sdlbasic smalltalk smarty sql tcl teraterm
    text thinbasic tsql typoscript vb vbnet verilog vhdl vim visualfoxpro
    visualprolog whitespace winbatch xml xorg_conf xpp z80
>;

$_ = slurp;

for @langs -> $l {
    s:g:i [ '<' 'lang '?  $l '>' ] = "<lang $l>";
    s:g [ '</' $l '>' ] = '</' ~ 'lang>';
}

s:g [ '<code '(.+?) '>' (.*?) '</code>' ] = "<lang $0>{$1}</"~"lang>";

.say;
use v6;

constant @langs = < abap actionscript actionscript3 ada  … >;

slurp().subst(
    rx:r{
        | '<' <( $<need-add-space>=<?> )> @langs '>'
        | '</' <( @langs )> '>'
        | '<' '/'? <( code )> [<.ws> @langs]? '>'
    },
    'lang' ~ " " x *<need-add-space>.so,
    :g,
).print

REXX

/*REXX program  fixes (changes)  depreciated  HTML  code tags  with  newer tags.        */
@="<";  old.=;        old.1 = @'%s>'         ;     new.1 = @"lang %s>"
                      old.2 = @'/%s>'        ;     new.2 = @"/lang>"
                      old.3 = @'code %s>'    ;     new.3 = @"lang %s>"
                      old.4 = @'/code>'      ;     new.4 = @"/lang>"

iFID = 'Wikisource.txt'                          /*the  Input File  IDentifier.         */
oFID = 'converted.txt'                           /*the Output   "      "                */

  do  while lines(iFID)\==0                      /*keep reading the file until finished.*/
  $= linein(iFID)                                /*read a record from the input file.   */
                     do j=1  while old.j \== ''  /*change old ──► new  until  finished. */
                     $= changestr(old.j,$,new.j) /*let REXX do the heavy lifting.       */
                     end   /*j*/
  call lineout oFID,$                            /*write re-formatted record to output. */
  end   /*while*/                                /*stick a fork in it,  we're all done. */

Some older REXXes don't have a   changestr   BIF, so one is included here   ───►   CHANGESTR.REX.

Ruby

# get all stdin in one string
#text = $stdin.read
# for testing, use
text = DATA.read
slash_lang = '/lang'
langs = %w(foo bar baz) # actual list of languages declared here
for lang in langs
  text.gsub!(Regexp.new("<(#{lang})>")) {"<lang #$1>"}
  text.gsub!(Regexp.new("</#{lang}>"), "<#{slash_lang}>")
end
text.gsub!(/<code (.*?)>/, '<lang \1>')
text.gsub!(/<\/code>/, "<#{slash_lang}>")
print text

__END__
Lorem ipsum <code foo>saepe audire</code> elaboraret ne quo, id equidem
atomorum inciderint usu. <foo>In sit inermis deleniti percipit</foo>,
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea, <code bar>soluta appetere ut mel</bar>.
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</code>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.
Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
atomorum inciderint usu. <lang foo>In sit inermis deleniti percipit</lang>,
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</lang>.
Quo quis graecis vivendo te, <lang baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus. 

Rust

extern crate regex;

use std::io;
use std::io::prelude::*;

use regex::Regex;

const LANGUAGES: &str =
    "_div abap actionscript actionscript3 ada apache applescript apt_sources asm asp autoit \
     avisynth bash basic4gl bf blitzbasic bnf boo c c_mac caddcl cadlisp cfdg cfm cil cobol cpp \
     cpp-qt csharp css d delphi diff dos dot eiffel email fortran freebasic genero gettext glsl \
     gml gnuplot groovy haskell hq9plus html4strict idl ini inno intercal io java java5 \
     javascript kixtart klonec klonecpp latex lisp lolcode lotusformulas lotusscript lscript lua \
     m68k make matlab mirc modula3 mpasm mxml mysql nsis objc ocaml ocaml-brief oobas oracle11 \
     oracle8 pascal per perl php php-brief pic16 pixelbender plsql povray powershell progress \
     prolog providex python qbasic rails reg robots ruby rust sas scala scheme scilab sdlbasic \
     smalltalk smarty sql tcl teraterm text thinbasic tsql typoscript vb vbnet verilog vhdl vim \
     visualfoxpro visualprolog whitespace winbatch xml xorg_conf xpp z80";

fn fix_tags(languages: &[&str], text: &str) -> String {
    let mut replaced_text = text.to_owned();

    for lang in languages.iter() {
        let bad_open = Regex::new(&format!("<{lang}>|<code {lang}>", lang = lang)).unwrap();
        let bad_close = Regex::new(&format!("</{lang}>|</code>", lang = lang)).unwrap();
        let open = format!("<lang {}>", lang);
        let close = "&lt;/lang&gt;";

        replaced_text = bad_open.replace_all(&replaced_text, &open[..]).into_owned();
        replaced_text = bad_close
            .replace_all(&replaced_text, &close[..])
            .into_owned();
    }

    replaced_text
}

fn main() {
    let stdin = io::stdin();
    let mut buf = String::new();
    stdin.lock().read_to_string(&mut buf).unwrap();
    println!(
        "{}",
        fix_tags(&LANGUAGES.split_whitespace().collect::<Vec<_>>(), &buf)
    );
}

Scala

Single pass converter

Tackles e.g. also multiple whitespaces.

Output:

Experience it running in your browser at Scastie (remote JVM).

object FixCodeTags extends App {
  val rx = // See for regex explanation: https://regex101.com/r/N8X4x7/3/
           // Flags ignore case, dot matching line breaks, unicode support
    s"(?is)<(?:(?:code\\s+)?(${langs.mkString("|")}))>(.+?|)<\\/(?:code|\\1)>".r

  def langs = // Real list of langs goes here
    Seq("bar", "baz", "foo", "Scala", "உயிர்/Uyir", "Müller")

  def markDown =
    """Lorem ipsum <Code foo>saepe audire</code> elaboraret ne quo, id equidem
      |atomorum inciderint usu. <foo>In sit inermis deleniti percipit</foo>, ius
      |ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
      |altera electram. Tota adhuc altera te sea, <code bar>soluta appetere ut mel
      |</bar>. Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</code>.
      |Eam volumus perpetua constituto id, mea an omittam fierent vituperatoribus.
      |Empty element: <Müller></Müller><scaLa></Scala><உயிர்/Uyir></உயிர்/Uyir>""".stripMargin

  println(rx.replaceAllIn(markDown, _ match {
    case rx(langName, langCode) => s"<lang ${langName.capitalize}>${langCode}<${"/lan"}g>"
  })) // ${"/lan"}g is the <noWiki> escape.

}

Sidef

var langs = %w(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext).join('|');

var text = ARGF.slurp;

text.gsub!(Regex.new('<(' + langs + ')>'), {|s1| "<lang #{s1}>" });
text.gsub!(Regex.new('</(' + langs + ')>'), "</" + "lang>");
text.gsub!(
    Regex.new('<code\h+(' + langs + ')>(.*?)</code>', 's'),
    {|s1,s2| "<lang #{s1}>#{s2}</" + "lang>"}
);

print text;

Tcl

set langs {
    ada cpp-qt pascal lscript z80 visualprolog html4strict cil objc asm progress teraterm
    hq9plus genero tsql email pic16 tcl apt_sources io apache vhdl avisynth winbatch vbnet
    ini scilab ocaml-brief sas actionscript3 qbasic perl bnf cobol powershell php kixtart
    visualfoxpro mirc make javascript cpp sdlbasic cadlisp php-brief rails verilog xml
    csharp actionscript nsis bash typoscript freebasic dot applescript haskell dos oracle8
    cfdg glsl lotusscript mpasm latex sql klonec ruby ocaml smarty python oracle11 caddcl
    robots groovy smalltalk diff fortran cfm lua modula3 vb autoit java text scala lotusformulas
    pixelbender reg _div whitespace providex asp css lolcode lisp inno mysql plsql matlab
    oobas vim delphi xorg_conf gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl
    abap intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel povray c gettext
}

set text [read stdin]
set slang /lang
foreach lang $langs {
    set text [regsub -all "<$lang>" $text "<lang $lang>"]
    set text [regsub -all "</$lang>" $text "<$slang>"]
}
set text [regsub -all "<code (.+?)>(.+?)</code>" $text "<lang \\1>\\2<$slang>"]</lang>
Alternatively, for foreach loop may be replaced with:
<lang tcl>set text [regexp -all "<([join $langs |])>" $text {<lang \1>}]
set text [regexp -all "</(?:[join $langs |])>" $text "<$slang>"]</lang>

This task does not require regular expressions at all:
<lang tcl>set replacements [list </code> <$slang>]
foreach lang $langs {
    lappend replacements "<$lang>" "<lang $lang>"
    lappend replacements "</$lang>" "<$slang>"
    lappend replacements "<code $lang>" "<lang $lang>"
}
set text [string map $replacements $text]

Wren

Library: Wren-pattern

Although it shouldn't make any difference for these examples, I've put the language name in quotes as that's what Pygments recommends.

import "./pattern" for Pattern

var source1 = """
<lang AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
slang = /lang
slang := "<" . slang . "/>"
Loop, Parse, langs, `,
{
  tag1 = <%A_LoopField%>
  tag2 = </%A_LoopField%>
  text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
  text := RegExReplace(text, tag2, slang)
  text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
          , "<lang $1>$2" . slang)
}
FileAppend, % text, %2%
</lang>
"""

var source2 = """
<lang perl>my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext);

my $text = join "", <STDIN>;
my $slang="/lang";
for (@langs) {
    $text =~ s|<$_>|<lang $_>|g;
    $text =~ s|</$_>|<$slang>|g;
}

$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;

print $text;</lang>
"""

var source3 = """
<lang>HAI 1.3

I HAS A bottles ITZ 99 I HAS A plural ITZ "Z" I HAS A lyric ITZ "99 BOTTLZ OV BEER"

IM IN YR song

   VISIBLE lyric " ON TEH WALL"
   VISIBLE lyric
   VISIBLE "TAEK 1 DOWN, PAZ IT AROUN"
   bottles R DIFF OF bottles AN 1
   NOT bottles, O RLY?
       YA RLY, VISIBLE "NO MOAR BOTTLZ OV BEER ON TEH WALL", GTFO
   OIC
   BOTH SAEM bottles AN 1, O RLY?
       YA RLY, plural R ""
   OIC
   lyric R SMOOSH bottles " BOTTL" plural " OV BEER" MKAY
   VISIBLE lyric " ON TEH WALL:)"
IM OUTTA YR song

KTHXBYE</lang>
"""

var p = Pattern.new("<lang [+1^>]>")
var sh = "syntaxhighlight"
var repl = "<%(sh) lang=\"$1\">"
for (source in [source1, source2, source3]) {
    source = p.replace(source, repl, 1, 0).
             replace("<lang>", "<%(sh) lang=\"text\">").
             replace("</lang>", "</%(sh)>")
    System.print(source)
    System.print()
}
Output:

Just showing the examples after updating.

<syntaxhighlight lang="AutoHotkey">; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
slang = /lang
slang := "<" . slang . "/>"
Loop, Parse, langs, `,
{
  tag1 = <%A_LoopField%>
  tag2 = </%A_LoopField%>
  text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
  text := RegExReplace(text, tag2, slang)
  text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
          , "<lang $1>$2" . slang)
}
FileAppend, % text, %2%
</syntaxhighlight>

<syntaxhighlight lang="perl">my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
vbnet ini scilab ocaml-brief sas actionscript3 qbasic perl bnf
cobol powershell php kixtart visualfoxpro mirc make javascript
cpp sdlbasic cadlisp php-brief rails verilog xml csharp
actionscript nsis bash typoscript freebasic dot applescript
haskell dos oracle8 cfdg glsl lotusscript mpasm latex sql klonec
ruby ocaml smarty python oracle11 caddcl robots groovy smalltalk
diff fortran cfm lua modula3 vb autoit java text scala
lotusformulas pixelbender reg _div whitespace providex asp css
lolcode lisp inno mysql plsql matlab oobas vim delphi xorg_conf
gml prolog bf per scheme mxml d basic4gl m68k gnuplot idl abap
intercal c_mac thinbasic java5 xpp boo klonecpp blitzbasic eiffel
povray c gettext);

my $text = join "", <STDIN>;
my $slang="/lang";
for (@langs) {
    $text =~ s|<$_>|<lang $_>|g;
    $text =~ s|</$_>|<$slang>|g;
}

$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;

print $text;</syntaxhighlight>

<syntaxhighlight lang="text">HAI 1.3

I HAS A bottles ITZ 99 I HAS A plural ITZ "Z" I HAS A lyric ITZ "99 BOTTLZ OV BEER"

IM IN YR song

   VISIBLE lyric " ON TEH WALL"
   VISIBLE lyric
   VISIBLE "TAEK 1 DOWN, PAZ IT AROUN"
   bottles R DIFF OF bottles AN 1
   NOT bottles, O RLY?
       YA RLY, VISIBLE "NO MOAR BOTTLZ OV BEER ON TEH WALL", GTFO
   OIC
   BOTH SAEM bottles AN 1, O RLY?
       YA RLY, plural R ""
   OIC
   lyric R SMOOSH bottles " BOTTL" plural " OV BEER" MKAY
   VISIBLE lyric " ON TEH WALL:)"
IM OUTTA YR song

KTHXBYE</syntaxhighlight>

zkl

fcn replace(data,src,dstpat){
   re,n,buf:=RegExp(src),0,Data();
   while(re.search(data,True,n)){
      matched:=re.matched;	// L(L(12,3),"c")
      data[matched[0].xplode()]=re.sub(data,dstpat,buf);  // "\1" --> "c"
      n=matched[0].sum(0);  // move past change
   }
}
data:=File.stdin.read();
foreach src,dst in (T(
     T(0'|<(\w+)>|,     0'|<lang \1>|), T(0'|</(\w+)>|,"</" "lang>"),
     T(0'|<code (\w+)>|,0'|<lang \1>|) )){
   replace(data,src,dst)
}
print(data.text);

Note: the "</" "lang>" to keep /lang the wiki from getting confused (it is string concatenation).

Output:
$ cat wikisource.txt 
lorem ipsum <c>some c code</c>dolor sit amet, <csharp>some 
csharp code</csharp> consectetur adipisicing elit, <code r>
   some r code </code>sed do eiusmod tempor incididunt

$ zkl bbb < wikisource.txt  
lorem ipsum <lang c>some c code</lang>dolor sit amet, <lang csharp>some 
csharp code</lang> consectetur adipisicing elit, <lang r>
   some r code </lang>sed do eiusmod tempor incididunt