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



fcn replace(data,src,dstpat){
   re,n,buf:=RegExp(src),0,Data();
   while(re.search(data,True,n)){
      matched:=re.matched;	// L(L(12,3),"c")
      data[matched[0].xplode()]=re.sub(data,dstpat,buf);  // "\1" --> "c"
      n=matched[0].sum(0);  // move past change
   }
}
data:=File.stdin.read();
foreach src,dst in (T(
     T(0'|<(\w+)>|,     0'|<lang \1>|), T(0'|</(\w+)>|,"</" "lang>"),
     T(0'|<code (\w+)>|,0'|<lang \1>|) )){
   replace(data,src,dst)
}
print(data.text);