class NRSER::MeanStreak

Tag up terminals with color and style. Uses {CommonMarker} for the parsing (Markdown / CommonMark / GFM syntax).

{NRSER::MeanStreak} instances hold configuration and provide functionality through instance methods, making it easy to use multiple configurations or subclass {NRSER::MeanStreak} to further customize functionality.

An instance with default configuration is available via the {.default} class method, and additional class methods are provided that proxy to the default's instance methods, providing convenient use of the default config.

Declarations

Attributes

type_renderers[R]

TODO document `type_renderers` attribute.

@return [Hash<Symbol, Proc>]

Public Class Methods

default() click to toggle source

Get the default instance, which has the default configuration and is used by the class methods.

@return [NRSER::MeanStreak]

# File lib/nrser/mean_streak.rb, line 51
def self.default
  # TODO cache?
  new
end
new(&block) click to toggle source
# File lib/nrser/mean_streak.rb, line 79
def initialize &block
  @type_renderers = {}
  block.call( self ) if block
end
parse(source, options = :DEFAULT, extensions = []) click to toggle source

Public: Parses a Markdown string into a `document` node.

string - {String} to be parsed option - A {Symbol} or {Array of Symbol}s indicating the parse options extensions - An {Array of Symbol}s indicating the extensions to use

@return [CommonMarker::Node]

The `document` node.
# File lib/nrser/mean_streak.rb, line 66
def self.parse source, options = :DEFAULT, extensions = []
  default.parse source, cm_options: options, cm_extensions: extensions
end

Public Instance Methods

parse(source, **options) click to toggle source
# File lib/nrser/mean_streak.rb, line 93
def parse source, **options
  NRSER::MeanStreak::Document.parse \
    source,
    **options,
    mean_streak: self
end
render(doc_or_source) click to toggle source

Render a {NRSER::MeanStreak::Document} or a source string.

@param [NRSER::MeanStreak::Document | String] doc_or_source

Document or source to render.

@return [String]

Rendered string.
# File lib/nrser/mean_streak.rb, line 109
def render doc_or_source
  case doc_or_source
  when NRSER::MeanStreak::Document
    doc_or_source.render
    
  when ''
    # Short-circuit for empty strings because CommonMark's document
    # node for empty strings returns a weird `#sourcepos` that ends before it
    # begins (start: line 1, column 1; end: line 0, column 0).
    #
    # Going to protect against line 0 / column 0 in
    # {NRSER::MeanStreak::Document#source_byte_indexes} too but since the
    # empty strings just renders the empty string we can just return that
    # here.
    #
    ''
  
  else
    parse( doc_or_source ).render
  end
end
render_type(type, &renderer) click to toggle source

Instance Methods

# File lib/nrser/mean_streak.rb, line 88
def render_type type, &renderer
  @type_renderers[type] = renderer
end