Bring up to current.
This commit is contained in:
parent
69b62b6f33
commit
b19e1e4f47
71 changed files with 8305 additions and 39 deletions
27
lib/syntax/convertors/abstract.rb
Normal file
27
lib/syntax/convertors/abstract.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
require 'syntax'
|
||||
|
||||
module Syntax
|
||||
module Convertors
|
||||
|
||||
# The abstract ancestor class for all convertors. It implements a few
|
||||
# convenience methods to provide a common interface for all convertors.
|
||||
class Abstract
|
||||
|
||||
# A reference to the tokenizer used by this convertor.
|
||||
attr_reader :tokenizer
|
||||
|
||||
# A convenience method for instantiating a new convertor for a
|
||||
# specific syntax.
|
||||
def self.for_syntax( syntax )
|
||||
new( Syntax.load( syntax ) )
|
||||
end
|
||||
|
||||
# Creates a new convertor that uses the given tokenizer.
|
||||
def initialize( tokenizer )
|
||||
@tokenizer = tokenizer
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
51
lib/syntax/convertors/html.rb
Normal file
51
lib/syntax/convertors/html.rb
Normal file
|
@ -0,0 +1,51 @@
|
|||
require 'syntax/convertors/abstract'
|
||||
|
||||
module Syntax
|
||||
module Convertors
|
||||
|
||||
# A simple class for converting a text into HTML.
|
||||
class HTML < Abstract
|
||||
|
||||
# Converts the given text to HTML, using spans to represent token groups
|
||||
# of any type but <tt>:normal</tt> (which is always unhighlighted). If
|
||||
# +pre+ is +true+, the html is automatically wrapped in pre tags.
|
||||
def convert( text, pre=true )
|
||||
html = ""
|
||||
html << "<pre>" if pre
|
||||
regions = []
|
||||
@tokenizer.tokenize( text ) do |tok|
|
||||
value = html_escape(tok)
|
||||
case tok.instruction
|
||||
when :region_close then
|
||||
regions.pop
|
||||
html << "</span>"
|
||||
when :region_open then
|
||||
regions.push tok.group
|
||||
html << "<span class=\"#{tok.group}\">#{value}"
|
||||
else
|
||||
if tok.group == ( regions.last || :normal )
|
||||
html << value
|
||||
else
|
||||
html << "<span class=\"#{tok.group}\">#{value}</span>"
|
||||
end
|
||||
end
|
||||
end
|
||||
html << "</span>" while regions.pop
|
||||
html << "</pre>" if pre
|
||||
html
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Replaces some characters with their corresponding HTML entities.
|
||||
def html_escape( string )
|
||||
string.gsub( /&/, "&" ).
|
||||
gsub( /</, "<" ).
|
||||
gsub( />/, ">" ).
|
||||
gsub( /"/, """ )
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue