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

#path_to_link_text, #trim_leading_slashes

Constructor Details

#initialize(page) ⇒ Markup

Initialize a new Markup object.

page - The Gollum::Page.

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



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gollum-lib/markup.rb', line 89

def initialize(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.url_path}")
  @metadata    = nil
end

Instance Attribute Details

#dirObject (readonly)

Returns the value of attribute dir.



81
82
83
# File 'lib/gollum-lib/markup.rb', line 81

def dir
  @dir
end

#encodingObject (readonly)

Returns the value of attribute encoding.



73
74
75
# File 'lib/gollum-lib/markup.rb', line 73

def encoding
  @encoding
end

#formatObject (readonly)

Returns the value of attribute format.



74
75
76
# File 'lib/gollum-lib/markup.rb', line 74

def format
  @format
end

#historicalObject (readonly)

Returns the value of attribute historical.



82
83
84
# File 'lib/gollum-lib/markup.rb', line 82

def historical
  @historical
end

#include_levelsObject (readonly)

Returns the value of attribute include_levels.



80
81
82
# File 'lib/gollum-lib/markup.rb', line 80

def include_levels
  @include_levels
end

#metadataObject

Returns the value of attribute metadata.



72
73
74
# File 'lib/gollum-lib/markup.rb', line 72

def 
  @metadata
end

#nameObject (readonly)

Returns the value of attribute name.



79
80
81
# File 'lib/gollum-lib/markup.rb', line 79

def name
  @name
end

#pageObject (readonly)

Returns the value of attribute page.



76
77
78
# File 'lib/gollum-lib/markup.rb', line 76

def page
  @page
end

#parent_pageObject (readonly)

Returns the value of attribute parent_page.



77
78
79
# File 'lib/gollum-lib/markup.rb', line 77

def parent_page
  @parent_page
end

#sub_pageObject (readonly)

Returns the value of attribute sub_page.



78
79
80
# File 'lib/gollum-lib/markup.rb', line 78

def sub_page
  @sub_page
end

#tocObject

Returns the value of attribute toc.



71
72
73
# File 'lib/gollum-lib/markup.rb', line 71

def toc
  @toc
end

#wikiObject (readonly)

Returns the value of attribute wiki.



75
76
77
# File 'lib/gollum-lib/markup.rb', line 75

def wiki
  @wiki
end

Class Method Details

.extensionsObject



38
39
40
# File 'lib/gollum-lib/markup.rb', line 38

def extensions
  @extensions
end

.formatsObject

Only use the formats that are specified in config.rb



30
31
32
33
34
35
36
# File 'lib/gollum-lib/markup.rb', line 30

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 format

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

extensions - Array of valid file extensions, for instance ['md']
enabled - Whether the markup is enabled

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gollum-lib/markup.rb', line 52

def register(ext, name, options = {}, &block)
  if options[:regexp] then
    STDERR.puts <<-EOS
    Warning: attempted to register a markup (name: #{name.to_s}) by passing the deprecated :regexp option.
    Please pass an Array of valid file extensions (:extensions => ['ext1', 'ext2']) instead.
    EOS
  end
  new_extension = options.fetch(:extensions, [ext.to_s])
  @formats[ext] = { :name => name,
    :extensions => new_extension,
    :reverse_links => options.fetch(:reverse_links, false),
    :skip_filters => options.fetch(:skip_filters, nil),
    :enabled => options.fetch(:enabled, true),
    :render => options.fetch(:render, nil)
  }
  @extensions.concat(new_extension)
end

.to_xml_optsObject



25
26
27
# File 'lib/gollum-lib/markup.rb', line 25

def to_xml_opts
  { :save_with => Nokogiri::XML::Node::SaveOptions::DEFAULT_XHTML & (~Nokogiri::XML::Node::SaveOptions::FORMAT), :indent => 0, :encoding => 'UTF-8' }
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.



177
178
# File 'lib/gollum-lib/markup.rb', line 177

def check_cache(type, id)
end

#custom_rendererObject



107
108
109
# File 'lib/gollum-lib/markup.rb', line 107

def custom_renderer
  self.class.formats[@format].fetch(:render, nil)
end

#process_chain(data, filter_chain) {|Nokogiri::HTML::DocumentFragment.parse(data)| ... } ⇒ Object

Process the filter chain

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

Returns the formatted data

Yields:

  • (Nokogiri::HTML::DocumentFragment.parse(data))


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/gollum-lib/markup.rb', line 128

def process_chain(data, filter_chain, &block)
  # First we extract the data through the chain...
  filter_chain.each do |filter|
    data = filter.extract(data)
  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
  yield Nokogiri::HTML::DocumentFragment.parse(data) if block_given?

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

  data
end

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

Render the content with Gollum wiki syntax on top of the file’s own markup language. Takes an optional block that will be executed after the markup rendering step in the filter chain.

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

<a> tags.

encoding - Encoding Constant or String.

Returns the formatted String content.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/gollum-lib/markup.rb', line 156

def render(no_follow = false, encoding = nil, include_levels = 10, &block)
  @historical     = no_follow
  @encoding       = encoding
  @include_levels = include_levels

  data = @data.dup

  filter_chain = @wiki.filter_chain.reject {|filter| skip_filter?(filter)}
  filter_chain.map! do |filter_sym|
    Gollum::Filter.const_get(filter_sym).new(self)
  end

  process_chain(data, filter_chain, &block)
end

#reverse_links?Boolean

Whether or not this markup’s format uses reversed-order links ([description | url] rather than [url | description]). Defaults to false.

Returns:

  • (Boolean)


103
104
105
# File 'lib/gollum-lib/markup.rb', line 103

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

#skip_filter?(filter) ⇒ Boolean

Whether or not a particular filter should be skipped for this format.

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
# File 'lib/gollum-lib/markup.rb', line 112

def skip_filter?(filter)
  if self.class.formats[@format][:skip_filters].respond_to?(:include?)
    self.class.formats[@format][:skip_filters].include?(filter)
  elsif self.class.formats[@format][:skip_filters].respond_to?(:call)
    self.class.formats[@format][:skip_filters].call(filter)
  else
    false
  end
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.



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

def update_cache(type, id, data)
end