Class: Fetcher::Documentator

Inherits:
Object
  • Object
show all
Defined in:
lib/fetcher-documentator.rb,
lib/fetcher-documentator/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Documentator

Returns a new instance of Documentator.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# File 'lib/fetcher-documentator.rb', line 7

def initialize path
  @name = "doc/" + path[4..-4] + ".md"

  @text_flat = flatten File.read path

  @doc = []
  class_name_parts = path[0..-4].split("/").last.split "_"
  class_name = ""
  class_name_parts.each do |part|
    class_name += part.capitalize
  end
  
  @text_flat.length.times do |i|
    if @text_flat[i].start_with? "##"
      if @text_flat[i].start_with? "###"
        method = @text_flat[i][4..-1].split(" ").first
        @doc << ""
        @doc << method
        @doc << ("-" * method.length)
        @doc << ""
        @doc << ("    " + @text_flat[i][4..-1])
      elsif @text_flat[i].start_with? "## @"
        transform_to_link(@text_flat[i][4..-1]).each do |line|
          @doc << line
        end
      elsif @text_flat[i].start_with? "## >"
        @doc << @text_flat[i][3..-1]
      else
        @doc << @text_flat[i][3..-1] 
        if @doc.last.nil?
          @doc[-1] = ""
        else
          unless @doc.last.end_with? ":"
            @doc[-1] = "    #{@doc.last}"
          else
            last = @doc.last
            if @doc[-2] == ""
              @doc[-1] = "### #{last}" 
            else
              @doc[-1] = "" 
              @doc << "### #{last}"
            end
          end
        end
        unless @text_flat[i] == @text_flat.last
          @doc << "" unless @text_flat[i + 1].start_with? "##"
        end
      end
    end
  end

  unless @doc.empty?  
    @doc.unshift ("=" * class_name.length)
    @doc.unshift class_name
    file.write @name, @doc.join("\n") 
  end
end

Instance Method Details

#flatten(text) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/fetcher-documentator.rb', line 65

def flatten text
  flat = []
  text.lines.to_a.each do |line|
    flat << line.strip
  end
  flat
end


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fetcher-documentator.rb', line 73

def transform_to_link text
  method = text.split(".").last
  hierarchy = text.split(".")[0..-2].join(".")
  hierarchy_parts = hierarchy.split "/"
  domain = hierarchy_parts.shift
  capitalized_parts = []
  hierarchy_parts.each do |part|
    snaked = part.split "_"      
    up_snaked = []
    snaked.each do |sn|
      up_snaked << sn.capitalize
    end
    capitalized_parts << up_snaked.join("")
  end
  data = []
  data << "### #{domain}"
  data << ""
  data << "[#{method.upcase} #{domain}/#{capitalized_parts.join("/")}](https://github.com/Fetcher/#{domain}/blob/master/doc/#{domain}/#{hierarchy_parts.join("/")}.md##{method})"
  data
end