Module: Mdslide

Defined in:
lib/mdslide.rb,
lib/mdslide/path.rb,
lib/mdslide/config.rb,
lib/mdslide/themes.rb,
lib/mdslide/command.rb,
lib/mdslide/creator.rb

Defined Under Namespace

Classes: Creator

Constant Summary collapse

VERSION =
'3.2.0'
CONFIG_DIR =
File.expand_path('~/.mdslide')
Defaults =
{
  :theme => 'white',
  :bind  => '127.0.0.1',
  :port  => '3000',
  :title => 'Slides converted by Mdslide',
}
Themes =
YAML.load_file(File.dirname(__FILE__)+'/../../assets/themes.yaml')
ASSETS_DIR =
File.expand_path(File.dirname(__FILE__) + '/../../assets')

Class Method Summary collapse

Class Method Details

.command(argv = ARGV) ⇒ Object



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
104
105
106
107
108
109
110
# File 'lib/mdslide/command.rb', line 11

def Mdslide.command argv=ARGV
  parser = ArgsParser.parse argv do
    arg :version,"Shows Mdslide Version", :alias => :v
    arg :help,   "Shows Help",      :alias => :h
    arg :input,  "Input File",      :alias => :i
    arg :theme,  "Theme",           :alias => :t, :default => Mdslide::Defaults[:theme]
    arg :bind,   "Bind IP Address", :alias => :b, :default => Mdslide::Defaults[:bind ]
    arg :port,   "Port Number",     :alias => :p, :default => Mdslide::Defaults[:port ]
    arg :title,  "Title",           :alias => :T, :default => Mdslide::Defaults[:title]
    arg :output, "Output File",     :alias => :o
    arg :'without-assets-dir', "Does not create js/css directory"
    arg :'without-css-dir',    "Does not create css directory"
    arg :'without-js-dir',     "Does not create js directory"
  end
  if parser.has_option? :version
    puts VERSION
    return 0
  elsif parser.has_option? :help or !parser.has_param?(:input)
    puts "mdslide [options] -i <Input File>"
    puts parser.help
    return 0
  end

  creator = Creator.new
  creator.title = parser[:title]
  default_theme = parser[:theme]

  file_path = File.expand_path(parser[:input])
  dir_path = File.dirname(file_path)

  if parser.has_param? :output
    output_path = File.expand_path(parser[:output])
    output_dir_path = File.dirname(output_path)

    creator.set_theme default_theme
    input = nil
    File.open(file_path,'r'){|r| input = r.read }
    File.open(output_path,'w'){|f| f.puts creator.convert_markdown(input)}

    output_js  = !parser[:"without-assets-dir"] and !parser[:"without-js-dir"]
    output_css = !parser[:"without-assets-dir"] and !parser[:"without-css-dir"]
    src_dirs = [dir_path,CONFIG_DIR,ASSETS_DIR].delete_if{|e| e == output_dir_path}
    
    if output_js
      js_path = output_dir_path+'/js'
      Dir::mkdir(js_path) unless File.exist?(js_path) and File.directory?(js_path)
      (creator.scripts + creator.theme_scripts).each do |e| 
        src_file = Mdslide.find_js_path(e)
        FileUtils.cp("#{src_file}",js_path)
        #src_dir = Mdslide.find_path(e,src_dirs.map{|m| "#{m}/js/"})
        #FileUtils.cp("#{src_dir}/#{e}",js_path) if src_dir
      end
    end
    if output_css
      css_path = output_dir_path+'/css'
      Dir::mkdir(css_path) unless File.exist?(css_path) and File.directory?(css_path)

      (creator.stylesheets + creator.theme_stylesheets).each do |e| 
        src_file = Mdslide.find_css_path(e)
        FileUtils.cp("#{src_file}",css_path)
        #src_dir = Mdslide.find_path(e,src_dirs.map{|m| "#{m}/css/"})
        #FileUtils.cp("#{src_dir}/#{e}",css_path) if src_dir
      end
    end

  else
    require 'webrick'
    srv = WEBrick::HTTPServer.new( 
      :BindAddress => parser[:bind],
      :DoNotReverseLookup => true,
      :Port => parser[:port],
      :MimeTypes => WEBrick::HTTPUtils::DefaultMimeTypes.merge({"js"=>"application/javascript"})
    )
    srv.mount_proc '/' do |req,res|
      root_path = nil
      if req.path != '/'
        root_path = Mdslide.find_path(req.path,[dir_path,CONFIG_DIR,ASSETS_DIR])
        if root_path
          si = WEBrick::HTTPServlet::FileHandler.get_instance(srv, root_path)
          si.service(req,res)
        end
      end

      if !root_path
        Mdslide.load_config
        m = req.path.match(/\/(.+)/)
        theme = (m && m[1]) || default_theme
        creator.set_theme theme
        File.open(file_path,'r'){|r| input = r.read }

        res.body = creator.convert_markdown(input)
        res['Content-Type'] = 'text/html'
        res['Content-Length'] = res.body.bytesize
      end
    end
    Signal.trap(:INT){ srv.shutdown }
    srv.start
  end
  return 0
end

.find_css_path(file, current = nil) ⇒ Object



32
33
34
35
# File 'lib/mdslide/path.rb', line 32

def Mdslide.find_css_path file,current = nil
  dirs = Mdslide.get_directories(current).map{|e| "#{e}/css/"}
  return Mdslide.find_file file,dirs
end

.find_file(file, dir_list) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/mdslide/path.rb', line 3

def Mdslide.find_file file, dir_list
  dir_list.each do |dir_path|
    if File.exist?(dir_path + file)
      return dir_path + file
    end
  end
  return nil
end

.find_js_path(file, current = nil) ⇒ Object



27
28
29
30
# File 'lib/mdslide/path.rb', line 27

def Mdslide.find_js_path file,current = nil
  dirs = Mdslide.get_directories(current).map{|e| "#{e}/js/"}
  return Mdslide.find_file file,dirs
end

.find_path(path, dir_list) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/mdslide/path.rb', line 12

def Mdslide.find_path path, dir_list
  dir_list.each do |dir_path|
    if File.exist?(dir_path + path)
      return dir_path
    end
  end
  return nil
end

.get_directories(current = nil) ⇒ Object



21
22
23
24
25
# File 'lib/mdslide/path.rb', line 21

def Mdslide.get_directories current = nil
  dirs = [CONFIG_DIR,ASSETS_DIR]
  dirs.unshift current if current 
  return dirs
end

.load_config(config_file = (CONFIG_DIR + '/config.yaml')) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/mdslide/config.rb', line 11

def Mdslide.load_config config_file=(CONFIG_DIR + '/config.yaml')
  config_path = File.expand_path(config_file)
  my_config = nil
  if File.exist? config_path
    my_config = YAML.load_file(config_path)
    Themes.merge!   my_config[:themes] if my_config[:themes]
    Defaults.merge! my_config[:config] if my_config[:config]
  end
end