Class: JsDuck::Guides

Inherits:
GroupedAsset show all
Defined in:
lib/jsduck/guides.rb

Overview

Reads in guides and converts them to JsonP files

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GroupedAsset

#[], #build_map_by_name

Constructor Details

#initialize(filename, formatter, opts) ⇒ Guides

Parses guides config file



24
25
26
27
28
29
30
# File 'lib/jsduck/guides.rb', line 24

def initialize(filename, formatter, opts)
  @path = File.dirname(filename)
  @groups = JsonDuck.read(filename)
  build_map_by_name("Two guides have the same name")
  @formatter = formatter
  @opts = opts
end

Class Method Details

.create(filename, formatter, opts) ⇒ Object

Creates Guides object from filename and formatter



15
16
17
18
19
20
21
# File 'lib/jsduck/guides.rb', line 15

def self.create(filename, formatter, opts)
  if filename
    Guides.new(filename, formatter, opts)
  else
    NullObject.new(:to_array => [], :to_html => "", :[] => nil)
  end
end

Instance Method Details

#add_toc(guide, html) ⇒ Object

Creates table of contents at the top of guide by looking for <h2> elements in HTML.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/jsduck/guides.rb', line 104

def add_toc(guide, html)
  toc = [
    "<div class='toc'>\n",
    "<p><strong>Contents</strong></p>\n",
    "<ol>\n",
  ]
  new_html = []
  i = 0
  html.each_line do |line|
    if line =~ /^<h2>(.*)<\/h2>$/
      i += 1
      text = HTML.strip_tags($1)
      toc << "<li><a href='#!/guide/#{guide['name']}-section-#{i}'>#{text}</a></li>\n"
      new_html << "<h2 id='#{guide['name']}-section-#{i}'>#{text}</h2>\n"
    else
      new_html << line
    end
  end
  toc << "</ol>\n"
  toc << "</div>\n"
  # Inject TOC at below first heading if at least 2 items in TOC
  if i >= 2
    new_html.insert(1, toc)
    new_html.flatten.join
  else
    html
  end
end

#each_item(&block) ⇒ Object

Modified each_item that also loads HTML for each guide



39
40
41
42
43
44
45
46
47
48
# File 'lib/jsduck/guides.rb', line 39

def each_item(&block)
  unless @loaded
    super do |guide|
      guide[:html] = load_guide(guide)
    end
    @loaded = true
  end

  super(&block)
end

#fix_icon(dir) ⇒ Object

Ensures the guide dir contains icon.png. When there isn’t looks for icon-lg.png and renames it to icon.png. When neither exists, copies over default icon.



93
94
95
96
97
98
99
100
101
# File 'lib/jsduck/guides.rb', line 93

def fix_icon(dir)
  if File.exists?(dir+"/icon.png")
    # All ok
  elsif File.exists?(dir+"/icon-lg.png")
    FileUtils.mv(dir+"/icon-lg.png", dir+"/icon.png")
  else
    FileUtils.cp(@opts.template_dir+"/resources/images/default-guide.png", dir+"/icon.png")
  end
end

#icon_url(guide) ⇒ Object

Extracts guide icon URL from guide hash



152
153
154
# File 'lib/jsduck/guides.rb', line 152

def icon_url(guide)
  "guides/" + guide["name"] + "/icon.png"
end

#load_guide(guide) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/jsduck/guides.rb', line 60

def load_guide(guide)
  in_dir = @path + "/guides/" + guide["name"]
  return Logger.instance.warn(:guide, "Guide #{in_dir} not found") unless File.exists?(in_dir)

  guide_file = in_dir + "/README.md"
  return Logger.instance.warn(:guide, "README.md not found in #{in_dir}") unless File.exists?(guide_file)

  @formatter.doc_context = {:filename => guide_file, :linenr => 0}
  name = File.basename(in_dir)
  @formatter.img_path = "guides/#{name}"

  return add_toc(guide, @formatter.format(JsDuck::IO.read(guide_file)))
end

#to_arrayObject

Modified to_array that excludes the :html from guide nodes



51
52
53
54
55
56
57
58
# File 'lib/jsduck/guides.rb', line 51

def to_array
  @groups.map do |group|
    {
      "title" => group["title"],
      "items" => group["items"].map {|g| Hash[g.select {|k, v| k != :html }] }
    }
  end
end

#to_htmlObject

Returns HTML listing of guides



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/jsduck/guides.rb', line 134

def to_html
  html = @groups.map do |group|
    [
      "<h3>#{group['title']}</h3>",
      "<ul>",
      group["items"].map {|g| "<li><a href='#!/guide/#{g['name']}'>#{g['title']}</a></li>" },
      "</ul>",
    ]
  end.flatten.join("\n")

  return "    <div id='guides-content' style='display:none'>\n        \#{html}\n    </div>\n  EOHTML\nend\n"

#write(dir) ⇒ Object

Writes all guides to given dir in JsonP format



33
34
35
36
# File 'lib/jsduck/guides.rb', line 33

def write(dir)
  FileUtils.mkdir(dir) unless File.exists?(dir)
  each_item {|guide| write_guide(guide, dir) }
end

#write_guide(guide, dir) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/jsduck/guides.rb', line 74

def write_guide(guide, dir)
  return unless guide[:html]

  in_dir = @path + "/guides/" + guide["name"]
  out_dir = dir + "/" + guide["name"]

  Logger.instance.log("Writing guide", out_dir)
  # Copy the whole guide dir over
  FileUtils.cp_r(in_dir, out_dir)

  # Ensure the guide has an icon
  fix_icon(out_dir)

  JsonDuck.write_jsonp(out_dir+"/README.js", guide["name"], {:guide => guide[:html], :title => guide["title"]})
end