Module: Notorious

Defined in:
lib/notorious.rb,
lib/notorious/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.build(opts) ⇒ Object



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
# File 'lib/notorious.rb', line 8

def self.build(opts)
  file_name = opts[:file_name]
  stylesheet = opts[:stylesheet]
  title = opts[:title]
  output = opts[:output]
  verbose = opts[:verbose]


  # make the html
  html = self.render(file_name, stylesheet)

  # write to the output file
  outfile = File.new(File.expand_path(output), 'w')
  outfile.write(html)
  outfile.close

  if verbose
    puts "your notes are at #{output}. Attempting to open them with a web browser"
  end

  # try to open them
  begin
    `open #{output}`
  rescue
    puts "your notes are at #{output}. Please open them with a web browser"
  end
end

.ensure_extension(file, ext) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/notorious.rb', line 78

def self.ensure_extension(file, ext)
  split = file.split('.')
  unless split.length > 1 && split[-1] == ext
    file = "#{file}.#{ext}"
  end
  file
end

.html(styles, title, body) ⇒ Object



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

def self.html(styles, title, body)

"<!DOCTYPE html>\n<html>\n<head>\n<style type='text/css'>\n  \#{styles}\n</style>\n<title>\#{title}</title>\n</head>\n<body>\n\#{body}\n</body>\n</html>\n"

end

.render(file_name, stylesheet) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/notorious.rb', line 36

def self.render(file_name, stylesheet)
  begin
    styles = File.new(stylesheet, 'r').read
  rescue
    raise RuntimeError, "Could not load the stylesheet. Make sure there is a file at #{stylesheet}"
  end

  # read in and convert the markdown file
  begin
    md_file = File.new(file_name, 'r')
  rescue
    raise ArgumentError, "Could not find the file you specified: #{file_name}"
  end

  # convert markdown to HTML
  md_contents = md_file.read
  md = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
  body = md.render(md_contents)
  md_file.close

  self.html(styles, title, body)
end

.validate_extension(file, ext) ⇒ Object



86
87
88
89
90
91
# File 'lib/notorious.rb', line 86

def self.validate_extension(file, ext)
  split = file.split('.')
  unless split.length > 1 && ext.include?(split[-1])
    raise ArgumentError, "Input must have extension in #{ext}"
  end
end