Skip to content

Commit

Permalink
Add color setting to control when colors are applied
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Sep 2, 2020
1 parent af78238 commit 501e3b3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 10 deletions.
20 changes: 18 additions & 2 deletions lib/tty/markdown.rb
Expand Up @@ -108,15 +108,18 @@ module Markdown
# the color names for markdown elements
# @param [Integer] :width
# the width at which to wrap content
# @param [Boolean] :color
# when to enable coloring out of always, never or auto
# @param [Hash] :doc_opts
# the markdown document parser options
#
# @api public
def parse(source, width: TTY::Screen.width, theme: THEME, indent: 2,
colors: TTY::Color.mode, symbols: {}, **doc_opts)
colors: TTY::Color.mode, symbols: {}, color: :auto,
**doc_opts)
convert_options = { width: width, indent: indent, theme: theme,
colors: colors, symbols: build_symbols(symbols),
input: "KramdownExt" }
input: "KramdownExt", enabled: color_enabled(color) }
doc = Kramdown::Document.new(source, convert_options.merge(doc_opts))
Converter.convert(doc.root, doc.options).join
end
Expand All @@ -130,6 +133,19 @@ def parse_file(path, **options)
end
module_function :parse_file

# Convert color setting to Pastel setting
#
# @api private
def color_enabled(color)
case color.to_s
when "always" then true
when "never" then false
else nil
end
end
module_function :color_enabled
private_class_method :color_enabled

# Extract and build symbols
#
# @api private
Expand Down
8 changes: 4 additions & 4 deletions lib/tty/markdown/converter.rb
Expand Up @@ -19,10 +19,10 @@ def initialize(root, options = {})
super
@current_indent = 0
@indent = options[:indent]
mode = options[:colors]
pastel_opts = mode <= 0 ? { enabled: false } : {}
@pastel = Pastel.new(**pastel_opts)
@color_opts = { mode: mode, color: @pastel.yellow.detach }
@pastel = Pastel.new(enabled: options[:enabled])
@color_opts = { mode: options[:colors],
color: @pastel.yellow.detach,
enabled: options[:enabled] }
@width = options[:width]
@theme = options[:theme].each_with_object({}) do |(key, val), acc|
acc[key] = Array(val)
Expand Down
7 changes: 5 additions & 2 deletions lib/tty/markdown/syntax_highlighter.rb
Expand Up @@ -45,15 +45,18 @@ def guess_lang(code)
# the color mode supported by the terminal
# @param [String] lang
# the code snippet language
# @param [Boolean] enabled
# whether or not coloring is enabled
# @param [Proc] color
# the fallback coloring
#
# @api public
def highlight(code, mode: 256, lang: nil, color: ->(line) { line })
def highlight(code, mode: 256, lang: nil, enabled: nil,
color: ->(line) { line })
lang = guess_lang(code) if lang.nil?
lexer = Rouge::Lexer.find_fancy(lang, code) || Rouge::Lexers::PlainText

if mode <= 0
if enabled == false
code
elsif 256 <= mode
formatter = Rouge::Formatters::Terminal256.new
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/codeblock_spec.rb
Expand Up @@ -157,7 +157,7 @@ def say
end
```
TEXT
parsed = TTY::Markdown.parse(markdown, colors: 0)
parsed = TTY::Markdown.parse(markdown, color: :never)
expect(parsed).to eq([
"class Greeter",
" def say",
Expand Down
30 changes: 30 additions & 0 deletions spec/unit/color_spec.rb
@@ -0,0 +1,30 @@
# frozen_string_literal: true

RSpec.describe TTY::Markdown, "color" do
it "switches off coloring for all elements" do
markdown =<<-TEXT
# Header
**bold**
```
class Greeter
def say
end
end
```
TEXT
parsed = TTY::Markdown.parse(markdown, color: :never)

expect(parsed).to eq([
"Header",
"",
"bold",
"",
"class Greeter",
" def say",
" end",
"end"
].join("\n"))
end
end
2 changes: 1 addition & 1 deletion spec/unit/header_spec.rb
Expand Up @@ -8,7 +8,7 @@
end

it "disables top level header coloring" do
parsed = TTY::Markdown.parse("Header1\n======", colors: 0)
parsed = TTY::Markdown.parse("Header1\n======", color: :never)

expect(parsed).to eq("Header1\n")
end
Expand Down

0 comments on commit 501e3b3

Please sign in to comment.