class AsciiPress::Renderer

Public Class Methods

new(options = {asciidoc_options: {}}) click to toggle source

@param options [Hash] @option options [Hash] :asciidoc_options Passed directly to the Asciidoctor.load method. See the {asciidoctor.org/rdoc/Asciidoctor.html AsciiDoctor documentation} @option options [Proc] :before_convertion Proc which is given the asciidoctor text. Whatever is returned is passed to Asciidoctor.load. See the {asciidoctor.org/rdoc/Asciidoctor.html AsciiDoctor documentation} @option options [Proc] :after_conversion Proc which is given the html text after the Asciidoctor conversion. Whatever is returned will be uploaded to WordPress @option options [Proc] :rendering_proc Proc which is given the {Rendering} object (see below). Changes made be made to the rendering in-place

# File lib/ascii_press.rb, line 74
def initialize(options = {asciidoc_options: {}})
  @options = options
end

Public Instance Methods

render(adoc_file_path) click to toggle source

@!visibility private

# File lib/ascii_press.rb, line 79
def render(adoc_file_path)
  doc = nil
  errors = capture_stderr do
    document_text = File.read(adoc_file_path)
    base_dir = ::File.expand_path(File.dirname(adoc_file_path))
    if before_convertion = @options[:before_convertion]
      document_text = before_convertion.call(document_text)
    end
    doc = Asciidoctor.load(document_text, @options[:asciidoc_options].merge(base_dir: base_dir))
  end
  puts errors.split(/[\n\r]+/).reject {|line| line.match(/out of sequence/) }.join("\n")

  html = doc.convert

  if after_conversion = @options[:after_conversion]
    html = after_conversion.call(html)
  end

  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[:rendering_proc]
      rendering.tags = @options[:rendering_proc].call(rendering)
    end
  end
end

Private Instance Methods

capture_stderr() { || ... } click to toggle source
# File lib/ascii_press.rb, line 115
def capture_stderr
  real_stderr, $stderr = $stderr, StringIO.new
  yield
  $stderr.string
ensure
  $stderr = real_stderr
end