Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#! /usr/bin/env ruby
require 'optparse'
options = {
:parent_file => "index.md" ,
:parts_folder => "parts" ,
:suffix => "pandoc"
}
usage = "ruby include_markdown.rb [options]"
OptionParser.new do |opts|
opts.banner = usage
opts.separator ""
opts.separator "Options:"
opts.on("-p", "--parentfile STRING", String, "Path to the file in which snippets are to be included. Default is 'index.md'.") do |parentfile|
options[:parent_file] = parentfile
end
opts.on("-f", "--folder STRING", String, "Path to the folder containing snippets to be included. Default is 'parts'.") do |parts_folder|
options[:parts_folder] = parts_folder
end
opts.on("-s", "--suffix STRING", String, "Suffix of the markdown files to be included. Default is 'pandoc'.") do |suffix|
options[:suffix] = suffix
end
end.parse!
text = File.read(options[:parent_file])
text.gsub!(/\@include\(([^)]+?)\)/) do |include_statement|
replacement_path = File.join(options[:parts_folder], "#{$1}.#{options[:suffix]}")
File.read(replacement_path)
end
puts text