Class: WikiContent

Inherits:
String
  • Object
show all
Defined in:
app/models/wiki_content.rb

Overview

Wiki content is just a string that can process itself with a chain of actions. The actions can modify wiki content so that certain parts of it are protected from being rendered by later actions.

When wiki content is rendered, it can be interrogated to find out which chunks were rendered. This means things like categories, wiki links, can be determined.

Exactly how wiki content is rendered is determined by a number of settings that are optionally passed in to a constructor. The current options are:

* :engine 
  => The structural markup engine to use (Textile, Markdown, RDoc)
* :engine_opts
  => A list of options to pass to the markup engines (safe modes, etc)
* :pre_engine_actions
  => A list of render actions or chunks to be processed before the
     markup engine is applied. By default this is:    
     Category, Include, URIChunk, WikiChunk::Link
* :post_engine_actions
  => A list of render actions or chunks to apply after the markup 
     engine. By default these are:    
     Literal::Pre, Literal::Tags, WikiChunk::Word
     ToDo items
* :mode
  => How should the content be rendered? For normal display (:display), 
     publishing (:publish) or export (:export)?

AUTHOR: Mark Reid <mark @ threewordslong . com> CREATED: 15th May 2004 UPDATED: 22nd May 2004

Constant Summary collapse

PRE_ENGINE_ACTIONS =

Moved URIChunk from pre-engine to post-engine, as it clashed with the textile markup of “link”:URL.

[ NoWiki, Category, Include, WikiSymbol, 
WikiChunk::Link, WikiChunk::BlikiLink ]
POST_ENGINE_ACTIONS =
[ Literal::Tags, Literal::Pre, URIChunk, WikiChunk::Word, Todo ]
DEFAULT_OPTS =
{
  :pre_engine_actions  => PRE_ENGINE_ACTIONS,
  :post_engine_actions => POST_ENGINE_ACTIONS,
  :engine              => Engines::Textile,
  :engine_opts         => [],
  :mode                => [:display]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(revision, options = {}) ⇒ WikiContent

Create a new wiki content string from the given one. The options are explained at the top of this file.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/wiki_content.rb', line 64

def initialize(revision, options = {})
  @revision = revision
  @web = @revision.page.web

  # Deep copy of DEFAULT_OPTS to ensure that changes to PRE/POST_ENGINE_ACTIONS stay local
  @options = Marshal.load(Marshal.dump(DEFAULT_OPTS)).update(options)
  @options[:engine] = Engines::MAP[@web.markup] || Engines::Textile
  
  @options[:engine_opts] = DEFAULT_OPTS[:engine_opts]
  case @options[:engine].name
    when 'Engines::Textile'
      if %W{3.0.0 3.0.1 3.0.2 3.0.3}.include?(RedCloth::VERSION)
        # RedCloth v3 changes the default behaviour from not folding lines.
        @options[:engine_opts] += [:hard_breaks]
      end
      @options[:engine_opts] += (@web.safe_mode ? [:filter_html, :filter_styles] : [])
      
    when 'Engines::BlueMarkdown', 'Engines::RedMarkdown'
      @options[:engine_opts] += (@web.safe_mode ? [:filter_html, :filter_styles] : [])
      
    when 'Engines::RDoc'
      nil
      
  end

  @options[:post_engine_actions].delete(WikiChunk::Word) if @web.brackets_only

  super(@revision.content)

  begin
    render!(@options[:pre_engine_actions] + [@options[:engine]] + @options[:post_engine_actions])
  rescue => e
    puts "[#{DateTime.now.strftime '%F %T'}] DEBUG: #{e}"
    puts e.backtrace
    @rendered = "<strong>#{e.message}</strong><br/><br/>#{e.backtrace.join('<br/>')}"
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



60
61
62
# File 'app/models/wiki_content.rb', line 60

def options
  @options
end

#renderedObject (readonly)

Returns the value of attribute rendered.



60
61
62
# File 'app/models/wiki_content.rb', line 60

def rendered
  @rendered
end

#webObject (readonly)

Returns the value of attribute web.



60
61
62
# File 'app/models/wiki_content.rb', line 60

def web
  @web
end

Instance Method Details

#apply_type!(chunk_type) ⇒ Object

Find all the chunks of the given type in this content Each time the type’s pattern is matched, create a new chunk for it, and replace the occurance of the chunk in this content with its mask.



127
128
129
130
131
132
# File 'app/models/wiki_content.rb', line 127

def apply_type!(chunk_type)
  self.gsub!( chunk_type.pattern ) do |match|
    @chunks << chunk_type.new($~, @revision)
    @chunks.last.mask(self)
  end
end

#find_chunks(chunk_type) ⇒ Object

Find all the chunks of the given types



108
109
110
# File 'app/models/wiki_content.rb', line 108

def find_chunks(chunk_type)
  rendered.select { |chunk| chunk.kind_of?(chunk_type) }
end

Call @web.page_link using current options.



103
104
105
# File 'app/models/wiki_content.rb', line 103

def page_link(name, text) 
  @web.make_link(name, text, @options) 
end

#render!(chunk_types) ⇒ Object

Render this content using the specified actions.



113
114
115
116
117
118
119
120
121
# File 'app/models/wiki_content.rb', line 113

def render!(chunk_types)
  @chunks = []
  chunk_types.each { |chunk_type| self.apply_type!(chunk_type) }
  
  # unmasking order is reversed to allow chuncks that encapsulate other chunks,
  # e.g. a ToDo item with a WikiWork link.
  @rendered = @chunks.reverse.map { |chunk| chunk.unmask(self) }.compact
  (@chunks - @rendered).each { |chunk| chunk.revert(self) }
end