Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #96

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
8 changes: 0 additions & 8 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,3 @@ group :development, :test do
gem 'rspec', '>= 3'
gem 'tilt'
end

group :coderay do
gem 'coderay'
end

group :pygments do
gem 'pygments.rb'
end
10 changes: 8 additions & 2 deletions lib/org-ruby/headline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ class Headline < Line
# This matches a headline marked as COMMENT
CommentHeadlineRegexp = /^COMMENT\s+/

def initialize(line, parser = nil, offset=0)
def initialize(line, parser = nil, offset = 0)
super(line, parser)
@body_lines = []
@tags = []
@export_state = :exclude
@property_drawer = { }
if (@line =~ LineRegexp) then
@level = $&.strip.length + offset
new_offset = (parser && parser.title?) ? offset + 1 : offset
@level = $&.strip.length + new_offset
@headline_text = $'.strip
if (@headline_text =~ TagsRegexp) then
@tags = $&.split(/:/) # split tag text on semicolon
Expand Down Expand Up @@ -88,6 +89,11 @@ def paragraph_type
:"heading#{@level}"
end

def headline_level
title_offset = (parser && parser.title?) ? 1 : 0
level - title_offset
end

######################################################################
private

Expand Down
61 changes: 61 additions & 0 deletions lib/org-ruby/highlighter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module Orgmode
# Highlight code
module Highlighter
def self.highlight(code, lang)
highlighter = guess_highlighter
highlighter.highlight(code, lang)
end

def self.guess_highlighter
return RougeHighliter if gem_present?('rouge')
return PygmentsHighliter if gem_present?('pygments.rb')
return CodeRayHighliter if gem_present?('coderay')

DefaultHighliter
end

def self.gem_present?(gem)
Gem::Specification.find_all_by_name(gem).any?
end

# Default highliter does nothing to code
class DefaultHighliter
def self.highlight(buffer, _lang)
buffer
end
end

# Pygments wrapper
class PygmentsHighliter
def self.highlight(buffer, lang)
require 'pygments'
if Pygments::Lexer.find_by_alias(lang)
Pygments.highlight(buffer, lexer: lang)
else
Pygments.highlight(buffer, lexer: 'text')
end
end
end

# CodeRay wrapper
class CodeRayHighliter
def self.highlight(buffer, lang)
require 'coderay'
CodeRay.scan(buffer, lang).html(wrap: nil, css: :style)
rescue ArgumentError => _e
CodeRay.scan(buffer, 'text').html(wrap: nil, css: :style)
end
end

# Rouge wrapper
class RougeHighliter
def self.highlight(buffer, lang)
require 'rouge'
formatter = Rouge::Formatters::HTMLLegacy.new
lexer = Rouge::Lexer.find_fancy(lang, buffer) ||
Rouge::Lexers::PlainText
formatter.format(lexer.lex(buffer.strip))
end
end
end
end
Loading