Module: TTY::Markdown

Defined in:
lib/tty/markdown.rb,
lib/tty/markdown/version.rb,
lib/tty/markdown/converter.rb,
lib/tty/markdown/syntax_highlighter.rb

Overview

Responsible for converting Markdown to the terminal output

Defined Under Namespace

Modules: SyntaxHighliter Classes: Converter

Constant Summary collapse

SYMBOLS =
{
  arrow: "»",
  bullet: "",
  bar: "",
  diamond: "",
  pipe: "",
  line: "",
  hellip: "",
  laquo: "«",
  laquo_space: "« ",
  raquo: "»",
  raquo_space: " »",
  ndash: "-",
  mdash: "\u2014",
  lsquo: "",
  rsquo: "",
  ldquo: "",
  rdquo: "",
  top_left: "",
  top_right: "",
  top_center: "",
  mid_left: "",
  mid_right: "",
  mid_center: "",
  bottom_right: "",
  bottom_left: "",
  bottom_center: "",
  paren_left: "(",
  paren_right: ")",
  bracket_left: "[",
  bracket_right: "]",
  hash: "#",
  delete: "\u0336"
}.freeze
ASCII_SYMBOLS =
{
  arrow: "->",
  bullet: "*",
  diamond: "*",
  bar: "",
  pipe: "|",
  line: "-",
  hellip: "...",
  laquo: "<<",
  laquo_space: "<< ",
  raquo: ">>",
  raquo_space: " >>",
  ndash: "-",
  mdash: "--",
  lsquo: "\"",
  rsquo: "\"",
  ldquo: "\"",
  rdquo: "\"",
  top_left: "+",
  top_right: "+",
  top_center: "+",
  mid_left: "+",
  mid_right: "+",
  mid_center: "+",
  bottom_right: "+",
  bottom_left: "+",
  bottom_center: "+",
  paren_left: "(",
  paren_right: ")",
  bracket_left: "[",
  bracket_right: "]",
  hash: "#",
  delete: "\u0336"
}.freeze
THEME =
{
  em: :yellow,
  header: %i[cyan bold],
  hr: :yellow,
  link: %i[yellow underline],
  list: :yellow,
  strong: %i[yellow bold],
  table: :yellow,
  quote: :yellow,
  image: :bright_black,
  note: :yellow,
  comment: :bright_black
}.freeze
VERSION =
"0.7.2"

Class Method Summary collapse

Class Method Details

.parse(source, color: :auto, indent: 2, mode: TTY::Color.mode, symbols: {}, theme: {}, width: TTY::Screen.width, **doc_opts) ⇒ String

Parse a markdown string

Examples:

TTY::Markdown.parse("# Header")

Parameters:

  • source (String)

    the source with markdown

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

    the output coloring support out of always, auto or never

  • indent (Integer) (defaults to: 2)

    the converted output indent

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

    the number of supported colors

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

    the converted output symbols

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

    the converted output color theme

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

    the width at which to wrap content

  • doc_opts (Hash)

    the markdown document parser options

Returns:

  • (String)

    the converted terminal output



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/tty/markdown.rb', line 126

def parse(source,
          color: :auto,
          indent: 2,
          mode: TTY::Color.mode,
          symbols: {},
          theme: {},
          width: TTY::Screen.width,
          **doc_opts)
  converter_options = {
    enabled: color_enabled(color),
    indent: indent,
    input: "KramdownExt",
    mode: mode,
    symbols: build_symbols(symbols),
    theme: build_theme(theme),
    width: width
  }
  doc = Kramdown::Document.new(source, converter_options.merge(doc_opts))
  Converter.convert(doc.root, doc.options).join
end

.parse_file(path, **options) ⇒ String

Parse a markdown document

Examples:

TTY::Markdown.parse_file("example.md")

Parameters:

  • path (String)

    the file path

  • options (Hash)

    the conversion options

Returns:

  • (String)

    the converted terminal output



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

def parse_file(path, **options)
  parse(::File.read(path), **options)
end