Class: Gollum::Markup

Inherits:
Object
  • Object
show all
Defined in:
lib/gollum/markup.rb

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Markup

Initialize a new Markup object.

page - The Gollum::Page.

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



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gollum/markup.rb', line 14

def initialize(page)
  @wiki    = page.wiki
  @name    = page.filename
  @data    = page.text_data
  @version = page.version.id if page.version
  @format  = page.format
  @dir     = ::File.dirname(page.path)
  @tagmap  = {}
  @codemap = {}
  @texmap  = {}
  @wsdmap  = {}
  @premap  = {}
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.



474
475
# File 'lib/gollum/markup.rb', line 474

def check_cache(type, id)
end

#extract_code(data) ⇒ Object

Extract all code blocks into the codemap and replace with placeholders.

data - The raw String data.

Returns the placeholder’d String data.



363
364
365
366
367
368
369
370
371
372
373
# File 'lib/gollum/markup.rb', line 363

def extract_code(data)
  data.gsub!(/^([ \t]*)``` ?([^\r\n]+)?\r?\n(.+?)\r?\n\1```\r?$/m) do
    id     = Digest::SHA1.hexdigest("#{$2}.#{$3}")
    cached = check_cache(:code, id)
    @codemap[id] = cached   ?
      { :output => cached } :
      { :lang => $2, :code => $3, :indent => $1 }
    "#{$1}#{id}" # print the SHA1 ID with the proper indentation
  end
  data
end

#extract_tags(data) ⇒ Object

Extract all tags into the tagmap and replace with placeholders.

data - The raw String data.

Returns the placeholder’d String data.



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/gollum/markup.rb', line 118

def extract_tags(data)
  data.gsub!(/(.?)\[\[(.+?)\]\]([^\[]?)/m) do
    if $1 == "'" && $3 != "'"
      "[[#{$2}]]#{$3}"
    elsif $2.include?('][')
      if $2[0..4] == 'file:'
        pre = $1
        post = $3
        parts = $2.split('][')
        parts[0][0..4] = ""
        link = "#{parts[1]}|#{parts[0].sub(/\.org/,'')}"
        id = Digest::SHA1.hexdigest(link)
        @tagmap[id] = link
        "#{pre}#{id}#{post}"
      else
        $&
      end
    else
      id = Digest::SHA1.hexdigest($2)
      @tagmap[id] = $2
      "#{$1}#{id}#{$3}"
    end
  end
  data
end

#extract_tex(data) ⇒ Object

Extract all TeX into the texmap and replace with placeholders.

data - The raw String data.

Returns the placeholder’d String data.



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gollum/markup.rb', line 78

def extract_tex(data)
  data.gsub(/\\\[\s*(.*?)\s*\\\]/m) do
    tag = CGI.escapeHTML($1)
    id  = Digest::SHA1.hexdigest(tag)
    @texmap[id] = [:block, tag]
    id
  end.gsub(/\\\(\s*(.*?)\s*\\\)/m) do
    tag = CGI.escapeHTML($1)
    id  = Digest::SHA1.hexdigest(tag)
    @texmap[id] = [:inline, tag]
    id
  end
end

#extract_wsd(data) ⇒ Object

Extract all sequence diagram blocks into the wsdmap and replace with placeholders.

data - The raw String data.

Returns the placeholder’d String data.



445
446
447
448
449
450
451
# File 'lib/gollum/markup.rb', line 445

def extract_wsd(data)
  data.gsub(/^\{\{\{ ?(.+?)\r?\n(.+?)\r?\n\}\}\}\r?$/m) do
    id = Digest::SHA1.hexdigest($2)
    @wsdmap[id] = { :style => $1, :code => $2 }
    id
  end
end

#find_file(name) ⇒ 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.



326
327
328
329
330
331
332
333
# File 'lib/gollum/markup.rb', line 326

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

#find_page_from_name(cname) ⇒ Object

Find a page from a given cname. If the page has an anchor (#) and has no match, strip the anchor and try again.

cname - The String canonical page name.

Returns a Gollum::Page instance if a page is found, or an Array of

Gollum::Page, String extra

if a page without the extra anchor data

is found.



343
344
345
346
347
348
349
350
# File 'lib/gollum/markup.rb', line 343

def find_page_from_name(cname)
  if page = @wiki.page(cname)
    return page
  end
  if pos = cname.index('#')
    [@wiki.page(cname[0...pos]), cname[pos..-1]]
  end
end

#parse_image_tag_options(tag) ⇒ Object

Parse any options present on the image tag and extract them into a Hash of option names and values.

tag - The String tag contents (the stuff inside the double brackets).

Returns the options Hash:

key - The String option name.
val - The String option value or true if it is a binary option.


254
255
256
257
258
259
260
# File 'lib/gollum/markup.rb', line 254

def parse_image_tag_options(tag)
  tag.split('|')[1..-1].inject({}) do |memo, attr|
    parts = attr.split('=').map { |x| x.strip }
    memo[parts[0]] = (parts.size == 1 ? true : parts[1])
    memo
  end
end

#process_code(data, encoding = nil) ⇒ Object

Process all code from the codemap and replace the placeholders with the final HTML.

data - The String data (with placeholders). encoding - Encoding Constant or String.

Returns the marked up String data.



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/gollum/markup.rb', line 394

def process_code(data, encoding = nil)
  return data if data.nil? || data.size.zero? || @codemap.size.zero?

  blocks    = []
  @codemap.each do |id, spec|
    next if spec[:output] # cached

    code = spec[:code]

    remove_leading_space(code, /^#{spec[:indent]}/m)
    remove_leading_space(code, /^(  |\t)/m)

    blocks << [spec[:lang], code]
  end

  highlighted = begin
    encoding ||= 'utf-8'
    blocks.map { |lang, code|
      Pygments.highlight(code, :lexer => lang, :options => {:encoding => encoding.to_s})
    }
  rescue ::RubyPython::PythonError
    []
  end

  @codemap.each do |id, spec|
    body = spec[:output] || begin
      if (body = highlighted.shift.to_s).size > 0
        update_cache(:code, id, body)
        body
      else
        "<pre><code>#{CGI.escapeHTML(spec[:code])}</code></pre>"
      end
    end
    data.gsub!(id, body)
  end

  data
end

Attempt to process the tag as a file link tag.

tag - The String tag contents (the stuff inside the double

brackets).

Returns the String HTML if the tag is a valid file link tag or nil

if it is not.


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/gollum/markup.rb', line 269

def process_file_link_tag(tag)
  parts = tag.split('|')
  return if parts.size.zero?

  name  = parts[0].strip
  path  = parts[1] && parts[1].strip
  path  = if path && file = find_file(path)
    ::File.join @wiki.base_path, file.path
  elsif path =~ %r{^https?://}
    path
  else
    nil
  end

  if name && path && file
    %{<a href="#{::File.join @wiki.base_path, file.path}">#{name}</a>}
  elsif name && path
    %{<a href="#{path}">#{name}</a>}
  else
    nil
  end
end

#process_image_tag(tag) ⇒ Object

Attempt to process the tag as an image tag.

tag - The String tag contents (the stuff inside the double brackets).

Returns the String HTML if the tag is a valid image tag or nil

if it is not.


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/gollum/markup.rb', line 179

def process_image_tag(tag)
  parts = tag.split('|')
  return if parts.size.zero?

  name  = parts[0].strip
  path  = if file = find_file(name)
    ::File.join @wiki.base_path, file.path
  elsif name =~ /^https?:\/\/.+(jpg|png|gif|svg|bmp)$/i
    name
  end

  if path
    opts = parse_image_tag_options(tag)

    containered = false

    classes = [] # applied to whatever the outermost container is
    attrs   = [] # applied to the image

    align = opts['align']
    if opts['float']
      containered = true
      align ||= 'left'
      if %w{left right}.include?(align)
        classes << "float-#{align}"
      end
    elsif %w{top texttop middle absmiddle bottom absbottom baseline}.include?(align)
      attrs << %{align="#{align}"}
    elsif align
      if %w{left center right}.include?(align)
        containered = true
        classes << "align-#{align}"
      end
    end

    if width = opts['width']
      if width =~ /^\d+(\.\d+)?(em|px)$/
        attrs << %{width="#{width}"}
      end
    end

    if height = opts['height']
      if height =~ /^\d+(\.\d+)?(em|px)$/
        attrs << %{height="#{height}"}
      end
    end

    if alt = opts['alt']
      attrs << %{alt="#{alt}"}
    end

    attr_string = attrs.size > 0 ? attrs.join(' ') + ' ' : ''

    if opts['frame'] || containered
      classes << 'frame' if opts['frame']
      %{<span class="#{classes.join(' ')}">} +
      %{<span>} +
      %{<img src="#{path}" #{attr_string}/>} +
      (alt ? %{<span>#{alt}</span>} : '') +
      %{</span>} +
      %{</span>}
    else
      %{<img src="#{path}" #{attr_string}/>}
    end
  end
end

Attempt to process the tag as a page link tag.

tag - The String tag contents (the stuff inside the double

brackets).

Returns the String HTML if the tag is a valid page link tag or nil

if it is not.


299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/gollum/markup.rb', line 299

def process_page_link_tag(tag)
  parts = tag.split('|')
  parts.reverse! if @format == :mediawiki

  name, page_name = *parts.compact.map(&:strip)
  cname = @wiki.page_class.cname(page_name || name)

  if name =~ %r{^https?://} && page_name.nil?
    %{<a href="#{name}">#{name}</a>}
  else
    presence    = "absent"
    link_name   = cname
    page, extra = find_page_from_name(cname)
    if page
      link_name = @wiki.page_class.cname(page.name)
      presence  = "present"
    end
    link = ::File.join(@wiki.base_path, CGI.escape(link_name))
    %{<a class="internal #{presence}" href="#{link}#{extra}">#{name}</a>}
  end
end

#process_tag(tag) ⇒ Object

Process a single tag into its final HTML form.

tag - The String tag contents (the stuff inside the double

brackets).

Returns the String HTML version of the tag.



163
164
165
166
167
168
169
170
171
# File 'lib/gollum/markup.rb', line 163

def process_tag(tag)
  if html = process_image_tag(tag)
    html
  elsif html = process_file_link_tag(tag)
    html
  else
    process_page_link_tag(tag)
  end
end

#process_tags(data) ⇒ Object

Process all tags from the tagmap and replace the placeholders with the final markup.

data - The String data (with placeholders).

Returns the marked up String data.



150
151
152
153
154
155
# File 'lib/gollum/markup.rb', line 150

def process_tags(data)
  @tagmap.each do |id, tag|
    data.gsub!(id, process_tag(tag))
  end
  data
end

#process_tex(data) ⇒ Object

Process all TeX from the texmap and replace the placeholders with the final markup.

data - The String data (with placeholders).

Returns the marked up String data.



98
99
100
101
102
103
104
105
# File 'lib/gollum/markup.rb', line 98

def process_tex(data)
  @texmap.each do |id, spec|
    type, tex = *spec
    out = %{<img src="#{::File.join(@wiki.base_path, '_tex.png')}?type=#{type}&data=#{Base64.encode64(tex).chomp}" alt="#{CGI.escapeHTML(tex)}">}
    data.gsub!(id, out)
  end
  data
end

#process_wsd(data) ⇒ Object

Process all diagrams from the wsdmap and replace the placeholders with the final HTML.

data - The String data (with placeholders).

Returns the marked up String data.



459
460
461
462
463
464
465
466
# File 'lib/gollum/markup.rb', line 459

def process_wsd(data)
  @wsdmap.each do |id, spec|
    style = spec[:style]
    code = spec[:code]
    data.gsub!(id, Gollum::WebSequenceDiagram.new(code, style).to_tag)
  end
  data
end

#remove_leading_space(code, regex) ⇒ Object

Remove the leading space from a code block. Leading space is only removed if every single line in the block has leading whitespace.

code - The code block to remove spaces from regex - A regex to match whitespace



381
382
383
384
385
# File 'lib/gollum/markup.rb', line 381

def remove_leading_space(code, regex)
  if code.lines.all? { |line| line =~ /\A\r?\n\Z/ || line =~ regex }
    code.gsub!(regex, '')
  end
end

#render(no_follow = false, encoding = nil) ⇒ 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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gollum/markup.rb', line 36

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

  data = extract_tex(@data.dup)
  data = extract_code(data)
  data = extract_wsd(data)
  data = extract_tags(data)
  begin
    data = GitHub::Markup.render(@name, data)
    if data.nil?
      raise "There was an error converting #{@name} to HTML."
    end
  rescue Object => e
    data = %{<p class="gollum-error">#{e.message}</p>}
  end
  data = process_tags(data)
  data = process_code(data, encoding)
  if sanitize || block_given?
    doc  = Nokogiri::HTML::DocumentFragment.parse(data)
    doc  = sanitize.clean_node!(doc) if sanitize
    yield doc if block_given?
    data = doc.to_html
  end
  data = process_tex(data)
  data = process_wsd(data)
  data.gsub!(/<p><\/p>/, '')
  data
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.



484
485
# File 'lib/gollum/markup.rb', line 484

def update_cache(type, id, data)
end