Class: DocumentationEditor::Revision
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- DocumentationEditor::Revision
- Defined in:
- app/models/documentation_editor/revision.rb
Instance Method Summary collapse
- #section_title(options, section) ⇒ Object
- #to_html(options = {}) ⇒ Object
- #to_toc(options = {}) ⇒ Object
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(, section) = defaulted() doc = parse_document(.merge(no_wrap: true)) ids = id_generator(doc) doc.root.children.each do |child| text = child.[:raw_text] return text if child.type == :header && child.[:level] == 1 && section == ids.generate_id(resolve_variables(, 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( = {}) = defaulted() html = parse_document().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(, html) # resolve the code condition block html = html.gsub(/\[\[ *(.+?) *\]\](.+?)\[\[ *\/.+? *\]\]/m) do |m| [: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( = {}) = defaulted() roots = [] levels = [] h1 = nil doc = parse_document(.merge(no_wrap: true)) ids = id_generator(doc) doc.root.children.each do |child| next if child.type != :header text = child.[:raw_text] text = resolve_variables(, text) id = ids.generate_id(text) h1 = id if child.[:level] == 1 item = { label: text, id: id, children: [], level: child.[:level], h1: h1 } while !levels.empty? && levels.last[:level] >= child.[:level] levels.pop end if levels.empty? roots << item else levels.last[:children] << item end levels << item end roots end |