module TTY::Markdown
Constants
- ASCII_SYMBOLS
- SYMBOLS
- THEME
- VERSION
Public Class Methods
parse(source, width: TTY::Screen.width, theme: THEME, indent: 2, mode: TTY::Color.mode, symbols: {}, color: :auto, **doc_opts)
click to toggle source
Parse a markdown string
@param [String] source
the source with markdown
@param [Integer] :mode
a number of colors supported
@param [Integer] :indent
the indent of the converted output
@param [Hash<Symbol, String>] :symbols
the symbols to use when generating output
@param [Hash<Symbol, Symbol>] :theme
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
# File lib/tty/markdown.rb, line 117 def parse(source, width: TTY::Screen.width, theme: THEME, indent: 2, mode: TTY::Color.mode, symbols: {}, color: :auto, **doc_opts) convert_options = { width: width, indent: indent, theme: theme, mode: mode, symbols: build_symbols(symbols), input: "KramdownExt", enabled: color_enabled(color) } doc = Kramdown::Document.new(source, convert_options.merge(doc_opts)) Converter.convert(doc.root, doc.options).join end
parse_file(path, **options)
click to toggle source
Pase a markdown document
@api public
# File lib/tty/markdown.rb, line 131 def parse_file(path, **options) parse(::File.read(path), **options) end
Private Class Methods
build_symbols(options)
click to toggle source
Extract and build symbols
@api private
# File lib/tty/markdown.rb, line 152 def build_symbols(options) if options == :ascii ASCII_SYMBOLS elsif options.is_a?(Hash) base_symbols = options[:base] == :ascii ? ASCII_SYMBOLS : SYMBOLS if options[:override].is_a?(Hash) base_symbols.merge(options[:override]) else base_symbols end else SYMBOLS end end
color_enabled(color)
click to toggle source
Convert color setting to Pastel setting
@api private
# File lib/tty/markdown.rb, line 139 def color_enabled(color) case color.to_s when "always" then true when "never" then false else nil end end
Private Instance Methods
build_symbols(options)
click to toggle source
Extract and build symbols
@api private
# File lib/tty/markdown.rb, line 152 def build_symbols(options) if options == :ascii ASCII_SYMBOLS elsif options.is_a?(Hash) base_symbols = options[:base] == :ascii ? ASCII_SYMBOLS : SYMBOLS if options[:override].is_a?(Hash) base_symbols.merge(options[:override]) else base_symbols end else SYMBOLS end end
color_enabled(color)
click to toggle source
Convert color setting to Pastel setting
@api private
# File lib/tty/markdown.rb, line 139 def color_enabled(color) case color.to_s when "always" then true when "never" then false else nil end end
parse(source, width: TTY::Screen.width, theme: THEME, indent: 2, mode: TTY::Color.mode, symbols: {}, color: :auto, **doc_opts)
click to toggle source
Parse a markdown string
@param [String] source
the source with markdown
@param [Integer] :mode
a number of colors supported
@param [Integer] :indent
the indent of the converted output
@param [Hash<Symbol, String>] :symbols
the symbols to use when generating output
@param [Hash<Symbol, Symbol>] :theme
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
# File lib/tty/markdown.rb, line 117 def parse(source, width: TTY::Screen.width, theme: THEME, indent: 2, mode: TTY::Color.mode, symbols: {}, color: :auto, **doc_opts) convert_options = { width: width, indent: indent, theme: theme, mode: mode, symbols: build_symbols(symbols), input: "KramdownExt", enabled: color_enabled(color) } doc = Kramdown::Document.new(source, convert_options.merge(doc_opts)) Converter.convert(doc.root, doc.options).join end
parse_file(path, **options)
click to toggle source
Pase a markdown document
@api public
# File lib/tty/markdown.rb, line 131 def parse_file(path, **options) parse(::File.read(path), **options) end