Class: SourceFile

Inherits:
PersistentDocument show all
Defined in:
lib/almirah/doc_types/source_file.rb

Instance Attribute Summary collapse

Attributes inherited from PersistentDocument

#controlled_items, #frontmatter, #headings, #items, #path, #up_link_docs

Attributes inherited from BaseDocument

#dom, #headings, #id, #title

Instance Method Summary collapse

Methods inherited from BaseDocument

#save_html_to_file

Constructor Details

#initialize(repository_path, fele_path, repository_name) ⇒ SourceFile

Returns a new instance of SourceFile.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/almirah/doc_types/source_file.rb', line 11

def initialize(repository_path, fele_path, repository_name)
  super fele_path
  @root_path = repository_path
  @id = File.basename(fele_path).downcase
  @repository = repository_name
  @html_file_path = '' # available only afer rendering

  @dictionary = {}
  @wrong_links_hash = {}

  @items_with_uplinks_number = 0

  # Calculate the relative path depth to determine correct number of parent directory symbols
  relative_path = @path.sub("#{@root_path}/", '')
  depth = relative_path.count('/') + 1 # +1 for the repository folder
  depth += 1 # for the source_files folder
  @specifications_path = "./#{'../' * depth}specifications/"
  puts @specifications_path
end

Instance Attribute Details

#dictionaryObject

Returns the value of attribute dictionary.



8
9
10
# File 'lib/almirah/doc_types/source_file.rb', line 8

def dictionary
  @dictionary
end

#html_file_pathObject

Returns the value of attribute html_file_path.



8
9
10
# File 'lib/almirah/doc_types/source_file.rb', line 8

def html_file_path
  @html_file_path
end

Returns the value of attribute items_with_uplinks_number.



8
9
10
# File 'lib/almirah/doc_types/source_file.rb', line 8

def items_with_uplinks_number
  @items_with_uplinks_number
end

#repositoryObject

Returns the value of attribute repository.



8
9
10
# File 'lib/almirah/doc_types/source_file.rb', line 8

def repository
  @repository
end

#root_pathObject

Returns the value of attribute root_path.



8
9
10
# File 'lib/almirah/doc_types/source_file.rb', line 8

def root_path
  @root_path
end

#specifications_pathObject

Returns the value of attribute specifications_path.



8
9
10
# File 'lib/almirah/doc_types/source_file.rb', line 8

def specifications_path
  @specifications_path
end

Returns the value of attribute wrong_links_hash.



8
9
10
# File 'lib/almirah/doc_types/source_file.rb', line 8

def wrong_links_hash
  @wrong_links_hash
end

Instance Method Details

#save_to_file(html_rows, nav_pane, output_file_path) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



67
68
69
70
71
72
73
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
# File 'lib/almirah/doc_types/source_file.rb', line 67

def save_to_file(html_rows, nav_pane, output_file_path) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  gem_root = File.expand_path './../../..', File.dirname(__FILE__)
  template_file = "#{gem_root}/lib/almirah/templates/page.html"

  file = File.open(template_file)
  file_data = file.readlines
  file.close

  output_file_path += "#{@repository}/"
  output_file_path += @path.sub("#{@root_path}/", '')
  output_file_path += '.html'
  @html_file_path = output_file_path
  FileUtils.mkdir_p(File.dirname(output_file_path))

  # Calculate the relative path depth to determine correct number of parent directory symbols
  relative_path = @path.sub("#{@root_path}/", '')
  depth = relative_path.count('/') + 1 # +1 for the repository folder
  depth += 1 # for the source_files folder
  css_path = "#{'../' * depth}css/main.css"
  js_path = "#{'../' * depth}scripts/main.js"
  index_path = "#{'../' * depth}index.html"

  file = File.open(output_file_path, 'w')
  file_data.each do |s|
    if s.include?('{{CONTENT}}')
      html_rows.each do |r|
        file.puts r
      end
    elsif s.include?('{{NAV_PANE}}')
      file.puts nav_pane.to_html if nav_pane
    elsif s.include?('{{DOCUMENT_TITLE}}')
      file.puts s.gsub! '{{DOCUMENT_TITLE}}', @title
    elsif s.include?('{{STYLES_AND_SCRIPTS}}')
      file.puts "<link rel=\"stylesheet\" href=\"#{css_path}\">"
      file.puts "<script src=\"#{js_path}\"></script>"
    elsif s.include?('{{HOME_BUTTON}}')
      file.puts "<a id=\"index_menu_item\" href=\"#{index_path}\"><span><i class=\"fa fa-info\" aria-hidden=\"true\"></i></span>&nbsp;Index</a>"
    elsif s.include?('{{GEM_VERSION}}')
      file.puts "(#{Gem.loaded_specs['Almirah'].version.version})"
    else
      file.puts s
    end
  end
  file.close
end

#to_consoleObject



31
32
33
# File 'lib/almirah/doc_types/source_file.rb', line 31

def to_console
  puts "\e[32mSource File: [#{@repository}] #{@id}\e[0m"
end

#to_html(output_file_path) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/almirah/doc_types/source_file.rb', line 35

def to_html(output_file_path)
  html_rows = []

  html_rows.append('')

  @items.each do |item|
    a = item.to_html
    html_rows.append a
  end

  # make some nice lexed html
  source = File.read(@path.to_s)
  # Detect lexer from file extension
  # lexer = Rouge::Lexer.find_fancy(@path.to_s, source) || Rouge::Lexers::PlainText.new
  lexer = Rouge::Lexer.guess_by_filename(@path.to_s) || Rouge::Lexers::PlainText.new
  formatter = Rouge::Formatters::HTML.new

  # Add Base16 theme CSS
  theme_css = Rouge::Themes::Pastie.render(scope: '.highlight')
  html_rows.append "<style>\n#{theme_css}\n</style>"

  # Format the source code with syntax highlighting
  formatted_html = formatter.format(lexer.lex(source))

  # Add formatted code with highlighting styles
  html_rows.append '<div class="highlight" style="background-color:#f6f7f8;"><pre>'
  html_rows.append formatted_html
  html_rows.append '</pre></div>'

  save_to_file(html_rows, nil, output_file_path)
end