Skip to content

marcoroth/glamour-ruby

Repository files navigation

Glamour for Ruby

Stylesheet-based markdown rendering for your CLI apps.

Gem Version License

Ruby bindings for charmbracelet/glamour.
Render markdown documents & templates on ANSI compatible terminals.

Installation

Add to your Gemfile:

gem "glamour"

Or install directly:

gem install glamour

Usage

Basic Rendering

Render markdown with auto-detected style:

require "glamour"

puts Glamour.render("# Hello World")

Render with a specific style:

puts Glamour.render("# Hello", style: "dark")
puts Glamour.render("# Hello", style: "light")
puts Glamour.render("# Hello", style: "dracula")
puts Glamour.render("# Hello", style: "notty")

Render with word wrap:

puts Glamour.render(long_markdown, width: 80)

Render Options

Option Description
style "auto", "dark", "light", "notty", "dracula"
width Word wrap width
emoji Enable emoji rendering (:wave: → 👋)
preserve_newlines Preserve newlines in output
base_url Base URL for relative links
color_profile :auto, :true_color, :ansi256, :ansi, :ascii

Example with all options:

Glamour.render(markdown,
  style: "dark",
  width: 80,
  emoji: true,
  preserve_newlines: true,
  base_url: "https://example.com",
  color_profile: :true_color
)

Custom Styles with Hash

Define a custom style:

custom_style = {
  heading: { bold: true, color: "212" },
  strong: { bold: true, color: "196" },
  emph: { italic: true, color: "226" },
  code: { color: "203", background_color: "236" }
}

Render with the custom style:

Glamour.render_with_style("# Hello **World**", custom_style)

With width option:

Glamour.render_with_style("# Hello", custom_style, width: 60)

Style DSL

Define reusable styles using a Ruby DSL:

class MyStyle < Glamour::Style
  style :heading do
    bold true
    color "212"
  end

  style :h1 do
    prefix "# "
    color "99"
    bold true
  end

  style :strong do
    bold true
    color "196"
  end

  style :emph do
    italic true
    color "226"
  end

  style :code do
    color "203"
    background_color "236"
  end

  style :document do
    margin 2
  end
end

Use the style class directly:

MyStyle.render("# Hello **World**")
MyStyle.render("# Hello", width: 80)

Or pass to Glamour.render:

Glamour.render("# Hello", style: MyStyle)

Reusable Renderer

Create a renderer with preset options:

renderer = Glamour::Renderer.new(
  style: "dark",
  width: 80,
  emoji: true
)

Render multiple documents:

puts renderer.render("# Hello :wave:")
puts renderer.render("# Another document")

With a Style class:

renderer = Glamour::Renderer.new(style: MyStyle, width: 60)
puts renderer.render("# Styled output")

Available Style Elements

Block Elements

Element Description
document Root document wrapper
paragraph Text paragraphs
heading Base heading style (h1-h6 inherit from this)
h1 - h6 Individual heading levels
block_quote Block quotations
code_block Fenced code blocks
list List containers
item List items (bullets)
enumeration Numbered list items
table Markdown tables
hr Horizontal rules

Inline Elements

Element Description
text Base text styling
strong Bold text (**bold**)
emph Italic text (*italic*)
strikethrough Strikethrough text
code Inline code (`code`)
link Link elements
link_text Link text display
image Image references

Style Properties

Text decoration:

bold true
italic true
underline true
crossed_out true
faint true
inverse true
overlined true

Colors (ANSI 256 color codes):

color "212"
background_color "236"

Spacing:

margin 2
indent 1
indent_token "  "
level_indent 2

Prefix/suffix:

prefix "# "
suffix ""
block_prefix ""
block_suffix "\n"

Built-in Styles

  • "auto" - Auto-detect dark/light terminal
  • "dark" - Dark terminal theme
  • "light" - Light terminal theme
  • "notty" - No colors (for non-TTY output)
  • "dracula" - Dracula color scheme

Version Info

puts Glamour.version

Development

Requirements:

  • Go 1.23+
  • Ruby 3.2+

Install dependencies:

bundle install

Build the Go library and compile the extension:

bundle exec rake compile

Run tests:

bundle exec rake test

Run demos:

./demo/basic
./demo/styles
./demo/style_dsl

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/marcoroth/glamour-ruby.

License

The gem is available as open source under the terms of the MIT License.

Acknowledgments

This gem wraps charmbracelet/glamour, part of the excellent Charm ecosystem. Charm Ruby is not affiliated with or endorsed by Charmbracelet, Inc.


Part of Charm Ruby.

Charm Ruby

LipglossBubble TeaBubblesGlamourHuh?HarmonicaBubblezoneGumntcharts

The terminal doesn't have to be boring.

About

Stylesheet-based Markdown Rendering for Ruby, based on Charm's glamour.

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project