guider 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/bin/guider CHANGED
@@ -5,67 +5,44 @@ $:.unshift File.dirname(File.dirname(__FILE__)) + "/lib"
5
5
 
6
6
  require "rubygems"
7
7
  require "optparse"
8
- require "fileutils"
9
- require "guider/guide"
10
- require "guider/config"
11
- require "guider/template"
12
- require "guider/index"
13
- require "guider/inline_tags"
8
+ require "guider/app"
14
9
 
15
10
  options = {
16
11
  :output => Dir.pwd + "/out",
17
12
  :link_url => "http://localhost/extjs/",
13
+ :tpl_dir => File.dirname(File.dirname(__FILE__)) + "/template",
18
14
  }
19
15
 
20
16
  input_files = OptionParser.new do |opts|
21
17
  opts.banner = "Generates a guide.\n\n" +
22
- "Usage: guider <guides.json>\n\n"
18
+ "Usage: guider <guides/dir/>\n\n"
23
19
 
24
- opts.on("-o", "--output=DIR", "Where to output the guides.") do |dir|
25
- options[:output] = dir
20
+ opts.on("-o", "--output=DIR", "Where to output the guides.",
21
+ "Defaults to ./out") do |dir|
22
+ options[:output] = File.absolute_path(dir)
26
23
  end
27
24
 
28
- opts.on("--link-url=URL", "Base path for links created by {@link} tags.") do |url|
25
+ opts.on("--link-url=URL", "Base path for links created by {@link} tags.",
26
+ "Defaults to http://localhost/extjs/") do |url|
29
27
  options[:link_url] = url
30
28
  end
31
29
 
30
+ opts.on("--index=PATH", "The guides.json file to create index.html from.") do |path|
31
+ options[:index] = path
32
+ end
33
+
32
34
  opts.on("-h", "--help", "Show this help message") do
33
35
  puts opts
34
36
  exit
35
37
  end
36
38
  end.parse!
37
39
 
38
-
39
- # clean output dir
40
- FileUtils.rm_rf(options[:output])
41
- FileUtils.mkdir(options[:output])
42
-
43
- # copy over main template resources
44
- tpl_dir = File.dirname(File.dirname(__FILE__)) + "/template"
45
- Dir[tpl_dir+"/*.{js,css,ico}"].each do |fname|
46
- FileUtils.cp(fname, options[:output])
47
- end
48
-
49
- # Configure {@link} tags.
50
- inline_tags = Guider::InlineTags.new
51
- inline_tags.link_url = options[:link_url]
52
-
53
- # Read the template HTML file
54
- tpl = Guider::Template.new(tpl_dir + "/guide.html")
55
-
56
- # The guides.json file
57
- config_file = input_files[0]
58
-
59
- guides = Guider::Config.new(config_file, tpl, inline_tags).guides
60
-
61
- guides.each do |guide|
62
- guide.write(options[:output])
40
+ if input_files.length == 1
41
+ options[:input] = File.absolute_path(input_files[0])
42
+ else
43
+ $stderr.puts "ERROR: Exactly one input directory must be specified."
44
+ exit(1)
63
45
  end
64
46
 
65
- index_tpl = Guider::Template.new(tpl_dir + "/index.html")
66
- Guider::Index.new(guides, index_tpl).write(options[:output])
67
47
 
68
- html = index_tpl.apply({
69
- :title => "Guides",
70
- :content => "blah",
71
- })
48
+ Guider::App.new(options).run
data/guider.gemspec CHANGED
@@ -2,8 +2,8 @@ Gem::Specification.new do |s|
2
2
  s.required_rubygems_version = ">= 1.3.5"
3
3
 
4
4
  s.name = 'guider'
5
- s.version = '0.0.2'
6
- s.date = '2013-03-15'
5
+ s.version = '0.0.3'
6
+ s.date = '2013-03-18'
7
7
  s.summary = "Sencha guide generator"
8
8
  s.description = "JSDuck-compatible guides generator"
9
9
  s.homepage = "https://github.com/nene/guider"
data/lib/guider/app.rb ADDED
@@ -0,0 +1,52 @@
1
+ require "fileutils"
2
+ require "guider/guide_factory"
3
+ require "guider/index"
4
+
5
+ module Guider
6
+ class App
7
+ def initialize(options)
8
+ @options = options
9
+ @guide_factory = Guider::GuideFactory.new(@options)
10
+ end
11
+
12
+ def run
13
+ # clean output dir
14
+ FileUtils.rm_rf(@options[:output])
15
+
16
+ copy_dir(@options[:input], @options[:output])
17
+
18
+ copy_template_files
19
+
20
+ Index.new(@options).write if @options[:index]
21
+ end
22
+
23
+ private
24
+
25
+ # Copies all files and directories in source dir over to
26
+ # destination dir. Processes all .md files.
27
+ def copy_dir(src, dest)
28
+ FileUtils.mkdir(dest)
29
+
30
+ Dir[src+"/*"].each do |filename|
31
+ target = dest+"/"+File.basename(filename)
32
+
33
+ if filename =~ /\/README\.md$/
34
+ @guide_factory.create(filename).write(target.sub(/\/README\.md$/, "/index.html"))
35
+ elsif filename =~ /\.md$/
36
+ @guide_factory.create(filename).write(target.sub(/.md$/, ".html"))
37
+ elsif File.directory?(filename)
38
+ copy_dir(filename, target)
39
+ else
40
+ FileUtils.cp_r(filename, target)
41
+ end
42
+ end
43
+ end
44
+
45
+ # Copies over main template resources
46
+ def copy_template_files
47
+ Dir[@options[:tpl_dir]+"/*.{js,css,ico}"].each do |fname|
48
+ FileUtils.cp(fname, @options[:output])
49
+ end
50
+ end
51
+ end
52
+ end
data/lib/guider/config.rb CHANGED
@@ -1,48 +1,43 @@
1
1
  require "json"
2
- require "guider/guide"
3
2
 
4
3
  module Guider
5
4
  # Reads the guides config file.
6
- # Turns it into list Guide instances accessible through .guides attribute.
5
+ # Turns it into HTML table of contents.
7
6
  class Config
8
- def initialize(path, tpl, inline_tags)
9
- guide_cfgs = flatten(JSON.parse(IO.read(path)))
10
- guide_cfgs.each {|g| keys_to_symbols(g) }
11
- guide_cfgs.each {|g| add_path(g, path) }
12
- @guides = to_guides(guide_cfgs, tpl, inline_tags)
7
+ def initialize(path)
8
+ @guides = flatten(JSON.parse(IO.read(path)))
13
9
  end
14
10
 
15
- # List of Guide instances.
16
- attr_reader :guides
11
+ # Returns flat HTML list of guide titles.
12
+ def to_html
13
+ "<ul>" + @guides.map {|g| to_link(g) }.join("\n") + "</ul>"
14
+ end
17
15
 
18
16
  private
19
17
 
20
18
  # Turns grouped guides structure into flat list.
21
- def flatten(groups)
19
+ def flatten(group)
22
20
  arr = []
23
- groups.each do |group|
24
- group["items"].each {|guide| arr << guide }
21
+ group.each do |guide|
22
+ if guide["items"]
23
+ arr += flatten(guide["items"])
24
+ else
25
+ arr << guide
26
+ end
25
27
  end
26
28
  arr
27
29
  end
28
30
 
29
- # Turns all string keys in Hash into symbols
30
- def keys_to_symbols(hash)
31
- hash.keys.each do |k|
32
- hash[k.to_sym] = hash[k]
33
- hash.delete(k)
34
- end
35
- hash
31
+ def to_link(guide)
32
+ "<li><a href='#{to_href(guide)}'>#{guide['title']}</a></li>"
36
33
  end
37
34
 
38
- # adds :path fields to the configs of all the guides
39
- def add_path(guide, path)
40
- guide[:path] = File.dirname(path) + "/guides/" + guide[:name]
41
- end
42
-
43
- # Turns guide configs to actual Guide instances
44
- def to_guides(guide_cfgs, tpl, inline_tags)
45
- guide_cfgs.find_all {|g| File.exists?(g[:path]) }.map {|g| Guide.new(g, tpl, inline_tags) }
35
+ def to_href(guide)
36
+ if guide['url']
37
+ guide['url'].sub(/^guides\//)
38
+ else
39
+ guide['name']
40
+ end
46
41
  end
47
42
 
48
43
  end
data/lib/guider/guide.rb CHANGED
@@ -1,62 +1,46 @@
1
1
  require "kramdown"
2
- require "fileutils"
2
+ require "pathname"
3
3
 
4
4
  module Guider
5
5
  class Guide
6
- def initialize(cfg, tpl, inline_tags)
7
- @cfg = cfg
6
+ def initialize(filename, tpl, inline_tags, options)
8
7
  @template = tpl
9
8
  @inline_tags = inline_tags
10
- @markdown = IO.read(@cfg[:path]+"/README.md")
9
+ @markdown = IO.read(filename)
10
+ @rel_path = relative_path(options[:input], filename)
11
11
  @html = Kramdown::Document.new(@markdown).to_html
12
12
  end
13
13
 
14
- def write(path)
15
- guide_path = path + "/" + @cfg[:name]
16
- FileUtils.mkdir(guide_path)
17
- write_html(guide_path+"/index.html")
18
- copy_images(@cfg[:path], guide_path)
19
- end
20
-
21
- def write_html(filename)
14
+ def write(filename)
22
15
  html = @inline_tags.replace(@html)
23
- html = @template.apply(:content => html, :title => title)
16
+ html = @template.apply({
17
+ :content => html,
18
+ :title => title,
19
+ :path => @rel_path,
20
+ })
24
21
  File.open(filename, 'w') {|f| f.write(html) }
25
22
  end
26
23
 
24
+ def relative_path(filename, base_dir)
25
+ Pathname.new(filename).relative_path_from(Pathname.new(base_dir)).dirname
26
+ end
27
+
27
28
  # Extracts the first line from markdown
28
29
  def title
29
30
  @markdown =~ /\A(.*?)$/
30
31
  result = $1.sub(/^#/, '').strip
31
32
 
32
- # When guide name missing, take it from config file
33
- if result == ""
34
- @cfg[:title]
35
- else
36
- result
37
- end
38
- end
39
-
40
- # Returns the name of a guide, for use in links
41
- def name
42
- @cfg[:name]
33
+ return (result == "") ? "Untitled" : result
43
34
  end
44
35
 
45
36
  # Lists all h2 level headings within the guide
46
- def chapters
37
+ #
38
+ # XXX: Currently this is dead code.
39
+ def toc
47
40
  @html.scan(/<h2[^>]*id="(\S*)"[^>]*>([^\n]*)<\/h2>/).map do |m|
48
- {:href => @cfg[:name] + "#" + m[0], :title => m[1]}
41
+ {:href => "#" + m[0], :title => m[1]}
49
42
  end
50
43
  end
51
44
 
52
- # Copies all files and directories in source dir over to
53
- # destination dir. Skips README.md, which is treated separately.
54
- def copy_images(src, dest)
55
- Dir[src+"/*"].each do |img|
56
- if !["README.md"].include?(File.basename(img))
57
- FileUtils.cp_r(img, dest+"/"+File.basename(img))
58
- end
59
- end
60
- end
61
45
  end
62
46
  end
@@ -0,0 +1,25 @@
1
+ require "guider/template"
2
+ require "guider/inline_tags"
3
+ require "guider/guide"
4
+
5
+ module Guider
6
+ # Creates Guide instances
7
+ class GuideFactory
8
+ def initialize(options)
9
+ # Configure {@link} tags.
10
+ @inline_tags = InlineTags.new
11
+ @inline_tags.link_url = options[:link_url]
12
+
13
+ # Create guide rendering template
14
+ @tpl = Template.new(options[:tpl_dir] + "/guide.html")
15
+
16
+ @options = options
17
+ end
18
+
19
+ # Creates new Guide instance from filename
20
+ def create(filename)
21
+ Guide.new(filename, @tpl, @inline_tags, @options)
22
+ end
23
+
24
+ end
25
+ end
data/lib/guider/index.rb CHANGED
@@ -1,26 +1,17 @@
1
+ require 'guider/template'
2
+ require 'guider/config'
3
+
1
4
  module Guider
2
5
  class Index
3
- def initialize(guides, tpl)
4
- @guides = guides
5
- @template = tpl
6
- end
7
-
8
- def write(path)
9
- html = @template.apply(:title => "Guides", :content => guide_list)
10
- File.open(path + "/index.html", 'w') {|f| f.write(html) }
6
+ def initialize(options)
7
+ @options = options
8
+ @config = Config.new(@options[:index])
9
+ @tpl = Template.new(@options[:tpl_dir] + "/index.html")
11
10
  end
12
11
 
13
- private
14
-
15
- def guide_list
16
- list = @guides.map {|g| "<li><a href='#{g.name}'>#{g.title}</a>#{chapters_list(g.chapters)}</li>" }.join("\n")
17
- "<ul>#{list}</ul>"
18
- end
19
-
20
- def chapters_list(chapters)
21
- return "" if chapters.length == 0
22
-
23
- "\n<ul>" + chapters.map {|c| "<li><a href='#{c[:href]}'>#{c[:title]}</a></li>" }.join("\n") + "</ul>\n"
12
+ def write
13
+ html = @tpl.apply(:title => "Guides", :content => @config.to_html)
14
+ File.open(@options[:output] + "/index.html", 'w') {|f| f.write(html) }
24
15
  end
25
16
 
26
17
  end
@@ -1,5 +1,3 @@
1
- require "json"
2
-
3
1
  module Guider
4
2
  # Reads a template from file.
5
3
  # Allows replacing {placeholders} inside it.
data/template/guide.html CHANGED
@@ -3,10 +3,10 @@
3
3
  <head>
4
4
  <title>{title}</title>
5
5
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6
- <link rel="shortcut icon" type="image/ico" href="../favicon.ico" />
7
- <link rel="stylesheet" type="text/css" href="../styles.css" />
8
- <link rel="stylesheet" type="text/css" href="../prettify.css" />
9
- <script type="text/javascript" src="../prettify.js"></script>
6
+ <link rel="shortcut icon" type="image/ico" href="{path}/favicon.ico" />
7
+ <link rel="stylesheet" type="text/css" href="{path}/styles.css" />
8
+ <link rel="stylesheet" type="text/css" href="{path}/prettify.css" />
9
+ <script type="text/javascript" src="{path}/prettify.js"></script>
10
10
  </head>
11
11
  <body>
12
12
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guider
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-15 00:00:00.000000000 Z
12
+ date: 2013-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: kramdown
@@ -55,8 +55,10 @@ files:
55
55
  - README.md
56
56
  - bin/guider
57
57
  - guider.gemspec
58
+ - lib/guider/app.rb
58
59
  - lib/guider/config.rb
59
60
  - lib/guider/guide.rb
61
+ - lib/guider/guide_factory.rb
60
62
  - lib/guider/index.rb
61
63
  - lib/guider/inline_tags.rb
62
64
  - lib/guider/template.rb