Convert a TIO answer to a CMC answer

24

1

Let's for once do something useful.

Given the text from a Try It Online codegolf submission, produce the equivalent Chat Mini Challenge submission, which is of the form

Language, N bytes: [`code`](URL)

or (if submission is a single byte – TIO will add a trailing s on the first line only if needed)

Language, 1 byte: [`code`](URL)

You may assume that the submission

  • has only one line
  • does not contain backticks (`)
  • has no leading whitespace
  • has a regular format like in the examples below; the code line is indented with four spaces ( ), not with HTML tags (<pre><code></code></pre>), i.e. no trailing whitespace.

Note that the submission may have a syntax highlighting tag. See last example.

You may optionally include the title text, i.e. …url "Language - Try It Online".

Examples

Given

# [05AB1E], 1 byte

    Ô

[Try it online!][TIO-j3v72bss]

[05AB1E]: https://github.com/Adriandmen/05AB1E
[TIO-j3v72bss]: https://tio.run/##MzBNTDJM/f//8JT//zNSgSAnJycfDAA "05AB1E – Try It Online"

return

05AB1E, 1 byte: [`Ô`](https://tio.run/##MzBNTDJM/f//8JT//zNSgSAnJycfDAA)

or

05AB1E, 1 byte: [`Ô`](https://tio.run/##MzBNTDJM/f//8JT//zNSgSAnJycfDAA "05AB1E – Try It Online")

Given

# [Brachylog], 4 bytes

    ḅhᵐc

[Try it online!][TIO-j3v75978]

[Brachylog]: https://github.com/JCumin/Brachylog
[TIO-j3v75978]: https://tio.run/##SypKTM6ozMlPN/r//@GO1oyHWyck//@vlJGampOTDwZK/6MA "Brachylog – Try It Online"

return

Brachylog, 4 bytes: [`ḅhᵐc`](https://tio.run/##SypKTM6ozMlPN/r//@GO1oyHWyck//@vlJGampOTDwZK/6MA)

Given

# [APL (Dyalog Unicode)], 15 bytes

<!-- language-all: lang-apl -->

    ⍴⍨¨⍳

[Try it online!][TIO-j44jgrps]

[APL (Dyalog Unicode)]: https://www.dyalog.com/
[TIO-j44jgrps]: https://tio.run/##AScA2P9hcGwtZHlhbG9n//9m4oaQ4o204o2owqjijbP//zcxNjDijLZmIDQ "APL (Dyalog Unicode) – Try It Online"

return

APL (Dyalog Unicode), 15 bytes: [`⍴⍨¨⍳`](https://tio.run/##AScA2P9hcGwtZHlhbG9n//9m4oaQ4o204o2owqjijbP//zcxNjDijLZmIDQ)

Adám

Posted 2017-06-19T18:49:22.290

Reputation: 37 779

Can we assume the language name does not contain a ]? – ETHproductions – 2017-06-19T18:58:09.357

2No language name on TIO has ] though. – Leaky Nun – 2017-06-19T18:59:03.460

True, I guess. May we assume the answer does not have any leading whitespace? – ETHproductions – 2017-06-19T19:00:29.120

@ETHproductions Yes, because otherwise the backticks won't work. I'll edit. – Adám – 2017-06-19T19:04:41.313

@ais523 I think the 4th assumption already listed covers the secondary output format. – ETHproductions – 2017-06-19T19:17:58.677

@ais523 the first issues is dealt with as ETHproductions notes. The syntax highlighting, hm, I guess it should. – Adám – 2017-06-19T19:30:57.053

4Code golf is useful ಠ_ಠ – Conor O'Brien – 2017-06-19T19:41:39.577

@Adám I somehow missed that – Conor O'Brien – 2017-06-19T19:50:34.510

Answers

6

V, 37, 35 bytes

ç^</dj
H3xf]x3Ji[`A`]Jd2/: 
xr(A)

Try it online!

James

Posted 2017-06-19T18:49:22.290

Reputation: 54 537

I tried using the surround plugin that's sort of bundled with V, but it kept trimming whitespace off the end, which I think disqualifies it. – nmjcman101 – 2017-06-19T19:35:50.173

Can I bother you to add support for the syntax highlighting tag? – Adám – 2017-06-19T19:36:36.277

@nmjcman101 Umm, I think you can assume trailing whitespace doesn't exist. – Erik the Outgolfer – 2017-06-19T19:39:48.887

1@Adám Well, that kinda seems like pointlessly changing the spec in the middle, but I guess I can support it. – James – 2017-06-19T19:42:46.650

@DJMcMayhem Well, it was in sandbox for a while. And no-one thought of that. I wouldn't have made the change if it wasn't for the actual use value of these submissions. Sorry about that. Have some ice cream: – Adám – 2017-06-19T19:49:07.907

@DJMcMayhem I just did ds[ for the language brackets and then tried ys$[ for the actual code oneliner. I think that ys$] will skip adding extra whitespace, and you should be able to do `ys$`` to surround with backticks also – nmjcman101 – 2017-06-19T19:55:33.767

1@nmjcman101 Yeah, I did know about the difference between ys$[ and ys$], but since we're surrounding it with backticks and brackets, it's actually shorter to just put them in manually. – James – 2017-06-19T19:58:58.790

Could you use d/ instead of ç^</dj for both 2 bytes and back to Vim compatibility? Try it online!

– nmjcman101 – 2017-06-19T20:04:36.787

4

Python 3, 113 103 98 91 bytes

-1 byte thanks to Value Ink.

If you can, I could totally use some help with the regex. ><

lambda s:re.sub('(?s)# .(.*)](.*?)\n.* {4}(.*?)\n.+: (.*)',r'\1\2: [`\3`](\4)',s)
import re

Try it online!

totallyhuman

Posted 2017-06-19T18:49:22.290

Reputation: 15 378

Can I bother you to add support for the syntax highlighting tag? – Adám – 2017-06-19T19:37:05.617

Great, thanks. Sorry about that. – Adám – 2017-06-19T19:46:14.543

No need to use \s to match the code block part; replace it with a literal space character. Also the expected output doesn't have the "Language - Try It Online" segment, so you need to change your last group to (\S*) to match correctly, so the byte count comes out around the same. – Value Ink – 2017-06-19T21:27:59.843

Actually, it turns out you can keep that Try It Online part. In that case, changing \s to a space will save 1 byte. – Value Ink – 2017-06-19T21:35:28.080

Yup, I was the one who asked that question. ;) – totallyhuman – 2017-06-19T21:48:08.017

This is late, but you can do <sp><sp>+ in place of <sp>{4} (-1 byte). Also, with Python 3.6, you can use (?s:...) to apply dot-all to only part of the regex (-2 bytes). Try it online!

– DLosc – 2017-12-20T08:11:19.877

Actually, even better: ditch the flag entirely and use [^\t] (with \t representing a literal tab character) when you need to match all characters including newlines: 86 bytes

– DLosc – 2017-12-20T08:20:49.823

3

Retina, 54 48 52 57 48 bytes

Saved 1 byte thanks to @MartinEnder, 1 byte thanks to @Riley

...(.+)[^@]+    (.+)[^@]+: 
$1: [`$2`](
1`]

$
)

Try it online! This assumes there won't be an @ in the language name, which should be fine...

ETHproductions

Posted 2017-06-19T18:49:22.290

Reputation: 47 880

you could just replace ^# \[ with ^... I think – Martin Ender – 2017-06-19T19:20:56.980

@MartinEnder Thanks, I think that works... – ETHproductions – 2017-06-19T19:25:44.693

Can you skip the ^ and $? It should always match the whole string anyway. – Riley – 2017-06-19T19:26:16.113

@Riley That would fail if... oh shoot, mine does too... – ETHproductions – 2017-06-19T19:27:25.267

This doesn't work if the code contains <space>", but I don't know if you need to handle that. I'm guessing you just noticed that to. – Riley – 2017-06-19T19:28:26.667

@Riley Yeah, fixed now. The ^ can be removed though AFAICT. – ETHproductions – 2017-06-19T19:32:11.570

Can I bother you to add support for the syntax highlighting tag? – Adám – 2017-06-19T19:36:29.580

@Adám Ah sorry, fixed. – ETHproductions – 2017-06-19T23:01:56.973

3

05AB1E, 50 bytes

']¡J¦¦¦|J|`‚ʒнðQ}`ðÛ|D`θ'"Êi\|ë\}θ#1èr"ÿ: [`ÿ`](ÿ)

Try it online!

Now supports syntax highlighting too!

н is replaced by and θ is replaced by ®è since those aren't pulled to TIO yet.

I just wonder how to shorten ¦¦¦...

Erik the Outgolfer

Posted 2017-06-19T18:49:22.290

Reputation: 38 134

3

JavaScript (ES6), 73 bytes

f=
s=>s.replace(/...(.*)](.*)[^]+    (.*)[^]+: (.*) ".*/,"$1$2: [`$3`]($4)")
<textarea rows=8 cols=75 oninput=o.textContent=this.value==f(this.value)?``:f(this.value)></textarea><pre id=o>

Neil

Posted 2017-06-19T18:49:22.290

Reputation: 95 035

The answer I was waiting for. Can this be made into a bookmarklet? Otherwise I'll just bookmark it. – Adám – 2017-06-19T23:04:04.943

@Adám alert(prompt().replace(...)) might work, if the prompt will let you paste multiple lines in some way (the regexp doesn't actually need the newlines to be present). – Neil – 2017-06-19T23:10:56.877

Hm, I get the unmodified post as result. Maybe we can inject your code into TIO… – Adám – 2017-06-20T09:25:25.993

I was also working on a similar solution, but I didn't know about [^] to match anything. I have learnt something new from your solution. Thanks! – Arjun – 2017-06-20T13:36:59.493

2

MATLAB/Octave, 120 126 bytes

@(a)strjoin(regexprep(strsplit(a,'\n'),{'\[.+\]: (.+) ".+','^[\[<].+','# \[(.+)\](.+)','^ +'},{'`]($1)','','$1$2: [`',''}),'')

This should work. For MATLAB it won't work with Unicode characters though I don't think, mostly because MATLAB sucks when it comes to odd characters. However it will work with Unicode for Octave, or at least TIO seems to be able to cope.

It should also handle submissions both with and without the <-- --> syntax highlighting.

Updates:

  1. Added 4 bytes as I missed the @(a) when copying over
  2. Added 2 bytes to add back ticks around the code in the output

Try it online!

Tom Carpenter

Posted 2017-06-19T18:49:22.290

Reputation: 3 990

Can this be run on TIO Octave?

– Adám – 2017-06-20T12:14:54.313

@Adám it does indeed. I've updated the answer with TIO link. – Tom Carpenter – 2017-06-20T12:26:28.063

2

Ruby, 85 bytes

->s{s[/.*/].gsub(/# .|\]/){}+": [`#{s[/(?<=  )\S.*/]}`](#{s=~/TIO-\w+.: (\S+)/;$1})"}

Try it online!

Value Ink

Posted 2017-06-19T18:49:22.290

Reputation: 10 608

1

Python 2, 101 bytes

a=input().split('\n')
print'%s%s: [`%s`](%s)'%tuple(a[0][3:].split(']')+[a[-6][4:],a[-1].split()[1]])

Try it online! or Try all test cases

Rod

Posted 2017-06-19T18:49:22.290

Reputation: 17 588

1

PHP, 92 bytes

<?=preg_filter("<...(.+)\](, \d+ \w+).* {4}(\S+).*: (\S+).*>s","$1$2: [`$3`]($4)",$_GET[0]);

Try it online!

Jörg Hülsermann

Posted 2017-06-19T18:49:22.290

Reputation: 13 026

0

Pip, 43 39 38 bytes

[a@>3DC']": [`"g@-6|>s"`]("(g@v^s1)')]

Try it online!

Takes the lines as separate command-line arguments. An example invocation could look like:

> pip.py -f convert.pip "# [05AB1E], 1 byte" "" "    Ô" "" "[Try it online!][TIO-j3v72bss]" "" "[05AB1E]: https://github.com/Adriandmen/05AB1E" "[TIO-j3v72bss]: https://tio.run/##MzBNTDJM/f//8JT//zNSgSAnJycfDAA \"05AB1E – Try It Online\""

though actually this doesn't work for me because Windows cmd complains about the Ô... [inarticulate muttering]

--At any rate, it works on TIO. Or, use the -r flag and take input from lines of stdin instead.

Assumes that the language name will not contain ] (which would break the markdown anyway).

Explanation

                                        g is list of cmdline args; a is 1st arg;
                                        s is space; v is -1 (implicit)
[                                    ]  Build a list with these elements:
 a@>3                                    1st line, sans first three characters,
     DC']                                 with the ] character deleted
         ": [`"                          This string
               g@-6                      6th line from the end (the code)
                   |>s                    with leading spaces stripped
                      "`]("              This string
                            g@v          Last line (containing the URL)
                               ^s         split on spaces
                           (     1)       get element at index 1
                                   ')    This character
                                        By default, the list is concatenated and printed

DLosc

Posted 2017-06-19T18:49:22.290

Reputation: 21 213