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_or_options, options = nil) ⇒ SyntaxOn

Returns a new instance of SyntaxOn.



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

def initialize code_or_options, options = nil
  if code_or_options.is_a?(String)
    options ||= {}
    options[:code] = code_or_options
  else
    options = code_or_options
  end

  options[:syntax] ||= nil

  if options[:file] and File.file? options[:file]
    @file = options[:file] 
    @code = File.read @file
  end

  @line_numbers = options[:line_numbers].nil? ? true : options[:line_numbers]

  @code = options[:code] if @code.nil? and options[:code]

  @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



64
65
66
# File 'lib/syntax-on.rb', line 64

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

.theme_directoriesObject



72
73
74
# File 'lib/syntax-on.rb', line 72

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

.theme_directoryObject



68
69
70
# File 'lib/syntax-on.rb', line 68

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

.theme_namesObject



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

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

.themesObject



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

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 => @line_numbers }) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/syntax-on.rb', line 47

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