Class: WikiContent

Inherits:
String show all
Includes:
ChunkManager
Defined in:
app/models/wiki_content.rb

Constant Summary collapse

DEFAULT_OPTS =
{
  :active_chunks       => ACTIVE_CHUNKS,
  :engine              => Engines::Textile,
  :engine_opts         => [],
  :mode                => :show
}.freeze

Constants included from ChunkManager

ChunkManager::ACTIVE_CHUNKS, ChunkManager::HIDE_CHUNKS, ChunkManager::MASK_RE

Instance Attribute Summary collapse

Attributes included from ChunkManager

#chunk_id, #chunks, #chunks_by_id, #chunks_by_type

Instance Method Summary collapse

Methods included from ChunkManager

#add_chunk, #delete_chunk, #find_chunks, #init_chunk_manager, #merge_chunks, #scan_chunkid

Methods inherited from String

#texesc!

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.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/wiki_content.rb', line 132

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

  @options = DEFAULT_OPTS.dup.merge(options)
  @options[:engine] = Engines::MAP[@web.markup] 
  @options[:engine_opts] = [:filter_html, :filter_styles] if @web.safe_mode
  @options[:active_chunks] = (ACTIVE_CHUNKS - [WikiChunk::Word] ) if @web.brackets_only
  
  @not_rendered = @pre_rendered = nil
  
  super(@revision.content)
  init_chunk_manager
  build_chunks
  @not_rendered = String.new(self)
end

Instance Attribute Details

#not_renderedObject (readonly)

Returns the value of attribute not_rendered.



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

def not_rendered
  @not_rendered
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#pre_renderedObject (readonly)

Returns the value of attribute pre_rendered.



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

def pre_rendered
  @pre_rendered
end

#revisionObject (readonly)

Returns the value of attribute revision.



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

def revision
  @revision
end

#webObject (readonly)

Returns the value of attribute web.



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

def web
  @web
end

Instance Method Details

#build_chunksObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/models/wiki_content.rb', line 155

def build_chunks
  # create and mask Includes and "active_chunks" chunks
  Include.apply_to(self)
  @options[:active_chunks].each{|chunk_type| chunk_type.apply_to(self)}

  # Handle hiding contexts like "pre" and "code" etc..
  # The markup (textile, rdoc etc) can produce such contexts with its own syntax.
  # To reveal them, we work on a copy of the content.
  # The copy is rendered and used to detect the chunks that are inside protecting context  
  # These chunks are reverted on the original content string.

  copy = WikiContentStub.new(self, @options)
  @options[:engine].apply_to(copy)

  copy.inside_chunks(HIDE_CHUNKS) do |id|
    @chunks_by_id[id].revert
  end
end

#page_idObject



201
202
203
# File 'app/models/wiki_content.rb', line 201

def page_id
  @revision.page.id
end

Call @web.page_link using current options.



150
151
152
153
# File 'app/models/wiki_content.rb', line 150

def page_link(name, text, link_type)
  @options[:link_type] = (link_type || :show)
  @web.make_link(name, text, @options)
end

#page_nameObject



197
198
199
# File 'app/models/wiki_content.rb', line 197

def page_name
  @revision.page.name
end

#pre_render!Object



174
175
176
177
178
179
180
# File 'app/models/wiki_content.rb', line 174

def pre_render!
  unless @pre_rendered 
    @chunks_by_type[Include].each{|chunk| chunk.unmask }
    @pre_rendered = String.new(self)
  end
  @pre_rendered 
end

#render!Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/models/wiki_content.rb', line 182

def render!
  pre_render!
  @options[:engine].apply_to(self)
  # unmask in one go. $~[1] is the chunk id
  gsub!(MASK_RE[ACTIVE_CHUNKS]){ 
    if chunk = @chunks_by_id[$~[1]]
      chunk.unmask_text 
      # if we match a chunkmask that existed in the original content string
      # just keep it as it is
    else 
      $~[0]
    end}
  self
end