Class: Md

Inherits:
Gloo::Core::Obj
  • Object
show all
Defined in:
lib/md.rb

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

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/md.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.



57
58
59
# File 'lib/md.rb', line 57

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

.short_typenameObject

The short name of the object type.



23
24
25
# File 'lib/md.rb', line 23

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



16
17
18
# File 'lib/md.rb', line 16

def self.typename
  return KEYWORD
end

Instance Method Details

#line_countObject

Get the number of lines of text.



45
46
47
# File 'lib/md.rb', line 45

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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/md.rb', line 73

def msg_render
  html = MarkdownExt.render_extensions( value )
  html = Md.md_2_html( html )

  # 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.



64
65
66
# File 'lib/md.rb', line 64

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/md.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:

  • (Boolean)


38
39
40
# File 'lib/md.rb', line 38

def multiline_value?
  return false
end

#set_value(new_value) ⇒ Object

Set the value with any necessary type conversions.



30
31
32
# File 'lib/md.rb', line 30

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