Class: Rpub::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/rpub/document.rb

Defined Under Namespace

Classes: OutlineElement

Constant Summary

KRAMDOWN_OPTIONS =
{
  :coderay_line_numbers => nil
}

Instance Method Summary (collapse)

Constructor Details

- (Document) initialize(content, layout)

A new instance of Document



9
10
11
# File 'lib/rpub/document.rb', line 9

def initialize(content, layout)
  @document = Kramdown::Document.new(content, KRAMDOWN_OPTIONS.merge(:template => layout))
end

Instance Method Details

- (Array<String>) images

List of all image references

Returns:

  • (Array<String>)

    list of all image references



45
46
47
# File 'lib/rpub/document.rb', line 45

def images
  @images ||= elements(:img).map { |e| e.attr['src'] }
end

- (Array<#text,#level,#html_id>) outline

Ordered headers for this chapter, each header as an object responding to #level and #text.

Returns:

  • (Array<#text,#level,#html_id>)

    list of headers for this chapter



22
23
24
25
26
27
28
29
30
# File 'lib/rpub/document.rb', line 22

def outline
  @outline ||= elements(:header).map do |element|
    OutlineElement.new(
      element.options[:level],
      element_text(element),
      Kramdown::Converter::Html.send(:new, document, { :auto_id_prefix => '' }).generate_id(element.options[:raw_text])
    )
  end
end

- (String) title

Text of the first heading in this chapter

Returns:

  • (String)

    Text of the first heading in this chapter



38
39
40
41
42
# File 'lib/rpub/document.rb', line 38

def title
  @title ||= begin
    (heading = outline.first) ? heading.text : 'untitled'
  end
end

- (String) to_html

Content parsed to HTML by the markdown engine.

Returns:

  • (String)

    content parsed to HTML by the markdown engine.



33
34
35
# File 'lib/rpub/document.rb', line 33

def to_html
  @to_html ||= Typogruby.improve(@document.to_html)
end

- (Kramdown::Element) toc

Toc elements hierarchy

Returns:

  • (Kramdown::Element)

    Toc elements hierarchy



14
15
16
# File 'lib/rpub/document.rb', line 14

def toc
  Kramdown::Converter::Toc.convert(document.root).first
end