Rosetta Code/Fix code tags: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Fixed lang tags.)
(Undo revision 67951 by UnderBot (Talk) -- repair UnderBot damage)
Line 12: Line 12:
<pre>cat wikisource.txt | ./convert.py > converted.txt</pre>
<pre>cat wikisource.txt | ./convert.py > converted.txt</pre>
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>; usage: > fixtags.ahk input.txt ouput.txt
<lang AutoHotkey>
; usage: > fixtags.ahk input.txt ouput.txt
FileRead, text, %1%
FileRead, text, %1%
langs = ada,awk,autohotkey,etc
langs = ada,awk,autohotkey,etc
Line 21: Line 22:
tag1 = <%A_LoopField%>
tag1 = <%A_LoopField%>
tag2 = </%A_LoopField%>
tag2 = </%A_LoopField%>
text := RegExReplace(text, tag1, "<lang autohotkey>")
text := RegExReplace(text, tag1, "<lang " . A_LoopField . ">")
text := RegExReplace(text, tag2, slang)
text := RegExReplace(text, tag2, slang)
text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
text := RegExReplace(text, "<code (.+?)>(.*?)</code>"
, "<lang autohotkey>$2" . slang)
, "<lang $1>$2" . slang)
}
}
FileAppend, % text, %2%</lang>
FileAppend, % text, %2%
</lang>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>
<lang perl>my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
my @langs = qw(ada cpp-qt pascal lscript z80 visualprolog
html4strict cil objc asm progress teraterm hq9plus genero tsql
html4strict cil objc asm progress teraterm hq9plus genero tsql
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
email pic16 tcl apt_sources io apache vhdl avisynth winbatch
Line 48: Line 51:
my $slang="/lang";
my $slang="/lang";
for (@langs) {
for (@langs) {
$text =~ s|<$_>|<lang perl>|g;
$text =~ s|<$_>|<lang $_>|g;
$text =~ s|</$_>|<$slang>|g;
$text =~ s|</$_>|<$slang>|g;
}
}


$text =~ s|<code (.+?)>(.*?)</code>|<lang perl>$2<$slang>|sg;
$text =~ s|<code (.+?)>(.*?)</code>|<lang $1>$2<$slang>|sg;


print $text;</lang>
print $text;
</lang>


=={{header|Python}}==
=={{header|Python}}==


<lang python># coding: utf-8
<lang python>
# coding: utf-8


import sys
import sys
Line 87: Line 92:


for i in langs:
for i in langs:
text = text.replace("<%s>" % i,"<lang python>" % i)
text = text.replace("<%s>" % i,"<lang %s>" % i)
text = text.replace("</%s>" % i, "<%s>" % slang)
text = text.replace("</%s>" % i, "<%s>" % slang)


text = re.sub("(?s)<code (.+?)>(.*?)</code>", r"<lang python>\2<%s>" % slang, text)
text = re.sub("(?s)<code (.+?)>(.*?)</code>", r"<lang \1>\2<%s>" % slang, text)


sys.stdout.write(text)</lang>
sys.stdout.write(text)
</lang>


=={{header|R}}==
=={{header|R}}==
Note that the instances of ##### are to stop the wiki getting confused. Please remove them before running the code.
Note that the instances of ##### are to stop the wiki getting confused. Please remove them before running the code.
<lang R>fixtags <- function(page)
<lang R>
fixtags <- function(page)
{
{
langs <- c("c", "c-sharp", "r") # a complete list is required, obviously
langs <- c("c", "c-sharp", "r") # a complete list is required, obviously
langs <- paste(langs, collapse="|")
langs <- paste(langs, collapse="|")
page <- gsub(paste("<(", langs, ")>", sep=""), "<lang r>", page)
page <- gsub(paste("<(", langs, ")>", sep=""), "<lang \\1>", page)
page <- gsub(paste("</(", langs, ")>", sep=""), "</#####lang>", page)
page <- gsub(paste("</(", langs, ")>", sep=""), "</#####lang>", page)
page <- gsub(paste("<code(", langs, ")>", sep=""), "<lang r>", page)
page <- gsub(paste("<code(", langs, ")>", sep=""), "<lang \\1>", page)
page <- gsub(paste("</code>", sep=""), "</#####lang>", page)
page <- gsub(paste("</code>", sep=""), "</#####lang>", page)
page
page
Line 109: Line 116:
page <- "lorem ipsum <c>some c code</c>dolor sit amet,<c-sharp>some c-sharp code</c-sharp>
page <- "lorem ipsum <c>some c code</c>dolor sit amet,<c-sharp>some c-sharp code</c-sharp>
consectetur adipisicing elit,<code r>some r code</code>sed do eiusmod tempor incididunt"
consectetur adipisicing elit,<code r>some r code</code>sed do eiusmod tempor incididunt"
fixtags(page)</lang>
fixtags(page)
</lang>


=={{header|Ruby}}==
=={{header|Ruby}}==
Line 119: Line 127:
langs = %w(foo bar baz) # actual list of languages declared here
langs = %w(foo bar baz) # actual list of languages declared here
for lang in langs
for lang in langs
text.gsub!(Regexp.new("<(#{lang})>")) {"<lang ruby>"}
text.gsub!(Regexp.new("<(#{lang})>")) {"<lang #$1>"}
text.gsub!(Regexp.new("</#{lang}>"), "<#{slash_lang}>")
text.gsub!(Regexp.new("</#{lang}>"), "<#{slash_lang}>")
end
end
text.gsub!(/<code (.*?)>/, '<lang ruby>')
text.gsub!(/<code (.*?)>/, '<lang \1>')
text.gsub!(/<\/code>/, "<#{slash_lang}>")
text.gsub!(/<\/code>/, "<#{slash_lang}>")
print text
print text
Line 132: Line 140:
altera electram. Tota adhuc altera te sea, <code bar>soluta appetere ut mel</bar>.
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
Quo quis graecis vivendo te, <baz>posse nullam lobortis ex usu</code>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus.</lang>
constituto id, mea an omittam fierent vituperatoribus. </lang>


<pre>Lorem ipsum<lang ruby>saepe audire</lang> elaboraret ne quo, id equidem
<pre>Lorem ipsum <lang foo>saepe audire</lang> elaboraret ne quo, id equidem
atomorum inciderint usu.<lang ruby>In sit inermis deleniti percipit</lang>,
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
ius ex tale civibus omittam. <barf>Vix ut doctus cetero invenire</barf>, his eu
altera electram. Tota adhuc altera te sea,<lang ruby>soluta appetere ut mel</lang>.
altera electram. Tota adhuc altera te sea, <lang bar>soluta appetere ut mel</lang>.
Quo quis graecis vivendo te,<lang ruby>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
Quo quis graecis vivendo te, <lang baz>posse nullam lobortis ex usu</lang>. Eam volumus perpetua
constituto id, mea an omittam fierent vituperatoribus. </pre>
constituto id, mea an omittam fierent vituperatoribus. </pre>


Line 158: Line 166:
set slang /lang
set slang /lang
foreach lang $langs {
foreach lang $langs {
set text [regsub -all "<$lang>" $text "<lang tcl>"]
set text [regsub -all "<$lang>" $text "<lang $lang>"]
set text [regsub -all "</$lang>" $text "<$slang>"]
set text [regsub -all "</$lang>" $text "<$slang>"]
}
}
set text [regsub -all "<code (.+?)>(.+?)</code>" $text "<lang tcl>\\2<$slang>"]</lang>
set text [regsub -all "<code (.+?)>(.+?)</code>" $text "<lang \\1>\\2<$slang>"]</lang>
Alternatively, for foreach loop may be replaced with:
Alternatively, for foreach loop may be replaced with:
<lang tcl>set text [regexp -all "<([join $langs |])>" $text {<lang tcl>}]
<lang tcl>set text [regexp -all "<([join $langs |])>" $text {<lang \1>}]
set text [regexp -all "</(?:[join $langs |])>" $text "<$slang>"]</lang>
set text [regexp -all "</(?:[join $langs |])>" $text "<$slang>"]</lang>

Revision as of 18:59, 11 December 2009

Task
Rosetta Code/Fix code tags
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:

cat wikisource.txt | ./convert.py > 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>

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>

Python

<lang python>

  1. 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'

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)(.*?)", 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 codesed do eiusmod tempor incididunt" fixtags(page) </lang>

Ruby

<lang ruby># get all stdin in one string

  1. text = $stdin.read
  2. 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>