Class: Leandocument::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/leandocument/document.rb

Constant Summary collapse

SETTING_FILE_NAME =
"settings.yml"
SUPPORT_EXTENSIONS =
%w(md textile markdown mdown rdoc org creole mediawiki rst rest)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Document

Generate Document class.

Args

options

lang:Default document language. base_path: Document path. indent: Document indent level.

Return

New Leandocument Document class.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/leandocument/document.rb', line 23

def initialize(options = {})
  self.base_path = options[:base_path] || Dir.pwd
  self.settings = options[:settings] || load_config
  self.lang = options[:lang] || setting_value("settings", "default_locale")
  self.web_path  = "#{options[:web_path]}/"
  self.indent = options[:indent] || 1
  self.extension = get_extension
  self.filename = options[:filename] || file_name
  self.childs = []
  self.browsers = []
  self.repository = options[:repository]
  self.commit     = options[:commit]
end

Instance Attribute Details

#base_pathObject

lang

Document language. TODO support default language.

settings

LeanDocument Settings. TODO read settings.

base_path

Document path.

indent

Document indent. Child documents are plus on from parent. Then change to h tag from h tag. <h1> -> <h2>



16
17
18
# File 'lib/leandocument/document.rb', line 16

def base_path
  @base_path
end

#browsersObject

lang

Document language. TODO support default language.

settings

LeanDocument Settings. TODO read settings.

base_path

Document path.

indent

Document indent. Child documents are plus on from parent. Then change to h tag from h tag. <h1> -> <h2>



16
17
18
# File 'lib/leandocument/document.rb', line 16

def browsers
  @browsers
end

#childsObject

lang

Document language. TODO support default language.

settings

LeanDocument Settings. TODO read settings.

base_path

Document path.

indent

Document indent. Child documents are plus on from parent. Then change to h tag from h tag. <h1> -> <h2>



16
17
18
# File 'lib/leandocument/document.rb', line 16

def childs
  @childs
end

#commitObject

lang

Document language. TODO support default language.

settings

LeanDocument Settings. TODO read settings.

base_path

Document path.

indent

Document indent. Child documents are plus on from parent. Then change to h tag from h tag. <h1> -> <h2>



16
17
18
# File 'lib/leandocument/document.rb', line 16

def commit
  @commit
end

#error_messageObject

lang

Document language. TODO support default language.

settings

LeanDocument Settings. TODO read settings.

base_path

Document path.

indent

Document indent. Child documents are plus on from parent. Then change to h tag from h tag. <h1> -> <h2>



16
17
18
# File 'lib/leandocument/document.rb', line 16

def error_message
  @error_message
end

#extensionObject

lang

Document language. TODO support default language.

settings

LeanDocument Settings. TODO read settings.

base_path

Document path.

indent

Document indent. Child documents are plus on from parent. Then change to h tag from h tag. <h1> -> <h2>



16
17
18
# File 'lib/leandocument/document.rb', line 16

def extension
  @extension
end

#filenameObject

lang

Document language. TODO support default language.

settings

LeanDocument Settings. TODO read settings.

base_path

Document path.

indent

Document indent. Child documents are plus on from parent. Then change to h tag from h tag. <h1> -> <h2>



16
17
18
# File 'lib/leandocument/document.rb', line 16

def filename
  @filename
end

#indentObject

lang

Document language. TODO support default language.

settings

LeanDocument Settings. TODO read settings.

base_path

Document path.

indent

Document indent. Child documents are plus on from parent. Then change to h tag from h tag. <h1> -> <h2>



16
17
18
# File 'lib/leandocument/document.rb', line 16

def indent
  @indent
end

#langObject

lang

Document language. TODO support default language.

settings

LeanDocument Settings. TODO read settings.

base_path

Document path.

indent

Document indent. Child documents are plus on from parent. Then change to h tag from h tag. <h1> -> <h2>



16
17
18
# File 'lib/leandocument/document.rb', line 16

def lang
  @lang
end

#repositoryObject

lang

Document language. TODO support default language.

settings

LeanDocument Settings. TODO read settings.

base_path

Document path.

indent

Document indent. Child documents are plus on from parent. Then change to h tag from h tag. <h1> -> <h2>



16
17
18
# File 'lib/leandocument/document.rb', line 16

def repository
  @repository
end

#settingsObject

lang

Document language. TODO support default language.

settings

LeanDocument Settings. TODO read settings.

base_path

Document path.

indent

Document indent. Child documents are plus on from parent. Then change to h tag from h tag. <h1> -> <h2>



16
17
18
# File 'lib/leandocument/document.rb', line 16

def settings
  @settings
end

#web_pathObject

lang

Document language. TODO support default language.

settings

LeanDocument Settings. TODO read settings.

base_path

Document path.

indent

Document indent. Child documents are plus on from parent. Then change to h tag from h tag. <h1> -> <h2>



16
17
18
# File 'lib/leandocument/document.rb', line 16

def web_path
  @web_path
end

Instance Method Details

#contentObject

Return file content or blank string

Return

File content. Or blank string if file not found.



97
98
99
100
101
102
103
104
105
# File 'lib/leandocument/document.rb', line 97

def content
  return @content if @content
  if self.commit
    @content = find_content ? find_content.data.force_encoding('UTF-8') : ""
  else
    @content = File.exist?(self.file_path) ? open(self.file_path).read.force_encoding('UTF-8') : ""
  end
  @content
end

#eObject



44
45
46
# File 'lib/leandocument/document.rb', line 44

def e
  self.error_message
end

#e?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/leandocument/document.rb', line 37

def e?
  return (self.error_message = "File not found. #{file_path}(#{SUPPORT_EXTENSIONS.join("|")})") unless get_extension
  return (self.error_message = "Something wrong setting file. #{config_file_path}") unless self.settings
  return (self.error_message = "File convert error. Please check file encoding. LeanDocument is allow only UTF-8. #{file_path}") unless self.title
  nil
end

#titleObject



72
73
74
75
76
77
78
79
# File 'lib/leandocument/document.rb', line 72

def title
  return "" unless render
  begin
    render.title
  rescue ArgumentError => e
    nil
  end
end

#to_htmlObject

Generate HTML content.

Return

HTML content.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/leandocument/document.rb', line 51

def to_html
  return "" unless file_name
  return e if e?
  page = render.to_html
  path = File.dirname(file_path)
  # Get child content.
  # A reflexive.
  files(path).each do |file|
    doc = Document.new :base_path => self.base_path, :lang => self.lang, :indent => self.indent, :settings => self.settings, :web_path => self.web_path, :commit => self.commit, :repository => self.repository, :filename => file
    self.browsers << doc
    page += doc.to_html
  end
  dirs(path).each do |dir|
    # Plus one indent from parent. Change h[1-6] tag to h[2-7] if indent is 1.
    doc = Document.new :base_path => dir, :lang => self.lang, :indent => self.indent + 1, :settings => self.settings, :web_path => self.web_path[0..-2] + dir.gsub(self.base_path, ""), :commit => self.commit, :repository => self.repository
    self.childs << doc
    page += doc.to_html
  end
  page
end

#tocObject



81
82
83
84
85
86
87
88
# File 'lib/leandocument/document.rb', line 81

def toc
  return @toc if @toc
  @toc = render.toc
  self.childs.each do |doc|
    @toc += doc.toc
  end
  @toc
end

#toc=(toc) ⇒ Object



90
91
92
# File 'lib/leandocument/document.rb', line 90

def toc=(toc)
  @toc += toc
end