Utopia::ProjectSourceUtopiaProjectBase

class Base

Provides structured access to a project directory which contains source code and guides.

Usage

To get an instance for the current project, use Base#instance.

Definitions

def initialize(root = Dir.pwd)

Initialize the project with the given root path.

Signature

parameter root String

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

Implementation

def initialize(root = Dir.pwd)
	@root = root
	
	@source_path = Utopia::Path["/source"]
	
	@index = Decode::Index.new
	
	@links = Utopia::Content::Links.new(@root)
end

attr :root

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

Signature

attribute String

attr :index

The source code index which is used for generating pages.

Signature

attribute Decode::Index

def path_for(file_name)

Return the absolute path for the given file name, if it exists in the project.

Signature

parameter file_name String

The relative path to the project file, e.g. readme.md.

returns String

The file-system path.

Implementation

def path_for(file_name)
	full_path = File.expand_path(file_name, @root)
	if File.exist?(full_path)
		return full_path
	end
end

def update(paths)

Update the index with the specified paths.

Signature

parameter paths Array(String)

The paths to load and parse.

Implementation

def update(paths)
	@index.update(paths)
end

def best(definitions)

Given an array of defintions, return the best definition for the purposes of generating documentation.

Signature

returns Decode::Definition | Nil

Implementation

def best(definitions)
	definitions.each do |definition|
		if definition.documentation
			return definition
		end
	end
	
	return definitions.first
end

def lookup(path)

Given a lexical path, find the best definition for that path.

Signature

returns Tuple(Decode::Trie::Node, Decode::Definition)

Implementation

def lookup(path)
	if node = @index.trie.lookup(path.map(&:to_sym))
		return node, best(node.values)
	end
end

def format(text, definition = nil, language: definition&.language, **options)

Format the given text in the context of the given definition and language. See Utopia::Project::Base#document for details.

Signature

returns XRB::MarkupString

Implementation

def format(text, definition = nil, language: definition&.language, **options)
	case text
	when Enumerable
		text = text.to_a.join("\n")
	when nil
		return nil
	end
	
	if document = self.document(text, definition, language: language)
		return XRB::MarkupString.raw(
			document.to_html(**options)
		)
	end
end

def document(text, definition = nil, language: definition&.language)

Convert the given markdown text into HTML.

Updates source code references ({language identifier}) into links.

Signature

returns Document

Implementation

def document(text, definition = nil, language: definition&.language)
	Document.new(text, self, definition: definition, default_language: language)
end

def id_for(definition, suffix = nil)

Compute a unique string which can be used as id attribute in the HTML output.

Signature

returns String

Implementation

def id_for(definition, suffix = nil)
	if suffix
		"#{definition.qualified_name}-#{suffix}"
	else
		definition.qualified_name
	end
end

def guides

Enumerate over all available guides in order.

Signature

yields {|guide| ...}

If a block is given.

parameter guide Guide
returns Enumerator(Guide)

If no block given.

Implementation

def guides
	return to_enum(:guides) unless block_given?
	
	@links.index("/guides").each do |link|
		guide_path = File.join(@root, link.path)
		
		next unless File.directory?(guide_path)
		
		yield Guide.new(self, guide_path)
	end
end