Module: Notorious

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

Constant Summary collapse

VERSION =
"0.1.4"

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_names = opts[:file_names]
  stylesheet = opts[:stylesheet]
  title = opts[:title]
  output = opts[:output]
  verbose = opts[:verbose]


  # make the html
  html = self.render(file_names, title, 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



82
83
84
85
86
87
88
# File 'lib/notorious.rb', line 82

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/notorious.rb', line 63

def self.html(styles, title, body)

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

end

.render(file_names, title, stylesheet) ⇒ Object



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

def self.render(file_names, title, 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

  body = ""

  file_names.each do |f|
    # read in and convert the markdown file
    begin
      md_file = File.new(f, 'r')
    rescue
      raise ArgumentError, "Could not find the file you specified: #{f}"
    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
  end

  self.html(styles, title, body)
end

.validate_extension(file, ext) ⇒ Object



90
91
92
93
94
95
# File 'lib/notorious.rb', line 90

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