Class: Gollum::Markup

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/gollum-lib/markup.rb,
lib/gollum-lib/markups.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#trim_leading_slash

Constructor Details

#initialize(page) ⇒ Markup

Initialize a new Markup object.

page - The Gollum::Page.

Returns a new Gollum::Markup object, ready for rendering.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gollum-lib/markup.rb', line 70

def initialize(page)
  if page
    @wiki        = page.wiki
    @name        = page.filename
    @data        = page.text_data
    @version     = page.version.id if page.version
    @format      = page.format
    @sub_page    = page.sub_page
    @parent_page = page.parent_page
    @page        = page
    @dir         = ::File.dirname(page.path)
  end
  @metadata    = nil
  @to_xml_opts = { :save_with => Nokogiri::XML::Node::SaveOptions::DEFAULT_XHTML ^ 1, :indent => 0, :encoding => 'UTF-8' }
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



63
64
65
# File 'lib/gollum-lib/markup.rb', line 63

def dir
  @dir
end

#encodingObject (readonly)

Returns the value of attribute encoding.



53
54
55
# File 'lib/gollum-lib/markup.rb', line 53

def encoding
  @encoding
end

#formatObject (readonly)

Returns the value of attribute format.



55
56
57
# File 'lib/gollum-lib/markup.rb', line 55

def format
  @format
end

#include_levelsObject (readonly)

Returns the value of attribute include_levels.



61
62
63
# File 'lib/gollum-lib/markup.rb', line 61

def include_levels
  @include_levels
end

#metadataObject

Returns the value of attribute metadata.



52
53
54
# File 'lib/gollum-lib/markup.rb', line 52

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



60
61
62
# File 'lib/gollum-lib/markup.rb', line 60

def name
  @name
end

#pageObject (readonly)

Returns the value of attribute page.



57
58
59
# File 'lib/gollum-lib/markup.rb', line 57

def page
  @page
end

#parent_pageObject (readonly)

Returns the value of attribute parent_page.



58
59
60
# File 'lib/gollum-lib/markup.rb', line 58

def parent_page
  @parent_page
end

#sanitizeObject (readonly)

Returns the value of attribute sanitize.



54
55
56
# File 'lib/gollum-lib/markup.rb', line 54

def sanitize
  @sanitize
end

#sub_pageObject (readonly)

Returns the value of attribute sub_page.



59
60
61
# File 'lib/gollum-lib/markup.rb', line 59

def sub_page
  @sub_page
end

#to_xml_optsObject (readonly)

Returns the value of attribute to_xml_opts.



62
63
64
# File 'lib/gollum-lib/markup.rb', line 62

def to_xml_opts
  @to_xml_opts
end

#tocObject

Returns the value of attribute toc.



51
52
53
# File 'lib/gollum-lib/markup.rb', line 51

def toc
  @toc
end

#wikiObject (readonly)

Returns the value of attribute wiki.



56
57
58
# File 'lib/gollum-lib/markup.rb', line 56

def wiki
  @wiki
end

Class Method Details

.formatsObject

Only use the formats that are specified in config.rb



26
27
28
29
30
31
32
# File 'lib/gollum-lib/markup.rb', line 26

def formats
  if defined? Gollum::Page::FORMAT_NAMES
    @formats.select { |_, value| Gollum::Page::FORMAT_NAMES.values.include? value[:name] }
  else
    @formats
  end
end

.register(ext, name, options = {}, &block) ⇒ Object

Register a file extension and associated markup type

ext - The file extension name - The name of the markup type options - Hash of options:

regexp - Regexp to match against.
         Defaults to exact match of ext.

If given a block, that block will be registered with GitHub::Markup to render any matching pages



44
45
46
47
48
# File 'lib/gollum-lib/markup.rb', line 44

def register(ext, name, options = {}, &block)
  @formats[ext] = { :name => name,
    :regexp => options.fetch(:regexp, Regexp.new(ext.to_s)),
    :reverse_links => options.fetch(:reverse_links, false) }
end

Instance Method Details

#check_cache(type, id) ⇒ Object

Hook for getting the formatted value of extracted tag data.

type - Symbol value identifying what type of data is being extracted. id - String SHA1 hash of original extracted tag data.

Returns the String cached formatted data, or nil.



187
188
# File 'lib/gollum-lib/markup.rb', line 187

def check_cache(type, id)
end

#find_file(name, version = @version) ⇒ Object

Find the given file in the repo.

name - The String absolute or relative path of the file.

Returns the Gollum::File or nil if none was found.



172
173
174
175
176
177
178
179
# File 'lib/gollum-lib/markup.rb', line 172

def find_file(name, version=@version)
  if name =~ /^\//
    @wiki.file(name[1..-1], version)
  else
    path = @dir == '.' ? name : ::File.join(@dir, name)
    @wiki.file(path, version)
  end
end

#process_chain(data, filter_chain) ⇒ Object

Process the filter chain

data - the data to send through the chain filter_chain - the chain to process

Returns the formatted data



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/gollum-lib/markup.rb', line 117

def process_chain(data, filter_chain)
  # First we extract the data through the chain...
  filter_chain.each do |filter|
    data = filter.extract(data)
  end

  # Then we process the data through the chain *backwards*
  filter_chain.reverse.each do |filter|
    data = filter.process(data)
  end

  # Finally, a little bit of cleanup, just because
  data.gsub!(/<p><\/p>/) do
    ''
  end

  data
end

#render(no_follow = false, encoding = nil, include_levels = 10) ⇒ Object

Render the content with Gollum wiki syntax on top of the file’s own markup language.

no_follow - Boolean that determines if rel=“nofollow” is added to all

<a> tags.

encoding - Encoding Constant or String.

Returns the formatted String content.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/gollum-lib/markup.rb', line 144

def render(no_follow = false, encoding = nil, include_levels = 10)
  @sanitize = no_follow ?
      @wiki.history_sanitizer :
      @wiki.sanitizer

  @encoding       = encoding
  @include_levels = include_levels

  data         = @data.dup
  filter_chain = @wiki.filter_chain.map do |r|
    Gollum::Filter.const_get(r).new(self)
  end

  # Since the last 'extract' action in our chain *should* be the markup
  # to HTML converter, we now have HTML which we can parse and yield, for
  # anyone who wants it
  if block_given?
    yield Nokogiri::HTML::DocumentFragment.parse(data)
  end

  process_chain data, filter_chain
end

#render_default(data, format = :markdown, name = 'render_default.md') ⇒ Object

Render data using default chain in the target format.

data - the data to render format - format to use as a symbol name - name using the extension of the format

Returns the processed data



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gollum-lib/markup.rb', line 97

def render_default(data, format=:markdown, name='render_default.md')
  # set instance vars so we're able to render data without a wiki or page.
  @format = format
  @name   = name

  chain = [:Metadata, :PlainText, :Emoji, :TOC, :RemoteCode, :Code, :Sanitize, :WSD, :Tags, :Render]

  filter_chain = chain.map do |r|
    Gollum::Filter.const_get(r).new(self)
  end

  process_chain data, filter_chain
end

#reverse_links?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/gollum-lib/markup.rb', line 86

def reverse_links?
  self.class.formats[@format][:reverse_links]
end

#update_cache(type, id, data) ⇒ Object

Hook for caching the formatted value of extracted tag data.

type - Symbol value identifying what type of data is being extracted. id - String SHA1 hash of original extracted tag data. data - The String formatted value to be cached.

Returns nothing.



197
198
# File 'lib/gollum-lib/markup.rb', line 197

def update_cache(type, id, data)
end