Class: Rwiki::Utils::TextileHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/rwiki/utils/textile_helper.rb

Constant Summary collapse

HEADER_REGEXP =

Regexp for extracting an article headers

/^\s*h(?<number>[1-6]?)\.\s+(?<name>.*)/.freeze
CODE_REGEXP =

RedCloth for extracting code blocks

/\<code( lang="(?<lang>.+?)")?\>(?<code>.+?)\<\/code\>/m.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ TextileHelper

Returns a new instance of TextileHelper.



14
15
16
17
# File 'lib/rwiki/utils/textile_helper.rb', line 14

def initialize(content)
  @content = content.clone
  @processed_content = content.clone
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



11
12
13
# File 'lib/rwiki/utils/textile_helper.rb', line 11

def content
  @content
end

#processed_contentObject (readonly)

Returns the value of attribute processed_content.



12
13
14
# File 'lib/rwiki/utils/textile_helper.rb', line 12

def processed_content
  @processed_content
end

Instance Method Details

#parsed_contentObject



19
20
21
22
23
24
# File 'lib/rwiki/utils/textile_helper.rb', line 19

def parsed_content
  process_coderay!
  process_toc!

  RedCloth.new(processed_content).to_html
end

#parsed_tocObject



26
27
28
# File 'lib/rwiki/utils/textile_helper.rb', line 26

def parsed_toc
  RedCloth.new(textile_toc).to_html
end

#textile_tocObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rwiki/utils/textile_helper.rb', line 30

def textile_toc
  toc_items = []
  content.gsub(HEADER_REGEXP) do
    number = $~[:number].to_i
    name = $~[:name].strip
    anchor = sanitize_anchor_name(name)

    toc_items << ('#' * number) + %Q{ "#{name}":##{anchor}}
  end

  toc_items.join("\n")
end