Module: Maliq::FileUtils

Included in:
Converter
Defined in:
lib/maliq/file_utils.rb

Constant Summary collapse

SPLIT_MARKER =
/^<<<---(.*)--->>>\n/

Class Method Summary collapse

Class Method Details

.create_filename(prev_name) ⇒ Object

create filename from previous filename with sequence number.



34
35
36
# File 'lib/maliq/file_utils.rb', line 34

def create_filename(prev_name)
  prev_name[/\d+$/] ? prev_name.next : prev_name + '02'
end

.retrieveYFM(text) ⇒ Object

Retrieve Yaml Front Matter from text. Returns [yfm, text]



9
10
11
12
13
14
15
16
# File 'lib/maliq/file_utils.rb', line 9

def retrieveYFM(text)
  yfm = ""
  text.match(/^(---\s*\n.*?\n?)^(---\s*$\n?)/m) do |md|
    yfm = md.to_s
    text = md.post_match
  end
  return yfm, text
end

.split(path, marker = nil) ⇒ Object

Split a file with SPLIT_MARKER. Returns a Hash of filename key with its content.



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

def split(path, marker=nil)
  marker ||= SPLIT_MARKER
  content = File.read(path)
  filename = File.basename(path, '.*')
  yfm, content = retrieveYFM(content)
  contents = [filename] + content.split(marker)
  prev_name = filename
  contents.each_slice(2).with({}) do |(fname, text), h|
    fname = prev_name = create_filename(prev_name) if fname.strip.empty?
    h[fname.strip] = yfm + text
  end
end