Your goal is to input any arbitrary text, such as:
This is some text
And output that text formatted as code for posts (SE-flavored Markdown) and comments (mini-Markdown) on Stack Exchange, such as:
`This is some text`
`This is some text`
However, it gets trickier. Consider the input:
Perl has a variable called $`
The output must be
`` Perl has a variable called $` ``
`Perl has a varaible called $\``
The full rules for the formatting are:
Full posts (full SE Markdown)
Step 1: count the least number for which there are not exactly that many consecutive backticks in the string. Let's call this
n
. (For example, for the stringpotato
,n
is 1, forthis is a backtick: `
,n
is 2, and for` `` ``` ````` ``````
,n
is 4.)Step 2: wrap the string in
n
backticks. If the string starts or ends with a backtick, you must wrap with spaces as well. (For example,potato
becomes`potato`
,this is a backtick: `
becomes`` this is a backtick: ` ``
, and` `` ``` ````` ``````
becomes```` ` `` ``` ````` `````` ````
.
Comments (mini-Markdown)
- Wrap in
`
and escape all`
's with\`
. (For example,potato
becomes`potato`
,this is a backtick: `
becomesthis is a backtick: \`
, and` `` ``` ````` ``````
becomes\` \`\` \`\`\` \`\`\`\`\` \`\`\`\`\`\`
.
- Wrap in
This is code-golf; the shortest answer in byte count wins.
\
; to format a\
, you need to do``\``
. Also, anything ending with a\
runs into the same problem:asdf\
produces `asdf` \$\endgroup\$