forked from rouge-ruby/rouge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
themes, visual tests, etc.
- Loading branch information
Showing
12 changed files
with
433 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,6 @@ gem 'sexp_processor', '~> 3.0' | |
gem 'wrong', '~> 0.6.2' | ||
|
||
gem 'rake' | ||
|
||
# for visual tests | ||
gem 'sinatra' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'pathname' | ||
|
||
here = Pathname.new(__FILE__).dirname | ||
load here.join('spec/visual/app.rb') | ||
|
||
run VisualTestApp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
module Rouge | ||
class Theme | ||
class << self | ||
def styles | ||
@styles ||= {} | ||
end | ||
|
||
def main_style | ||
@main_style ||= {} | ||
end | ||
|
||
def style(*tokens) | ||
opts = {} | ||
opts = tokens.pop if tokens.last.is_a? Hash | ||
|
||
if tokens.empty? | ||
@main_style = opts | ||
end | ||
|
||
tokens.each do |tok| | ||
styles[tok.to_s] = opts | ||
end | ||
end | ||
|
||
def name(n=nil) | ||
return @name if n.nil? | ||
|
||
@name = n.to_s | ||
registry[@name] = self | ||
end | ||
|
||
def find(n) | ||
registry[n.to_s] | ||
end | ||
|
||
def registry | ||
@registry ||= {} | ||
end | ||
end | ||
end | ||
|
||
class CSSTheme < Theme | ||
def initialize(opts={}) | ||
@opts = opts | ||
end | ||
|
||
def render | ||
out = [] | ||
stream { |line| out << line } | ||
out.join("\n") | ||
end | ||
|
||
def stream(&b) | ||
self.class.styles.each do |tokname, style| | ||
stream_single(Token[tokname], style, &b) | ||
end | ||
|
||
render_stanza('.highlight', self.class.main_style, &b) | ||
end | ||
|
||
private | ||
def stream_single(tok, style, &b) | ||
render_stanza(css_selector(tok), style, &b) | ||
end | ||
|
||
def render_stanza(selector, style, &b) | ||
return if style.empty? | ||
|
||
yield "#{selector} {" | ||
yield " color: #{style[:fg]};" if style[:fg] | ||
yield " background-color: #{style[:bg]};" if style[:bg] | ||
yield " font-weight: bold;" if style[:bold] | ||
yield " font-style: italic;" if style[:italic] | ||
yield " text-decoration: underline;" if style[:underline] | ||
|
||
(style[:rules] || []).each do |rule| | ||
yield " #{rule};" | ||
end | ||
|
||
yield "}" | ||
end | ||
|
||
def css_selector(token) | ||
tokens = [token] | ||
parent = token.parent | ||
|
||
inflate_token(token).map do |tok| | ||
base = ".highlight" | ||
base << " .#{tok.shortname}" unless tok.shortname.empty? | ||
|
||
base | ||
end.join(', ') | ||
end | ||
|
||
def inflate_token(tok) | ||
Enumerator.new do |out| | ||
out << tok | ||
tok.sub_tokens.each_value do |st| | ||
next if self.class.styles.include? st.name | ||
|
||
out << st | ||
inflate_token(st).each { |t| out << t } | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.