Class: SyntaxOn::Bin

Inherits:
Object
  • Object
show all
Includes:
SimpleCLI
Defined in:
lib/syntax-on/bin.rb

Instance Method Summary collapse

Instance Method Details

#browser(*args) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/syntax-on/bin.rb', line 82

def browser *args
  begin
    require 'thin'
  rescue
    puts "Please install the 'thin' gem to run `#{script_name} server`" ; exit
  end

  options = { :port => 6789  }
  opts = OptionParser.new do |opts|
    opts.on('-P','--port [PORT]'){ |port| options[:port] = port }
    opts.on('-p','--preview'){ options[:preview] = true }
  end
  opts.parse! args
  dir = args.last
  
  Thread.new { sleep 1 ; system(SyntaxOn::PREVIEW_COMMAND.call("http://localhost:#{ options[:port] }")) } if options[:preview]

  require 'syntax-on/browser'
  Thin::Server.start('0.0.0.0', options[:port].to_i) do
    use Rack::CommonLogger
    use Rack::ShowExceptions
    run SyntaxOn::Browser.new(dir)
  end
end

#browser_helpObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/syntax-on/bin.rb', line 64

def browser_help
  <<doco
Usage: #{ script_name } server [DIRECTORY]

Options:
  -P, --port            Port to run on
  -p, --preview         Preview browser in Firefox

Arguments:
  DIRECTORY             Root directory to browse

Requires gems: thin

Summary:
  Starts a web server to browse syntax highlighted
  files in the current directory
doco
end

#highlight(*args) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/syntax-on/bin.rb', line 129

def highlight *args
  options = { :theme => :remi }
  opts = OptionParser.new do |opts|
    opts.on('-p','--preview'){ options[:preview] = true }
    opts.on('-t','--theme [THEME]'){ |theme| options[:theme] = theme }
    opts.on('-s','--syntax [SYNTAX]'){ |syntax| options[:syntax] = syntax }
    opts.on('-o','--output [FILE]'){ |file| options[:output] = file }
    opts.on('-c','--code [CODE]'){ |code| options[:code] = (code.nil? ? STDIN.readlines.join('') : code) }
    opts.on('-b','--bash'){ options[:bash] = true }
  end
  opts.parse! args
  file = args.last
  out  = File.expand_path( options[:output] || "#{ file }.html" )

  if options[:code] or ( file and File.file? file )
    css  = SyntaxOn::theme options[:theme]
    html = SyntaxOn.new( options[:code], :syntax => options[:syntax], :file => file, :use_session => options[:bash] ).to_html
    html = <<HTML
<html>
<head>
<style type="text/css">
<!--#{ css }-->
</style>
</head>
<body>
<pre>
#{ html }
</pre>
</body>
</html>
HTML
    unless options[:output] == 'STDOUT'
      File.open(out, 'w') do |f|
        f << html
      end
      system(SyntaxOn::PREVIEW_COMMAND.call(out)) if options[:preview]
    else
      puts html
    end
  else
    help :highlight
  end
end

#highlight_helpObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/syntax-on/bin.rb', line 107

def highlight_help
  <<doco
Usage: #{ script_name } highlight [OPTIONS] FILE

Options:
  -p, --preview         Preview file in Firefox
  -t, --theme THEME     Theme to use (default 'remi')
  -s, --syntax SYNTAX   Specify syntax to use
  -o, --output FILE     Specify name of file to create,
                          output is echoed if -o STDOUT
  -c, --code CODE       Specify code to use in place of 
                          a file (also checks STDIN)
  -b,--bash             EXPERIMENTAL: use a new bash session 
                          for vim (requires 'session' gem)

Arguments:
  FILE                  Name of the file to highlight

Summary:
  Create a syntax highlighted HTML file
doco
end

#themes(*args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/syntax-on/bin.rb', line 46

def themes *args
  options = {}
  opts = OptionParser.new do |opts|
    opts.on('-l','--list'){ options[:list] = true }
    opts.on('-p','--path'){ options[:path] = true }
    opts.on('-c','--cat'){ options[:cat] = true }
    opts.on('-n','--names'){ options[:names] = true }
  end
  opts.parse! args

  theme = args.last

  puts SyntaxOn::themes.sort.join("\n") if options[:list]
  puts SyntaxOn::theme_directories.join("\n") if options[:path]
  puts SyntaxOn::theme_names.join("\n") if options[:names]
  puts SyntaxOn::theme(theme) if options[:cat] and theme
end

#themes_helpObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/syntax-on/bin.rb', line 28

def themes_help
  <<doco
Usage: #{ script_name } themes [OPTIONS] [THEME]

Options:
  -l, --list            List all available themes
  -p, --path            Print out the THEME_PATH directories
  -c, --cat             Cat (show) a theme
  -n, --names           Print out the names of available themes

Arguments:
  THEME                 A theme name, eg. 'default'

Summary:
  Command for managing #{ script_name } themes
end
doco
end

#usage(*args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/syntax-on/bin.rb', line 7

def usage *args
  puts <<doco

syntax-on == %{ vim-based syntax highlighing }

  Usage:
    syntax-on -h/--help      [Not Implemented Yet]
    syntax-on -v/--version   [Not Implemented Yet]
    syntax-on command [options]

  Examples:
    syntax-on highlight my-file.rb

  Further help:
    syntax-on commands         # list all available commands
    syntax-on help <COMMAND>   # show help for COMMAND
    syntax-on help             # show this help message

doco
end