Anonymous user
Rosetta Code/Fix code tags: Difference between revisions
Scala contribution added.
Thundergnat (talk | contribs) (Undo revision 266616 by Dijkstra Incorrectly marked incorrect. There IS no language named "barf" so those tags were not changed.) |
(Scala contribution added.) |
||
Line 837:
constituto id, mea an omittam fierent vituperatoribus. </pre>
=={{header|Scala}}==
===Single pass converter===
Tackles e.g. also multiple whitespaces.
{{Out}}
Experience it running in your browser at [https://scastie.scala-lang.org/5U6vqsOaTi6AU5FcqfA2lA Scastie (remote JVM)].
<lang Scala>object FixCodeTags extends App {
val rx = // See for regex explanation: https://regex101.com/r/N8X4x7/3/
// Flags ignore case, dot matching line breaks, unicode support
s"(?is)<(?:(?:code\\s+)?(${langs.mkString("|")}))>(.+?|)<\\/(?:code|\\1)>".r
def langs = // Real list of langs goes here
Seq("bar", "baz", "foo", "Scala", "உயிர்/Uyir", "Müller")
def markDown =
"""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.
|Empty element: <Müller></Müller><scaLa></Scala><உயிர்/Uyir></உயிர்/Uyir>""".stripMargin
println(rx.replaceAllIn(markDown, _ match {
case rx(langName, langCode) => s"<lang ${langName.capitalize}>${langCode}<${"/lan"}g>"
})) // ${"/lan"}g is the <noWiki> escape.
}</lang>
=={{header|Sidef}}==
<lang ruby>var langs = %w(ada cpp-qt pascal lscript z80 visualprolog
|