Class: SyntaxOn

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

Defined Under Namespace

Classes: Bin, Browser

Constant Summary collapse

VIM_BIN =
'vim'
VIM_OPTIONS =
[ "syntax on", "let html_use_css = 1", 'let html_use_encoding = "utf8"', "let use_xhtml = 1" ]
VIM_RENDER =
[ "exe 'normal zR'", "runtime\\! syntax/2html.vim", "wq", "q" ]
TEMP_DIRECTORY =
'/tmp/syntax-on'
TEMP_FILENAME =
lambda { Time.now.strftime '%Y-%d-%m_%Hh-%Mm-%Ss' }
THEME_PATH =
[ '~/.syntaxon/themes' ]
PREVIEW_COMMAND =
lambda { |file| (Gem::Platform.local.os == 'darwin') ? "open -a Firefox '#{file}'" : "firefox '#{file}' &" }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, options = { :syntax => nil }) ⇒ SyntaxOn

Returns a new instance of SyntaxOn.



24
25
26
27
28
29
30
31
32
# File 'lib/syntax-on.rb', line 24

def initialize code, options = { :syntax => nil }
  @code   = code
  if options[:file] and File.file? options[:file]
    @file = options[:file] 
    @code = File.read @file
  end
  @syntax = options[:syntax]
  @use_session = options[:use_session]
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



22
23
24
# File 'lib/syntax-on.rb', line 22

def code
  @code
end

#syntaxObject

Returns the value of attribute syntax.



22
23
24
# File 'lib/syntax-on.rb', line 22

def syntax
  @syntax
end

Class Method Details

.theme(name = :remi) ⇒ Object



51
52
53
# File 'lib/syntax-on.rb', line 51

def self.theme name = :remi
  File.read themes.find { |theme| File.basename(theme).downcase == "#{ name }.css".downcase }
end

.theme_directoriesObject



59
60
61
# File 'lib/syntax-on.rb', line 59

def self.theme_directories
  SyntaxOn::THEME_PATH << self.theme_directory
end

.theme_directoryObject



55
56
57
# File 'lib/syntax-on.rb', line 55

def self.theme_directory
  File.expand_path( File.join( File.dirname(__FILE__), "/../themes/" ))
end

.theme_namesObject



47
48
49
# File 'lib/syntax-on.rb', line 47

def self.theme_names
  themes.map { |theme| File.basename(theme).sub(/\.css$/,'') }.uniq.sort
end

.themesObject



43
44
45
# File 'lib/syntax-on.rb', line 43

def self.themes
  theme_directories.inject([]){ |all,this| all + Dir[File.join(File.expand_path(this), '*.css')] }
end

Instance Method Details

#to_html(options = { :line_numbers => true }) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/syntax-on.rb', line 34

def to_html options = { :line_numbers => true }
  setup_temp_dir
  create_temp_file
  setup_vim_options options
  render
  @html = File.read(@html_file).match(/<pre>(.*)<\/pre>/m)[1].strip
  finish
end