Class: Guider::Guide
- Inherits:
-
Object
- Object
- Guider::Guide
- Defined in:
- lib/guider/guide.rb
Instance Method Summary collapse
-
#guide_name ⇒ Object
Extracts the first line from markdown.
-
#initialize(filename, tpl, inline_tags, options) ⇒ Guide
constructor
A new instance of Guide.
- #relative_path(filename, base_dir) ⇒ Object
-
#toc ⇒ Object
Lists all h2 level headings within the guide.
- #write(filename) ⇒ Object
Constructor Details
#initialize(filename, tpl, inline_tags, options) ⇒ Guide
Returns a new instance of Guide.
7 8 9 10 11 12 13 14 15 |
# File 'lib/guider/guide.rb', line 7 def initialize(filename, tpl, , ) @template = tpl @inline_tags = @input_filename = filename @options = @markdown = IO.read(filename) @rel_path = relative_path([:input], filename) @html = Kramdown::Document.new(@markdown).to_html end |
Instance Method Details
#guide_name ⇒ Object
Extracts the first line from markdown
34 35 36 37 38 39 |
# File 'lib/guider/guide.rb', line 34 def guide_name @markdown =~ /\A(.*?)$/ result = $1.sub(/^#/, '').strip return (result == "") ? "Untitled" : result end |
#relative_path(filename, base_dir) ⇒ Object
29 30 31 |
# File 'lib/guider/guide.rb', line 29 def relative_path(filename, base_dir) Pathname.new(filename).relative_path_from(Pathname.new(base_dir)).dirname end |
#toc ⇒ Object
Lists all h2 level headings within the guide
XXX: Currently this is dead code.
44 45 46 47 48 |
# File 'lib/guider/guide.rb', line 44 def toc @html.scan(/<h2[^>]*id="(\S*)"[^>]*>([^\n]*)<\/h2>/).map do |m| {:href => "#" + m[0], :title => m[1]} end end |
#write(filename) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/guider/guide.rb', line 17 def write(filename) Logger.context = @input_filename @inline_tags.base_url = @rel_path html = @inline_tags.replace(@html) html = @template.apply({ :content => html, :guide_name => guide_name, :path => @rel_path, }) File.open(filename, 'w') {|f| f.write(html) } end |