Rosetta Code/Fix code tags: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
mNo edit summary |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(6 intermediate revisions by 4 users not shown) | |||
Line 2:
[[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:
Line 13:
</pre>
;
<pre>
<lang AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
Line 88:
{{trans|Java}}
<
‘ada’, ‘apache’, ‘applescript’, ‘apt_sources’, ‘asm’, ‘asp’,
‘autoit’, ‘avisynth’, ‘bar’, ‘bash’, ‘basic4gl’, ‘bf’,
Line 124:
text = text.replace(‘</code>’, ‘</’""‘lang>’)
print(text)</
{{out}}
Line 137:
=={{header|AutoHotkey}}==
<
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
Line 152:
}
FileAppend, % text, %2%
</syntaxhighlight>
=={{header|D}}==
{{trans|Python}}
<
immutable langs = "_div abap actionscript actionscript3 ada apache
Line 195:
.fixTags
.writeln;
}</
{{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 204:
{{libheader| System.RegularExpressions}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program Fix_code_tags;
Line 322:
begin
Fix;
end.</
=={{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">
#! /usr/bin/env escript
-module( fix_code_tags ).
Line 365:
loop( eof, Acc ) -> lists:reverse( Acc );
loop( Line, Acc ) -> loop( io:get_line(""), [Line | Acc] ).
</syntaxhighlight>
{{out}}
<pre>
Line 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.
<
open System.Text.RegularExpressions
Line 415:
printfn "%s" (regexForOldLangSyntax.Replace(Console.In.ReadToEnd(), replaceEvaluator))
0</
Output
<pre>>Rosetta.exe
Line 425:
=={{header|Go}}==
<
import "fmt"
Line 512:
return in
}</
=={{header|J}}==
'''Solution:'''
<
langs=. <;._1 LF -.~ noun define NB. replace with real lang strings
foo bar
baz
)</
<pre>patterns=. noun define
<%s>|<lang %s>|
Line 528:
</code>|</lang>|
)</pre>
<
'''Example Usage:'''
Line 548:
constituto id, mea an omittam fierent vituperatoribus.</pre>
Reading/writing file:
<
=={{header|Java}}==
<
import java.io.File;
import java.io.FileReader;
Line 618:
}
}
</syntaxhighlight>
Example:
Line 643:
=={{header|JavaScript}}==
{{works with|SpiderMonkey}}
<
var end_tag = '</'+'lang>';
Line 653:
.replace(new RegExp('</' + langs[i] + '>', 'gi'), end_tag);
print(line);
}</
=={{header|Julia}}==
Line 659:
{{trans|Python}}
<
langs = ["ada", "cpp-qt", "pascal", "lscript", "z80", "visualprolog",
"html4strict", "cil", "objc", "asm", "progress", "teraterm", "hq9plus",
Line 691:
const txt = readstring(ARGS[1])
println(fixtags(txt))</
=={{header|Lua}}==
<
--thanks, random python guy
langs = {'ada', 'cpp-qt', 'pascal', 'lscript', 'z80', 'visualprolog',
Line 724:
print(line)
end
</syntaxhighlight>
=={{header|Maple}}==
<
txt := FileTools[Text][ReadFile]("C:/Users/username/Desktop/text.txt"):
#langs should be a real list of programming languages
Line 737:
txt := StringTools:-SubstituteAll(txt, "</code>", "<#/lang>"):
end do;
print(txt);</
{{Out|Output}}
<pre>
Line 749:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{"</"~~Shortest[__]~~">"->"</lang>",
("<code "|"<")~~Shortest[x__]~~">"->"<lang "~~ x~~">"}
]>>"converted.txt"</
{{out}}
<pre>Lorem ipsum<lang foo>saepe audire</lang>elaboraret ne quo,id equidem
Line 763:
=={{header|Nim}}==
{{trans|Python}}
<
const
Line 793:
text = text.replacef(re"(?s)<code (.+?)>(.*?)</code>", r"<lang $1>$2</lang>")
stdout.write text</
=={{header|OCaml}}==
<
let langs =
Line 854:
List.fold_left (fun str lang ->
(repl4 lang (repl3 lang (repl2 lang (repl1 lang str))))
) (read_in stdin) langs)</
(in the code the strings <nowiki></lang></nowiki> have been split in order to not confuse the wiki)
this line of code:
<
could also be written like this:
<
Line 868:
=={{header|Perl}}==
<
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
Line 893:
$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;
print $text;</
=={{header|Phix}}==
{{trans|D}}
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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
Line 936:
"""</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>
<!--</
{{out}}
<pre>
Line 945:
=={{header|PicoLisp}}==
<
(let Lang '("ada" "awk" "c" "forth" "prolog" "python" "z80")
Line 958:
(prin "</lang") )
(T (prin "<" S)) ) ) ) ) )
(bye)</
=={{header|PureBasic}}==
<
End
ElseIf CountProgramParameters() <> 2
Line 1,000:
CloseFile(1)
EndIf
EndIf</
=={{header|Python}}==
Line 1,076:
for txt in testtexts:
text2 = sub(r'<lang\s+\"?([\w\d\s]+)\"?\s?>', r'<syntaxhighlight lang=\1>', txt)
text2 = sub(r'<lang\s*>',
text2 = sub(r'</lang\s*>',
print(txt, '\n=>\n', text2)
</
<pre>
<lang AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
Line 1,143:
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
Line 1,192:
=>
<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"
Line 1,217:
=={{header|R}}==
Note that the instances of ##### are to stop the wiki getting confused. Please remove them before running the code.
<syntaxhighlight lang="r">
fixtags <- function(page)
{
Line 1,232:
consectetur adipisicing elit,<code r>some r code</code>sed do eiusmod tempor incididunt"
fixtags(page)
</syntaxhighlight>
=={{header|Racket}}==
<
#lang racket
Line 1,257:
[else all]))
(loop)))
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
abap actionscript actionscript3 ada apache applescript apt_sources
asm asp autoit avisynth bash basic4gl bf blitzbasic bnf boo c caddcl
Line 1,286:
s:g [ '<code '(.+?) '>' (.*?) '</code>' ] = "<lang $0>{$1}</"~"lang>";
.say;</
<syntaxhighlight lang="raku"
constant @langs = < abap actionscript actionscript3 ada … >;
Line 1,299:
'lang' ~ " " x *<need-add-space>.so,
:g,
).print</
=={{header|REXX}}==
<
@="<"; old.=; old.1 = @'%s>' ; new.1 = @"lang %s>"
old.2 = @'/%s>' ; new.2 = @"/lang>"
Line 1,317:
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]].
<br><br>
=={{header|Ruby}}==
<
#text = $stdin.read
# for testing, use
Line 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. </
<pre>Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
Line 1,352:
=={{header|Rust}}==
<
extern crate regex;
Line 1,400:
}
</syntaxhighlight>
=={{header|Scala}}==
Line 1,407:
{{Out}}
Experience it running in your browser at [https://scastie.scala-lang.org/5U6vqsOaTi6AU5FcqfA2lA Scastie (remote JVM)].
<
val rx = // See for regex explanation: https://regex101.com/r/N8X4x7/3/
// Flags ignore case, dot matching line breaks, unicode support
Line 1,428:
})) // ${"/lan"}g is the <noWiki> escape.
}</
=={{header|Sidef}}==
<
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
Line 1,456:
);
print text;</
=={{header|Tcl}}==
<
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,490:
lappend replacements "<code $lang>" "<lang $lang>"
}
set text [string map $replacements $text]</
=={{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
<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}}==
<
re,n,buf:=RegExp(src),0,Data();
while(re.search(data,True,n)){
Line 1,552 ⟶ 1,665:
replace(data,src,dst)
}
print(data.text);</
Note: the "</" "lang>" to keep /lang the wiki from getting confused (it is string concatenation).
{{out}}
|