Backtickify snippet di codice su Stack Exchange


9

Il tuo obiettivo è inserire qualsiasi testo arbitrario, come ad esempio:

This is some text

E genera quel testo formattato come codice per i post (Markdown con sapore SE) e i commenti (mini Markdown) su Stack Exchange, come:

`This is some text`
`This is some text`

Tuttavia, diventa più complicato. Considera l'input:

Perl has a variable called $`

L'output deve essere

`` Perl has a variable called $` ``
`Perl has a varaible called $\``

Le regole complete per la formattazione sono:

  • Post completi (Markdown completo SE)

    • Passaggio 1: contare il numero minimo per il quale non ci sono esattamente tanti backtick consecutivi nella stringa. Chiamiamo questo n. (Ad esempio, per la stringa potato, nè 1, per this is a backtick: `, nè 2 e per ` `` ``` ````` ``````, nè 4.)

    • Passaggio 2: avvolgere la stringa in nbacktick. Se la stringa inizia o termina con un backtick, è necessario avvolgere anche gli spazi. (Ad esempio, potatodiventa `potato`, this is a backtick: `diventa `` this is a backtick: ` ``e ` `` ``` ````` ``````diventa ```` ` `` ``` ````` `````` ````.

  • Commenti (mini-markdown)

    • Avvolgere in `e fuggire tutti `'s con \`. (Ad esempio, potatodiventa `potato`, this is a backtick: `diventa this is a backtick: \`e ` `` ``` ````` ``````diventa \` \`\` \`\`\` \`\`\`\`\` \`\`\`\`\`\`.

Questo è ; vince la risposta più breve nel conteggio dei byte.


E lo spazio? `` non ottiene la formattazione del codice ...
Justin il

@Quincunx Hmm ... come dovresti comunque formattare gli spazi nel codice?
Maniglia della porta

Non ne ho idea. Questo post è dove ho imparato a formattare i backtick e così via.
Justin il

2
Anche il modulo per i commenti non funziona \; per formattare un \, devi farlo ``\``. Inoltre, tutto ciò che termina con uno \incontra lo stesso problema: asdf\produce `asdf`
Justin il

Risposte:


4

APL (90)

(Sì, il set di caratteri APL si adatta a un byte, ecco la tabella codici IBM 907. )

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

Questa è una funzione che accetta una stringa e restituisce una matrice di due stringhe, in cui la prima stringa è la rappresentazione del commento e la seconda stringa è la rappresentazione post completa .

test:

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

Nota: ho dato alla funzione un nome per amor di leggibilità, questo non è strettamente necessario per usarlo (puoi semplicemente mettere l'argomento accanto alla funzione anonima) quindi non l'ho contato.


2

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
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.