Class: Mdexport

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

Class Method Summary collapse

Class Method Details

.generate_html_for(file_path) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mdexport.rb', line 80

def self.generate_html_for(file_path)
  file_content = File.read(file_path)
  html_body = Markdown.render(file_content)
  
  title = file_path.basename
  template = File.read("lib/templates/page.mustache")
  content = Mustache.render(template, :title => title, :yield => html_body)

  html_file_path = file_path.html_file_path
  FileUtils.rm(html_file_path) if File.exist?(html_file_path)
  File.write(html_file_path, content)
end

.refresh_page(keyword) ⇒ Object



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

def self.refresh_page keyword
  %x{osascript<<ENDGAME
        tell application "Safari"
          set windowList to every window
          repeat with aWindow in windowList
            set tabList to every tab of aWindow
            repeat with atab in tabList
              if (URL of atab contains "#{keyword}") then
                tell atab to do javascript "window.location.reload()"
              end if
            end repeat
          end repeat
        end tell
ENDGAME
}
end

.runObject



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
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/mdexport.rb', line 11

def self.run
  folder = nil
  watching = false
  cleaning = false
  
  if ARGV.size > 0
    ARGV.each do |param|
      if param == '-w' || param == '--watch'
        puts 'Watching file changes.'
        watching = true
      end
      
      if param == '-c' || param == '--clean'
        puts 'Removing html files.'
        cleaning = true
      end
      
      if folder == nil && File.exist?(param)
        folder = File.expand_path param
      end
    end
  end
  
  unless folder
    folder = File.expand_path "."
  end
  
  pattern = "#{folder}/**/*.md"

  files = Dir[pattern] || Array.new
  
  if files.size == 0
    puts "There is no markdown files here."; exit 1
  end
  
  files.each do |file_path|
    if cleaning
      html_file = file_path.html_file_path
      FileUtils.rm(html_file) if File.exist?(html_file)
    else
      self.generate_html_for(file_path)
    end
  end

  if watching
    FileWatcher.new(pattern).watch do |file_path|
      self.generate_html_for(file_path)
      self.refresh_page(file_path.basename)
    end
  end
end