Class: AsciiPress::Renderer

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

Defined Under Namespace

Classes: Rendering

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Renderer

Returns a new instance of Renderer.



48
49
50
# File 'lib/ascii_press.rb', line 48

def initialize(options = {})
  @options = options
end

Instance Method Details

#capture_stderrObject



76
77
78
79
80
81
82
# File 'lib/ascii_press.rb', line 76

def capture_stderr
  real_stderr, $stderr = $stderr, StringIO.new
  yield
  $stderr.string
ensure
  $stderr = real_stderr
end

#render(adoc_file_path) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ascii_press.rb', line 52

def render(adoc_file_path)
  doc = nil
  errors = capture_stderr { doc = Asciidoctor.load_file(adoc_file_path, @options) }
  puts errors.split(/[\n\r]+/).reject {|line| line.match(/out of sequence/) }.join("\n")

  html = doc.convert

  data = doc.attributes.each_with_object({}) do |(key, value), result|
    result[key.to_sym] = value.to_s
  end

  data.reject! {|_, value| value.nil? }

  Rendering.new(html, doc, data).tap do |rendering|
    rendering.tags = rendering.list_attribute_value('tags')
    rendering.tags << 'public' if rendering.attribute_exists?(:public)
    rendering.tags << 'private' if rendering.attribute_exists?(:private)

    if @options[:extra_tags_proc]
      rendering.tags.concat @options[:extra_tags_proc].call(rendering)
    end
  end
end