Class: MdSplitter::Splitter

Inherits:
Object
  • Object
show all
Defined in:
lib/md_splitter/splitter.rb

Overview

Wraps Kramdown for converting Markdown documents to html, with a few extra features

Constant Summary collapse

KRAM_OPTIONS =
{ :input => "kramdown", :output => "html" }
LIVE_PREFIX =
"Live ->"
DRY_RUN_PREFIX =
"Dry Run ->"
DEFAULTS =
{
  :dry_run     => false,
  :split_on    => nil,
  :output_path => nil,
  :manifest    => nil
}

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Splitter

Returns a new instance of Splitter.



22
23
24
# File 'lib/md_splitter/splitter.rb', line 22

def initialize opts = {} 
	@mode = "LIVE"
end

Instance Method Details

#split(path, options = {}) ⇒ Object

  • :split_on -> STRING marker to split the document on

    - :dry_run -> BOOL performs no file actions if TRUE 
    - :output_path -> STRING where to save resulting files
    


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/md_splitter/splitter.rb', line 34

def split path, options = {}  
@options = DEFAULTS.merge options 
pages = Array.new     

  src_markdown = read_file_contents path 
  target_path = get_target_path(path)
  create_output_path target_path
  source_file_name = get_source_file_name(path)
  say "No files will be hurt in this operation" if @options[:dry_run]

  if options[:split_on].nil? 
    htm = convert_md_to_html(src_markdown)
    output_file_name = get_target_file_name(source_file_name) 
    pages << "#{output_file_name}.html"
    save_file File.join("#{target_path}","#{output_file_name}.html"), htm
    say "Converting #{source_file_name} to html"
  else  
    say "Splitting #{source_file_name} on '#{options[:split_on]}'"
    mds = src_markdown.split(options[:split_on]) 
output_path = target_path  
output_file_name = get_target_file_name(source_file_name)

if @options[:output_path].nil?    
  # if no output path specified, and more than one output file required
  # then create a child directory to store the multiple files
  target_path = File.join "#{target_path}", "#{output_file_name}" if mds.length > 1
  create_output_path target_path
 end
                                       
    mds.each do |md| 
      htm = convert_md_to_html(md) 
      file_suffix = ""
      if mds.length > 1 

		file_suffix = "_#{mds.index(md) + 1}" 
      end
      
      pages << "#{output_file_name}#{file_suffix}.html"                     
      save_file File.join("#{target_path}","#{output_file_name}#{file_suffix}.html"), htm
    end
  end   
                                             
  # save a manifest if requested
  save_manifest(pages, target_path) if @options[:manifest]
  
  return
end