Class: Renderer::Helpers
Class Attribute Summary collapse
-
.current_renderer ⇒ Object
Returns the value of attribute current_renderer.
Instance Method Summary collapse
- #anchor_for(object) ⇒ Object
- #capture(&block) ⇒ Object
- #clean_file_path(source) ⇒ Object
- #div(class_name, **kwargs, &block) ⇒ Object
- #git_url(source) ⇒ Object
- #line_range(source) ⇒ Object
- #link_for(object) ⇒ Object
- #make_path ⇒ Object
- #path_of(object, root: true) ⇒ Object
- #source_of(obj, parent = nil) ⇒ Object
- #svg(name, **kwargs) ⇒ Object
Class Attribute Details
.current_renderer ⇒ Object
Returns the value of attribute current_renderer.
4 5 6 |
# File 'lib/renderer/helpers.rb', line 4 def current_renderer @current_renderer end |
Instance Method Details
#anchor_for(object) ⇒ Object
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/renderer/helpers.rb', line 120 def anchor_for(object) case object when Docrb::Parser::Attribute then "#{object.type}-attr-#{object.name}" when Docrb::Parser::Method then "#{object.type}-method-#{object.name}" when Docrb::Parser::Constant then "const-#{object.name}" when Docrb::Parser::Class, Docrb::Parser::Module then "" else raise ArgumentError, "#anchor_for cannot process #{object.class.name}" end end |
#capture(&block) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/renderer/helpers.rb', line 59 def capture(&block) fake_buffer = "" old_buffer = block.binding.local_variable_get(:_erbout) block.binding.local_variable_set(:_erbout, fake_buffer) raw = block.call captured = if fake_buffer.empty? raw else fake_buffer end ensure block.binding.local_variable_set(:_erbout, old_buffer) end |
#clean_file_path(source) ⇒ Object
76 |
# File 'lib/renderer/helpers.rb', line 76 def clean_file_path(source) = self.class.current_renderer.clean_file_path(source) |
#div(class_name, **kwargs, &block) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/renderer/helpers.rb', line 36 def div(class_name, **kwargs, &block) classes = [class_name, kwargs.delete(:class_name)].flatten.compact args = kwargs .compact .map { |k, v| "#{k}=\"#{v.gsub('"', "\\\"")}\"" } start_tag = "<div class=\"#{classes.join(" ")}\" #{args.join(" ")}>" end_tag = "</div>" fake_buffer = "" old_buffer = block.binding.local_variable_get(:_erbout) block.binding.local_variable_set(:_erbout, fake_buffer) raw = block.call captured = if fake_buffer.empty? raw else fake_buffer end ensure block.binding.local_variable_set(:_erbout, "#{start_tag}#{captured}#{end_tag}") end |
#git_url(source) ⇒ Object
74 |
# File 'lib/renderer/helpers.rb', line 74 def git_url(source) = self.class.current_renderer.git_url(source) |
#line_range(source) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/renderer/helpers.rb', line 78 def line_range(source) from = source.line_start to = source.line_end if from == to "line #{from}" else "lines #{from} to #{to}" end end |
#link_for(object) ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/renderer/helpers.rb', line 109 def link_for(object) case object when Docrb::Parser::Attribute, Docrb::Parser::Method, Docrb::Parser::Constant "#{path_of(object.parent)}##{anchor_for(object)}" when Docrb::Parser::Class, Docrb::Parser::Module path_of(object) else raise ArgumentError, "#link_for cannot link #{object.class.name}" end end |
#make_path ⇒ Object
99 |
# File 'lib/renderer/helpers.rb', line 99 def make_path(*) = self.class.current_renderer.make_path(*) |
#path_of(object, root: true) ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/renderer/helpers.rb', line 101 def path_of(object, root: true) return [] if object.nil? && !root return [object.name] + path_of(object.parent, root: false) unless root path = ["#{object.name}.html"] + path_of(object.parent, root: false) make_path(*path.reverse) end |
#source_of(obj, parent = nil) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/renderer/helpers.rb', line 89 def source_of(obj, parent = nil) parent ||= obj.parent case (v = parent.source_of(obj)) when :inherited, :included, :extended then "inherited" when :self then ("override" if obj.try(:overriding)) else raise "WTF? Source of #{obj} is #{v.inspect}!" end end |
#svg(name, **kwargs) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/renderer/helpers.rb', line 11 def svg(name, **kwargs) attrs = {} cls = kwargs.delete(:class_name) title = kwargs.delete(:title) attrs["class"] = cls if cls && !title kwargs.each { |k, v| attrs[k.to_s] = v.to_s } svg = File.read(Renderer::ASSETS_PATH.join("images/#{name}.svg")) return svg if attrs.empty? && title.nil? doc = Nokogiri::XML svg attrs.each do |k, v| doc.css("svg")[0][k] = v end doc = doc.to_xml.split("\n").tap(&:shift).join("\n") return doc if title.nil? <<~HTML <div class="svg-container #{cls}"> <div class="svg-title">#{title}</div> #{doc} </div> HTML end |