Class: Plain::DocsService

Inherits:
Object
  • Object
show all
Defined in:
app/services/plain/docs_service.rb

Constant Summary collapse

DEFAULT_POSITION =
999

Class Method Summary collapse

Class Method Details

.configObject



36
37
38
39
40
# File 'app/services/plain/docs_service.rb', line 36

def self.config
  file_path = Rails.root.join('docs', 'config.yml')
  return {} if !File.exist?(file_path) 
  config = YAML.safe_load(File.read(file_path)) || {}
end

.get_all_files(structure) ⇒ Object

used in the compile rake task



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/plain/docs_service.rb', line 17

def self.get_all_files(structure)
  files = []

  structure[:children].each do |child|
    if child[:type] == 'file'
      files << child
    elsif child[:type] == 'directory'
      files.concat(get_all_files(child))
    end
  end

  files
end

.get_content(file_path) ⇒ Object



74
75
76
77
78
79
80
81
# File 'app/services/plain/docs_service.rb', line 74

def self.get_content(file_path)
  parsed = self.get_markdown(file_path)
 
  # markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: true)
  # markdown.render(parsed.content).html_safe
  # binding.pry
  Plain::AiDocs.convert_markdown(parsed.content)
end

.get_markdown(file_path) ⇒ Object



69
70
71
72
# File 'app/services/plain/docs_service.rb', line 69

def self.get_markdown(file_path)
  parsed = FrontMatterParser::Parser.parse_file(Rails.root.join('docs', "#{file_path}.md"))
  parsed
end

.get_structureObject



6
7
8
9
10
11
12
13
14
# File 'app/services/plain/docs_service.rb', line 6

def self.get_structure
  # Get markdown files at root directory
  root_files = get_files('docs', true)
  {
    name: 'docs',
    type: 'directory',
    children: root_files[:children] + parse_main_sections  # Add root files to children array
  }
end

.parse_main_sectionsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/services/plain/docs_service.rb', line 42

def self.parse_main_sections
  #file_path = Rails.root.join('docs', 'config.yml')
  #config = YAML.safe_load(File.read(file_path)) || {}
  main_sections = config['sections'] || []

  # Get all directories under 'docs'
  all_sections = Dir.children(Rails.root.join('docs')).select do |entry|
    File.directory?(Rails.root.join('docs', entry))
  end.map do |dir|
    section = get_files(File.join('docs', dir))  # Get children here
    section[:name] = dir
    section[:position] = DEFAULT_POSITION
    section
  end

  # Overwrite positions with those found in config.yml
  main_sections.each do |section|
    matching_section = all_sections.find { |s| s[:name] == section['name'] }
    if matching_section
      matching_section[:position] = section['position']
    end
  end

  # Sort by position and return children
  all_sections.sort_by { |section| section[:position] }
end

.parse_section_itemsObject



31
32
33
34
# File 'app/services/plain/docs_service.rb', line 31

def self.parse_section_items
  config = self.config
  main_sections = config['sections']
end