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.
Merge pull request rouge-ruby#450 from jneen/refactor.split-html
[Rouge 2.0] split Formatters::HTML into different classes
- Loading branch information
Showing
16 changed files
with
318 additions
and
137 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
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
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,22 @@ | ||
# -*- coding: utf-8 -*- # | ||
|
||
module Rouge | ||
module Formatters | ||
class HTMLInline < HTML | ||
tag 'html_inline' | ||
|
||
def initialize(theme) | ||
@theme = theme | ||
end | ||
|
||
def safe_span(tok, safe_val) | ||
return safe_val if tok == Token::Tokens::Text | ||
|
||
rules = @theme.style_for(tok).rendered_rules | ||
|
||
"<span style=\"#{rules.to_a.join(';')}\">#{safe_val}</span>" | ||
end | ||
end | ||
end | ||
end | ||
|
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,44 @@ | ||
# -*- coding: utf-8 -*- # | ||
|
||
# stdlib | ||
require 'cgi' | ||
|
||
module Rouge | ||
module Formatters | ||
# Transforms a token stream into HTML output. | ||
class HTMLLegacy < Formatter | ||
tag 'html_legacy' | ||
|
||
# @option opts [String] :css_class ('highlight') | ||
# @option opts [true/false] :line_numbers (false) | ||
# @option opts [Rouge::CSSTheme] :inline_theme (nil) | ||
# @option opts [true/false] :wrap (true) | ||
# | ||
# Initialize with options. | ||
# | ||
# If `:inline_theme` is given, then instead of rendering the | ||
# tokens as <span> tags with CSS classes, the styles according to | ||
# the given theme will be inlined in "style" attributes. This is | ||
# useful for formats in which stylesheets are not available. | ||
# | ||
# Content will be wrapped in a tag (`div` if tableized, `pre` if | ||
# not) with the given `:css_class` unless `:wrap` is set to `false`. | ||
def initialize(opts={}) | ||
@formatter = opts[:inline_theme] ? HTMLInline.new(opts[:inline_theme]) | ||
: HTML.new | ||
|
||
|
||
@formatter = HTMLTable.new(@formatter, opts) if opts[:line_numbers] | ||
|
||
if opts.fetch(:wrap, true) | ||
@formatter = HTMLPygments.new(@formatter, opts.fetch(:css_class, 'codehilite')) | ||
end | ||
end | ||
|
||
# @yield the html output. | ||
def stream(tokens, &b) | ||
@formatter.stream(tokens, &b) | ||
end | ||
end | ||
end | ||
end |
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,27 @@ | ||
# -*- coding: utf-8 -*- # | ||
|
||
module Rouge | ||
module Formatters | ||
class HTMLLinewise < Formatter | ||
def initialize(formatter, opts={}) | ||
@formatter = formatter | ||
@class_format = opts.fetch(:class, 'line-%i') | ||
end | ||
|
||
def stream(tokens, &b) | ||
token_lines(tokens) do |line| | ||
yield "<div class=#{next_line_class}>" | ||
line.each do |tok, val| | ||
yield @formatter.span(tok, val) | ||
end | ||
yield '</div>' | ||
end | ||
end | ||
|
||
def next_line_class | ||
@lineno ||= 0 | ||
sprintf(@class_format, @lineno += 1).inspect | ||
end | ||
end | ||
end | ||
end |
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,16 @@ | ||
module Rouge | ||
module Formatters | ||
class HTMLPygments < Formatter | ||
def initialize(inner, css_class='codehilite') | ||
@inner = inner | ||
@css_class = css_class | ||
end | ||
|
||
def stream(tokens, &b) | ||
yield %<<pre class="#@css_class"><code>> | ||
@inner.stream(tokens, &b) | ||
yield "</code></pre>" | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.