Module: Webapidoc

Defined in:
lib/webapidoc/base.rb,
lib/webapidoc/version.rb,
lib/generators/webapidoc/install_generator.rb

Defined Under Namespace

Modules: Generators

Constant Summary collapse

NAME =
"webapidoc"
VERSION =
"0.1.2"
@@docDir =
"app/documentation"
@@publicDir =
"public/documentation"
@@configFile =
'config/webapidoc.yml'

Class Method Summary collapse

Class Method Details

.buildObject



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/webapidoc/base.rb', line 33

def self.build
  # get config hash
  @data = YAML.load_file(@@configFile).freeze

  puts "building webapidoc for " + @data["title"]
  puts "api url: " + @data["url"] if @data["url"]

  # clean up
  FileUtils.remove_dir @@publicDir if File.exists?(@@publicDir)

  # create @s
  FileUtils.mkdir_p @@publicDir
  FileUtils.mkdir @@publicDir + "/css"

  # copy javascripts
  FileUtils.cp_r(libDir + "/js", @@publicDir + "/js")

  # compile webapidoc style
  sass_filename = libDir + "/css/webapidoc.scss"
  css = Sass::Engine.for_file(sass_filename, {:style => :compressed}).render
  File.open(@@publicDir + "/css/webapidoc.css", "wb") {|f| f.write(css) }

  # open index file
  inFile = "#{@@docDir}/documentation.md"
  # look for erb
  if !File.exists?(inFile)
    inFile = inFile + ".erb"
  end
  # not found?
  if !File.exists?(inFile)
    puts "documentation not found: #{inFile}"
    exit 0
  end

  # open file
  contents = ERB.new(File.open(inFile, 'r').read).result(binding)
  maruku = Maruku.new(contents)

  # build html
  html = Nokogiri::HTML(maruku.to_html)

  # build chapters
  @chapters = []
  @chapters = html.xpath('//body').children.inject([]) do |chapters_hash, child|
    # build chapter
    if child.name == 'h1'
      title = child.inner_text
      anchor = title.downcase.gsub(/[^\d\w\s]/, "").tr(" ", "_")
      chapters_hash << { :title => title, :contents => '', :file => anchor}
    end

    next chapters_hash if chapters_hash.empty?
    chapters_hash.last[:contents] << child.to_xhtml
    chapters_hash
  end

  # build sections
  @sections = []
  cidx = -1
  html.xpath('//body').children.inject([]) do |chapters_hash, child|
    # store sections for navigation
    if child.name == 'h1' or child.name == 'h2' or child.name == 'h3'
      title = child.inner_text
      anchor = title.downcase.gsub(/[^\d\w\s]/, "").tr(" ", "_")
      level = child.name[-1, 1]
      cidx = cidx + 1 if level == "1"
      next unless @chapters[cidx].present?
      file = @chapters[cidx][:file]
      @sections << {:title => title, :file => file, :anchor => anchor, :level => level}
    end
  end

  # open template
  template = "#{libDir}/template.html.erb"

  # write each chapter to file
  @chapters.each_with_index do | chapter, idx |
    outFile = "#{@@publicDir}/#{chapter[:file]}.html"
    puts idx.to_s + ":" + chapter[:title] + " > " + outFile
    # convert md to html
    @html = chapter[:contents]
    # write it
    File.open(outFile, 'w') { |file| file.write(ERB.new(File.open(template, 'r').read).result(binding)) }
  end
end

.libDirObject



29
30
31
# File 'lib/webapidoc/base.rb', line 29

def self.libDir
  File.dirname(__FILE__)
end

.partial(template) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/webapidoc/base.rb', line 8

def self.partial(template)
  # rails like _ templates, simple injection
  template = template.to_s.split('/')
  template[ template.length - 1 ] = '_' + template[ template.length - 1 ]
  template = template.join('/')
  inFile = @@docDir + "/" + template + ".md"

  if !File.exists?(inFile)
    inFile = inFile + ".erb"
  end

  if !File.exists?(inFile)
    puts "partial not found: #{inFile}"
    return
  end

  # read partials contents and inject it
  ERB.new(File.open(inFile, 'r').read).result(binding)

end