Class: Carta::CLI::HTMLRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/carta/cli/html_renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ HTMLRenderer

Returns a new instance of HTMLRenderer.



11
12
13
14
15
16
17
18
# File 'lib/carta/cli/html_renderer.rb', line 11

def initialize(path)
  @markdown = ''
  @epub_toc_html = ''
  @html_toc_html = ''
  @manuscript_html = ''
  @outline = nil
  load_markdown(path)
end

Instance Attribute Details

#epub_toc_htmlObject

Returns the value of attribute epub_toc_html.



5
6
7
# File 'lib/carta/cli/html_renderer.rb', line 5

def epub_toc_html
  @epub_toc_html
end

#html_toc_htmlObject

Returns the value of attribute html_toc_html.



5
6
7
# File 'lib/carta/cli/html_renderer.rb', line 5

def html_toc_html
  @html_toc_html
end

#manuscript_htmlObject

Returns the value of attribute manuscript_html.



5
6
7
# File 'lib/carta/cli/html_renderer.rb', line 5

def manuscript_html
  @manuscript_html
end

#markdownObject

Returns the value of attribute markdown.



5
6
7
# File 'lib/carta/cli/html_renderer.rb', line 5

def markdown
  @markdown
end

#outlineObject

Returns the value of attribute outline.



5
6
7
# File 'lib/carta/cli/html_renderer.rb', line 5

def outline
  @outline
end

Instance Method Details

#load_markdown(path) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/carta/cli/html_renderer.rb', line 20

def load_markdown(path)
  FileList.new("#{path}/manuscript/**/*.md").sort.each do |md_file|
    IO.readlines(md_file).each { |line| markdown << line }
    markdown << "\n\n"
  end
  render_manuscript
end

#render_manuscriptObject



28
29
30
31
32
33
34
35
# File 'lib/carta/cli/html_renderer.rb', line 28

def render_manuscript
  renderer = OutlineRenderer.new
  r = Redcarpet::Markdown.new(renderer)
  manuscript_html << r.render(markdown)
  @outline = renderer.outline
  @epub_toc_html = render_outline
  @html_toc_html = render_outline('toc',true)
end

#render_outline(html_class = 'toc', for_html = false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/carta/cli/html_renderer.rb', line 37

def render_outline(html_class = 'toc', for_html = false)
  final_class = "class='#{html_class}'"
  html = ''
  outline.each_with_index do |data, i|
    level, text, link, *children = data
    html << "<ol #{final_class}>" if i == 0
    if for_html
      html << "\n  <li><a href='##{link}'>#{text}</a>"
    else
      html << "\n  <li><a href='content.xhtml##{link}'>#{text}</a>"
    end

    if children.empty?
      html << '</li>'
    else
      children.each_with_index do |child, j|
        level, text, link = child
        html << "\n    <ol>" if j == 0
        if for_html
          html << "\n      <li><a href='##{link}'>#{text}</a></li>"
        else
          html << "\n      <li><a href='content.xhtml##{link}'>#{text}</a></li>"
        end
        html << "\n    </ol>\n  </li>" if j == children.length - 1
      end
    end
    html << "\n</ol>" if i == outline.length - 1
  end
  return html
end