Class: Utopia::Project::Guide

Inherits:
Object
  • Object
show all
Defined in:
lib/utopia/project/guide.rb

Overview

Provides structured access to a directory which contains documentation and source code to explain a specific process.

Constant Summary collapse

README =
"README.md"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, root) ⇒ Guide

Initialize the example with the given root path.

Parameters:

  • base (Base)

    The base instance for the project.

  • root (String)

    The file-system path to the root of the example.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/utopia/project/guide.rb', line 35

def initialize(base, root)
	@base = base
	@root = root
	
	@documentation = nil
	
	@document = nil
	@title = nil
	@description = nil
	
	self.document
end

Instance Attribute Details

#baseObject (readonly)

The base instance of the project this example is loaded from.



92
93
94
# File 'lib/utopia/project/guide.rb', line 92

def base
  @base
end

#descriptionObject (readonly)

The description from the first paragraph in the README.



50
51
52
# File 'lib/utopia/project/guide.rb', line 50

def description
  @description
end

#rootObject (readonly)

The file-system path to the root of the project.



95
96
97
# File 'lib/utopia/project/guide.rb', line 95

def root
  @root
end

Instance Method Details

#documentObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/utopia/project/guide.rb', line 68

def document
	if self.readme?
		@document ||= self.readme_document.tap do |document|
			root = document.root
			if element = root.children.first
				if element.type == :header
					@title = element.children.first.value
					
					# Remove the title:
					root.children.shift
					
					# Remove any blank lines:
					root.children.shift while root.children.first&.type == :blank
					
					# Read the description:
					root.children.first.options[:encoding] = root.options[:encoding]
					@description = Kramdown::Converter::Kramdown.convert(root.children.first).first
				end
			end
		end
	end
end

#documentationObject



109
110
111
# File 'lib/utopia/project/guide.rb', line 109

def documentation
	@documentation ||= self.best_documentation
end

#filesObject



113
114
115
# File 'lib/utopia/project/guide.rb', line 113

def files
	Dir.glob(File.expand_path("*", @root))
end

#hrefObject



105
106
107
# File 'lib/utopia/project/guide.rb', line 105

def href
	"/guides/#{self.name}/index"
end

#nameObject



97
98
99
# File 'lib/utopia/project/guide.rb', line 97

def name
	File.basename(@root)
end

#readme?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/utopia/project/guide.rb', line 58

def readme?
	File.exist?(readme_path)
end

#readme_documentObject



62
63
64
65
66
# File 'lib/utopia/project/guide.rb', line 62

def readme_document
	content = File.read(self.readme_path)
	
	return @base.document(content)
end

#readme_pathObject



54
55
56
# File 'lib/utopia/project/guide.rb', line 54

def readme_path
	File.expand_path(README, @root)
end

#sourcesObject



117
118
119
120
121
122
123
124
125
# File 'lib/utopia/project/guide.rb', line 117

def sources
	return to_enum(:sources) unless block_given?
	
	files.each do |path|
		if source = @base.index.languages.source_for(path)
			yield source
		end
	end
end

#titleObject



101
102
103
# File 'lib/utopia/project/guide.rb', line 101

def title
	@title || Trenni::Strings.to_title(self.name)
end