Class: Renderer

Inherits:
Object show all
Defined in:
lib/docrb-html.rb,
lib/docrb-html.rb,
lib/renderer/page.rb,
lib/renderer/helpers.rb,
lib/renderer/version.rb,
lib/renderer/entities.rb,
lib/renderer/markdown.rb,
lib/renderer/metadata.rb,
lib/renderer/template.rb,
lib/renderer/component.rb,
lib/renderer/entities/base.rb,
lib/renderer/entities/class.rb,
lib/renderer/entities/method.rb,
lib/renderer/entities/module.rb,
lib/renderer/component/footer.rb,
lib/renderer/component/symbol.rb,
lib/renderer/component/doc_box.rb,
lib/renderer/component/tab_bar.rb,
lib/renderer/component/typedef.rb,
lib/renderer/component/checkbox.rb,
lib/renderer/component/markdown.rb,
lib/renderer/entities/attribute.rb,
lib/renderer/entities/container.rb,
lib/renderer/entities/reference.rb,
lib/renderer/component/attribute.rb,
lib/renderer/component/reference.rb,
lib/renderer/component/breadcrumb.rb,
lib/renderer/component/text_block.rb,
lib/renderer/component/field_block.rb,
lib/renderer/component/method_list.rb,
lib/renderer/component/class_header.rb,
lib/renderer/component/class_mod_name.rb,
lib/renderer/component/component_list.rb,
lib/renderer/component/method_display.rb,
lib/renderer/component/project_header.rb,
lib/renderer/entities/method_argument.rb,
lib/renderer/component/method_argument.rb,
lib/renderer/component/type_definition.rb,
lib/renderer/component/constant_display.rb,
lib/renderer/entities/method_definition.rb,
lib/renderer/entities/source_definition.rb,
lib/renderer/component/attribute_display.rb,
lib/renderer/component/documentation_block.rb,
lib/renderer/entities/attribute_definition.rb,
lib/renderer/component/class_path_reference.rb,
lib/renderer/component/documentation_comment.rb

Defined Under Namespace

Modules: Entities Classes: Component, Helpers, InlineRenderer, Markdown, MarkdownRenderer, Metadata, Page, Template

Constant Summary collapse

ASSETS_PATH =
Pathname.new(__dir__).join("../assets")
TEMPLATES_PATH =
Pathname.new(__dir__).join("../templates")
STYLE_BASE =
SassC::Engine.new(File.read(ASSETS_PATH.join("style.scss")),
style: :compressed,
load_paths: [ASSETS_PATH]).render
VERSION =
"0.3.4"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, spec, output) ⇒ Renderer

Returns a new instance of Renderer.



38
39
40
41
42
43
44
# File 'lib/docrb-html.rb', line 38

def initialize(source, spec, output)
  @output = output
  @footer = Component::Footer.new(version: VERSION, updated_at: now)
  @spec = spec
  @source = source
  Helpers.current_renderer = self
end

Instance Attribute Details

#specObject (readonly)

Returns the value of attribute spec.



32
33
34
# File 'lib/docrb-html.rb', line 32

def spec
  @spec
end

Instance Method Details

#clean_file_path(definition) ⇒ Object



53
# File 'lib/docrb-html.rb', line 53

def clean_file_path(definition) = definition.file_path.gsub(@spec[:git_root], "")

#copy_assetsObject



131
132
133
134
135
136
137
138
139
# File 'lib/docrb-html.rb', line 131

def copy_assets
  [
    "js/filtering.js",
    "favicon.ico"
  ].each do |file|
    File.write(output_path(File.basename(file)),
      File.read(ASSETS_PATH.join(file)))
  end
end

#git_url(loc) ⇒ Object



46
47
48
49
50
51
# File 'lib/docrb-html.rb', line 46

def git_url(loc)
  url = @spec[:git_url]
  tip = @spec[:git_tip]
  filename = clean_file_path(loc)
  "#{url}/blob/#{tip}#{filename}#L#{loc.line_start}"
end

#make_outlineObject



55
56
57
58
# File 'lib/docrb-html.rb', line 55

def make_outline
  (@source.nodes.by_kind(:module) + @source.nodes.by_kind(:class))
    .map { outline(_1) }
end

#make_path(*args) ⇒ Object



60
61
62
63
# File 'lib/docrb-html.rb', line 60

def make_path(*args)
  root = spec.fetch(:base_path, "/")
  File.join(root, *args.flatten.map(&:to_s))
end

#nowObject



34
# File 'lib/docrb-html.rb', line 34

def now = Time.now.strftime("%A, %-d %b %Y %H:%M:%S %Z")

#outline(object, level = 0) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/docrb-html.rb', line 65

def outline(object, level = 0)
  {
    level:,
    object:,
    classes: object.classes.map { outline(_1, level + 1) },
    modules: object.modules.map { outline(_1, level + 1) }
  }
end

#output_path(*args) ⇒ Object



36
# File 'lib/docrb-html.rb', line 36

def output_path(*args) = File.join(@output, *args.map(&:to_s))

#pages(comps, parents = []) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/docrb-html.rb', line 141

def pages(comps, parents = [])
  comps.each do |comp|
    title = "#{comp.name} - #{@spec[:name]} - Docrb"
    page = Page.new(title:, level: parents.count) do
      [
        Component::ClassHeader.new(
          type: comp.kind,
          name: comp.name,
          definitions: comp.defined_by.map do |by|
            {
              filename: File.basename(by.file_path),
              git_url: git_url(by)
            }
          end
        ),
        Component::Breadcrumb.new(
          project_name: @spec[:name],
          items: (parents + [comp]).map.with_index do |p, idx|
            { name: p.name, parents: parents[0...idx].map(&:name) }
          end
        ),
        Component::DocBox.new(
          item: comp,
          meta: @spec
        ),
        @footer
      ]
    end

    parent_dir = output_path(*parents.map(&:name))
    FileUtils.mkdir_p(parent_dir)
    page.render_to(File.join(parent_dir, "#{comp.name}.html"))

    pages(comp.classes + comp.modules, parents + [comp])
  end
end

#renderObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/docrb-html.rb', line 74

def render
  project_header = Component::ProjectHeader.new(
    name: @spec[:name],
    description: @spec[:summary],
    license: @spec[:license],
    owner: Metadata.format_authors(@spec[:authors]),
    links: Metadata.project_links(@spec)
  )

  readme = if @spec[:readme]
    Component::Markdown.new(source: Markdown.render(@spec[:readme]))
  else
    Component::Markdown.new(source: "<div class=\"html\">" \
                                    "<div class=\"faded\">This project does not contain a README.</div></div>")
  end

  index = Page.new(title: "#{@spec[:name]} - Docrb") do
    [
      project_header,
      Component::TabBar.new(
        selected_index: 0,
        items: [
          { name: "Readme", href: make_path("/") },
          { name: "Components", href: make_path("/components.html") }
        ]
      ),
      readme,
      @footer
    ]
  end

  components = Page.new(title: "Components - #{@spec[:name]} - Docrb") do
    [
      project_header,
      Component::TabBar.new(
        selected_index: 1,
        items: [
          { name: "Readme", href: make_path("/") },
          { name: "Components", href: make_path("/components.html") }
        ]
      ),
      Component::ComponentList.new(list: make_outline),
      @footer
    ]
  end

  pages(@source.nodes.by_kind(:class, :module))

  FileUtils.mkdir_p @output

  index.render_to(output_path("index.html"))
  components.render_to(output_path("components.html"))
  File.write(output_path("style.css"), STYLE_BASE)

  copy_assets
end