10
\$\begingroup\$

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 string potato, n is 1, for this 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: ` becomes this is a backtick: \`, and ` `` ``` ````` `````` becomes \` \`\` \`\`\` \`\`\`\`\` \`\`\`\`\`\`.

This is ; the shortest answer in byte count wins.

\$\endgroup\$
4
  • \$\begingroup\$ What about space? ` ` does not get code-formatting... \$\endgroup\$
    – Justin
    Jan 18, 2014 at 6:44
  • \$\begingroup\$ @Quincunx Hmm... how are you supposed to format spaces in code anyway? \$\endgroup\$
    – Doorknob
    Jan 18, 2014 at 13:04
  • \$\begingroup\$ I have no idea. This post is where I learned how to format backticks and such. \$\endgroup\$
    – Justin
    Jan 18, 2014 at 17:41
  • 2
    \$\begingroup\$ The comment form also doesn't work for \; to format a \, you need to do ``\``. Also, anything ending with a \ runs into the same problem: asdf\ produces `asdf` \$\endgroup\$
    – Justin
    Jan 19, 2014 at 9:19

2 Answers 2

4
\$\begingroup\$

APL (90)

(Yes, the APL charset does fit in a byte, here's IBM's codepage 907.)

{⎕ML←3⋄(,/m,m,⍨{⍵=m:'\`'⋄⍵}¨⍵),⊂(⌽g),⍵,g←(''↑⍨∨/t[1,⍴t]),m/⍨0⍳⍨z∊⍨⍳⌈/0,z←,⊃⍴¨⍵⊂⍨t←⍵=m←'`'}

This is a function that takes a string, and returns an array of two strings, where the first string is the comment representation and the second string is the full post representation.

Tests:

      backtickify←{⎕ML←3⋄(,/m,m,⍨{⍵=m:'\`'⋄⍵}¨⍵),⊂(⌽g),⍵,g←(''↑⍨∨/t[1,⍴t]),m/⍨0⍳⍨z∊⍨⍳⌈/0,z←,⊃⍴¨⍵⊂⍨t←⍵=m←'`'}
      ↑backtickify 'potato'
`potato`
`potato`

      ↑backtickify 'this is a backtick: `'
`this is a backtick: \``   
`` this is a backtick: ` ``

      ↑backtickify '` `` ``` ````` ``````'
`\` \`\` \`\`\` \`\`\`\`\` \`\`\`\`\`\``
```` ` `` ``` ````` `````` ````  

      ⍝ testcase for not wrapping with spaces
      ↑backtickify 'no`spaces``at````the`edges'
`no\`spaces\`\`at\`\`\`\`the\`edges`
```no`spaces``at````the`edges``` 

Note: I gave the function a name for readability's sake, this is not strictly necessary to use it (you can just put the argument next to the anonymous function) so I didn't count it.

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

Ruby, 119

s=gets.chop
c=1
c+=1while s=~/([^`]|\A)#{?`*c}([^`]|\z)/
puts ?`+s.gsub('`','\\\\`')+'`
'+?`*c+(s=~/$`|`^/?" #{s} ":s)+?`*c

Ungolfed:

def backtickify str, comment = false
  return "`#{str.gsub '`', '\\\\`'}`" if comment
  c = 1
  c += 1 while str =~ /([^`]|\A)#{?` * c}([^`]|\z)/
  str = ' ' + str + ' ' if str[0] == ?` || str[-1] == ?`
  return ?` * c + str + ?` * c
end
\$\endgroup\$

Your Answer

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

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