Rosetta Code/Fix code tags: Difference between revisions
m →{{header|REXX}}: re-aligned first comment, added whitespace. -- ~~~~ |
→{{header|REXX}}: changed comments and indentation, added a comment. -- ~~~~ |
||
Line 347: | Line 347: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
⚫ | |||
<lang rexx>/*REXX program fixes (changes) depreciated code tags with newer tags.*/ |
<lang rexx>/*REXX program fixes (changes) depreciated code tags with newer tags.*/ |
||
⚫ | |||
old. = /*define a default value for OLD.*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
ifid = 'Wikisource.txt' /* input file identifier. */ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
_=linein(ifid) /*read a record from the input. */ |
_=linein(ifid) /*read a record from the input. */ |
||
do k=1 while old.j\=='' |
do k=1 while old.j\=='' /*change old --> new until done. */ |
||
_=changestr(new.k,_,old.k) /*let REXX do the heavy lifting. */ |
_=changestr(new.k,_,old.k) /*let REXX do the heavy lifting. */ |
||
end |
end /*k*/ |
||
call lineout ofid,_ /*write out the re-formatted rec.*/ |
call lineout ofid,_ /*write out the re-formatted rec.*/ |
||
end |
end /*while lines(ifid)\==0*/ |
||
/*stick a fork in it, we're done.*/</lang> |
|||
⚫ | |||
/*───────────────────────────CHANGESTR subroutine───────────────────────*/ |
<lang rexx>/*───────────────────────────CHANGESTR subroutine───────────────────────*/ |
||
changestr: procedure; parse arg o,h,n; r=; w=length(o); if w==0 then return n||h |
changestr: procedure; parse arg o,h,n; r=; w=length(o); if w==0 then return n||h |
||
do forever; parse var h y (o) _ +(w) h; if _=='' then return r||y; r=r||y||n; end</lang> |
do forever; parse var h y (o) _ +(w) h; if _=='' then return r||y; r=r||y||n; end</lang> |
Revision as of 07:37, 26 November 2012
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
Fix Rosetta Code deprecated code tags, with these rules:
Change <%s> to <lang %s> Change </%s> to </lang> Change <code %s> to <lang %s> Change </code> to </lang>
Usage:
./convert.py < wikisource.txt > converted.txt
AutoHotkey
<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, "(.*?)
"
, "<lang $1>$2" . slang)
} FileAppend, % text, %2% </lang>
J
Solution: <lang j>require 'printf strings files'
langs=. <;._1 LF -.~ noun define NB. replace with real lang strings
foo bar baz
)</lang>
patterns=. noun define <%s>|<lang %s>| </%s>|</lang>| <code %s>|<lang %s>| </code>|</lang>| )
<lang j>fixCodeTags=: rplc&(, <;._2;._2 &> patterns vbsprintf _5]\ 5#langs)</lang>
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: <lang j> 'converted.txt' fwrite~ fixCodeTags fread 'wikisource.txt'</lang>
JavaScript
<lang javascript>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('', '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);
}</lang>
Lua
<lang 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("", "<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
</lang>
Mathematica
StringReplace[Import["wikisource.txt"],
{"</"~~Shortest[__]~~">"->"</lang>",
("<code "|"<")~~Shortest[x__]~~">"->"<lang "~~ x~~">"}
]>>"converted.txt"
->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
OCaml
<lang 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 \
perl6 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 to <lang %s> *)
let repl3 lang str =
let pat = ""
and tpl = "<lang " ^ lang ^ ">" in
(repl pat tpl str)
(* change
to </la\ng> *)
let repl4 lang str =
let pat = "
"
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)</lang>
(in the code the strings </lang> have been split in order to not confuse the wiki)
this line of code:
<lang ocaml> (repl4 lang (repl3 lang (repl2 lang (repl1 lang str))))</lang>
could also be written like this:
<lang ocaml> List.fold_right (fun repl -> repl lang) [repl1; repl2; repl3; repl4] str</lang>
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
<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|(.*?)
|<lang $1>$2<$slang>|sg;
print $text;
</lang>
PicoLisp
<lang 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)</lang>
PureBasic
<lang 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</lang>
Python
<lang python># coding: utf-8
import sys
import re
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'
text = sys.stdin.read()
for i in langs:
text = text.replace("<%s>" % i,"<lang %s>" % i)
text = text.replace("</%s>" % i, "<%s>" % slang)
text = re.sub("(?s)<%s (.+?)>(.*?)</%s>"%(code,code), r"<lang \1>\2<%s>" % slang, text)
sys.stdout.write(text)
</lang>
R
Note that the instances of ##### are to stop the wiki getting confused. Please remove them before running the code.
<lang R>
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("
", 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,some r code
sed do eiusmod tempor incididunt"
fixtags(page)
</lang>
REXX
<lang rexx>/*REXX program fixes (changes) depreciated code tags with newer tags.*/ @ = '<' /*ensure RC won't get confused. */ old. = /*define a default value for 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' /* input file identifier. */ ofid = 'converted.txt' /*output " " */
do while lines(ifid)\==0 /*keep trunkin' until done. */ _=linein(ifid) /*read a record from the input. */
do k=1 while old.j\== /*change old --> new until done. */ _=changestr(new.k,_,old.k) /*let REXX do the heavy lifting. */ end /*k*/
call lineout ofid,_ /*write out the re-formatted rec.*/ end /*while lines(ifid)\==0*/ /*stick a fork in it, we're done.*/</lang>
Some older REXXes don't have the changestr bif, so one is include below. <lang rexx>/*───────────────────────────CHANGESTR subroutine───────────────────────*/ changestr: procedure; parse arg o,h,n; r=; w=length(o); if w==0 then return n||h do forever; parse var h y (o) _ +(w) h; if _== then return r||y; r=r||y||n; end</lang>
Ruby
<lang 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!(//, '<lang \1>')
text.gsub!(/<\/code>/, "<#{slash_lang}>")
print text
__END__
Lorem ipsum
saepe audire
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, soluta appetere ut mel</bar>.
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu
. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus. </lang>
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.
Tcl
<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
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 "(.+?)
" $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
<$slang>]
foreach lang $langs {
lappend replacements "<$lang>" "<lang $lang>"
lappend replacements "</$lang>" "<$slang>"
lappend replacements "" "<lang $lang>"
}
set text [string map $replacements $text]</lang>