Task

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



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