guider 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/bin/guider CHANGED
@@ -11,6 +11,7 @@ options = {
11
11
  :output => Dir.pwd + "/out",
12
12
  :link_url => "http://localhost/extjs/",
13
13
  :tpl_dir => File.dirname(File.dirname(__FILE__)) + "/template",
14
+ :warnings => false,
14
15
  }
15
16
 
16
17
  input_files = OptionParser.new do |opts|
@@ -31,13 +32,17 @@ input_files = OptionParser.new do |opts|
31
32
  options[:index] = path
32
33
  end
33
34
 
35
+ opts.on("--warnings", "Enables warnings.") do
36
+ options[:warnings] = true
37
+ end
38
+
34
39
  opts.on("-h", "--help", "Show this help message") do
35
40
  puts opts
36
41
  exit
37
42
  end
38
43
 
39
44
  opts.on("--version", "Prints guider version number.") do
40
- puts "Guider 0.0.4"
45
+ puts "Guider 0.0.5"
41
46
  exit
42
47
  end
43
48
  end.parse!
@@ -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.4'
6
- s.date = '2013-03-18'
5
+ s.version = '0.0.5'
6
+ s.date = Time.new.strftime('%Y-%m-%d')
7
7
  s.summary = "Sencha guide generator"
8
8
  s.description = "JSDuck-compatible guides generator"
9
9
  s.homepage = "https://github.com/nene/guider"
@@ -1,12 +1,14 @@
1
1
  require "fileutils"
2
2
  require "guider/guide_factory"
3
3
  require "guider/index"
4
+ require "guider/logger"
4
5
 
5
6
  module Guider
6
7
  class App
7
8
  def initialize(options)
8
9
  @options = options
9
10
  @guide_factory = Guider::GuideFactory.new(@options)
11
+ Logger.warnings = options[:warnings]
10
12
  end
11
13
 
12
14
  def run
@@ -5,31 +5,32 @@ module Guider
5
5
  # Turns it into HTML table of contents.
6
6
  class Config
7
7
  def initialize(path)
8
- @guides = flatten(JSON.parse(IO.read(path)))
8
+ @guides = JSON.parse(IO.read(path))
9
9
  end
10
10
 
11
- # Returns flat HTML list of guide titles.
11
+ # Returns HTML list of guide titles.
12
12
  def to_html
13
- "<ul>" + @guides.map {|g| to_link(g) }.join("\n") + "</ul>"
13
+ to_list(@guides)
14
14
  end
15
15
 
16
16
  private
17
17
 
18
- # Turns grouped guides structure into flat list.
19
- def flatten(group)
20
- arr = []
21
- group.each do |guide|
18
+ def to_list(items)
19
+ list = []
20
+
21
+ items.each do |guide|
22
22
  if guide["items"]
23
- arr += flatten(guide["items"])
23
+ list << "<li>" + guide["title"] + "\n" + to_list(guide["items"]) + "</li>"
24
24
  else
25
- arr << guide
25
+ list << "<li>#{to_link(guide)}</li>"
26
26
  end
27
27
  end
28
- arr
28
+
29
+ "<ul>" + list.join("\n") + "</ul>"
29
30
  end
30
31
 
31
32
  def to_link(guide)
32
- "<li><a href='#{to_href(guide)}'>#{guide['title']}</a></li>"
33
+ "<a href='#{to_href(guide)}'>#{guide['title']}</a>"
33
34
  end
34
35
 
35
36
  def to_href(guide)
@@ -1,17 +1,20 @@
1
1
  require "kramdown"
2
2
  require "pathname"
3
+ require "guider/logger"
3
4
 
4
5
  module Guider
5
6
  class Guide
6
7
  def initialize(filename, tpl, inline_tags, options)
7
8
  @template = tpl
8
9
  @inline_tags = inline_tags
10
+ @input_filename = filename
9
11
  @markdown = IO.read(filename)
10
12
  @rel_path = relative_path(options[:input], filename)
11
13
  @html = Kramdown::Document.new(@markdown).to_html
12
14
  end
13
15
 
14
16
  def write(filename)
17
+ Logger.context = @input_filename
15
18
  html = @inline_tags.replace(@html)
16
19
  html = @template.apply({
17
20
  :content => html,
@@ -1,3 +1,5 @@
1
+ require 'guider/logger'
2
+
1
3
  module Guider
2
4
  class InlineTags
3
5
  # The base URL for links created by {@link} tags.
@@ -10,39 +12,59 @@ module Guider
10
12
  def replace(html)
11
13
  replace_link!(html)
12
14
  replace_img!(html)
15
+ replace_old_guide_links!(html)
13
16
  html
14
17
  end
15
18
 
16
19
  private
17
20
 
18
21
  def replace_link!(html)
19
- re = /\{@link\s+([^\s\}]*)(?:\s+([^\}]*))?}/
20
- html.gsub!(re) do |m|
21
- m =~ re # re-run regex to extract $1 $2 fields
22
- ref = $1
23
- alt = $2
24
-
25
- cls, mref = ref.split('#')
22
+ replace!(html, /\{@link\s+([^\s\}]*)(?:\s+([^\}]*))?}/) do |ref, alt|
23
+ cls, mref = ref.split(/#/)
26
24
  if mref
27
- apiref = [cls, "method", mref].join("-")
25
+ parts = mref.split(/-/)
26
+
27
+ if parts.length < 2
28
+ Logger.warn("Ambiguous member reference: #{cls}##{mref}")
29
+ # Default unqualified member references to method
30
+ mref = "method-" + mref
31
+ end
32
+
33
+ api_ref = [cls, mref].join("-")
34
+ api_alt = [cls, parts.last].join(".")
28
35
  else
29
- apiref = cls
36
+ api_ref = cls
37
+ api_alt = cls
30
38
  end
31
39
 
32
- url = @link_url + "#!/api/" + apiref
33
- "<a href='#{url}'>#{alt || ref}</a>"
40
+ url = @link_url + "#!/api/" + api_ref
41
+ "<a href='#{url}'>#{alt || api_alt}</a>"
34
42
  end
35
43
  end
36
44
 
37
45
  def replace_img!(html)
38
- re = /\{@img\s+([^\s\}]*)(?:\s+([^\}]*))?}/
39
- html.gsub!(re) do |m|
40
- m =~ re # re-run regex to extract $1 $2 fields
41
- ref = $1
42
- alt = $2
46
+ replace!(html, /\{@img\s+([^\s\}]*)(?:\s+([^\}]*))?}/) do |ref, alt|
43
47
  "<img src='#{ref}' alt='#{alt}'>"
44
48
  end
45
49
  end
46
50
 
51
+ def replace_old_guide_links!(html)
52
+ replace!(html, /<a href="#!?\/guide\/(\w+)">/) do |name|
53
+ "<a href='../#{name}'>"
54
+ end
55
+ end
56
+
57
+ # Helper method to easily do a regex-replace with a block where
58
+ # the block gets called with a list of all the captured strings.
59
+ # The normal String#gsub! method only passes the full match to the
60
+ # block, so we have to do some tedious extra work to extract the
61
+ # actual captures.
62
+ def replace!(string, re, &block)
63
+ string.gsub!(re) do |m|
64
+ captures = re.match(m)[1..-1]
65
+ block.call(*captures)
66
+ end
67
+ end
68
+
47
69
  end
48
70
  end
@@ -0,0 +1,20 @@
1
+ module Guider
2
+ class Logger
3
+ # Turns printing of warnings on or off.
4
+ def self.warnings=(enabled)
5
+ @warnings = enabled
6
+ end
7
+
8
+ # Sets file context for warnings
9
+ def self.context=(filename)
10
+ @context = filename
11
+ end
12
+
13
+ # Prints out a warning when warnings enabled.
14
+ def self.warn(msg)
15
+ file = @context ? @context + ": " : ""
16
+ $stderr.puts(file + msg) if @warnings
17
+ end
18
+
19
+ end
20
+ end
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.4
4
+ version: 0.0.5
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-18 00:00:00.000000000 Z
12
+ date: 2013-03-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: kramdown
@@ -61,6 +61,7 @@ files:
61
61
  - lib/guider/guide_factory.rb
62
62
  - lib/guider/index.rb
63
63
  - lib/guider/inline_tags.rb
64
+ - lib/guider/logger.rb
64
65
  - lib/guider/template.rb
65
66
  - template/favicon.ico
66
67
  - template/guide.html