25
\$\begingroup\$

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)
\$\endgroup\$
8
  • \$\begingroup\$ Can we assume the language name does not contain a ]? \$\endgroup\$ Jun 19, 2017 at 18:58
  • 2
    \$\begingroup\$ No language name on TIO has ] though. \$\endgroup\$
    – Leaky Nun
    Jun 19, 2017 at 18:59
  • \$\begingroup\$ True, I guess. May we assume the answer does not have any leading whitespace? \$\endgroup\$ Jun 19, 2017 at 19:00
  • \$\begingroup\$ @ETHproductions Yes, because otherwise the backticks won't work. I'll edit. \$\endgroup\$
    – Adám
    Jun 19, 2017 at 19:04
  • 4
    \$\begingroup\$ Code golf is useful ಠ_ಠ \$\endgroup\$ Jun 19, 2017 at 19:41

10 Answers 10

6
\$\begingroup\$

V, 37, 35 bytes

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

Try it online!

\$\endgroup\$
8
  • \$\begingroup\$ 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. \$\endgroup\$
    – nmjcman101
    Jun 19, 2017 at 19:35
  • \$\begingroup\$ Can I bother you to add support for the syntax highlighting tag? \$\endgroup\$
    – Adám
    Jun 19, 2017 at 19:36
  • \$\begingroup\$ @nmjcman101 Umm, I think you can assume trailing whitespace doesn't exist. \$\endgroup\$ Jun 19, 2017 at 19:39
  • 1
    \$\begingroup\$ @Adám Well, that kinda seems like pointlessly changing the spec in the middle, but I guess I can support it. \$\endgroup\$
    – DJMcMayhem
    Jun 19, 2017 at 19:42
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – DJMcMayhem
    Jun 19, 2017 at 19:58
4
\$\begingroup\$

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!

\$\endgroup\$
7
  • \$\begingroup\$ Can I bother you to add support for the syntax highlighting tag? \$\endgroup\$
    – Adám
    Jun 19, 2017 at 19:37
  • \$\begingroup\$ Great, thanks. Sorry about that. \$\endgroup\$
    – Adám
    Jun 19, 2017 at 19:46
  • \$\begingroup\$ 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. \$\endgroup\$
    – Value Ink
    Jun 19, 2017 at 21:27
  • \$\begingroup\$ Actually, it turns out you can keep that Try It Online part. In that case, changing \s to a space will save 1 byte. \$\endgroup\$
    – Value Ink
    Jun 19, 2017 at 21:35
  • \$\begingroup\$ Yup, I was the one who asked that question. ;) \$\endgroup\$ Jun 19, 2017 at 21:48
3
\$\begingroup\$

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>

\$\endgroup\$
4
  • \$\begingroup\$ The answer I was waiting for. Can this be made into a bookmarklet? Otherwise I'll just bookmark it. \$\endgroup\$
    – Adám
    Jun 19, 2017 at 23:04
  • \$\begingroup\$ @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). \$\endgroup\$
    – Neil
    Jun 19, 2017 at 23:10
  • \$\begingroup\$ Hm, I get the unmodified post as result. Maybe we can inject your code into TIO… \$\endgroup\$
    – Adám
    Jun 20, 2017 at 9:25
  • \$\begingroup\$ 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! \$\endgroup\$
    – Arjun
    Jun 20, 2017 at 13:36
3
\$\begingroup\$

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

\$\endgroup\$
8
  • \$\begingroup\$ you could just replace ^# \[ with ^... I think \$\endgroup\$ Jun 19, 2017 at 19:20
  • \$\begingroup\$ @MartinEnder Thanks, I think that works... \$\endgroup\$ Jun 19, 2017 at 19:25
  • \$\begingroup\$ Can you skip the ^ and $? It should always match the whole string anyway. \$\endgroup\$
    – Riley
    Jun 19, 2017 at 19:26
  • \$\begingroup\$ @Riley That would fail if... oh shoot, mine does too... \$\endgroup\$ Jun 19, 2017 at 19:27
  • \$\begingroup\$ 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. \$\endgroup\$
    – Riley
    Jun 19, 2017 at 19:28
3
\$\begingroup\$

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 ¦¦¦...

\$\endgroup\$
0
2
\$\begingroup\$

Ruby, 85 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ Can this be run on TIO Octave? \$\endgroup\$
    – Adám
    Jun 20, 2017 at 12:14
  • \$\begingroup\$ @Adám it does indeed. I've updated the answer with TIO link. \$\endgroup\$ Jun 20, 2017 at 12:26
1
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

PHP, 92 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.