Class: DocumentationEditor::Revision

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/documentation_editor/revision.rb

Instance Method Summary collapse

Instance Method Details

#to_html(options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'app/models/documentation_editor/revision.rb', line 5

def to_html(options = {})
  html = parse_document(options).to_html
  # resolve the variables
  html = resolve_variables(options, html)
  # resolve the code condition block
  html = html.gsub(/\[\[ *(.+?) *\]\](.+?)\[\[ *\/.+? *\]\]/m) do |m|
    options[:language] == $1 ? $2 : ''
  end
  # add anchor links before headers
  html.gsub(/<h[1-6] id="([^"]+)">/) { |m| "#{m}<a href=\"##{$1}\" class=\"anchor\"><i class=\"fa fa-link\"></i></a>"  }
end

#to_toc(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/documentation_editor/revision.rb', line 17

def to_toc(options = {})
  roots = []
  levels = []
  h1 = nil
  doc = parse_document(options.merge(no_wrap: true))
  ids = id_generator(doc)
  doc.root.children.each do |child|
    next if child.type != :header
    text = child.options[:raw_text]
    text = resolve_variables(options, text)
    id = ids.generate_id(text)
    h1 = id if child.options[:level] == 1
    item = { label: text, id: id, children: [], level: child.options[:level], h1: h1 }
    while !levels.empty? && levels.last[:level] >= child.options[:level]
      levels.pop
    end
    if levels.empty?
      roots << item
    else
      levels.last[:children] << item
    end
    levels << item
  end
  roots
end