Class: DocumentationEditor::Revision

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

Instance Method Summary collapse

Instance Method Details

#section_title(options, section) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'app/models/documentation_editor/revision.rb', line 47

def section_title(options, section)
  options = defaulted(options)
  doc = parse_document(options.merge(no_wrap: true))
  ids = id_generator(doc)
  doc.root.children.each do |child|
    text = child.options[:raw_text]
    return text if child.type == :header && child.options[:level] == 1 && section == ids.generate_id(resolve_variables(options, text))
  end
  nil
end

#to_html(options = {}) ⇒ Object



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

def to_html(options = {})
  options = defaulted(options)
  html = parse_document(options).to_html
  # lower titles
  html.gsub!(/<h([1-6])/) { |m| "<h#{$1.to_i + 1}"  } if DocumentationEditor::Config.lower_title_levels
  # 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



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

def to_toc(options = {})
  options = defaulted(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