Module: Quarto::UrlHelper

Included in:
Generator
Defined in:
lib/quarto/url_helper.rb

Overview

This module is included in Generator and thus made available in generate.rb files.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/quarto/url_helper.rb', line 8

def self.included(base)
  if defined? RAILS_GEM_VERSION
    base.class_eval do
      alias_method :url_for_without_element_wrapper, :url_for
      alias_method :url_for, :url_for_with_element_wrapper
    end
  end
end

Instance Method Details

#abs_path(str) ⇒ Object

Generates an absolute path, using the :site_root config value. (To change :site_root, put something like this in generate.rb:

config(:site_root, 'http://your_domain.com/whatever')


20
21
22
# File 'lib/quarto/url_helper.rb', line 20

def abs_path(str)
  "#{Quarto.config[:site_root]}#{str}"
end

#abs_url(str) ⇒ Object



24
25
26
27
# File 'lib/quarto/url_helper.rb', line 24

def abs_url(str)
  warn 'Quarto::UrlHelper#abs_url is deprecated. Use abs_path instead.'
  abs_path(str)
end

Similar to Rails’ link_to, but with less flexibility. Anything you pass to this link_to will also work in Rails, but the reverse is not true.

This method is only defined if you’re not using Quarto within Rails. If you are, the Rails link_to will not be overriden by this one. However, you can still pass instances of ElementWrapper::Base in, because Quarto patches url_for (which is called by Rails’ link_to).

target must be either an instance of ElementWrapper::Base, an absolute URL, or a relative URL.



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/quarto/url_helper.rb', line 83

def link_to(text, target, options = {})
  if !target.is_a?(String) and !target.is_a?(Quarto::ElementWrapper::Base)
    raise ArgumentError, "Expected String or ElementWrapper::Base, but got #{target.inspect}"
  end
  url = url_for(target)
  options = {:html_options => {}}.merge(options)
  output = "<a href=\"#{url}\""
  options[:html_options].each do |attr, value|
    output << " #{attr}=\"#{value}\""
  end
  output + '>' + text + '</a>'
end

#relative_path(path) ⇒ Object

Generates a relative path based on the location of the current output file and path. path must be relative to the project’s output directory. For example, if the current output file is in output/employees, and you call relative_path('images/foo.jpg'), the result will be ../images/foo.jpg.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/quarto/url_helper.rb', line 33

def relative_path(path)
  current_hierarchy = Quarto::Generator.current_output_file_path
  unless current_hierarchy.is_a?(String)
    raise "Expected Quarto::Generator.current_output_file_path to be a String, but got #{current_hierarchy.inspect}"
  end
  current_hierarchy = current_hierarchy.split('/')
  current_hierarchy.pop # remove the filename
  target_hierarchy = path.split('/')
  while current_hierarchy[0] == target_hierarchy[0]
    current_hierarchy.shift
    target_hierarchy.shift
  end
  rel_path = current_hierarchy.inject('') do |result, dir|
    result + '../'
  end
  rel_path << target_hierarchy.join('/')
end

#url_for(options = {}) ⇒ Object

Somewhat compatible with the Rails url_for helper.



97
98
99
100
101
102
103
# File 'lib/quarto/url_helper.rb', line 97

def url_for(options = {})
  if options.is_a?(String)
    options
  else
    url_for_with_element_wrapper(options)
  end
end

#url_for_with_element_wrapper(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/quarto/url_helper.rb', line 51

def url_for_with_element_wrapper(options = {})
  if options.is_a?(Quarto::ElementWrapper::Base)
    if options.respond_to?(:to_path)
      return relative_path(options.to_path)
    else
      raise "#{options.class} must define to_path if you want to pass an instance into link_to or url_for"
    end
  else
    if defined? RAILS_GEM_VERSION
      return url_for_without_element_wrapper(options)
    else
      raise ArgumentError, "Don\'t know how to generate URL from #{options.inspect}"
    end
  end
end

#urlize(str) ⇒ Object

Replaces spaces with dashes and deletes special characters.



68
69
70
# File 'lib/quarto/url_helper.rb', line 68

def urlize(str)
  str.to_s.gsub(/[^ a-zA-Z0-9_-]/, '').tr(' ', '-')
end