Code block

Source code with syntax colours and a copy button.

Helper
code_block(
  source = nil,          # or the block's text
  language: :plaintext,  # any Rouge lexer: :ruby, :erb, :javascript, :css, :json, :sql…
  title: nil,
  copy: true,
  **html
)

Highlighting runs on the server with Rouge, so it works without JavaScript and supports 200+ languages (ruby, erb, javascript, html, css, json, yaml, sql, shell…). The colours are Trail UI's code-* tokens. Pass the source as the first argument or as the block's text; the title defaults to the language name.

app/models/quest.rb
class Quest < ApplicationRecord
  belongs_to :project
  has_many :missions, dependent: :destroy

  scope :active, -> { where(completed_at: nil, abandoned_at: nil) }

  def progress
    missions.completed.count.fdiv(missions.count) # 0.0..1.0
  end
end
JSON
{ "quest": "Lançar o canal", "missions": 13, "continuous": false }
shell
bin/rails server -p 3000

Options

OptionWhat it does
languageRouge lexer name or alias; unknown names fall back to plain text
titleText on the bar above the code (a file name, for example)
copyShow the copy button (default true)