Class: Mkmatter::Methods

Inherits:
Object
  • Object
show all
Defined in:
lib/mkmatter/cli/methods.rb

Class Method Summary collapse

Class Method Details

.check_if_jekyllBoolean

Returns whether current directory is inside a jekyll site source directory.

Returns:

  • (Boolean)

    whether current directory is inside a jekyll site source directory



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/mkmatter/cli/methods.rb', line 7

def Methods.check_if_jekyll
  cwd = Pathname.new('.')
  cwd.ascend do |path|
    if path.join('_config.yml').exist?
      break true
    elsif cwd.to_s == '/'
      # hit root, stop
      break false
    end
  end
end

.find_front_matter(type, key) ⇒ Hash

Returns front matter.

Parameters:

  • type (String)

    type of content

  • key (String)

    root yaml directive to return

Returns:

  • (Hash)

    front matter



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mkmatter/cli/methods.rb', line 59

def Methods.find_front_matter(type, key)
  unless type =~ /^(post|page)$/
    $stderr.puts "#{HighLine.color('Error', :red, :bold)}: Invalid Argument, allowed values: post, page"
    exit 1
  end
  files             = {}
  html_front_matter = []
  md_front_matter   = []
  front_matter      = {}
  case type
    when 'page'
      Find.find(Methods.get_jekyll_root.to_s) do |path|
        Find.prune if path =~ /(_includes|_layouts|_docs|_site)/ # don't include layouts, includes, site, docs
        Find.prune if path =~ /(_posts)/ # don't include our own posts either
        Find.prune if path =~ /(vendor\/bundle)/ # don't include vendor/
        Find.prune if path =~ /(\/tag\/)/ # don't include our own tags
        html_front_matter << path if path =~ /.*\.html$/
        md_front_matter << path if path =~ /.*\.md$/
      end
    
    when 'post'
      Find.find(Pathname(Methods.get_jekyll_root).join('_posts').to_path) do |path|
        html_front_matter << path if path =~ /.*\.html$/
        md_front_matter << path if path =~ /.*\.md$/
      end
    else
      # noop
  end
  files['html'] = html_front_matter
  files['md']   = md_front_matter
  files.each do |ftype, array|
    array.each do |ele|
      front_matter[ele] = YAML.load_file(ele)[key]
    end
  end
  front_matter.select! {|k, v| !v.nil?}
  front_matter
end

.get_jekyll_rootPathname

Returns path of current jekyll site source root.

Returns:

  • (Pathname)

    path of current jekyll site source root



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/mkmatter/cli/methods.rb', line 20

def Methods.get_jekyll_root
  if Methods.check_if_jekyll
    cwd = Pathname.new('.').realdirpath
    cwd.ascend do |path|
      if path.join('_config.yml').exist?
        return path
      else
        next
      end
    end
  end
end

.launch_editor(editor, file_path) ⇒ NilClass

Returns nil.

Parameters:

  • editor (String)

    An editor to use

  • file_path (String)

    Path to file to open

Returns:

  • (NilClass)

    nil



46
47
48
49
50
51
52
53
54
# File 'lib/mkmatter/cli/methods.rb', line 46

def Methods.launch_editor(editor, file_path)
  hl = HighLine.new($stdin, $stderr, 80)
  if file_path
    if hl.agree("Would you like to open an editor? ('editor' command) ", true)
      pid = spawn("#{editor} #{file_path}")
      Process.wait pid
    end
  end
end

.which(cmd) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/mkmatter/cli/methods.rb', line 33

def Methods.which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each { |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    }
  end
  return nil
end