Class: MDNotes

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

Instance Method Summary collapse

Constructor Details

#initializeMDNotes

Params



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mdnotes.rb', line 5

def initialize
	@first_setup = false
	@notes = Dir['./md/*.md']
	@pwd = Dir::pwd;
	@publishing = false

	@params = {}

	# Set the params
	ARGV.each do |arg|
		cur_arg = arg.tr "-", ""
		@params[cur_arg] = true
	end
end

Instance Method Details

#check_directoriesObject

Check if directory is ready



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/mdnotes.rb', line 22

def check_directories
	missing_dirs = {}

	['pdf','html', 'md', 'html/images'].each do |f|
		cur_dir = "./" + f
		curr_dir_exists = FileTest::directory?(cur_dir) 
		missing_dirs[f] = curr_dir_exists
		@first_setup ||= curr_dir_exists
	end

	# Make the name meaningful
	@first_setup = !@first_setup

	if @first_setup
		puts "Detecting first time setup..."
	end

	# Create the mssign directories
	create_directories missing_dirs
end

#compile_notesObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mdnotes.rb', line 58

def compile_notes
	if !@first_setup
		@notes.each do |n|
			name = n.split('/')[-1].split('.')[0]

			puts "Compiling #{name}..."

			raw = open(n).read
			md = RDiscount.new raw
			out = md.to_html
			open("./html/#{name}.html", "w").write out
		end

	end
	puts "Notes compiled."
end

#create_directories(dirs) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mdnotes.rb', line 43

def create_directories dirs
	dirs.each_pair do |k,v|

		cur_dir = @pwd + "/" + k

		if (!v)
			Dir::mkdir(cur_dir) 
			puts "Creating directory #{k}..."
		end
	end
end

#publish_notesObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mdnotes.rb', line 76

def publish_notes
	require 'fileutils'
	require 'pdfkit'
	
	publishing = (@params['p'] || @params['publish'])

	if publishing
		pdf_home = "./pdf"
		Dir::mkdir(pdf_home) unless FileTest::directory?(pdf_home)	

		Dir["./html/*.html"].each do |html|

			f_name = html.split("/")[-1].split(".")[0]
			output_file = File.join(".", "pdf", f_name + ".pdf")
			html_file = File.open(html, 'r') { |f| f.read }

			# Delete the file first.
			FileUtils.rm output_file if FileTest.exists? output_file

			puts "Publishing #{f_name}..."
			
			kit = PDFKit.new(html_file, :page_size => 'Letter')
			kit.to_file(output_file)
		end
		puts "Notes published."
	end

end

#startObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/mdnotes.rb', line 106

def start
	if @params['h'] || @params['help']
	
	tabs = 3
	str = %Q{			
MDNOTES
-------

Usage: mdnotes [optional params]

Description:
- Using a terminal, [cd] into the directory you want to takes notes.
- Use command [mdnotes] to create a notes directory.
- Create your markdown (.md) in the md/ directory.
- Use the command [mdnotes] to 'compile' your notes into html. These will be located in the html/ folder.
- Use [mdnotes -p] or [mdnotes --publish] to create PDFs of your notes. These will be located in the pdf/ folder
- If you want to include images in your notes you can place them in the images folder located under ./html/images. Use ![alt-text](./images/my_image.png) to reference an image.

Options:
-p, --publish#{"\t"*tabs}Create PDFs.
-h, --help#{"\t"*tabs}Show the help.

Repo:
You can find this at http://bitbucket.org/ugorelik/mdnotes
---
	}
	
	puts str
	else
		check_directories
		compile_notes
		publish_notes
	end
end