Rosetta Code/Fix code tags: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(16 intermediate revisions by 7 users not shown)
Line 1:
{{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:
<pre>
Change <lang %s> to <langsyntaxhighlight lang=%s>
Change </%slang> to </langsyntaxhighlight>
Change <code %slang> to <langsyntaxhighlight %slang=text>
Change </code> to </lang>
</pre>
 
;Demonstrate the task on these examples:
<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>
</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;
 
print $text;</lang>
</pre>
<pre>
<lang>HAI 1.3
./convert.py < wikisource.txt > converted.txt
 
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}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
Line 34 ⟶ 152:
}
FileAppend, % text, %2%
</syntaxhighlight>
</lang>
 
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.regex, std.string, std.array;
 
immutable langs = "_div abap actionscript actionscript3 ada apache
Line 77 ⟶ 195:
.fixTags
.writeln;
}</langsyntaxhighlight>
{{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>
Line 86 ⟶ 204:
{{libheader| System.RegularExpressions}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Fix_code_tags;
 
Line 204 ⟶ 322:
begin
Fix;
end.</langsyntaxhighlight>
 
=={{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.
<syntaxhighlight lang="erlang">
<lang Erlang>
#! /usr/bin/env escript
-module( fix_code_tags ).
Line 247 ⟶ 365:
loop( eof, Acc ) -> lists:reverse( Acc );
loop( Line, Acc ) -> loop( io:get_line(""), [Line | Acc] ).
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 268 ⟶ 386:
=={{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.
<langsyntaxhighlight lang="fsharp">open System
open System.Text.RegularExpressions
 
Line 297 ⟶ 415:
 
printfn "%s" (regexForOldLangSyntax.Replace(Console.In.ReadToEnd(), replaceEvaluator))
0</langsyntaxhighlight>
Output
<pre>&gt;Rosetta.exe
Line 307 ⟶ 425:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 394 ⟶ 512:
 
return in
}</langsyntaxhighlight>
 
=={{header|J}}==
'''Solution:'''
<langsyntaxhighlight lang="j">require 'printf strings files'
 
langs=. <;._1 LF -.~ noun define NB. replace with real lang strings
foo bar
baz
)</langsyntaxhighlight>
<pre>patterns=. noun define
<%s>|<lang %s>|
Line 410 ⟶ 528:
</code>|</lang>|
)</pre>
<langsyntaxhighlight lang="j">fixCodeTags=: rplc&(, <;._2;._2 &> patterns vbsprintf _5]\ 5#langs)</langsyntaxhighlight>
 
'''Example Usage:'''
Line 430 ⟶ 548:
constituto id, mea an omittam fierent vituperatoribus.</pre>
Reading/writing file:
<langsyntaxhighlight lang="j"> 'converted.txt' fwrite~ fixCodeTags fread 'wikisource.txt'</langsyntaxhighlight>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
Line 481 ⟶ 599:
String lang=languages[i];
line=line.replaceAll("<"+lang+">", "<lang "+lang+">");
line=line.replaceAll("</"+lang+">", "</"+lang+"lang>");
line=line.replaceAll("<code "+lang+">", "<lang "+lang+">");
line=line.replaceAll("</code>", "</"+"lang>");
Line 500 ⟶ 618:
}
}
</syntaxhighlight>
</lang>
 
Example:
Line 515 ⟶ 633:
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</foolang>,
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</barlang>.
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.
Line 525 ⟶ 643:
=={{header|JavaScript}}==
{{works with|SpiderMonkey}}
<langsyntaxhighlight lang="javascript">var langs = ['foo', 'bar', 'baz']; // real list of langs goes here
var end_tag = '</'+'lang>';
 
Line 535 ⟶ 653:
.replace(new RegExp('</' + langs[i] + '>', 'gi'), end_tag);
print(line);
}</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 541 ⟶ 659:
{{trans|Python}}
 
<langsyntaxhighlight lang="julia">function fixtags(text::AbstractString)
langs = ["ada", "cpp-qt", "pascal", "lscript", "z80", "visualprolog",
"html4strict", "cil", "objc", "asm", "progress", "teraterm", "hq9plus",
Line 573 ⟶ 691:
 
const txt = readstring(ARGS[1])
println(fixtags(txt))</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">
--thanks, random python guy
langs = {'ada', 'cpp-qt', 'pascal', 'lscript', 'z80', 'visualprolog',
Line 606 ⟶ 724:
print(line)
end
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="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
Line 619 ⟶ 737:
txt := StringTools:-SubstituteAll(txt, "</code>", "<#/lang>"):
end do;
print(txt);</langsyntaxhighlight>
{{Out|Output}}
<pre>
Line 630 ⟶ 748:
</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<presyntaxhighlight lang="mathematica">StringReplace[Import["wikisource.txt"],
{"</"~~Shortest[__]~~">"->"</lang>",
("<code "|"<")~~Shortest[x__]~~">"->"<lang "~~ x~~">"}
]>>"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>,
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</pre>
</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight Nimlang="nim">import re, strutils
 
const
Line 677 ⟶ 793:
text = text.replacef(re"(?s)<code (.+?)>(.*?)</code>", r"<lang $1>$2</lang>")
 
stdout.write text</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">#load "str.cma"
 
let langs =
Line 738 ⟶ 854:
List.fold_left (fun str lang ->
(repl4 lang (repl3 lang (repl2 lang (repl1 lang str))))
) (read_in stdin) langs)</langsyntaxhighlight>
 
(in the code the strings <nowiki></lang></nowiki> have been split in order to not confuse the wiki)
 
this line of code:
<langsyntaxhighlight lang="ocaml"> (repl4 lang (repl3 lang (repl2 lang (repl1 lang str))))</langsyntaxhighlight>
 
could also be written like this:
<langsyntaxhighlight lang="ocaml"> List.fold_right (fun repl -> repl lang) [repl1; repl2; repl3; repl4] str</langsyntaxhighlight>
 
 
Line 752 ⟶ 868:
 
=={{header|Perl}}==
<langsyntaxhighlight 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
Line 777 ⟶ 893:
$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;
 
print $text;</langsyntaxhighlight>
 
=={{header|Phix}}==
{{trans|D}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant ltext = `_div abap actionscript actionscript3 ada apache
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
applescript apt_sources asm asp autoit avisynth bash basic4gl bf
<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
blitzbasic bnf boo c c_mac caddcl cadlisp cfdg cfm cil cobol cpp
applescript apt_sources asm asp autoit avisynth bash basic4gl bf
cpp-qt csharp css d delphi diff dos dot eiffel email fortran
blitzbasic bnf boo c c_mac caddcl cadlisp cfdg cfm cil cobol cpp
freebasic genero gettext glsl gml gnuplot groovy haskell hq9plus
cpp-qt csharp css d delphi diff dos dot eiffel email fortran
html4strict idl ini inno intercal io java java5 javascript kixtart
freebasic genero gettext glsl gml gnuplot groovy haskell hq9plus
klonec klonecpp latex lisp lolcode lotusformulas lotusscript
html4strict idl ini inno intercal io java java5 javascript kixtart
lscript lua m68k make matlab mirc modula3 mpasm mxml mysql nsis
klonec klonecpp latex lisp lolcode lotusformulas lotusscript
objc ocaml ocaml-brief oobas oracle11 oracle8 pascal per perl php
lscript lua m68k make matlab mirc modula3 mpasm mxml mysql nsis
php-brief pic16 pixelbender plsql povray powershell progress
objc ocaml ocaml-brief oobas oracle11 oracle8 pascal per perl php
prolog providex python qbasic r rails reg robots ruby sas scala
php-brief pic16 pixelbender plsql povray powershell progress
scheme scilab sdlbasic smalltalk smarty sql tcl teraterm text
prolog providex python qbasic r rails reg robots ruby sas scala
thinbasic tsql typoscript vb vbnet verilog vhdl vim visualfoxpro
scheme scilab sdlbasic smalltalk smarty sql tcl teraterm text
visualprolog whitespace winbatch xml xorg_conf xpp z80`,
thinbasic tsql typoscript vb vbnet verilog vhdl vim visualfoxpro
langs = split(substitute(ltext,"\n"," "))
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>
function fix_tags(string text)
for i=1 to length(langs) do
<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>
string lang = langs[i],
<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>
openl = sprintf("<%s>",{lang}),
<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>
openc = sprintf("<code %s>",{lang}),
<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>
lopen = sprintf("<lang %s>",{lang}),
<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>
closl = sprintf("</%s>",{lang}),
<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>
closc = sprintf("</%s>",{"code"}),
<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>
lclos = sprintf("</%s>",{"lang"})
<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>
text = substitute_all(text,{openl,openc,closl,closc},
<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>
{lopen,lopen,lclos,lclos})
<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>
end for
<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>
return text
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">text</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
constant test = """
lorem ipsum <c>some c code</c>dolor sit amet, <csharp>some
<span style="color: #008080;">constant</span> <span style="color: #000000;">test</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
csharp code</csharp> consectetur adipisicing elit, <code r>
lorem ipsum &lt;c&gt;some c code&lt;/c&gt;dolor sit amet, &lt;csharp&gt;some
some r code </code>sed do eiusmod tempor incididunt
csharp code&lt;/csharp&gt; consectetur adipisicing elit, &lt;code r&gt;
"""
some r code &lt;/code&gt;sed do eiusmod tempor incididunt
puts(1,fix_tags(test))</lang>
"""</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>
Line 826 ⟶ 945:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="lisp">#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
 
(let Lang '("ada" "awk" "c" "forth" "prolog" "python" "z80")
Line 839 ⟶ 958:
(prin "</lang") )
(T (prin "<" S)) ) ) ) ) )
(bye)</langsyntaxhighlight>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="basic">If Not OpenConsole()
End
ElseIf CountProgramParameters() <> 2
Line 881 ⟶ 1,000:
CloseFile(1)
EndIf
EndIf</langsyntaxhighlight>
 
=={{header|Python}}==
 
<syntaxhighlight lang =python># coding: utf-8
""" Rosetta Code task rosettacode.org/wiki/Rosetta_Code/Fix_code_tags """
 
from re import syssub
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>"HAI % i)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}}==
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)
{
Line 940 ⟶ 1,232:
consectetur adipisicing elit,<code r>some r code</code>sed do eiusmod tempor incididunt"
fixtags(page)
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 965 ⟶ 1,257:
[else all]))
(loop)))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>my @langs = <
abap actionscript actionscript3 ada apache applescript apt_sources
asm asp autoit avisynth bash basic4gl bf blitzbasic bnf boo c caddcl
Line 994 ⟶ 1,286:
s:g [ '<code '(.+?) '>' (.*?) '</code>' ] = "<lang $0>{$1}</"~"lang>";
 
.say;</langsyntaxhighlight>
<syntaxhighlight lang="raku" perl6line>use v6;
 
constant @langs = < abap actionscript actionscript3 ada … >;
Line 1,007 ⟶ 1,299:
'lang' ~ " " x *<need-add-space>.so,
:g,
).print</langsyntaxhighlight>
 
=={{header|REXX}}==
<langsyntaxhighlight 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>"
Line 1,025 ⟶ 1,317:
end /*j*/
call lineout oFID,$ /*write re-formatted record to output. */
end /*while*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, so one is included here &nbsp; ───► &nbsp; [[CHANGESTR.REX]].
<br><br>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby"># get all stdin in one string
#text = $stdin.read
# for testing, use
Line 1,050 ⟶ 1,342:
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. </langsyntaxhighlight>
 
<pre>Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
Line 1,060 ⟶ 1,352:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
extern crate regex;
 
Line 1,108 ⟶ 1,400:
}
 
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
Line 1,115 ⟶ 1,407:
{{Out}}
Experience it running in your browser at [https://scastie.scala-lang.org/5U6vqsOaTi6AU5FcqfA2lA Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="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
Line 1,136 ⟶ 1,428:
})) // ${"/lan"}g is the <noWiki> escape.
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight 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
Line 1,164 ⟶ 1,456:
);
 
print text;</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="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
Line 1,198 ⟶ 1,490:
lappend replacements "<code $lang>" "<lang $lang>"
}
set text [string map $replacements $text]</langsyntaxhighlight>
 
=={{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}}==
<langsyntaxhighlight lang="zkl">fcn replace(data,src,dstpat){
re,n,buf:=RegExp(src),0,Data();
while(re.search(data,True,n)){
Line 1,215 ⟶ 1,665:
replace(data,src,dst)
}
print(data.text);</langsyntaxhighlight>
Note: the "</" "lang>" to keep /lang the wiki from getting confused (it is string concatenation).
{{out}}
Line 1,229 ⟶ 1,679:
some r code </lang>sed do eiusmod tempor incididunt
</pre>
 
 
{{omit from|Brlcad}}
9,476

edits