Class: SemiStatic::CLI

Inherits:
Object
  • Object
show all
Includes:
WEBrick
Defined in:
lib/semi-static/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



18
19
20
21
22
23
24
25
26
# File 'lib/semi-static/cli.rb', line 18

def initialize
    self.clean_first     = false
    self.start_server    = false
    self.show_statistics = false
    self.use_pygments    = true
    self.quick_mode      = false
    self.check_mtime     = true
    self.start_updater   = false
end

Instance Attribute Details

#check_mtimeObject

Returns the value of attribute check_mtime.



15
16
17
# File 'lib/semi-static/cli.rb', line 15

def check_mtime
  @check_mtime
end

#clean_firstObject

Returns the value of attribute clean_first.



10
11
12
# File 'lib/semi-static/cli.rb', line 10

def clean_first
  @clean_first
end

#output_dirObject

Returns the value of attribute output_dir.



9
10
11
# File 'lib/semi-static/cli.rb', line 9

def output_dir
  @output_dir
end

#quick_modeObject

Returns the value of attribute quick_mode.



14
15
16
# File 'lib/semi-static/cli.rb', line 14

def quick_mode
  @quick_mode
end

#server_portObject

Returns the value of attribute server_port.



11
12
13
# File 'lib/semi-static/cli.rb', line 11

def server_port
  @server_port
end

#show_statisticsObject

Returns the value of attribute show_statistics.



12
13
14
# File 'lib/semi-static/cli.rb', line 12

def show_statistics
  @show_statistics
end

#source_dirObject

Returns the value of attribute source_dir.



9
10
11
# File 'lib/semi-static/cli.rb', line 9

def source_dir
  @source_dir
end

#start_serverObject

Returns the value of attribute start_server.



11
12
13
# File 'lib/semi-static/cli.rb', line 11

def start_server
  @start_server
end

#start_updaterObject

Returns the value of attribute start_updater.



16
17
18
# File 'lib/semi-static/cli.rb', line 16

def start_updater
  @start_updater
end

#use_pygmentsObject

Returns the value of attribute use_pygments.



13
14
15
# File 'lib/semi-static/cli.rb', line 13

def use_pygments
  @use_pygments
end

Class Method Details

.runObject



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
# File 'lib/semi-static/cli.rb', line 28

def self.run
    cli = CLI.new
    opts = OptionParser.new do |opts|
        opts.banner = 'Usage: semi [<options>] [[<source path>] <output path>]'
        opts.separator ''
        opts.separator 'Options:'
        
        opts.on('-s', '--server [PORT]', Integer,
                'Start a web server (on port PORT)') do |port|
            cli.start_server = true
            cli.server_port = port || 4000
        end
        
        opts.on('-a', '--[no-]auto-update', 'Automatically update generated files') do |a|
            cli.start_updater = a
        end
        
        opts.on('-c', '--[no-]clean', 'Clean the output dir first') do |d|
            cli.clean_first = d
        end
        
        opts.on('-t', '--[no-]stats', 'Display conversion statistics') do |t|
            cli.show_statistics = t
        end
        
        opts.on('-p', '--[no-]pygments', 'Use Pygments for code highlighting') do |p|
            cli.use_pygments = p
        end
        
        opts.on('-q', '--[no-]quick-mode', 'Only convert a few posts (for testing)') do |q|
            cli.quick_mode = q
        end
        
        opts.on('-m', '--[no-]mtime', 'Skip files that appear to be up-to-date') do |m|
            cli.check_mtime = m
        end
        
        opts.on_tail('-h', '--help', 'Show this message') do
            puts opts
            exit
        end
        
        opts.on_tail('-V', '--version', 'Show version number') do
            path = File.join File.dirname(__FILE__), '..', '..', 'VERSION.yml'
            version = File.open(path) { |io| YAML.load io }
            puts "#{version[:major]}.#{version[:minor]}.#{version[:patch]}"
        end
    end
    opts.parse!
    
    case ARGV.size
    when 0
        cli.source_dir = '.'
        cli.output_dir = 'site'
    when 1
        cli.source_dir = '.'
        cli.output_dir = ARGV[0]
    when 2
        cli.source_dir = ARGV[0]
        cli.output_dir = ARGV[1]
    else
        puts 'Invalid options.  Run `semi --help` for assistance.'
        exit 1
    end
    
    cli.run
end

Instance Method Details

#runObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/semi-static/cli.rb', line 96

def run
    Pygmentize.enabled = use_pygments
    SemiStatic::Site.open(source_dir) do |site|
        site.clean_first     = clean_first
        site.show_statistics = show_statistics
        site.quick_mode      = quick_mode
        site.check_mtime     = check_mtime
        
        site.output output_dir
        if start_updater
            updater = DirectoryWatcher.new(source_dir)
            updater.interval = 1
            updater.glob = '{indices,layouts,pages,posts,snippets,stylesheets}/**/*'
            updater.add_observer do |*args|
                puts "[#{Time.now}] #{args.length} files changed."
                site.output output_dir
            end
            updater.start
            sleep 0 unless start_server
        end
        if start_server
            server = HTTPServer.new :DocumentRoot => output_dir,
                                    :Port => server_port
            thread = Thread.new { server.start }
            trap('INT') { server.shutdown }
            thread.join
        end
    end
end