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.



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.



86
87
88
# File 'lib/utopia/project/guide.rb', line 86

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.



89
90
91
# File 'lib/utopia/project/guide.rb', line 89

def root
  @root
end

Instance Method Details

#documentObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/utopia/project/guide.rb', line 62

def document
  if self.readme?
    @document ||= Kramdown::Document.new(File.read(self.readme_path), syntax_highlighter: nil).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



103
104
105
# File 'lib/utopia/project/guide.rb', line 103

def documentation
  @documentation ||= self.best_documentation
end

#filesObject



107
108
109
# File 'lib/utopia/project/guide.rb', line 107

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

#hrefObject



99
100
101
# File 'lib/utopia/project/guide.rb', line 99

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

#nameObject



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

def name
  File.basename(@root)
end

#readme?Boolean



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

def readme?
  File.exist?(readme_path)
end

#readme_pathObject



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

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

#sourcesObject



111
112
113
114
115
116
117
118
119
# File 'lib/utopia/project/guide.rb', line 111

def sources
  return to_enum(:sources) unless block_given?
  
  files.each do |path|
    if source = Decode::Source.for?(path)
      yield source
    end
  end
end

#titleObject



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

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