Module: PageHub::Markdown

Defined in:
lib/pagehub-markdown/markdown.rb,
lib/pagehub-markdown/processors/embedder.rb,
lib/pagehub-markdown/mutators/date_injector.rb,
lib/pagehub-markdown/processors/toc_generator.rb,
lib/pagehub-markdown/processors/pagehub_options.rb

Defined Under Namespace

Modules: Embedder, ToC Classes: HTMLWithAlbino

Class Method Summary collapse

Class Method Details

.add_mutator(m) ⇒ Object

:nodoc:



24
25
26
27
28
29
30
# File 'lib/pagehub-markdown/markdown.rb', line 24

def add_mutator(m) # :nodoc:
  unless m.respond_to?(:call)
    raise "Mutator must be a callable object."
  end

  @@mutators << m
end

.add_processor(stage, p) ⇒ Object

:nodoc:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pagehub-markdown/markdown.rb', line 5

def add_processor(stage, p) # :nodoc:
  Stages.each { |s| @@hooks[s] ||= [] }

  unless Stages.include?(stage.to_sym)
    raise "Invalid stage #{stage}. Allowed stages are #{Stages.join(', ')}"
  end

  unless p.respond_to?(:call)
    raise "Processor must be a callable object."
  end

  if stage.is_a? Array
    stage.each { |s| @@hooks[s] << p }
  else
    @@hooks[stage.to_sym] << p
  end

end

.configure(ph_options = {}, options = {}, extensions = {}) ⇒ Object

(re)constructs the renderer with the given options, see PageHubOptions, RendererOptions, and RendererExtensions for accepted values



35
36
37
38
39
40
41
# File 'lib/pagehub-markdown/markdown.rb', line 35

def configure(ph_options = {}, options = {}, extensions = {})
  @@options  = PageHubOptions.merge(ph_options)

  @@renderer = Redcarpet::Markdown.new(
    HTMLWithAlbino.new(RendererOptions.merge(options)),
    RendererExtensions.merge(extensions))
end

.mutate!(str) ⇒ Object



67
68
69
70
71
# File 'lib/pagehub-markdown/markdown.rb', line 67

def mutate!(str)
  mutated = false
  @@mutators.each { |m| mutated ||= m.call(str) }
  mutated
end

.render(str) ⇒ Object



63
64
65
# File 'lib/pagehub-markdown/markdown.rb', line 63

def render(str)
  o = str.dup; render!(o); o
end

.render!(str) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pagehub-markdown/markdown.rb', line 43

def render!(str)
  configure unless @@renderer

  @@hooks[:pre_render].each { |processor| processor.call(str) }

  # escape any JavaScript snippets
  if @@options[:escape_scripts]
    str.gsub!(/\<script(.*)\>/i) {
      mutated = true
      "&lt;script#{$1}&gt;"
    }
  end

  str = @@renderer.render(str)

  @@hooks[:post_render].each { |processor| processor.call(str) }

  str
end