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



import "./pattern" for Pattern
 
var source = """
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.
"""
 
// to avoid problems dispaying code on RC
var entag = Fn.new { |s| "<%(s)>" }
 
var langs = ["foo", "bar", "baz"] // in principle these can be anything
 
for (lang in langs) {
    var s = "[%(lang)]"
    var pairs = [
        ["<%(s)>", entag.call("lang $1")],
        ["<//%(s)>", entag.call("/lang")],
        ["<code %(s)>", entag.call("lang $1")],
        ["<//code>", entag.call("/lang")]
    ]
 
    for (pair in pairs) {
        var p = Pattern.new(pair[0])
        source = p.replaceAll(source, pair[1])
    }
}
 
System.print(source)