Class: Gloo::Objs::Markdown

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/data/markdown.rb

Constant Summary collapse

KEYWORD =
'markdown'.freeze
KEYWORD_SHORT =
'md'.freeze

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, #add_children_on_create?, #add_default_children, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, #find_child_resolve_alias, #find_child_value, help, inherited, #initialize, #is_alias?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_unload, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.md_2_html(md) ⇒ Object

Convert markdown to HTML using the Redcarpet markdown processor.



119
120
121
122
123
124
125
126
127
128
# File 'lib/gloo/objs/data/markdown.rb', line 119

def self.md_2_html( md )
  markdown = Redcarpet::Markdown.new( 
    Redcarpet::Render::HTML, 
    autolink: true, 
    fenced_code_blocks: true,
    tables: true, 
    strikethrough: true )
    
  return markdown.render( md )
end

.messagesObject

Get a list of message names that this object receives.



58
59
60
# File 'lib/gloo/objs/data/markdown.rb', line 58

def self.messages
  return super + %w[show render update_asset_path]
end

.short_typenameObject

The short name of the object type.



25
26
27
# File 'lib/gloo/objs/data/markdown.rb', line 25

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



18
19
20
# File 'lib/gloo/objs/data/markdown.rb', line 18

def self.typename
  return KEYWORD
end

Instance Method Details

#line_countObject

Get the number of lines of text.



47
48
49
# File 'lib/gloo/objs/data/markdown.rb', line 47

def line_count
  return value.split( "\n" ).count
end

#msg_renderObject

Render the markdown as HTML. Needs an optional parameter of where to put the rendered html. The html will be in ‘it’ as well.



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gloo/objs/data/markdown.rb', line 74

def msg_render
  html = Gloo::Objs::Markdown.md_2_html( value )

  # Put the HTML in the optional parameter if one is given.
  if @params&.token_count&.positive?
    pn = Gloo::Core::Pn.new( @engine, @params.first )
    o = pn.resolve
    o.set_value html
  end

  # Put the HTML in it, in any case.
  @engine.heap.it.set_to html
end

#msg_showObject

Show the markdown data in the terminal.



65
66
67
# File 'lib/gloo/objs/data/markdown.rb', line 65

def msg_show
  @engine.platform.show self.value
end

#msg_update_asset_pathObject

Update the asset path in the markdown. Take out leading relative path so that path starts at the asset root.



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gloo/objs/data/markdown.rb', line 93

def msg_update_asset_path
  data = self.value
  out_data = ""
  
  data.lines.each do |line|
    if line.include?( '![' ) && line.include?( '](') && line.include?( '/asset/')
      prefix = line[ 0, ( line.index( '](' ) + 2 ) ]
      suffix = line[ (line.index( '/asset/' )) .. -1 ]
      out_data << "#{prefix}#{suffix}"
    else
      out_data << line
    end
  end

  self.value = out_data
end

#multiline_value?Boolean

Does this object support multi-line values? Initially only true for scripts.

Returns:



40
41
42
# File 'lib/gloo/objs/data/markdown.rb', line 40

def multiline_value?
  return false
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



32
33
34
# File 'lib/gloo/objs/data/markdown.rb', line 32

def set_value( new_value )
  self.value = new_value.to_s
end