Class: MarkdownHandler

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

Instance Method Summary collapse

Instance Method Details

#call(template, source) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hotdocs/markdown.rb', line 6

def call(template, source)
  # If the template contains a `fetcher`, do not allow Rails to cache the page.
  if source.match?(%r{<%= fetcher.* do %>})
    ActionView::PathRegistry.all_resolvers.each do |resolver|
      resolver.instance_eval do
        @unbound_templates.delete(template.virtual_path)
      end
    end
  end

  compiled_template = ::HotdocsController.render(inline: source, handler: :erb)
  out = Kramdown::Document.new(
    compiled_template,
    input: "GFM",
    auto_ids: false,
    syntax_highlighter_opts: { css_class: "highlight" }
  ).to_html

  content_fors = ActionView::Template::Handlers::ERB
    .new
    .call(template, source)
    .split(";")
    .grep(/@output_buffer.append=\(\scontent_for.*\(.*:/)

  "    \#{content_fors.join(\";\")}\n    @output_buffer.safe_append='\#{out.gsub(\"'\", \"\\\\\\\\'\")}'.freeze;\n    @output_buffer\n  STRING\nend\n"