Class: NRSER::MeanStreak

Inherits:
Object show all
Defined in:
lib/nrser/mean_streak.rb,
lib/nrser/mean_streak/document.rb

Overview

Declarations

Defined Under Namespace

Classes: Document

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ MeanStreak

Returns a new instance of MeanStreak.



79
80
81
82
# File 'lib/nrser/mean_streak.rb', line 79

def initialize &block
  @type_renderers = {}
  block.call( self ) if block
end

Instance Attribute Details

#type_renderersHash<Symbol, Proc> (readonly)

TODO document ‘type_renderers` attribute.

Returns:



75
76
77
# File 'lib/nrser/mean_streak.rb', line 75

def type_renderers
  @type_renderers
end

Class Method Details

.defaultNRSER::MeanStreak

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

Returns:



51
52
53
54
# File 'lib/nrser/mean_streak.rb', line 51

def self.default
  # TODO cache?
  new
end

.parse(source, options = :DEFAULT, extensions = []) ⇒ CommonMarker::Node

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

string - String to be parsed option - A Symbol or of Symbols indicating the parse options extensions - An of Symbols indicating the extensions to use

Returns:

  • (CommonMarker::Node)

    The ‘document` node.



66
67
68
# 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

Instance Method Details

#parse(source, **options) ⇒ Object



93
94
95
96
97
98
# 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) ⇒ String

Render a Document or a source string.

Parameters:

Returns:

  • (String)

    Rendered string.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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) ⇒ Object

Instance Methods



88
89
90
# File 'lib/nrser/mean_streak.rb', line 88

def render_type type, &renderer
  @type_renderers[type] = renderer
end