Class: AIA::Glow

Inherits:
Tools show all
Defined in:
lib/aia/tools/glow.rb

Overview

This class supports two use cases:

1) rendering markdown from an existing file
2) rendering markdown from a String object via a temporary file

In both cases a String object is created and returned that contains the
rendered version of the content so that it can be written to STDOUT
by the caller.

Constant Summary collapse

DEFAULT_PARAMETERS =

Magic: -2 just because I want it

"--width #{TTY::Screen.width-2}"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Tools

catalog, get_meta, inherited, load_tools, #meta, meta, search_for, setup_backend, validate_tools

Constructor Details

#initialize(content: nil, file_path: nil) ⇒ Glow

Returns a new instance of Glow.



35
36
37
38
# File 'lib/aia/tools/glow.rb', line 35

def initialize(content: nil, file_path: nil)
  @content = content
  @file_path = file_path
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



32
33
34
# File 'lib/aia/tools/glow.rb', line 32

def content
  @content
end

#file_pathObject

Returns the value of attribute file_path.



32
33
34
# File 'lib/aia/tools/glow.rb', line 32

def file_path
  @file_path
end

Instance Method Details

#build_command(file_path) ⇒ Object



41
42
43
# File 'lib/aia/tools/glow.rb', line 41

def build_command(file_path)
  "#{self.class.meta[:name]} #{DEFAULT_PARAMETERS} #{Shellwords.escape(file_path)}"
end

#runObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/aia/tools/glow.rb', line 46

def run
  return unless content || file_path

  if @file_path && File.exist?(@file_path)
    command = build_command(@file_path)
    system(command)
  else
    Tempfile.create(['glow', '.md']) do |file|
      file.write(@content)
      file.close
      command = build_command(file.path)
      system(command)
    end
  end
end