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

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.0"

Class Method Summary collapse

Class Method Details

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

Parse a markdown string

Parameters:

  • source (String)

    the source with markdown

  • :mode (Integer)

    a number of colors supported

  • :indent (Integer)

    the indent of the converted output

  • :symbols (Hash<Symbol, String>)

    the symbols to use when generating output

  • :theme (Hash<Symbol, Symbol>)

    the color names for markdown elements

  • :width (Integer)

    the width at which to wrap content

  • :color (Boolean)

    when to enable coloring out of always, never or auto

  • :doc_opts (Hash)

    the markdown document parser options


117
118
119
120
121
122
123
124
125
# 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) ⇒ Object

Pase a markdown document


131
132
133
# File 'lib/tty/markdown.rb', line 131

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