Class: Codnar::GVim

Inherits:
Object
  • Object
show all
Defined in:
lib/codnar/gvim.rb

Overview

Syntax highlight using GVim.

Class Method Summary collapse

Class Method Details

.cached_syntax_to_html(text, syntax, commands = []) ⇒ Object

Highlight syntax of text using GVim. This uses the GVim standard CSS classes to mark keywords, identifiers, and so on. See the GVim documentation for details. The commands array allows configuring the way that GVim will format the output. For example:

  • The command "+:colorscheme <name>" will override the default color scheme used.

  • The command "+:let html_use_css=1" will just annotate each HTML tag with a CSS class, instead of embedding some specific style directly into the tag. In this case the colorscheme and background are ignored; you will need to provide your own CSS stylesheet as part of the final woven document to style the marked-up words.

Additional commands may be useful; GVim provides a full scripting environment so there is no theoretical limit to what can be done here.

Since GVim is as slow as molasses to start up, we cache the results of highlighting the syntax of each code fragment in a directory called .codnar-cache, which can appear at the current working directory or in any of its parents.



48
49
50
51
# File 'lib/codnar/gvim.rb', line 48

def self.cached_syntax_to_html(text, syntax, commands = [])
  data = { "text" => text, "syntax" => syntax, "commands" => commands }
  return @cache[data]
end

.force_recompute=(force_recompute) ⇒ Object

Force recomputation of the syntax highlighting HTML, even if a cached version exists.



24
25
26
# File 'lib/codnar/gvim.rb', line 24

def self.force_recompute=(force_recompute)
  @cache.force_recompute = force_recompute
end

.lines_to_html(lines, syntax, commands = []) ⇒ Object

Convert a sequence of classified code lines to HTML using GVim syntax highlighting. The commands array allows configuring the way that GVim will format the output (see the cached_syntax_to_html method for details).



10
11
12
13
14
# File 'lib/codnar/gvim.rb', line 10

def self.lines_to_html(lines, syntax, commands = [])
  return Formatter.merge_lines(lines, "html") do |payload|
    GVim.cached_syntax_to_html(payload + "\n", syntax, commands).chomp
  end
end

.uncached_syntax_to_html(text, syntax, commands = []) ⇒ Object

Highlight syntax of text using GVim, without caching. This is slow (measured in seconds), due to GVim’s start-up tim. See the cached_syntax_to_html method for a faster variant and functionality details.



57
58
59
60
61
62
63
# File 'lib/codnar/gvim.rb', line 57

def self.uncached_syntax_to_html(text, syntax, commands = [])
  file = write_temporary_file(text)
  run_gvim(file, syntax, commands)
  html = read_html_file(file)
  delete_temporary_files(file)
  return clean_html(html, syntax)
end