This module is included in the output String from thew HTML Encoder.
It provides methods like wrap, div, page etc.
Remember to use # instead of # to keep the modules the object was extended with.
TODO: more doc.
Raises an exception if an object that doesn’t respond to to_str is extended by Output, to prevent users from misuse. Use Module#remove_method to disable.
# File lib/coderay/encoders/html/output.rb, line 37
37: def extended o
38: warn "The Output module is intended to extend instances of String, not #{o.class}." unless o.respond_to? :to_str
39: end
# File lib/coderay/encoders/html/output.rb, line 41
41: def make_stylesheet css, in_tag = false
42: sheet = css.stylesheet
43: sheet = <style type="text/css">#{sheet}</style> if in_tag
44: sheet
45: end
This makes Output look like a class.
Example:
a = Output.new '<span class="co">Code</span>' a.wrap! :page
# File lib/coderay/encoders/html/output.rb, line 28
28: def new string, css = CSS.new, element = nil
29: output = string.clone.extend self
30: output.wrapped_in = element
31: output.css = css
32: output
33: end
# File lib/coderay/encoders/html/output.rb, line 51
51: def page_template_for_css css
52: sheet = make_stylesheet css
53: PAGE.apply 'CSS', sheet
54: end
Define a new wrapper. This is meta programming.
# File lib/coderay/encoders/html/output.rb, line 57
57: def wrapper *wrappers
58: wrappers.each do |wrapper|
59: define_method wrapper do |*args|
60: wrap wrapper, *args
61: end
62: define_method "#{wrapper}!".to_sym do |*args|
63: wrap! wrapper, *args
64: end
65: end
66: end
# File lib/coderay/encoders/html/output.rb, line 90
90: def apply_title! title
91: self.sub!(/(<title>)(<\/title>)/) { $1 + title + $2 }
92: self
93: end
# File lib/coderay/encoders/html/numerization.rb, line 117
117: def line_count
118: line_count = count("\n")
119: position_of_last_newline = rindex(\n\)
120: if position_of_last_newline
121: after_last_newline = self[position_of_last_newline + 1 .. 1]
122: ends_with_newline = after_last_newline[/\A(?:<\/span>)*\z/]
123: line_count += 1 if not ends_with_newline
124: end
125: line_count
126: end
# File lib/coderay/encoders/html/numerization.rb, line 8
8: def numerize *args
9: clone.numerize!(*args)
10: end
# File lib/coderay/encoders/html/numerization.rb, line 19
19: def numerize! mode = :table, options = {}
20: return self unless mode
21:
22: options = DEFAULT_OPTIONS.merge options
23:
24: start = options[:line_number_start]
25: unless start.is_a? Integer
26: raise ArgumentError, "Invalid value %p for :line_number_start; Integer expected." % start
27: end
28:
29: #allowed_wrappings = NUMERIZABLE_WRAPPINGS[mode]
30: #unless allowed_wrappings == :all or allowed_wrappings.include? options[:wrap]
31: # raise ArgumentError, "Can't numerize, :wrap must be in %p, but is %p" % [NUMERIZABLE_WRAPPINGS, options[:wrap]]
32: #end
33:
34: bold_every = options[:bold_every]
35: highlight_lines = options[:highlight_lines]
36: bolding =
37: if bold_every == false && highlight_lines == nil
38: proc { |line| line.to_s }
39: elsif highlight_lines.is_a? Enumerable
40: highlight_lines = highlight_lines.to_set
41: proc do |line|
42: if highlight_lines.include? line
43: "<strong class=\"highlighted\">#{line}</strong>" # highlighted line numbers in bold
44: else
45: line.to_s
46: end
47: end
48: elsif bold_every.is_a? Integer
49: raise ArgumentError, ":bolding can't be 0." if bold_every == 0
50: proc do |line|
51: if line % bold_every == 0
52: "<strong>#{line}</strong>" # every bold_every-th number in bold
53: else
54: line.to_s
55: end
56: end
57: else
58: raise ArgumentError, 'Invalid value %p for :bolding; false or Integer expected.' % bold_every
59: end
60:
61: case mode
62: when :inline
63: max_width = (start + line_count).to_s.size
64: line_number = start
65: gsub!(/^/) do
66: line_number_text = bolding.call line_number
67: indent = ' ' * (max_width - line_number.to_s.size) # TODO: Optimize (10^x)
68: res = "<span class=\"no\">#{indent}#{line_number_text}</span> "
69: line_number += 1
70: res
71: end
72:
73: when :table
74: # This is really ugly.
75: # Because even monospace fonts seem to have different heights when bold,
76: # I make the newline bold, both in the code and the line numbers.
77: # FIXME Still not working perfect for Mr. Internet Exploder
78: line_numbers = (start ... start + line_count).to_a.map(&bolding).join("\n")
79: line_numbers << "\n" # also for Mr. MS Internet Exploder :-/
80: line_numbers.gsub!(/\n/) { "<tt>\n</tt>" }
81:
82: line_numbers_table_tpl = TABLE.apply('LINE_NUMBERS', line_numbers)
83: gsub!(/<\/div>\n/) { '</div>' }
84: gsub!(/\n/) { "<tt>\n</tt>" }
85: wrap_in! line_numbers_table_tpl
86: @wrapped_in = :div
87:
88: when :list
89: opened_tags = []
90: gsub!(/^.*$\n?/) do |line|
91: line.chomp!
92:
93: open = opened_tags.join
94: line.scan(%<(/)?span[^>]*>?!) do |close,|
95: if close
96: opened_tags.pop
97: else
98: opened_tags << $&
99: end
100: end
101: close = '</span>' * opened_tags.size
102:
103: "<li>#{open}#{line}#{close}</li>\n"
104: end
105: chomp!("\n")
106: wrap_in! LIST
107: @wrapped_in = :div
108:
109: else
110: raise ArgumentError, 'Unknown value %p for mode: expected one of %p' %
111: [mode, [:table, :list, :inline]]
112: end
113:
114: self
115: end
# File lib/coderay/encoders/html/output.rb, line 125
125: def stylesheet in_tag = false
126: Output.make_stylesheet @css, in_tag
127: end
# File lib/coderay/encoders/html/output.rb, line 121
121: def wrap *args
122: clone.wrap!(*args)
123: end
# File lib/coderay/encoders/html/output.rb, line 95
95: def wrap! element, *args
96: return self if not element or element == wrapped_in
97: case element
98: when :div
99: raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? nil
100: wrap_in! DIV
101: when :span
102: raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? nil
103: wrap_in! SPAN
104: when :page
105: wrap! :div if wrapped_in? nil
106: raise "Can't wrap %p in %p" % [wrapped_in, element] unless wrapped_in? :div
107: wrap_in! Output.page_template_for_css(@css)
108: if args.first.is_a?(Hash) && title = args.first[:title]
109: apply_title! title
110: end
111: self
112: when nil
113: return self
114: else
115: raise "Unknown value %p for :wrap" % element
116: end
117: @wrapped_in = element
118: self
119: end
# File lib/coderay/encoders/html/output.rb, line 81
81: def wrap_in template
82: clone.wrap_in! template
83: end
# File lib/coderay/encoders/html/output.rb, line 85
85: def wrap_in! template
86: Template.wrap! self, template, 'CONTENT'
87: self
88: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.