Class: TTY::Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/markdown.rb,
lib/tty/markdown/color.rb,
lib/tty/markdown/error.rb,
lib/tty/markdown/theme.rb,
lib/tty/markdown/parser.rb,
lib/tty/markdown/symbols.rb,
lib/tty/markdown/version.rb,
lib/tty/markdown/converter.rb,
lib/tty/markdown/decorator.rb,
lib/tty/markdown/formatter.rb,
lib/tty/markdown/highlighter.rb

Overview

Responsible for converting Markdown to the terminal output

Defined Under Namespace

Classes: Color, Converter, Decorator, Error, Formatter, Highlighter, Parser, Symbols, Theme

Constant Summary collapse

VERSION =
"0.7.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(color: :auto, indent: 2, mode: TTY::Color.mode, symbols: {}, theme: {}, width: TTY::Screen.width, **document_options) ⇒ Markdown

Create a TTY::Markdown instance

Examples:

tty_markdown = TTY::Markdown.new
tty_markdown = TTY::Markdown.new(mode: 16)
tty_markdown = TTY::Markdown.new(symbols: :ascii)
tty_markdown = TTY::Markdown.new(theme: {link: :blue})

Parameters:

  • color (String, Symbol) (defaults to: :auto)

    the color support out of always, auto or never

  • indent (Integer) (defaults to: 2)

    the output indent

  • mode (Integer) (defaults to: TTY::Color.mode)

    the color mode

  • symbols (Hash, String, Symbol, nil) (defaults to: {})

    the output symbols

  • theme (Hash{Symbol => Array, String, Symbol}, nil) (defaults to: {})

    the color theme

  • width (Integer) (defaults to: TTY::Screen.width)

    the maximum width

  • document_options (Hash)

    the document parser options

Raises:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/tty/markdown.rb', line 112

def initialize(
  color: :auto,
  indent: 2,
  mode: TTY::Color.mode,
  symbols: {},
  theme: {},
  width: TTY::Screen.width,
  **document_options
)
  @converter_options = {
    enabled: Color.new(color).to_enabled,
    indent: indent,
    input: INPUT_PARSER,
    mode: mode,
    symbols: Symbols.from(symbols),
    theme: Theme.from(theme),
    width: width
  }.merge(document_options)
end

Class Method Details

.parse(content, **options) ⇒ String

Parse Markdown content

Examples:

TTY::Markdown.parse("# TTY Toolkit")
TTY::Markdown.parse("# TTY Toolkit", mode: 16)

Parameters:

  • content (String)

    the Markdown content

  • options (Hash)

    the conversion options

Returns:

  • (String)

    the converted terminal output

Raises:

See Also:



49
50
51
# File 'lib/tty/markdown.rb', line 49

def self.parse(content, **options)
  new(**options).parse(content)
end

.parse_file(path, **options) ⇒ String

Parse a Markdown file

Examples:

TTY::Markdown.parse_file("example.md")
TTY::Markdown.parse_file("example.md", mode: 16)

Parameters:

  • path (String)

    the Markdown file path

  • options (Hash)

    the conversion options

Returns:

  • (String)

    the converted terminal output

Raises:

See Also:



75
76
77
# File 'lib/tty/markdown.rb', line 75

def self.parse_file(path, **options)
  new(**options).parse_file(path)
end

Instance Method Details

#parse(content) ⇒ String

Parse Markdown content

Examples:

tty_markdown.parse("# TTY Toolkit")

Parameters:

  • content (String)

    the Markdown content

Returns:

  • (String)

    the converted terminal output



144
145
146
147
# File 'lib/tty/markdown.rb', line 144

def parse(content)
  document = Kramdown::Document.new(content, @converter_options)
  Converter.convert(document.root, document.options).join
end

#parse_file(path) ⇒ String

Parse a Markdown file

Examples:

tty_markdown.parse_file("example.md")

Parameters:

  • path (String)

    the Markdown file path

Returns:

  • (String)

    the converted terminal output



161
162
163
# File 'lib/tty/markdown.rb', line 161

def parse_file(path)
  parse(::File.read(path))
end